Skip to content

Commit 63f2559

Browse files
committed
done
1 parent acb29d6 commit 63f2559

File tree

7 files changed

+99
-152
lines changed

7 files changed

+99
-152
lines changed

config/server.conf

Lines changed: 58 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,21 @@ server
1414
error_page 413 www/html/error_pages/413.html;
1515
error_page 500 www/html/error_pages/500.html;
1616

17-
# index index.html;
17+
index index.html;
1818

1919
location / {
20-
autoindex on;
20+
autoindex off;
2121
}
2222

2323
location /cgi_bin
2424
{
2525
root ./www/html/cgi_bin;
2626
allow_Methods GET;
2727
index hello.html;
28-
path_info .py /usr/bin/python3;
29-
path_info .php /php-cgi;
30-
#listen 8001;
31-
autoindex on;
28+
# path_info .py /usr/bin/python3;
29+
# path_info .php /php-cgi;
30+
# #listen 8001;
31+
autoindex off;
3232
}
3333

3434
location /red
@@ -59,67 +59,67 @@ server
5959
autoindex on;
6060
}
6161
}
62-
# server
63-
# {
64-
# listen 8002;
62+
server
63+
{
64+
listen 8002;
6565

66-
# host 127.0.0.1;
67-
# client_max_body_size 10000000;
68-
# root ./www/html;
69-
# server_name localhostp;
70-
# error_page 404 www/html/error_pages/404.html;
71-
# error_page 403 www/html/error_pages/403.html;
72-
# error_page 409 www/html/error_pages/409.html;
73-
# error_page 400 www/html/error_pages/400.html;
74-
# error_page 405 www/html/error_pages/405.html;
75-
# error_page 413 www/html/error_pages/413.html;
76-
# error_page 500 www/html/error_pages/500.html;
66+
host 127.0.0.1;
67+
client_max_body_size 10000000;
68+
root ./www/html;
69+
server_name localhostp;
70+
error_page 404 www/html/error_pages/404.html;
71+
error_page 403 www/html/error_pages/403.html;
72+
error_page 409 www/html/error_pages/409.html;
73+
error_page 400 www/html/error_pages/400.html;
74+
error_page 405 www/html/error_pages/405.html;
75+
error_page 413 www/html/error_pages/413.html;
76+
error_page 500 www/html/error_pages/500.html;
7777

78-
# index index.html;
78+
index index.html;
7979

80-
# location / {
81-
# autoindex on;
82-
# }
80+
location / {
81+
autoindex on;
82+
}
8383

84-
# location /cgi_bin
85-
# {
84+
location /cgi_bin
85+
{
8686

87-
# root ./www/html;
88-
# allow_Methods GET POST;
89-
# index hello.html;
90-
# path_info .py /usr/bin/python3;
91-
# path_info .php /php-cgi;
92-
# #listen 8001;
93-
# #autoindex on;
94-
# }
87+
root ./www/html/cgi_bin;
88+
allow_Methods GET POST;
89+
index hello.html;
90+
path_info .py /usr/bin/python3;
91+
path_info .php /php-cgi;
92+
#listen 8001;
93+
#autoindex on;
94+
}
9595

96-
# location /red
97-
# {
98-
# return 301 https://www.youtube.com/watch?v=_Nbm2yn8WA8&t=30s;
99-
# }
96+
location /red
97+
{
98+
return 301 https://www.youtube.com/watch?v=_Nbm2yn8WA8&t=30s;
99+
}
100100

101-
# location /delete {
102-
# root ./www/html/delete;
103-
# allow_methods DELETE;
104-
# index todelete;
105-
# path_info .py /usr/bin/python3;
106-
# }
101+
location /delete {
102+
root ./www/html/delete;
103+
allow_methods DELETE;
104+
index todelete;
105+
path_info .py /usr/bin/python3;
106+
}
107107

108-
# location /upload{
109-
# root ./www/html/upload;
110-
# index upload.html;
111-
# allow_methods POST GET;
112-
# upload on;
113-
# upload_store /upload;
114-
# }
108+
location /upload{
109+
root ./www/html/upload;
110+
index upload.html;
111+
allow_methods POST GET;
112+
upload on;
113+
upload_store ./www/html/upload;
114+
}
115115

116-
# location /getto {
117-
# root ./www/html/upload;
118-
# index vid.mp4;
119-
# allow_methods GET;
120-
# autoindex off;
121-
# }
122-
# }
116+
location /getto {
117+
root ./www/html/upload;
118+
index vid.mp4;
119+
allow_methods GET;
120+
autoindex off;
121+
}
122+
}
123123

