Skip to content

Commit 742baf7

Browse files
committed
update: feedback from PR
1 parent 5e10e4c commit 742baf7

File tree

6 files changed

+235
-123
lines changed

6 files changed

+235
-123
lines changed

include/HttpRequest.hpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: minakim <minakim@student.42berlin.de> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/06/30 16:23:00 by sanghupa #+# #+# */
9-
/* Updated: 2024/07/12 19:17:46 by minakim ### ########.fr */
9+
/* Updated: 2024/07/14 14:49:37 by minakim ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -18,13 +18,14 @@
1818

1919
# define WHITESPACE " \t\r\n"
2020

21-
typedef struct s_read_request {
21+
22+
struct t_read_request {
2223
std::string request;
2324
std::vector<std ::string> headers;
2425
std::string body;
2526
bool iscomplete;
26-
27-
} t_read_request;
27+
t_read_request() : iscomplete(false) {}
28+
};
2829

2930
class HttpRequest
3031
{
@@ -50,10 +51,10 @@ class HttpRequest
5051
std::map<std::string, std::string> _headers;
5152
std::string _body;
5253

54+
t_read_request _splitRequestData(const std::string& requestData);
5355
bool _parseRequestLine(const std::string requestLine);
5456
bool _parseHeaders(const std::vector<std ::string> headerLines);
5557
bool _parseBody(const std::string bodylines);
56-
t_read_request _separateRequestData(const std::string& requestData);
5758
std::vector<std ::string> _dataToHeaders(std::istringstream& iss);
5859
std::string _dataToBody(std::istringstream& iss);
5960

include/HttpResponse.hpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: minakim <minakim@student.42berlin.de> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/06/30 16:23:00 by sanghupa #+# #+# */
9-
/* Updated: 2024/07/12 21:22:52 by minakim ### ########.fr */
9+
/* Updated: 2024/07/14 17:27:02 by minakim ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -16,12 +16,18 @@
1616
# include <string>
1717
# include <map>
1818

19+
std::string getErrorPagePath(int pageCode);
20+
1921
class HttpResponse
2022
{
2123
public:
2224
HttpResponse();
25+
HttpResponse(const std::string filePath);
26+
HttpResponse(const HttpResponse& other);
27+
HttpResponse& operator=(const HttpResponse& other);
2328
~HttpResponse();
24-
29+
30+
void setStatusCode(int code);
2531
void setStatusCode(int code, const std::string statusMessage);
2632
void setHeader(const std::string key, const std::string value);
2733
void setBody(const std::string bodyContent);
@@ -50,6 +56,9 @@ class HttpResponse
5056
std::string _getStatusLine() const;
5157
std::string _getHeadersString() const;
5258
std::string _getResponseLine() const;
59+
60+
static const std::map<int, std::string>& _getStatusMessages();
61+
static HttpResponse _errorResponse(int code);
5362
};
5463

5564
#endif

include/StaticFileHandler.hpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: minakim <minakim@student.42berlin.de> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/06/30 16:23:00 by sanghupa #+# #+# */
9-
/* Updated: 2024/07/12 19:24:36 by minakim ### ########.fr */
9+
/* Updated: 2024/07/14 16:44:21 by minakim ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -18,7 +18,10 @@
1818
# include "HttpRequest.hpp"
1919
# include "HttpResponse.hpp"
2020

21-
# define FOLDER_PATH "./www/static"
21+
// FIXME: Delete when correct logic is applied in the future
22+
# define LOCATION_FIXME "./www/static"
23+
# define LOOT_DIR "./www/static"
24+
2225
class StaticFileHandler
2326
{
2427
public:
@@ -28,11 +31,14 @@ class StaticFileHandler
2831
HttpResponse handleRequest(const HttpRequest request);
2932

3033
private:
31-
static std::map<std::string, std::string> _mimeTypes;
34+
static std::map<std::string, std::string> _staticMimeTypes;
3235

33-
std::string getMimeType(const std::string path) const;
34-
bool fileExists(const std::string path) const;
35-
static void initializeMimeTypes();
36+
std::string _getMimeType(const std::string path) const;
37+
bool _fileExists(const std::string path) const;
38+
static void _staticInitializeMimeTypes();
39+
HttpResponse _handleRoot();
40+
std::string _getFilePath(const std::string& uri) const;
41+
HttpResponse _handleFileNotFound();
3642
};
3743

38-
#endif
44+
#endif

src/server/HttpRequest.cpp

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: minakim <minakim@student.42berlin.de> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/06/30 16:23:00 by sanghupa #+# #+# */
9-
/* Updated: 2024/07/12 19:20:58 by minakim ### ########.fr */
9+
/* Updated: 2024/07/14 15:39:21 by minakim ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -49,6 +49,7 @@ std::string HttpRequest::getBody() const
4949
return (_body);
5050
}
5151

52+
// TODO: check for necessary initialization
5253
bool HttpRequest::isConnectionClose() const
5354
{
5455
std::map<std::string, std::string>::const_iterator it = _headers.find("Connection");
@@ -80,38 +81,20 @@ std::string HttpRequest::_dataToBody(std::istringstream& iss)
8081
return ("");
8182
return (drafts);
8283
}
83-
/// @brief Separate the request data into request line, headers, and body.
84-
/// @param requestData The request data to be separated.
85-
/// @return A struct containing the separated request data.
86-
t_read_request HttpRequest::_separateRequestData(const std::string& requestData)
87-
{
88-
t_read_request data;
89-
std::istringstream iss(requestData);
90-
std::string readline;
91-
std::string drafts;
9284

93-
data.iscomplete = false;
85+
////////////////////////////////////////////////////////////////////////////////
86+
/// The current parse logic for my request is to first split the syntax into
87+
/// request, header, and body lines, and then parse the split syntax separately.
88+
/// I've separated the 'split' and 'parse' parts so that one function does one job.
9489

95-
if (requestData.empty())
96-
return (data);
97-
if (!std::getline(iss, readline))
98-
return (data);
99-
data.request = readline;
100-
data.headers = _dataToHeaders(iss);
101-
if (data.headers.empty())
102-
return (data);
103-
data.body = _dataToBody(iss);
104-
data.iscomplete = true;
105-
return (data);
106-
}
10790

10891
/// @brief This function parses the request data and extracts
10992
/// the method, path, version, headers, and body.
11093
/// @param requestData The request data to be parsed.
11194
/// @return bool
11295
bool HttpRequest::parse(const std::string& requestData)
11396
{
114-
t_read_request separatedData = _separateRequestData(requestData);
97+
t_read_request separatedData = _splitRequestData(requestData);
11598
if (!separatedData.iscomplete)
11699
return (false);
117100
if (!_parseRequestLine(separatedData.request))
@@ -123,6 +106,29 @@ bool HttpRequest::parse(const std::string& requestData)
123106
return (true);
124107
}
125108

109+
110+
/// @brief Separate the request data into request line, headers, and body.
111+
/// @param requestData The request data to be separated.
112+
/// @return A struct containing the separated request data.
113+
t_read_request HttpRequest::_splitRequestData(const std::string& requestData)
114+
{
115+
t_read_request data;
116+
std::istringstream iss(requestData);
117+
std::string readline;
118+
std::string drafts;
119+
if (requestData.empty())
120+
return (data);
121+
if (!std::getline(iss, readline))
122+
return (data);
123+
data.request = readline;
124+
data.headers = _dataToHeaders(iss);
125+
if (data.headers.empty())
126+
return (data);
127+
data.body = _dataToBody(iss);
128+
data.iscomplete = true;
129+
return (data);
130+
}
131+
126132
/// @brief Parses the request line and extracts the method, path, and version.
127133
/// @param requestLine `_method` `_uri` `_version`, example: GET /path/resource HTTP/1.1
128134
/// @return bool

0 commit comments

Comments
 (0)