Skip to content

Commit 0be60cb

Browse files
committed
done: basic test
But strangely, in vscode debug, the body is recognized normally, but when compiling in terminal, the body is not read and an error occurs.
1 parent 5d0bad0 commit 0be60cb

File tree

9 files changed

+95
-24
lines changed

9 files changed

+95
-24
lines changed

.vscode/tasks.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
"${workspaceFolder}/src/server/HttpResponse.cpp",
2222
"${workspaceFolder}/src/server/ErrorResponse.cpp",
2323
"${workspaceFolder}/src/server/Context.cpp",
24-
"${workspaceFolder}/src/server/RequestParser.cpp",
2524
"${workspaceFolder}/src/server/RequestHandler.cpp",
2625
"${workspaceFolder}/src/server/StaticFileHandler.cpp",
2726
"${workspaceFolder}/src/util/Location.cpp",

include/HttpRequest.hpp

Lines changed: 5 additions & 4 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/10/22 23:10:40 by minakim ### ########.fr */
9+
/* Updated: 2024/10/23 11:19:30 by minakim ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -29,7 +29,7 @@ struct ReadedLines
2929
{
3030
std::string request;
3131
std::vector<std::string> headers;
32-
std::vector<std::string> bodyLines;
32+
std::string bodyLines;
3333
};
3434

3535

@@ -64,6 +64,7 @@ class HttpRequest
6464
void setVersion(const std::string& version);
6565
void setHeaders(const std::map<std::string, std::string>& headers);
6666
void setBody(const std::vector<std::string>& bodyLines, e_body_type type);
67+
void setBody(const std::string& bodyLines, e_body_type type);
6768
void setContentLength(const ssize_t& contentLength);
6869

6970
bool hasBody() const;
@@ -83,12 +84,12 @@ class HttpRequest
8384

8485
ReadedLines _splitRequestData(const std::string& requestData);
8586

86-
bool _processRequestBody(const std::vector<std::string>& bodyLines);
87+
bool _processRequestBody(const std::string& bodyLines);
8788
bool _parseRequestLine(const std::string& requestLine);
8889
bool _parseHeaders(const std::vector<std ::string>& headerLines);
8990

9091
std::vector<std ::string> _convertPartToHeaders(std::istringstream& iss);
91-
std::vector<std ::string> _convertPartToBodyLines(std::istringstream& iss);
92+
std ::string _convertPartToBodyLines(std::istringstream& iss);
9293

9394
};
9495

include/webserv.hpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
/* ::: :::::::: */
44
/* webserv.hpp :+: :+: :+: */
55
/* +:+ +:+ +:+ */
6-
/* By: sanghupa <sanghupa@student.42berlin.de> +#+ +:+ +#+ */
6+
/* By: minakim <minakim@student.42berlin.de> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/06/22 21:04:39 by sanghupa #+# #+# */
9-
/* Updated: 2024/07/23 00:01:43 by sanghupa ### ########.fr */
9+
/* Updated: 2024/10/23 10:15:54 by minakim ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -59,4 +59,15 @@
5959

6060
extern volatile bool g_sigint;
6161

62+
#define RESET "\033[0m"
63+
64+
// Regular Colors
65+
#define RED "\033[1;31m"
66+
#define GREEN "\033[1;32m"
67+
#define YELLOW "\033[1;33m"
68+
#define BLUE "\033[1;34m"
69+
#define MAGENTA "\033[1;35m"
70+
#define CYAN "\033[1;36m"
71+
#define WHITE "\033[1;37m"
72+
6273
#endif

src/server/HttpRequest.cpp

Lines changed: 22 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/10/22 23:07:40 by minakim ### ########.fr */
9+
/* Updated: 2024/10/23 11:29:10 by minakim ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -53,7 +53,7 @@ bool HttpRequest::parse(const std::string& requestData)
5353
return (true);
5454
}
5555

56-
bool HttpRequest::_processRequestBody(const std::vector<std::string>& bodyLines)
56+
bool HttpRequest::_processRequestBody(const std::string& bodyLines)
5757
{
5858
if (!hasBody())
5959
return (true);
@@ -142,13 +142,14 @@ std::vector<std ::string> HttpRequest::_convertPartToHeaders(std::istringstream&
142142
return (res);
143143
}
144144

145-
std::vector<std::string> HttpRequest::_convertPartToBodyLines(std::istringstream& iss)
145+
std::string HttpRequest::_convertPartToBodyLines(std::istringstream& iss)
146146
{
147147
std::string readline;
148-
std::vector<std::string> drafts;
148+
// std::vector<std::string> drafts;
149+
std::string drafts;
149150

150151
while (std::getline(iss, readline))
151-
drafts.push_back(readline);
152+
drafts += readline + "\n";
152153
return (drafts);
153154
}
154155

@@ -290,6 +291,7 @@ bool HttpRequest::hasBody() const
290291
{
291292
return (_content.first);
292293
}
294+
293295
void HttpRequest::setBody(const std::vector<std::string>& bodyLines, e_body_type type)
294296
{
295297
if (!hasBody() || getContentLength() == 0)
@@ -298,12 +300,25 @@ void HttpRequest::setBody(const std::vector<std::string>& bodyLines, e_body_type
298300
if (bodyLinesToString.length() != getContentLength())
299301
throw std::runtime_error(
300302
"HTTP method [" + getMethod() + "] at URI [" + getUri() + "] encountered a body length mismatch: "
301-
"Expected Content-Length = " + std::to_string(getContentLength()) +
302-
", but received body length = " + std::to_string(bodyLinesToString.length()) + ".");
303+
"Expected Content-Length = " + toString(getContentLength()) +
304+
", but received body length = " + toString(bodyLinesToString.length()) + ".");
303305
_body = bodyLinesToString;
304306
_type = type;
305307
}
306308

309+
void HttpRequest::setBody(const std::string& bodyLines, e_body_type type)
310+
{
311+
if (!hasBody() || getContentLength() == 0)
312+
return;
313+
if (bodyLines.length() != getContentLength())
314+
throw std::runtime_error(
315+
"HTTP method [" + getMethod() + "] at URI [" + getUri() + "] encountered a body length mismatch: "
316+
"Expected Content-Length = " + toString(getContentLength()) +
317+
", but received body length = " + toString(bodyLines.length()) + ".");
318+
_body = bodyLines;
319+
_type = type;
320+
}
321+
307322
void HttpRequest::setContentLength(const ssize_t& contentLength)
308323
{
309324
if (hasBody())

src/server/RequestHandler.cpp

Lines changed: 13 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/10/22 19:57:51 by minakim ### ########.fr */
9+
/* Updated: 2024/10/23 11:32:44 by minakim ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -62,9 +62,20 @@ HttpResponse RequestHandler::handleRequest(const Context& context)
6262
/// @return bool
6363
bool RequestHandler::_isCGIReqeust(const Context& context) const
6464
{
65+
66+
67+
std::map<std::string, std::string> cgiMap = context.getLocation().getCgi();
68+
std::cout << YELLOW << "TEST | location.getCgi()\n" << RESET << std::endl;
69+
for (std::map<std::string, std::string>::const_iterator it = cgiMap.begin(); it != cgiMap.end(); ++it)
70+
{
71+
std::cout << YELLOW << " key: " << it->first << " -> value: " << it->second << RESET << std::endl;
72+
}
73+
std::cout << YELLOW << " --- cgi map end" << RESET << std::endl;
74+
75+
6576
if (context.getLocation().getCgi().empty())
6677
return (false);
67-
return (false);
78+
return (true);
6879
}
6980

7081
/// @brief Not implemented.

src/server/Server.cpp

Lines changed: 7 additions & 6 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:46 by sanghupa #+# #+# */
9-
/* Updated: 2024/10/22 23:02:54 by minakim ### ########.fr */
9+
/* Updated: 2024/10/23 11:36:18 by minakim ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -150,15 +150,18 @@ void Server::start()
150150
// --i;
151151
continue ;
152152
}
153-
std::cout << "TEST | count: " << count << "\n" << std::endl;
154-
std::cout << "TEST | buffer: " << buffer << "\n" << std::endl;
155153
// buffer[count] = '\0';
156154

157155
////////////////////////////////////////////////////////////////////////////////////////
158-
std::cout << "TEST | start: handleClientData_2" << std::endl;
159156
// FIXME: need to handle bigger size of data, not only 1024
160157

161158
std::string requestData(buffer, count);
159+
160+
std::cout << YELLOW << "TEST | requestData (server.cpp, buffer to std::string)" << std::endl;
161+
std::cout << "buffer: " << count << ", string size: "<< requestData.size() << std::endl;
162+
std::cout << RESET << "\nrequestData\n\n" << YELLOW << requestData << "\n\n" << RESET << std::endl;
163+
164+
162165
// @author minakim
163166
// FIXME: @sanghupa please update this part
164167

@@ -173,8 +176,6 @@ void Server::start()
173176
HttpResponse response = _requestHandler.handleRequest(contextFromTarget);
174177
std::string responseData = response.generateResponseToString();
175178
// std::string responseData = handle_request(requestData);
176-
177-
std::cout << "TEST | " << requestData << std::endl;
178179
////////////////////////////////////////////////////////////////////////////////////////
179180

180181
// Send the response

src/server/StaticFileHandler.cpp

Lines changed: 2 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/10/22 23:08:32 by minakim ### ########.fr */
9+
/* Updated: 2024/10/23 10:29:02 by minakim ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -66,7 +66,7 @@ HttpResponse StaticFileHandler::handleget(const Context& context)
6666
return (_handleDirListing(context));
6767
return (_handleDirRequest(context));
6868
}
69-
if (isFile(_handledPath))
69+
else if (isFile(_handledPath))
7070
return (_handleFileRequest(context));
7171
return (_handleNotFound(context));
7272
}

webserv_test

1.78 MB
Binary file not shown.

www/static/test.html

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>File Upload</title>
7+
</head>
8+
<body>
9+
<h2>File Upload</h2>
10+
<form action="/cgi-bin/save_file.py" method="post" enctype="multipart/form-data">
11+
<input type="file" id="fileInput" name="filename">
12+
<input type="submit" value="Upload">
13+
</form>
14+
<div id="fileInfo"></div>
15+
<a id="downloadLink" style="display: none;" download>Download File</a>
16+
<br>
17+
18+
<hr><h2>Files currently stored on the server</h2>
19+
<embed type="text/html" src="/cgi-bin/list.sh" width="200" height="50">
20+
21+
<br><br><hr>
22+
<form action="/cgi-bin/delete.py" method="POST" enctype="multipart/form-data">
23+
<label for="filename"><h2>Which file to delete?</h2></label>
24+
<input type="text" id="fileInput" name="filename">
25+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
26+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
27+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
28+
<input type="submit" value="Delete">
29+
</form>
30+
<br>
31+
32+
</body>
33+
</html>

0 commit comments

Comments
 (0)