124124
# server
125125
# {

prs_rsc/main.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,6 @@ int main(int ac, char **av)
1515
std::vector<int> all_ports;
1616
servers = ft_fill_servers(av, ac);
1717
all_ports = get_all_ports(servers);
18-
// for (size_t i = 0; i < servers.size(); i++)
19-
// {
20-
// servers[i].display_sever();
21-
// std::cout << "locations :::::::::::::::::::::::::::::::::::::::::::: \n";
22-
// for (size_t j = 0; j < servers[i]._location.size(); j++)
23-
// {
24-
// std::cout << "location : " << servers[i]._location[j].location_name << std::endl;
25-
// servers[i]._location[j].display_sever();
26-
// std::cout << ":::::::::::::::::::::::::::::::::::::::::::: \n";
27-
// }
28-
// std::cout << "++++++++++++++++++++++\n";
29-
// }
30-
// for (size_t i = 0; i < all_ports.size(); i++)
31-
// std::cout << all_ports[i] << "\n";
3218
http::http_sever server1(servers);
3319
server1.run();
3420
return (0);

respond/method_handling.cpp

Lines changed: 29 additions & 18 deletions
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/24 12:16:06 by aoumad ### ########.fr */
9+
/* Updated: 2023/05/24 16:04:10 by aoumad ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -82,7 +82,11 @@ void Respond::handle_post_response(std::vector<server> server)
8282
else
8383
{
8484
handle_urlencoded();
85-
create_decode_files();
85+
if (create_decode_files() == 2)
86+
{
87+
handle_error_response(server, 400);
88+
return ;
89+
}
8690
std::string path = r.get_uri();
8791
std::string::size_type i = r.get_uri().find_last_of('/');
8892
if (i != std::string::npos)
@@ -95,7 +99,8 @@ void Respond::handle_post_response(std::vector<server> server)
9599
}
96100
if (check_post_type() == "form-data")
97101
{
98-
handle_form_data(server);
102+
if (handle_form_data(server) == 2)
103+
handle_error_response(server, 400);
99104
set_status_code(201);
100105
set_status_message(get_response_status(201));
101106
return ;
@@ -127,7 +132,7 @@ void Respond::handle_urlencoded()
127132
}
128133
}
129134

130-
void Respond::create_decode_files()
135+
int Respond::create_decode_files()
131136
{
132137
std::string file_name;
133138
std::string file_content;
@@ -138,20 +143,23 @@ void Respond::create_decode_files()
138143
{
139144
file_name = _upload_store;
140145
file_name += "/" + it->key;
146+
// Check if you have permission to access the file
147+
if (access(file_name.c_str(), R_OK) != 0)
148+
return (2);
141149
file.open(file_name.c_str());
142150
file << it->value;
143151
file.close();
144152
it++;
145153
}
154+
return (0);
146155
}
147156

148-
void Respond::handle_form_data(std::vector<server> server)
157+
int Respond::handle_form_data(std::vector<server> server)
149158
{
150-
// std::cout << r.get_body() << std::endl;
151159
// Find the first boundary
152160
size_t pos = r.get_body().find(_boundary);
153161
if (!pos)
154-
return ;
162+
return (2);
155163
// Loop through the form data, locating boundaries and reading data betweem them
156164
while (true)
157165
{
@@ -164,24 +172,28 @@ void Respond::handle_form_data(std::vector<server> server)
164172
if (_file_too_large == true)
165173
{
166174
handle_error_response(server, 413);
167-
return ;
175+
return (2);
168176
}
169177
if (formData.isValid())
170178
_form_data.push_back(formData); // Add the form data to the list
171-
// std::cout << "pos before: " << pos << std::endl;
172179
if (_last_boundary == true)
173180
break;
174181
pos += _boundary.length() + 2;
175182
}
176-
create_form_data();
183+
if (create_form_data() == 2)
184+
{
185+
handle_error_response(server, 400);
186+
return (2);
187+
}
177188
std::string path = r.get_uri();
178189
std::string::size_type i = r.get_uri().find_last_of('/');
179190
if (i != std::string::npos)
180191
path = r.get_uri().substr(i);
181192
init_response_body(server, path, server[_server_index]._location[_location_index].get_root());
193+
return (0);
182194
}
183195

184-
void Respond::create_form_data()
196+
int Respond::create_form_data()
185197
{
186198
std::string file_name;
187199
std::string file_content;
@@ -197,12 +209,15 @@ void Respond::create_form_data()
197209
}
198210
file_name = _upload_store;
199211
file_name += "/" + it->get_file_name();
200-
std::cout << file_name << std::endl;
212+
// Check if you have permission to access the file
213+
if (access(file_name.c_str(), R_OK) != 0)
214+
return (2);
201215
file.open(file_name.c_str());
202216
file << it->get_data();
203217
file.close();
204218
it++;
205219
}
220+
return (0);
206221
}
207222
// Helper function to locate the next boundary in the form data
208223
size_t Respond::find_boundary(size_t pos)
@@ -226,7 +241,6 @@ FormData Respond::read_form_data(std::vector<server> servers ,size_t pos)
226241
size_t end = r.get_body().find(_boundary, start);
227242
if (end == std::string::npos)
228243
{
229-
std::cout << form_data.get_data() << std::endl;
230244
return (form_data); // Boundary not found
231245
}
232246

@@ -261,12 +275,11 @@ FormData Respond::read_form_data(std::vector<server> servers ,size_t pos)
261275

262276
// Process the data content
263277
std::string data_content = section.substr(header_end + 4); // Skip the CRLF delimiter
264-
std::cout << (unsigned int)servers[_server_index].get_client_max_body_size() << std::endl;
265-
std::cout << servers[_server_index].get_client_max_body_size() << std::endl;
278+
266279
if (data_content.length() * 8 >= (unsigned int)servers[_server_index].get_client_max_body_size())
267280
_file_too_large = true;
281+
268282
form_data.data = data_content;
269-
270283
return (form_data);
271284
}
272285

@@ -282,8 +295,6 @@ std::string Respond::check_post_type()
282295

283296
void Respond::handle_delete_response(std::vector<server> server)
284297
{
285-
//std::cout << "DKHLAAAAAAAAAAT" << std::endl;
286-
//std::cout << "rooted path:" << _rooted_path << std::endl;
287298
if (std::remove(_rooted_path.c_str()) == 0)
288299
{
289300
_status_code = 200;

respond/method_utils.cpp

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: aoumad <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/04/11 02:14:39 by aoumad #+# #+# */
9-
/* Updated: 2023/05/24 01:02:19 by aoumad ### ########.fr */
9+
/* Updated: 2023/05/24 14:24:34 by aoumad ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -202,7 +202,6 @@ void Respond::ft_handle_error(int error_code)
202202

203203
void Respond::ft_show_autoindex(std::vector<server> server)
204204
{
205-
//std::cout << "rooted path: " << _rooted_path << std::endl;
206205
std::string index_html = "<!DOCTYPE html>\n<html>\n<head>\n";
207206
index_html += "<meta charset=\"UTF-8\">\n";
208207
index_html += "<title>Index of " + _rooted_path + "</title>\n";
@@ -235,7 +234,6 @@ void Respond::ft_show_autoindex(std::vector<server> server)
235234
file_path = _path_found + file_name; // /cgi_bin
236235
else
237236
file_path = _path_found + "/" + file_name;
238-
// std::cout << "file path: " << file_path << std::endl;
239237
std::string match_path;
240238
if (_rooted_path[_rooted_path.size() - 1] == '/')
241239
match_path = _rooted_path + file_name; // www/html/cgi_bin
@@ -295,14 +293,4 @@ void Respond::handle_error_response(std::vector<server> server, int error_cod
295293
set_date();
296294
set_cache_control("no cache");
297295
}
298-
}
299-
300-
void Respond::print_response()
301-
{
302-
// std::cout << "HTTP/1.1 " << get_status_code() << " " << get_status_message() << "\r\n";
303-
// for (std::map<std::string, std::string>::iterator it = _headers.begin(); it != _headers.end(); it++)
304-
// std::cout << it->first << ": " << it->second << "\r\n";
305-
std::cout << "\r\n";
306-
// if (get_response_body() != "")
307-
// std::cout << "response body: " << _response_body << std::endl;;
308296
}

0 commit comments

Comments
 (0)