Skip to content

Commit

Permalink
docs(response): add comment doxygen style for documentation (#196)
Browse files Browse the repository at this point in the history
* docs(Response): add comment doxygen style

* test: error page
  • Loading branch information
chanhihi authored Jul 31, 2023
1 parent 9114019 commit 5a8ef3a
Show file tree
Hide file tree
Showing 6 changed files with 258 additions and 13 deletions.
Empty file added YoupiBanane/error_page/404error
Empty file.
54 changes: 54 additions & 0 deletions YoupiBanane/error_page/defaultError.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<!DOCTYPE html>
<html>

<head>
<title>404 Not Found</title>
<style>
@import url(http://www.openhiun.com/hangul/nanumbarungothic.css);

head,
body {
font-family: "Nanum Barun Gothic", Arial;
}

body {
background-color: #fafafa;
font-family: Arial, sans-serif;
text-align: center;
color: #333;
}

.error-container {
margin-top: 100px;
}

.error-container h1 {
font-size: 4em;
transition: 0.3s;
}

.error-container h1:hover {
text-shadow: 0px 0px 10px rgba(0, 0, 0, 0.5);
}

.error-container p {
font-size: 1.5em;
transition: 0.3s;
}

.error-container p:hover {
text-shadow: 0px 0px 10px rgba(0, 0, 0, 0.5);
}

</style>
</head>

<body>
<div class="error-container">
<h1>404</h1>
<p>Oops! The page you are looking for does not exist.</p>
<a href="/">Go to Home</a>
</div>
</body>

</html>
4 changes: 2 additions & 2 deletions config/default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ include config/mime.types;
server {
listen 8100;
server_name webserv;
error_page config/error_page/404error;
error_page /YoupiBanane/error_page/defaultError.html;
access_log logs/domain1.access.log main;
root html;
cgi .cgi;
Expand Down Expand Up @@ -186,7 +186,7 @@ server {
root /tests/html/;
allow_method GET,POST;
index index.html;
autoindex off;
autoindex on;
}

location /put_test {
Expand Down
22 changes: 22 additions & 0 deletions srcs/clients/response/include/IResponse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,28 @@

#include "Status.hpp"

/**
* @brief Response 클래스의 인터페이스
*
* @see getResponseFlag
* @see getStatus
* @see getResponse
* @see getBody
* @see getFieldValue
* @see assembleResponse
* @see eraseHeaderField
* @see addBody
* @see setHeaderField
* @see setBody
* @see setStatusCode
* @see setResponseParsed
* @see isParsed
* @see setCookie
* @see clear
*
* @author tocatoca
* @date 2023.07.29
*/
class IResponse {
public:
virtual ~IResponse() {}
Expand Down
6 changes: 6 additions & 0 deletions srcs/clients/response/include/Response.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
#include "Status.hpp"
#include "Utils.hpp"

/**
* @brief Response 클래스는 IResponse 인터페이스를 구현한 클래스이다.
*
* @author
* @date 2023.07.29
*/
class Response : public IResponse {
public:
Response();
Expand Down
185 changes: 174 additions & 11 deletions srcs/clients/response/src/Response.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,27 @@ std::string &Response::getFieldValue(const std::string &key) {
return (_headerFields[key]);
}

/**
* @brief 300번대 응답을 생성합니다.
*
* @details
* 301, 302, 303, 304, 307, 308번대 응답을 생성합니다.
*
* @see E_301_MOVED_PERMANENTLY
* @see E_302_FOUND
* @see E_303_SEE_OTHER
* @see E_304_NOT_MODIFIED
* @see E_307_TEMPORARY_REDIRECT
* @see E_308_PERMANENT_REDIRECT
*
* dts.path가 비어있으면 "/"로 설정합니다.
* query_string이 비어있지 않으면 path에 query_string을 붙여준다.
*
* @param dts
*
* @author tocatoca
* @date 2023.07.29
*/
void Response::create300Response(RequestDts &dts) {
if (dts.path->empty()) {
*dts.path = "/";
Expand All @@ -43,6 +64,36 @@ void Response::create300Response(RequestDts &dts) {
assembleResponse();
}

/**
* @brief 400, 500번대 응답을 생성합니다.
*
* @details
*
* @see E_400_BAD_REQUEST
* @see E_401_UNAUTHORIZED
* @see E_403_FORBIDDEN
* @see E_404_NOT_FOUND
* @see E_405_METHOD_NOT_ALLOWED
* @see E_406_NOT_ACCEPTABLE
* @see E_408_REQUEST_TIMEOUT
* @see E_409_CONFLICT
* @see E_413_REQUEST_ENTITY_TOO_LARGE
* @see E_414_URI_TOO_LONG
* @see E_415_UNSUPPORTED_MEDIA_TYPE
* @see E_417_EXPECTION_FAILED
* @see E_418_IM_A_TEAPOT
* @see E_500_INTERNAL_SERVER_ERROR
* @see E_501_NOT_IMPLEMENTED
* @see E_502_BAD_GATEWAY
* @see E_503_SERVICE_UNAVAILABLE
* @see E_504_GATEWAY_TIMEOUT
* @see E_505_HTTP_VERSION_NOT_SUPPORTE
*
* @param dts
*
* @author tocatoca
* @date 2023.07.29
*/
void Response::create400And500Response(RequestDts &dts) {
if (*dts.matchedServer != NULL) {
configureErrorPages(dts);
Expand All @@ -54,6 +105,15 @@ void Response::create400And500Response(RequestDts &dts) {
assembleResponse();
}

/**
* @brief 예외 응답을 생성합니다.
*
* @details
* 각각의 status code에 맞춰서 response를 생성합니다.
* 생성후 responseFlag를 on하여 response의 생성을 알립니다.
*
* @param dts
*/
void Response::createExceptionResponse(RequestDts &dts) {
resetResponse();
_statusCode = *dts.statusCode;
Expand All @@ -66,41 +126,106 @@ void Response::createExceptionResponse(RequestDts &dts) {
_responseFlag = true;
}

/**
* @brief 비어있는 예외 응답을 생성합니다.
*
* @details
* 400번대 status code들을 조건으로 response를 생성합니다.
* 생성후 responseFlag를 on하여 response의 생성을 알립니다.
*
* @param dts
*/
void Response::createEmptyExceptionResponse(RequestDts &dts) {
resetResponse();
_statusCode = *dts.statusCode;
if (_statusCode > E_308_PERMANENT_REDIRECT) create400And500Response(dts);
_responseFlag = true;
}

void Response::assembleResponseLine(void) {
_response = "HTTP/1.1 ";
_response += statusInfo[_statusCode].code;
_response += " ";
_response += statusInfo[_statusCode].message;
_response += "\r\n";
// std::cout << _response << std::endl;
}

/**
* @brief 응답을 초기화합니다.
*
* @details
* _responseFlag가 false면 리턴하여 response clear를 진행하지 않는다.
*
* @author tocatoca
* @date 2023.07.29
*/
void Response::resetResponse(void) {
if (_responseFlag == false) return;
_response.clear();
_responseFlag = false;
}

/**
* @brief 응답을 파싱했음을 설정합니다.
*
* @details
* _assembleFlag를 true로 설정합니다.
* _responseFlag를 true로 설정합니다.
*
* @author tocatoca
* @date 2023.07.29
*/
void Response::setResponseParsed() {
_assembleFlag = true;
_responseFlag = true;
}
bool Response::isParsed() { return _responseFlag; }

/**
* @brief 응답을 조립합니다.
*
* @details
* assembleResponseLine을 호출합니다.
* putHeaderFields를 호출합니다.
* putBody를 호출합니다.
*
* @author tocatoca
* @date 2023.07.29
*/
void Response::assembleResponse(void) {
assembleResponseLine();
putHeaderFields();
putBody();
// std::cout << _response << std::endl;
}

/**
* @brief 응답 라인을 조립합니다.
*
* @details
* HTTP/1.1 200 OK\r\n 와 같이 조립해야합니다.
*
* _statusCode를 이용하여 응답 라인을 조립합니다.
*
* @see _statusCode.code : 응답 코드
* @see _statusCode.message : 응답 메시지
* @see statusInfo : 응답 코드와 메시지를 저장한 구조체
*
* @author tocatoca
* @date 2023.07.29
*/
void Response::assembleResponseLine(void) {
_response = "HTTP/1.1 ";
_response += statusInfo[_statusCode].code;
_response += " ";
_response += statusInfo[_statusCode].message;
_response += "\r\n";
}

/**
* @brief 헤더 필드를 조립합니다.
*
* @details
* _headerFields를 순회하며 헤더 필드를 조립합니다.
*
* @see _headerFields : 헤더 필드를 저장한 맵
* @see _response : 조립된 응답
*
* @author tocatoca
* @date 2023.07.29
*/
void Response::putHeaderFields(void) {
std::map<std::string, std::string>::const_iterator it = _headerFields.begin();
std::map<std::string, std::string>::const_iterator end = _headerFields.end();
Expand All @@ -110,6 +235,12 @@ void Response::putHeaderFields(void) {
}
}

/**
* @brief Body를 추가합니다.
*
* @author tocatoca
* @date 2023.07.29
*/
void Response::putBody(void) {
if (_body.empty() == true) {
_response += "\r\n";
Expand All @@ -120,6 +251,12 @@ void Response::putBody(void) {

void Response::setStatusCode(Status code) { _statusCode = code; }

/**
* @brief 헤더 필드를 설정합니다.
*
* @param key
* @param value
*/
void Response::setHeaderField(const std::string &key,
const std::string &value) {
_headerFields[key] = value;
Expand All @@ -133,17 +270,32 @@ void Response::addBody(const std::string &str) { _body += str; }

void Response::setBody(const std::string &str) { _body = str; }

/**
* @brief 에러페이지를 구성합니다.
*
* @details
* config file에서 serverblock의 error_page를 이용하여 에러페이지를 구성합니다.
* 3000 port에서는 에러페이지를 directory로 사용하여 구성합니다.
* 8080 port는 에러페이지를 file로 사용하여 구성합니다.
*
* @param dts
*/
void Response::configureErrorPages(RequestDts &dts) {
IServerConfig &serverConfig = **dts.matchedServer;

std::string path =
serverConfig.getErrorPage() + statusInfo[*dts.statusCode].code + ".html";
std::string path = serverConfig.getErrorPage();
if (path[path.size() - 1] == '/') {
path += statusInfo[*dts.statusCode].code;
path += ".html";
}

if (path[0] == '/') {
path = path.substr(1);
}
std::cout << "path : " << path << std::endl;
std::ifstream file(path.c_str(), std::ios::in);
if (file.is_open() == false) {
std::cout << "FAIL path : " << path << std::endl;
setHeaderField("Content-Type", "text/plain");
addBody(statusInfo[*dts.statusCode].body);
addBody("\r\n");
Expand All @@ -159,6 +311,17 @@ void Response::configureErrorPages(RequestDts &dts) {
}
}

/**
* @brief 맛있는 쿠키를 구워줍니다.
*
* @details
* httponly, Max-Age=60; 옵션을 추가합니다.
*
* @see httpOnly : 자바스크립트에서 쿠키를 읽을 수 없도록 합니다.
* @see Max-Age : 쿠키의 유효기간을 설정합니다. (초 단위)
*
* @param session_id
*/
void Response::setCookie(std::string &session_id) {
setHeaderField("Set-Cookie",
"session_id=" + session_id + "; httponly; Max-Age=60;");
Expand Down

0 comments on commit 5a8ef3a

Please sign in to comment.