Skip to content

Commit 4510f95

Browse files
committed
before changing
1 parent abe3ece commit 4510f95

File tree

7 files changed

+86
-86
lines changed

7 files changed

+86
-86
lines changed

Makefile

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

prs_rsc/server.conf

+16-49
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,50 @@
1-
# server {
2-
# listen 8004;
3-
# listen 8002;
4-
# listen 8003;
5-
# server_name localhost;
6-
# host 127.0.0.1;
7-
# client_max_body_size 200000;
8-
# error_page 404 error_pages/404.html;
9-
# error_page 500 error_pages/500.html;
10-
# autoindex on;
11-
12-
# location /favicon.ico
13-
# {
14-
# #allow_methods GET POST DELETE;
15-
# root ./web_pages/favicon.ico;
16-
# return 301 https://www.example.com$request_uri;
17-
# }
18-
19-
# location /favicon.php
20-
# {
21-
# allow_methods GET POST DELETE;
22-
# }
23-
# location /favicon.php
24-
# {
25-
# allow_methods GET POST DELETE;
26-
# }
27-
# }
28-
29-
30-
31-
321
server {
33-
# default server;
342

353
listen 8001;
364

375

386
host 127.0.0.1;
397
client_max_body_size 10000000;
40-
root ./www/html/ffuyu;
8+
root ./www/html;
419
error_page 404 www/html/error_pages/404.html;
4210
error_page 500 www/html/error_pages/500.html;
4311

44-
#allow_methods GET POST DELETE;
4512
autoindex on;
46-
index index.html ;
47-
#meme_types ./configs/types.conf;
13+
index index.html;
4814

4915

16+
location /favicon.ico {
17+
allow_methods GET POST DELETE;
18+
root ./www/html/favicon.ico;
19+
}
20+
5021
location / {
5122
allow_methods GET POST DELETE;
5223
root ./www/html/favicon.ico;
53-
autoindex on;
5424
}
5525

56-
location /cgi_bin {
26+
location /cgi_bin
27+
{
5728
root ./www/html/cgi_bin;
5829
allow_methods GET POST;
5930
index hello.html;
6031
path_info .py /usr/bin/python3;
6132
path_info .php ./php-cgi;
6233
}
6334

64-
location /red {
35+
location /red
36+
{
6537
return 301 https://www.youtube.com/watch?v=_Nbm2yn8WA8&t=30s;
6638
}
6739

68-
location /delete
69-
{
40+
location /delete {
7041
root ./www/html/delete;
7142
allow_methods DELETE;
7243
index todelete;
7344
path_info .py /usr/bin/python3;
7445
}
7546

76-
location /upload
77-
{
47+
location /upload{
7848
root ./www/html/upload;
7949
index upload.html;
8050
allow_methods POST GET;
@@ -84,23 +54,20 @@ server {
8454

8555
server {
8656
listen 8000;
87-
root ./www/html/delete;
57+
8858
host 127.0.0.1;
8959

60+
server_name mazhari;
9061
client_max_body_size 26336;
91-
#allow_methods GET POST DELETE;
9262
autoindex on;
9363
index server2.html;
94-
#meme_types ./configs/types.conf;
9564
}
9665

9766
server {
9867
listen 8003;
9968

10069
host 127.0.0.1;
10170
client_max_body_size 26000;
102-
#allow_methods GET POST DELETE;
10371
autoindex on;
10472
index server3.html;
105-
#meme_types ./configs/types.conf;
106-
}
73+
}

respond/method_handling.cpp

+18-9
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/08 18:04:33 by aoumad ### ########.fr */
9+
/* Updated: 2023/05/09 17:50:06 by aoumad ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -22,16 +22,25 @@ void Respond::handle_get_response(std::vector<server> servers)
2222
}
2323
// step 3: check if it's a file or not
2424
if (ft_check_file() == true)
25-
return (ft_handle_file());
26-
else
27-
return (handle_error_response(404));
25+
{
26+
ft_handle_file();
27+
return ;
28+
}
29+
// else
30+
// {
31+
// std::cout << "___--_------__------_-_-_--_-__-_-_-_-HEREEEEE_--_-_-_--_-_-_-_-" << std::endl;
32+
// handle_error_response(404);
33+
// return ;
34+
// }
2835
// step 4 : check the index in the configuration file and render it
29-
ft_handle_index(servers);
30-
36+
if (ft_handle_index(servers))
37+
return ;
3138
// step 5: check if the autoindex if on or off
32-
ft_handle_autoindex(servers);
33-
34-
// ft_handle_error(404);
39+
if (ft_handle_autoindex(servers))
40+
{
41+
handle_error_response(403);
42+
return ;
43+
}
3544

3645
}
3746

respond/method_utils.cpp

+25-21
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/08 18:03:27 by aoumad ### ########.fr */
9+
/* Updated: 2023/05/09 17:29:24 by aoumad ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -31,7 +31,6 @@ void Respond::ft_handle_file()
3131
handle_error_response(404);
3232
return;
3333
}
34-
3534
file.open(_rooted_path.c_str(), std::ifstream::in);
3635
if (file.is_open())
3736
{
@@ -62,15 +61,16 @@ int Respond::ft_handle_index(std::vector<server> server)
6261
if (server[_server_index]._location[_location_index].get_index().empty())
6362
{
6463
if (server[_server_index].get_index().empty())
65-
return (handle_error_response(403));
64+
{
65+
handle_error_response(403);
66+
return (1);
67+
}
6668
else
6769
{
6870
index = server[_server_index].get_index();
6971
_rooted_path = server[_server_index].get_root() + _removed_path + index;
7072
if (ft_handle_index_2())
7173
return (1);
72-
else
73-
return (0);
7474
}
7575
}
7676
else
@@ -79,15 +79,12 @@ int Respond::ft_handle_index(std::vector<server> server)
7979
_rooted_path = server[_location_index].get_root() + _removed_path + index;
8080
if (ft_handle_index_2())
8181
return (1);
82-
else
83-
return (0);
8482
}
8583
}
86-
}
87-
}
84+
return (0);
8885
}
8986

90-
void Respond::ft_handle_index_2()
87+
int Respond::ft_handle_index_2()
9188
{
9289
std::ifstream file;
9390
if (_rooted_path != "")
@@ -103,21 +100,24 @@ void Respond::ft_handle_index_2()
103100
_headers["Connection"] = "keep-alive";
104101
set_date();
105102
set_cache_control("no cache");
103+
return (0);
106104
}
107-
else
108-
{
109-
handle_error_response(403);
110-
return (1);
111-
}
105+
// else
106+
// {
107+
// std::cout << "___--_------__------_-_-_--_-__-_-_-_-HEREEEEE_--_-_-_--_-_-_-_-" << std::endl;
108+
// handle_error_response(403);
109+
// return (1);
110+
// }
112111
}
113112
else
114113
{
115114
handle_error_response(404);
116115
return (1);
117116
}
117+
return (0);
118118
}
119119

120-
void Respond::ft_handle_autoindex(std::vector<server> server)
120+
int Respond::ft_handle_autoindex(std::vector<server> server)
121121
{
122122
for (size_t i = 0; i < server.size(); i++)
123123
{
@@ -128,18 +128,22 @@ void Respond::ft_handle_autoindex(std::vector<server> server)
128128
if (!server[i]._location[j].get_autoindex())
129129
{
130130
if (!server[i].get_autoindex())
131-
{
132-
// show forbidden result
133-
ft_handle_error(403);
134-
}
131+
return (1);
135132
else
133+
{
136134
ft_show_autoindex();
135+
return (0);
136+
}
137137
}
138138
else
139+
{
139140
ft_show_autoindex();
141+
return (0);
142+
}
140143
}
141144
}
142145
}
146+
return (1);
143147
}
144148

145149
void Respond::ft_handle_error(int error_code)
@@ -205,7 +209,7 @@ void Respond::ft_show_autoindex()
205209

206210
void Respond::handle_error_response(int error_code)
207211
{
208-
set_status_code(_status_code);
212+
set_status_code(error_code);
209213
set_status_message(get_response_status(get_status_code()));
210214
set_header("Content-Type", "text/html");
211215
set_header("Connection", "keep-alive");

respond/respond.cpp

+19-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ Content-Length: 1234\r\n
1313
Respond::Respond(request& req, int index_) : r(req)
1414
{
1515
_http_version = "HTTP/1.1";
16-
_response_body = "";
1716
_status_code = 200;
1817
_status_message = "OK";
1918
_path_found = "";
@@ -167,4 +166,23 @@ std::string Respond::rtn_response()
167166
response += "\r\n";
168167
response += _response_body;
169168
return (response);
169+
}
170+
171+
void Respond::init_response_body(std::string file, std::string _root)
172+
{
173+
std::ifstream file_;
174+
std::string line;
175+
176+
std::string f;
177+
f = _root + "/" + file;
178+
file_.open(f);
179+
std::cout << "file path: " << f << std::endl;
180+
if (file_.is_open())
181+
{
182+
while (getline(file_, line))
183+
_response_body += line + "\n";
184+
file_.close();
185+
}
186+
else
187+
std::cout << "Unable to open file" << std::endl;
170188
}

respond/respond.hpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: aoumad <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/03/28 20:49:15 by aoumad #+# #+# */
9-
/* Updated: 2023/05/07 16:09:14 by aoumad ### ########.fr */
9+
/* Updated: 2023/05/09 17:44:26 by aoumad ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -84,11 +84,11 @@ class Respond
8484
// GET RESPONSE
8585
void ft_handle_cgi();
8686
void ft_handle_file();
87-
void ft_handle_autoindex(std::vector<server> servers);
87+
int ft_handle_autoindex(std::vector<server> servers);
8888
void ft_check_cgi();
8989
int ft_check_file();
9090
int ft_handle_index(std::vector<server> servers);
91-
void ft_handle_index_2();
91+
int ft_handle_index_2();
9292
void ft_show_autoindex();
9393

9494
// POST RESPONSE
@@ -129,6 +129,7 @@ class Respond
129129

130130
void handle_get_response(std::vector<server> servers);
131131
void print_response();
132+
void init_response_body(std::string file, std::string _root);
132133

133134
request& r;
134135
void create_decode_files();

respond/respond_root.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,23 @@
66
/* By: aoumad <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/04/29 14:53:31 by aoumad #+# #+# */
9-
/* Updated: 2023/05/08 18:03:36 by aoumad ### ########.fr */
9+
/* Updated: 2023/05/09 17:57:45 by aoumad ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

1313
# include "respond.hpp"
1414

1515
std::string Respond::response_root(std::vector<server> servers)
1616
{
17+
init_response_body(servers[_server_index].get_index(), servers[_server_index].get_root());
1718
// step 1 :check the location
1819
if (ft_parse_location(servers))
1920
{
2021
// considering the location of the root directory is indeed the final step in handling requests when no specific location matches the requested URI.
2122
// if (root_location(servers) == 0)
2223
if (root_location(servers) == 1)
2324
{
25+
std::cout << "___--_------__------_-_-_--_-__-_-_-_-HEREEEEE_--_-_-_--_-_-_-_-" << std::endl;
2426
handle_error_response(404);
2527
return (rtn_response());
2628
}
@@ -44,7 +46,6 @@ std::string Respond::response_root(std::vector<server> servers)
4446
}
4547
// step 5 : check the autoindex
4648
ft_check_autoindex(servers);
47-
4849
// methods area
4950
if (r.get_method() == "GET")
5051
handle_get_response(servers);

0 commit comments

Comments
 (0)