Skip to content

Commit 5b72478

Browse files
committed
handled form data and works perfectly, now working on decode files type
1 parent 4510f95 commit 5b72478

16 files changed

+175
-103
lines changed

CGI/cgi.cpp

+10-6
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ void set_headers_cgi(std::string output, Respond &res) {
9494

9595
put_cookie(output, res);
9696
while (std::getline(ss, line)) {
97-
if (line.empty()) {
97+
if (line == "\r") {
9898
headers_finished = true;
9999
continue;
100100
}
@@ -105,10 +105,12 @@ void set_headers_cgi(std::string output, Respond &res) {
105105
std::string value = line.substr(pos + 2);
106106
if(key != "Set-Cookie")
107107
res.set_header(key, value);
108-
//puts("heeeeere");
109108
}
110-
} else {
111-
body += line;
109+
}
110+
else
111+
{
112+
std::cout << line << "\n";
113+
body += line + "\n";
112114
}
113115
}
114116
res.set_response_body(body);
@@ -117,11 +119,11 @@ void set_headers_cgi(std::string output, Respond &res) {
117119

118120
std::string run_cgi(request &r, Respond &res)
119121
{
120-
// puts("heeeeeeeeerererere");
122+
puts("heeeeeeeeerererere");
121123
char *file, *path;
122124
file = strdup(res.get_file_cgi().c_str());
123125
path = strdup(res.get_path_info_founded().c_str());
124-
//std::cout << path << " " << file << std::endl;
126+
std::cout << path << " " << file << std::endl;
125127
char *cmd[3] = {path, file, NULL};
126128

127129
std::string cgi_str;
@@ -202,7 +204,9 @@ std::string run_cgi(request &r, Respond &res)
202204
close(fdtemp1);
203205

204206
//put_cookie(content, res);
207+
std::cout << "Content :::::::::::\n" << content << std::endl;
205208
set_headers_cgi(content, res);
209+
std::cout << "body :: \n" << res.get_response_body() << std::endl;
206210
//std::cout << content << std::endl;
207211
return (content);
208212
}

Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
NAME = webserv
22
CC = c++
3-
CFLAGS = -Wall -Wextra -Werror -std=c++98 -g -fsanitize=address
3+
# CFLAGS = -Wall -Wextra -Werror -std=c++98 -g -fsanitize=address
4+
CFLAGS = -Wall -Wextra -Werror -std=c++98
45
SRCS = prs_rsc/main.cpp prs_rsc/server.cpp prs_rsc/location.cpp prs_rsc/server_utils.cpp \
56
request/request.cpp request/request_utils.cpp respond/respond.cpp respond/respond_root.cpp respond/pairs_def.cpp respond/method_utils.cpp \
67
respond/method_handling.cpp CGI/cgi.cpp server/sockets.cpp server/http_server.cpp

prs_rsc/server.conf

+3-6
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,10 @@ server {
1313
index index.html;
1414

1515

16-
location /favicon.ico {
17-
allow_methods GET POST DELETE;
18-
root ./www/html/favicon.ico;
19-
}
16+
2017

2118
location / {
2219
allow_methods GET POST DELETE;
23-
root ./www/html/favicon.ico;
2420
}
2521

2622
location /cgi_bin
@@ -29,7 +25,7 @@ server {
2925
allow_methods GET POST;
3026
index hello.html;
3127
path_info .py /usr/bin/python3;
32-
path_info .php ./php-cgi;
28+
path_info .php /php-cgi;
3329
}
3430

3531
location /red
@@ -49,6 +45,7 @@ server {
4945
index upload.html;
5046
allow_methods POST GET;
5147
upload on;
48+
upload_store /upload;
5249
}
5350
}
5451

prs_rsc/server.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,11 @@ server& server::operator=(const server& o)
596596
return (*this);
597597
}
598598

599+
bool server::get_upload() const
600+
{
601+
return(_upload);
602+
}
603+
599604
server::server(const server& o)
600605
{
601606
*this = o;

prs_rsc/server.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class server
4949
std::map<int, std::string> get_error_page() const;
5050
std::vector<std::string> get_allow_methods() const;
5151
bool get_autoindex () const;
52+
bool get_upload() const;
5253
std::pair <int , std::string> get_redirection() const;
5354
std::string get_upload_store() const;
5455
std::map<std::string ,std::string> get_path_info() const;

request/Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ SRC = request.cpp main.cpp request_utils.cpp
44

55
CC = c++
66

7-
FLAGS = -std=c++98 -Wall -Wextra -Werror -g -fsanitize=address
8-
# FLAGS = -std=c++98 -Wall -Wextra -Werror
7+
# FLAGS = -std=c++98 -Wall -Wextra -Werror -g -fsanitize=address
8+
FLAGS = -std=c++98 -Wall -Wextra -Werror
99

1010
OBJS = $(SRC:.c=.o)
1111

request/request.cpp

+20-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: aoumad <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/03/26 23:05:21 by aoumad #+# #+# */
9-
/* Updated: 2023/05/08 16:17:03 by aoumad ### ########.fr */
9+
/* Updated: 2023/05/10 23:56:28 by aoumad ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -111,6 +111,9 @@ std::map<std::string, std::string> request::get_headers() const
111111

112112
void request::parse_request(std::string request)
113113
{
114+
// std::cout << "___________33______" << std::endl;
115+
// std::cout << request << std::endl;
116+
// std::cout << "________55_________" << std::endl;
114117
// Split the request into lines
115118
std::vector<std::string> lines;
116119
std::istringstream iss(request);
@@ -141,7 +144,8 @@ void request::parse_request(std::string request)
141144
// Trim leading and trailing whitespaces from the value
142145
value.erase(0, value.find_first_not_of(" \t\r"));
143146
value.erase(value.find_last_not_of(" \t\r") + 1);
144-
this->_headers[key] = value;
147+
if (this->_headers.find(key) == this->_headers.end())
148+
this->_headers[key] = value;
145149
}
146150

147151
// function that checks if the request is POST or PUT to see if there is no content-length to return error
@@ -176,11 +180,18 @@ void request::parse_request(std::string request)
176180
if (content_len_str != "")
177181
{
178182
size_t content_len = std::stoi(content_len_str);
179-
this->_body = lines.back().substr(0, content_len);
183+
if (content_len > request.size())
184+
{
185+
std::cerr << "Invalid Content-Length" << std::endl;
186+
exit(1);
187+
}
188+
this->_body = request.substr(request.size() - content_len);
189+
// std::cout << "S T A R T O F REQUEST B O D Y" << std::endl;
190+
// std::cout << this->_body << std::endl;
191+
// std::cout << "E N D O F REQUEST B O D Y" << std::endl;
180192
}
181193
else
182194
{
183-
// std::cout << "____----_-_-_-_--_-_____-_-_-_-____-_-_-_-_-------" << std::endl;
184195
if (content_len_str == "" || this->_method == "POST")
185196
{
186197
set_body("");
@@ -199,4 +210,9 @@ void request::print_request()
199210
for (std::map<std::string, std::string>::const_iterator it = this->_headers.begin(); it != this->_headers.end(); ++it)
200211
std::cout << it->first << ": " << it->second << std::endl;
201212
std::cout << "Body: " << this->_body << std::endl;
213+
}
214+
215+
std::string request::get_boundary() const
216+
{
217+
return (this->_boundary);
202218
}

request/request.hpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: aoumad <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/03/26 18:00:51 by aoumad #+# #+# */
9-
/* Updated: 2023/04/30 19:25:11 by aoumad ### ########.fr */
9+
/* Updated: 2023/05/10 19:01:41 by aoumad ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -40,6 +40,7 @@ class request
4040
std::map<std::string, std::string> _charset;
4141
int _port;
4242
std::string _query;
43+
std::string _boundary;
4344

4445
public:
4546
request();
@@ -54,6 +55,7 @@ class request
5455
std::string get_version() const;
5556
std::string get_body() const;
5657
std::string get_query() const;
58+
std::string get_boundary() const;
5759
// int ft_get_port() const;
5860

5961
void set_method(std::string method);

request/request_utils.cpp

+20-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: aoumad <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/03/28 21:18:13 by aoumad #+# #+# */
9-
/* Updated: 2023/05/05 16:30:43 by aoumad ### ########.fr */
9+
/* Updated: 2023/05/10 22:53:09 by aoumad ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -45,7 +45,7 @@ void request::ft_parse_port(std::string host)
4545

4646
int request::ft_check_content_length()
4747
{
48-
if (this->_method == "POST" || this->_method == "PUT")
48+
if (this->_method == "POST")
4949
{
5050
if (this->_headers.find("Content-Length") == this->_headers.end())
5151
return (0);
@@ -55,10 +55,27 @@ int request::ft_check_content_length()
5555

5656
int request::ft_check_content_type()
5757
{
58-
if (this->_method == "POST" || this->_method == "PUT")
58+
if (this->_method == "POST")
5959
{
6060
if (this->_headers.find("Content-Type") == this->_headers.end())
6161
return (0);
62+
else
63+
{
64+
std::string content_type = this->_headers["Content-Type"];
65+
if (content_type.find("multipart/form-data") != std::string::npos)
66+
{
67+
std::string::size_type pos;
68+
pos = content_type.find("boundary=");
69+
if (pos == std::string::npos)
70+
return (0);
71+
this->_boundary = content_type.substr(pos + 9);
72+
std::string::size_type end_pos;
73+
end_pos = content_type.find(";");
74+
content_type = content_type.substr(0, end_pos);
75+
add_header("Content-Type", content_type);
76+
return (1);
77+
}
78+
}
6279
}
6380
return (1);
6481
}

respond/additional_class.hpp

+17
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,23 @@ class FormData
1111
{
1212
return (!name.empty() && !data.empty());
1313
}
14+
// getters
15+
std::string get_name() const
16+
{
17+
return (name);
18+
}
19+
std::string get_content_type() const
20+
{
21+
return (content_type);
22+
}
23+
std::string get_data() const
24+
{
25+
return (data);
26+
}
27+
std::string get_file_name() const
28+
{
29+
return (file_name);
30+
}
1431
};
1532
class Url_encoded
1633
{

0 commit comments

Comments
 (0)