Skip to content

Commit d3b601f

Browse files
author
Soufiane Nouae
committed
fin
1 parent b5aa910 commit d3b601f

File tree

9 files changed

+33
-52
lines changed

9 files changed

+33
-52
lines changed

CGI/cgi.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ std::string run_cgi(request &r, Respond &res, std::vector<server> server)
109109
free_all(file, path,envp, env.size());
110110
return res.get_response_status(res.get_status_code());
111111
}
112-
alarm(1);
112+
alarm(2);
113113
execve(cmd[0], cmd, envp);
114114
exit(1);
115115
}

config/server.conf

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ server
2424
{
2525
root ./www/html/cgi_bin;
2626
allow_Methods GET;
27-
index hello.html;
27+
#index hello.html;
2828
path_info .py /usr/bin/python3;
2929
path_info .php /php-cgi;
30-
#listen 8001;
30+
# listen 8001;
3131
autoindex on;
3232
}
3333

prs_rsc/location.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ std::string extract_path(std::string location_name)
66
{
77
if (!isspace(location_name[i]))
88
break;
9-
pos++;
9+
pos++;
1010
}
1111
int start = pos;
1212
size_t i;
@@ -57,6 +57,8 @@ void location::fill_rest(server &s)
5757
{
5858
// if (_index.empty())
5959
// _index = s.get_index();
60+
if (!_client_max_body_size)
61+
_client_max_body_size = s.get_client_max_body_size();
6062
if (_root.empty())
6163
_root = s.get_root();
6264
if(_error_page.empty())

prs_rsc/main.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +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-
// std::cout << "khghjgjghjghjgjgjgjgjhhjhjjhhghjg\n";
1918
// for (size_t i = 0; i < servers.size(); i++)
2019
// {
2120
// servers[i].display_sever();

prs_rsc/server.cpp

+17-33
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@ std::string toUpper(std::string str) {
1616
return str;
1717
}
1818

19-
bool isIpAddress(const std::string& ip) {
20-
int numDots = 0;
19+
bool isip(const std::string& ip) {
20+
int d = 0;
2121
std::string temp = "";
2222

2323
for (size_t i = 0; i < ip.size(); i++)
2424
{
2525
if (ip[i] == '.')
2626
{
27-
numDots++;
28-
if (numDots > 3)
27+
d++;
28+
if (d > 3)
2929
return 0;
30-
if (temp.size() == 0 || std::stoi(temp) > 255)
30+
if (temp.size() == 0 || std::stod(temp) > 255)
3131
return 0;
3232
temp = "";
3333
}
@@ -36,7 +36,7 @@ bool isIpAddress(const std::string& ip) {
3636
else
3737
temp += ip[i];
3838
}
39-
if (temp.size() == 0 || std::stoi(temp) > 255 || numDots != 3)
39+
if (temp.size() == 0 || std::stod(temp) > 255 || d != 3)
4040
return 0;
4141
return 1;
4242
}
@@ -75,36 +75,18 @@ int search_char(std::string str, char c)
7575
int is_number(const std::string& str)
7676
{
7777
size_t i = 0;
78-
int exp = 0;
79-
int digit = 0;
80-
int flag = 0;
8178

8279
for (i = 0; i < str.size(); i++)
8380
{
84-
if (i == 0 && (str[i] == '+' || str[i] == '-')) {
81+
if (i == 0 && (str[i] == '+')) {
8582
i++;
8683
continue;
8784
}
88-
if (str[i] == 'e' || str[i] == 'E') {
89-
if (exp || !digit) {
90-
return 0;
91-
}
92-
exp = 1;
93-
if (i + 1 < str.size() && (str[i + 1] == '+' || str[i + 1] == '-')) {
94-
if(str[i + 1] == '-')
95-
flag = 1;
96-
i++;
97-
}
98-
continue;
99-
}
10085
if(!isdigit(str[i]))
10186
return (0);
102-
digit = 1;
10387
}
104-
if(i == 1 && (str[0] == '+' || str[0] == '-'))
88+
if(i == 1 && (str[0] == '+'))
10589
return (0);
106-
if(flag)
107-
return (-1);
10890
return (1);
10991
}
11092

@@ -199,8 +181,7 @@ int is_world(std::string str, std::string tmp)
199181
}
200182

201183

202-
server::server(Data_config data, bool check_location)
203-
: _client_max_body_size(1048576)
184+
server::server(Data_config data, bool check_location)
204185
{
205186
std::istringstream ss(data.data_server);
206187
std::string line;
@@ -216,6 +197,7 @@ server::server(Data_config data, bool check_location)
216197
int c_redirection = 0;
217198
int c_upload = 0;
218199
int c_upload_store = 0;
200+
_client_max_body_size = 0;
219201
while (getline(ss, line))
220202
{
221203
if(line.empty() || line[0] == '#')
@@ -246,8 +228,6 @@ server::server(Data_config data, bool check_location)
246228
key = toLower(key);
247229
if (key == "server_name" && check_location)
248230
{
249-
// if(c_server_name)
250-
// ft_error(line, "Duplicated");
251231
is_empty_value(value, line);
252232
if (ft_numbers_value(iss) || !isValidServerName(value))
253233
ft_error(line, "Error");
@@ -262,7 +242,7 @@ server::server(Data_config data, bool check_location)
262242
if(c_host)
263243
ft_error(line, "Duplicated");
264244
is_empty_value(value, line);
265-
if (ft_numbers_value(iss) || (!isIpAddress(value) && value != "localhost"))
245+
if (ft_numbers_value(iss) || (!isip(value) && value != "localhost"))
266246
ft_error(line, "Error");
267247
c_host++;
268248
_host = value;
@@ -280,6 +260,9 @@ server::server(Data_config data, bool check_location)
280260
while (iss >> value)
281261
{
282262
int port = ft_number(value, line);
263+
std::vector<int>::iterator it = std::find(_listen.begin(), _listen.end(), port);
264+
if(it != _listen.end())
265+
ft_error(line, "duplicate port");
283266
if (port < 1024 || port > 65535)
284267
ft_error(line, "Error");
285268
_listen.push_back(port);
@@ -433,7 +416,7 @@ server::server(Data_config data, bool check_location)
433416
if(!c_allow_method && !check_location)
434417
_allow_methods.push_back("GET");
435418
if (!c_listen && check_location)
436-
_listen.push_back(80);
419+
_listen.push_back(8001);
437420
if (!c_host && check_location)
438421
_host = "127.0.0.1";
439422
if(!c_error_page && check_location)
@@ -450,7 +433,8 @@ server::server(Data_config data, bool check_location)
450433
_server_name.push_back("hostname");
451434
if (!c_root && check_location)
452435
_root = "www/html";
453-
436+
if (!c_client_max_body_size && check_location)
437+
_client_max_body_size = 1048576;
454438
if(data.location.size())
455439
{
456440
Data_config location_data;

prs_rsc/server.hpp

+2-10
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,15 @@ class server
1313
{
1414
protected:
1515
std::string _index;
16-
std::string _host; /*s*/
16+
std::string _host;
1717
std::string _root;
18-
unsigned int _client_max_body_size; /*l*/
18+
unsigned int _client_max_body_size;
1919
std::map<int, std::string> _error_page;
2020
std::vector<std::string> _allow_methods;
2121
std::pair <int , std::string> _redirection;
2222
std::string _upload_store;
2323
bool _autoindex;
2424
bool _upload;
25-
// std::vector<std::string> _access_log;
26-
// std::vector<std::string> _error_log;
27-
//std::vector<std::string> _meme_types;
28-
// std::string _gzip;
29-
// std::string _ssl_certificate;
30-
// std::string _ssl_certificate_key;
31-
// std::string _ssl_protocols;
32-
// std::string _ssl_ciphers;
3325
std::map<std::string ,std::string> _path_info;
3426
public:
3527
std::vector<std::string> _server_name; /*s*/

prs_rsc/server_utils.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,11 @@ std::vector<server> ft_fill_servers(char **av, int ac)
155155
std::cerr << "Error : not closed" << std::endl;
156156
exit (1);
157157
}
158-
158+
if(v.size() == 0)
159+
{
160+
std::cerr << "Error : config file empty" << std::endl;
161+
exit (1);
162+
}
159163
for (size_t i = 0; i < v.size(); i++)
160164
{
161165
c = 0;

respond/method_handling.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,8 @@ std::string Respond::check_post_type()
276276

277277
void Respond::handle_delete_response(std::vector<server> server)
278278
{
279-
std::cout << "DKHLAAAAAAAAAAT" << std::endl;
280-
std::cout << "rooted path:" << _rooted_path << std::endl;
279+
//std::cout << "DKHLAAAAAAAAAAT" << std::endl;
280+
//std::cout << "rooted path:" << _rooted_path << std::endl;
281281
if (std::remove(_rooted_path.c_str()) == 0)
282282
{
283283
_status_code = 200;

respond/method_utils.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ 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;
205+
//std::cout << "rooted path: " << _rooted_path << std::endl;
206206
std::string index_html = "<!DOCTYPE html>\n<html>\n<head>\n";
207207
index_html += "<meta charset=\"UTF-8\">\n";
208208
index_html += "<title>Index of " + _rooted_path + "</title>\n";

0 commit comments

Comments
 (0)