Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5e4787d

Browse files
author
Hamza Ait mohamed
committedApr 10, 2023
araaaa9
1 parent cb00e92 commit 5e4787d

File tree

8 files changed

+68
-68
lines changed

8 files changed

+68
-68
lines changed
 

‎parcing/config.conf

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ server
1212
allow_methods GET POST DELETE;
1313
index index.html index.php;
1414
autoindex 1;
15-
root /Users/azabir/Desktop/42Cursus/webserve;
16-
cgi_pass php /Users/azabir/Desktop/42Cursus/webserve/php-cgi;
15+
root /Users/hait-moh/Desktop/webserv/webserve;
16+
cgi_pass php /Users/hait-moh/Desktop/webserv/webserve/php-cgi;
1717
}
1818
location /dir {
1919
allow_methods GET POST DELETE;

‎server/http/CGI_Handler/cgi_handler.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ void http_response::CGI_executer() {
2121

2222
env[1] = strdup(("REQUEST_METHOD=" + request->get_method()).c_str());
2323
env[2] = strdup(("CONTENT_LENGTH=" + int_to_string(len)).c_str());
24-
env[3] = strdup(("SCRIPT_FILENAME=" + conf.root).c_str());
24+
env[3] = strdup(("SCRIPT_FILENAME=" + conf->root).c_str());
2525
env[4] = strdup("REDIRECT_STATUS=200");
2626
env[5] = NULL;
2727
free(pwd);
@@ -33,8 +33,8 @@ void http_response::CGI_executer() {
3333
dup2(fd, 0);
3434
dup2(out_fd, 1);
3535
close(fd);
36-
args[0] = strdup(conf.cgi_pass[0].cgi_param.c_str());
37-
args[1] = strdup(conf.root.c_str());
36+
args[0] = strdup(conf->cgi_pass[0].cgi_param.c_str());
37+
args[1] = strdup(conf->root.c_str());
3838
args[2] = NULL;
3939
execve(args[0], args, env);
4040
exit(1);

‎server/http/DELETE_Handler/delete_handler.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ void http_response::DELETE_check_state()
66
{
77
client->events = POLLOUT;
88
struct stat s;
9-
if (stat(conf.root.c_str(),&s) == 0)
9+
if (stat(conf->root.c_str(),&s) == 0)
1010
{
1111
if (S_ISDIR(s.st_mode))
1212
{
13-
if (access(conf.root.c_str(),W_OK))
13+
if (access(conf->root.c_str(),W_OK))
1414
throw FORBIDDEN;
1515
DIR *directory;
1616
struct dirent *d;
17-
directory = opendir(conf.root.c_str());
17+
directory = opendir(conf->root.c_str());
1818
if (!directory)
1919
throw FORBIDDEN;
2020
d = readdir(directory);
@@ -25,15 +25,15 @@ void http_response::DELETE_check_state()
2525
throw _CONFLICT;
2626
d = readdir(directory);
2727
}
28-
if (rmdir(conf.root.c_str()))
28+
if (rmdir(conf->root.c_str()))
2929
throw SERVER_ERROR;
3030
throw NO_CONTENT;
3131
}
3232
else
3333
{
34-
if (access((conf.root).c_str(),W_OK))
34+
if (access((conf->root).c_str(),W_OK))
3535
throw FORBIDDEN;
36-
if (remove(conf.root.c_str()))
36+
if (remove(conf->root.c_str()))
3737
throw SERVER_ERROR;
3838
throw NO_CONTENT;
3939
}

‎server/http/GET_Handler/get_handler.cpp

+14-14
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ void http_response::GET_check_state()
1515
{
1616
client->events = POLLOUT;
1717
struct stat s;
18-
std::cout << conf.root << std::endl;
19-
if (stat(conf.root.c_str(),&s) == 0)
18+
std::cout << conf->root << std::endl;
19+
if (stat(conf->root.c_str(),&s) == 0)
2020
{
2121
if (S_ISDIR(s.st_mode))
2222
{
23-
for (int i = 0;i < conf.index.size();i++)
23+
for (int i = 0;i < conf->index.size();i++)
2424
{
25-
std::ifstream f(conf.root + "/" + conf.index[i]);
25+
std::ifstream f(conf->root + "/" + conf->index[i]);
2626
if (f.good())
2727
{
2828
state = FILE;
29-
conf.root = conf.root + "/" + conf.index[i];
30-
if (check_cgi(conf.index[i]))
29+
conf->root = conf->root + "/" + conf->index[i];
30+
if (check_cgi(conf->index[i]))
3131
type = CGI;
3232
else
3333
type = GET;
@@ -37,14 +37,14 @@ void http_response::GET_check_state()
3737
else if (f.is_open())
3838
f.close();
3939
}
40-
if (conf.autoindex)
40+
if (conf->autoindex)
4141
state = LIST_DIRECTORY;
4242
else
4343
throw FORBIDDEN;
4444
}
4545
else
4646
{
47-
if (check_cgi(conf.root))
47+
if (check_cgi(conf->root))
4848
type = CGI;
4949
else
5050
{
@@ -60,15 +60,15 @@ void http_response::GET_check_state()
6060

6161
void http_response::GET_open_input()
6262
{
63-
file.open(conf.root);
64-
std::cout << conf.root << std::endl;
63+
file.open(conf->root);
64+
std::cout << conf->root << std::endl;
6565
if (!file)
6666
throw FORBIDDEN;
6767
res_header += "HTTP/1.1 200 OK\n";
68-
content_remaining = getSize(conf.root);
68+
content_remaining = getSize(conf->root);
6969
headers["Content-Length"] = int_to_string(content_remaining);
70-
std::string ext = (conf.root).substr(conf.root.find_last_of(".") + 1);
71-
if (ext != conf.root && content_type.count(ext))
70+
std::string ext = (conf->root).substr(conf->root.find_last_of(".") + 1);
71+
if (ext != conf->root && content_type.count(ext))
7272
headers["Content-Type"] = content_type[ext];
7373
else
7474
headers["Content-Type"] = "application/octet-stream";
@@ -99,7 +99,7 @@ void http_response::GET_list_directory()
9999
{
100100
DIR *directory;
101101
struct dirent *d;
102-
directory = opendir(conf.root.c_str());
102+
directory = opendir(conf->root.c_str());
103103
if (!directory)
104104
throw FORBIDDEN;
105105
d = readdir(directory);

‎server/http/POST_Handler/post_handler.cpp

+17-17
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ void http_response::POST_upload_chunked_handler(void)
6060
remove_white_spaces(body);
6161
substring = body.substr(0,body.find('\n'));
6262
content_remaining = strtoll(substring.c_str(),NULL,16);
63-
conf.client_max_body_size -= content_remaining;
64-
if (conf.client_max_body_size <= 0)
63+
conf->client_max_body_size -= content_remaining;
64+
if (conf->client_max_body_size <= 0)
6565
throw ENTITY_LARGE;
6666
if (errno == ERANGE)
6767
throw BAD_REQUEST;
@@ -123,20 +123,20 @@ void http_response::POST_upload_normal_handler(void)
123123
void http_response::POST_check_cgi()
124124
{
125125
struct stat s;
126-
if (stat(conf.root.c_str(),&s) == 0)
126+
if (stat(conf->root.c_str(),&s) == 0)
127127
{
128128
if (S_ISDIR(s.st_mode))
129129
{
130-
for (int i = 0;i < conf.index.size();i++)
130+
for (int i = 0;i < conf->index.size();i++)
131131
{
132-
if (check_cgi(conf.index[i]))
132+
if (check_cgi(conf->index[i]))
133133
{
134-
std::ifstream f(conf.root + "/" + conf.index[i]);
134+
std::ifstream f(conf->root + "/" + conf->index[i]);
135135
if (f.good())
136136
{
137137
is_cgi = 1;
138-
conf.upload_pass = "/tmp";
139-
conf.root = conf.root + "/" + conf.index[i];
138+
conf->upload_pass = "/tmp";
139+
conf->root = conf->root + "/" + conf->index[i];
140140
f.close();
141141
return;
142142
}
@@ -147,13 +147,13 @@ void http_response::POST_check_cgi()
147147
}
148148
else
149149
{
150-
if (check_cgi(conf.root))
150+
if (check_cgi(conf->root))
151151
{
152-
std::ifstream f(conf.root);
152+
std::ifstream f(conf->root);
153153
if (f.good())
154154
{
155155
is_cgi = 1;
156-
conf.upload_pass = "/tmp";
156+
conf->upload_pass = "/tmp";
157157
f.close();
158158
return;
159159
}
@@ -180,7 +180,7 @@ void http_response::POST_check_state()
180180
{
181181
content_remaining = strtoll(request->get_header("CONTENT-LENGTH").c_str(),NULL,10);
182182
state = NORMAL;
183-
if (content_remaining > conf.client_max_body_size)
183+
if (content_remaining > conf->client_max_body_size)
184184
throw ENTITY_LARGE;
185185
if (content_remaining <= ret)
186186
{
@@ -189,23 +189,23 @@ void http_response::POST_check_state()
189189
}
190190
}
191191

192-
if (conf.upload_pass == "")
192+
if (conf->upload_pass == "")
193193
POST_check_cgi();
194-
if (conf.upload_pass != "")
194+
if (conf->upload_pass != "")
195195
{
196196
std::string name;
197197
if (!is_cgi && request->get_header("FILE_NAME") != "")
198-
name = conf.upload_pass + '/' + request->get_header("FILE_NAME");
198+
name = conf->upload_pass + '/' + request->get_header("FILE_NAME");
199199
else
200-
name = conf.upload_pass + "/XXXXXX";
200+
name = conf->upload_pass + "/XXXXXX";
201201
int fd = mkstemp(&name[0]);
202202
if (fd == -1)
203203
throw SERVER_ERROR;
204204
cgi_data.input = name;
205205
if (is_cgi)
206206
{
207207
cgi_data.input_fd = fd;
208-
std::string output = conf.upload_pass + "/XXXXXX";
208+
std::string output = conf->upload_pass + "/XXXXXX";
209209
int ot_fd = mkstemp(&output[0]);
210210
if (ot_fd == -1)
211211
throw SERVER_ERROR;

‎server/http/http_response.cpp

+25-25
Original file line numberDiff line numberDiff line change
@@ -101,31 +101,31 @@ http_response::http_response(http_request *req,struct pollfd *fd,std::string &ho
101101
content_remaining = 0;
102102
request = req;
103103
void (http_response::*state_handlers[3])() = {&http_response::GET_check_state,&http_response::POST_check_state,&http_response::DELETE_check_state};
104-
conf = get_config(host,port,request->get_path(),request->get_header("HOST"));
105-
for (int i = 0; i < conf.methods.size(); i++) {
106-
std::cout << "method: [" << conf.methods[i] << "]\n";
104+
conf = &get_config(host,port,request->get_path(),request->get_header("HOST"));
105+
for (int i = 0; i < conf->methods.size(); i++) {
106+
std::cout << "method: [" << conf->methods[i] << "]\n";
107107
}
108-
std::cout << "root: [" << conf.root << "]\n";
109-
for (int i = 0; i < conf.index.size(); i++) {
110-
std::cout << "index: [" << conf.index[i] << "]\n";
108+
std::cout << "root: [" << conf->root << "]\n";
109+
for (int i = 0; i < conf->index.size(); i++) {
110+
std::cout << "index: [" << conf->index[i] << "]\n";
111111
}
112-
std::cout << "autoindex: [" << conf.autoindex << "]\n";
113-
std::cout << "upload_pass: [" << conf.upload_pass << "]\n";
114-
for (int i = 0; i < conf.cgi_pass.size(); i++) {
115-
std::cout << "cgi_param: [" << conf.cgi_pass[i].cgi_param << "]\n";
116-
std::cout << "cgi_pass: [" << conf.cgi_pass[i].cgi_pass << "]\n";
112+
std::cout << "autoindex: [" << conf->autoindex << "]\n";
113+
std::cout << "upload_pass: [" << conf->upload_pass << "]\n";
114+
for (int i = 0; i < conf->cgi_pass.size(); i++) {
115+
std::cout << "cgi_param: [" << conf->cgi_pass[i].cgi_param << "]\n";
116+
std::cout << "cgi_pass: [" << conf->cgi_pass[i].cgi_pass << "]\n";
117117
}
118-
std::cout << "mcbs: [" << conf.client_max_body_size << "]\n";
119-
for (std::map<int, std::string>::iterator it = conf.err_pages.begin(); it != conf.err_pages.end(); it++)
118+
std::cout << "mcbs: [" << conf->client_max_body_size << "]\n";
119+
for (std::map<int, std::string>::iterator it = conf->err_pages.begin(); it != conf->err_pages.end(); it++)
120120
{
121121
std::cout << "err_page: [" << it->first << "-" << it->second << "]\n";
122122
}
123-
std::cout << "ret_value: [" << conf.return_value << "]\n";
123+
std::cout << "ret_value: [" << conf->return_value << "]\n";
124124

125125

126126
std::cerr <<"["<< host << "] [" + port + "] [" + request->get_path() + "] [" + request->get_header("HOST") + "]\n";
127127
std::cerr <<"THE '/' is added to the root end, do not add it again!\n";
128-
std::cout << "------------------ " << conf.root << "----" << std::endl;
128+
std::cout << "------------------ " << conf->root << "----" << std::endl;
129129
std::map<std::string,int> met_map;
130130
met_map["GET"] = GET;
131131
met_map["POST"] = POST;
@@ -137,10 +137,10 @@ http_response::http_response(http_request *req,struct pollfd *fd,std::string &ho
137137
{
138138
if (met_map.find(request->get_method()) == met_map.end())
139139
throw NOT_IMPLEMENTED;
140-
if (conf.methods[0] != "ALL" && std::find(conf.methods.begin(),conf.methods.end(),request->get_method()) == conf.methods.end())
140+
if (conf->methods[0] != "ALL" && std::find(conf->methods.begin(),conf->methods.end(),request->get_method()) == conf->methods.end())
141141
throw NOT_ALLOWED;
142142
type = met_map[request->get_method()];
143-
if (conf.return_value != "")
143+
if (conf->return_value != "")
144144
throw 301;
145145
(this->*state_handlers[type])();
146146
if (type == CGI && request->get_method() == "GET")
@@ -227,21 +227,21 @@ void http_response::ERROR_handler(int x)
227227
client->events = POLLOUT;
228228
if (x == END)
229229
throw x;
230-
if (conf.err_pages[x] != "")
230+
if (conf->err_pages[x] != "")
231231
{
232-
file.open(conf.err_pages[x]);
232+
file.open(conf->err_pages[x]);
233233
if (file.good())
234234
{
235-
conf.root = conf.err_pages[x];
236-
conf.err_pages[x] = "";
235+
conf->root = conf->err_pages[x];
236+
conf->err_pages[x] = "";
237237
type = GET;
238238
state = FILE;
239239
file.close();
240240
return;
241241
}
242242
else if (file.is_open())
243243
file.close();
244-
conf.err_pages[x] = "";
244+
conf->err_pages[x] = "";
245245
}
246246
error_pages_map errors;
247247
body += errors.get_error(x);
@@ -250,7 +250,7 @@ void http_response::ERROR_handler(int x)
250250
std::cout << "excesss = " << body << std::endl;
251251
headers["Content-Type"] = content_type["html"];
252252
if (x == 301)
253-
headers["LOCATION"] = conf.return_value;
253+
headers["LOCATION"] = conf->return_value;
254254
res_header = "HTTP/1.1 " + int_to_string(x) + " " + errors.get_message(x) + "\n";
255255
for (std::map<std::string,std::string>::iterator it = headers.begin(); it != headers.end(); it++)
256256
res_header += (*it).first + ":" + (*it).second + "\n";
@@ -261,9 +261,9 @@ void http_response::ERROR_handler(int x)
261261

262262
int http_response::check_cgi(std::string &file)
263263
{
264-
for (int c = 0; c < conf.cgi_pass.size();c++)
264+
for (int c = 0; c < conf->cgi_pass.size();c++)
265265
{
266-
if (extention(file) == conf.cgi_pass[c].cgi_pass)
266+
if (extention(file) == conf->cgi_pass[c].cgi_pass)
267267
return 1;
268268
}
269269
return 0;

‎server/http/http_response.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class http_response
8383
long long content_remaining;
8484
std::string body;
8585
std::fstream file;
86-
config_match conf;
86+
config_match *conf;
8787
struct pollfd *client;
8888
int type;
8989

‎webserve

729 KB
Binary file not shown.

0 commit comments

Comments
 (0)
Please sign in to comment.