Skip to content

Commit 7c6b879

Browse files
authored
Merge pull request #47 from younes-ismaili/aoumad
fixed request and some stuff on response
2 parents e28afd9 + 73d8e01 commit 7c6b879

12 files changed

+51
-297
lines changed

CGI/cgi.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ std::string run_cgi(request &r, Respond &res)
149149
if (pid == -1)
150150
{
151151
res.set_status_code(500);
152+
res.set_status_message(res.get_response_status(res.get_status_code()));
152153
return NULL;
153154
}
154155
else if (pid == 0)
@@ -176,8 +177,11 @@ std::string run_cgi(request &r, Respond &res)
176177

177178
int status;
178179
waitpid(pid, &status, 0);
179-
if (WIFSIGNALED(status) || status != 0)
180+
if (WIFSIGNALED(status) || status != 0)
181+
{
180182
res.set_status_code(500);
183+
res.set_status_message(res.get_response_status(res.get_status_code()));
184+
}
181185
char buf[1];
182186
std::string content;
183187
int byt;

prs_rsc/server.conf

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ server {
4747
#meme_types ./configs/types.conf;
4848

4949

50-
location /favicon.ico {
50+
location / {
5151
allow_methods GET POST DELETE;
5252
root ./www/html/favicon.ico;
53+
autoindex on;
5354
}
5455

5556
location /cgi_bin {

request/http_server.cpp

-91
This file was deleted.

request/httpml.cpp

-91
This file was deleted.

request/index.html

-11
This file was deleted.

request/main.cpp

-31
This file was deleted.

request/request.cpp

+8-44
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/05 16:33:19 by aoumad ### ########.fr */
9+
/* Updated: 2023/05/08 16:17:03 by aoumad ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -119,6 +119,7 @@ void request::parse_request(std::string request)
119119
lines.push_back(line);
120120
}
121121
// Parse the request line
122+
// std::cout << lines[0] << std::endl;
122123
std::istringstream request_line(lines[0]);
123124
request_line >> this->_method >> this->_uri >> this->_version;
124125
// i need to call a function to check if the request line content is suitable or not
@@ -164,62 +165,25 @@ void request::parse_request(std::string request)
164165
exit(1);
165166
}
166167
ft_parse_port(this->get_header("Host"));
167-
ft_parse_language_charset();
168+
// ft_parse_language_charset();
168169

169170
// Parse the request body
170171
std::string content_len_str = this->get_header("Content-Length");
171-
if (!content_len_str.empty())
172+
if (content_len_str != "")
172173
{
173174
size_t content_len = std::stoi(content_len_str);
174175
this->_body = lines.back().substr(0, content_len);
175-
// std::string transfer_encoding = this->get_header("Accept-Encoding");
176-
// if (!transfer_encoding.empty())
177-
// {
178-
// // using pointers to member functions to call the functions handlers
179-
// std::vector<std::string> encoding_types;
180-
// size_t startPos = 0;
181-
// size_t endPos = transfer_encoding.find(',');
182-
// while (endPos != std::string::npos)
183-
// {
184-
// encoding_types.push_back(transfer_encoding.substr(startPos, endPos - startPos));
185-
// startPos = endPos + 1;
186-
// endPos = transfer_encoding.find(',', startPos);
187-
// }
188-
// encoding_types.push_back(transfer_encoding.substr(startPos, endPos - startPos));
189-
// for (std::vector<std::string>::const_iterator it = encoding_types.begin(); it != encoding_types.end(); ++it)
190-
// {
191-
// std::string type_tmp = *it;
192-
// // Trim leading and trailing whitespaces from the value
193-
// // type_tmp.erase(0, type_tmp.find_first_not_of(" \t"));
194-
// // type_tmp.erase(type_tmp.find_first_not_of(" \t") + 1);
195-
// bool supported = false;
196-
// for (size_t i = 0; i < sizeof(handlers) / sizeof(handlers[0]); ++i)
197-
// {
198-
// if (type_tmp == supported_encodings[i])
199-
// {
200-
// (this->*handlers[i])(this->_body);
201-
// supported = true;
202-
// break;
203-
// }
204-
// }
205-
// if (!supported)
206-
// std::cerr << "Unsupported encoding type: " << type_tmp << std::endl;
207-
// }
208-
209-
// }
210176
}
211177
else
212178
{
213-
if (this->_headers.find("Content-Length") != this->_headers.end() || this->_method == "POST" || this->_method == "PUT"
214-
|| this->_headers.find("Content-Type") != this->_headers.end())
179+
// std::cout << "____----_-_-_-_--_-_____-_-_-_-____-_-_-_-_-------" << std::endl;
180+
if (content_len_str == "" || this->_method == "POST")
215181
{
216-
std::cerr << "Body request is missing" << std::endl;
217-
exit(1);
182+
set_body("");
183+
return ;
218184
}
219185

220186
}
221-
222-
// print_request();
223187
}
224188

225189
void request::print_request()

respond/method_handling.cpp

+10-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: aoumad <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/04/09 17:52:50 by aoumad #+# #+# */
9-
/* Updated: 2023/05/07 00:31:03 by aoumad ### ########.fr */
9+
/* Updated: 2023/05/08 18:04:33 by aoumad ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -16,14 +16,13 @@ void Respond::handle_get_response(std::vector<server> servers)
1616
{
1717

1818
// step 2: check if it's a CGI or not (like if `index` of the configuration file has .py or .php...etc)
19-
2019
// step 3: check if it's a file or not
2120
if (ft_check_file() == true)
2221
ft_handle_file();
23-
22+
else
23+
handle_error_response(404);
2424
// step 4 : check the index in the configuration file and render it
25-
if (_is_index == true)
26-
ft_handle_index(servers);
25+
ft_handle_index(servers);
2726

2827
// step 5: check if the autoindex if on or off
2928
ft_handle_autoindex(servers);
@@ -34,6 +33,12 @@ void Respond::handle_get_response(std::vector<server> servers)
3433

3534
void Respond::handle_post_response(std::vector<server> server)
3635
{
36+
// step 1: check if the request body is empty or not
37+
if (r.get_body().empty())
38+
{
39+
set_response_body("Body request is missing");
40+
return ;
41+
}
3742
struct stat st;
3843
(void)server;
3944
// if (_is_cgi == false && (server[_server_index].get_upload_store().empty() || server[_server_index].get_upload() == "off"))

0 commit comments

Comments
 (0)