Skip to content

Commit acb29d6

Browse files
committed
2 parents d92680c + 36bf10e commit acb29d6

File tree

10 files changed

+56
-68
lines changed

10 files changed

+56
-68
lines changed

CGI/cgi.cpp

+3-4
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
}
@@ -123,12 +123,11 @@ std::string run_cgi(request &r, Respond &res, std::vector<server> server)
123123
return res.get_response_status(res.get_status_code());
124124
}
125125
char buf[1];
126-
std::string content;
127126
int byt;
127+
std::string content;
128128
std::rewind(temp);
129-
while ((byt = read(fdtemp, buf, 1)) > 0){
129+
while ((byt = read(fdtemp, buf, 1)) > 0)
130130
content.append(buf, 1);
131-
}
132131
if (byt == -1)
133132
{
134133
res.set_status_code(500);

config/server.conf

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ server
33
listen 8001;
44

55
host 127.0.0.1;
6-
client_max_body_size 999999999;
6+
client_max_body_size 100;
77

88

99
error_page 404 www/html/error_pages/404.html;
@@ -24,9 +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;
3031
autoindex on;
3132
}
3233

@@ -44,11 +45,11 @@ server
4445

4546
location /upload{
4647
root ./www/html/upload;
47-
# index upload.html;
48+
index upload.html;
4849
allow_methods POST GET;
4950
upload on;
5051
upload_store ./www/html/upload;
51-
autoindex on;
52+
#autoindex on;
5253
}
5354

5455
location /getto {

prs_rsc/location.cpp

+3-3
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;
@@ -55,8 +55,8 @@ location::~location()
5555

5656
void location::fill_rest(server &s)
5757
{
58-
// if (_index.empty())
59-
// _index = s.get_index();
58+
if (!_client_max_body_size)
59+
_client_max_body_size = s.get_client_max_body_size();
6060
if (_root.empty())
6161
_root = s.get_root();
6262
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

+28-35
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,23 +228,30 @@ 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);
252-
if (ft_numbers_value(iss) || !isValidServerName(value))
232+
if (!isValidServerName(value))
253233
ft_error(line, "Error");
254234
std::vector<std::string>::iterator it = std::find(_server_name.begin(), _server_name.end(), value);
255235
if(it != _server_name.end())
256236
ft_error(line, "duplicate server_name");
257-
c_server_name++;
258237
_server_name.push_back(value);
238+
while (iss >> value)
239+
{
240+
if (!isValidServerName(value))
241+
ft_error(line, "Error");
242+
std::vector<std::string>::iterator it = std::find(_server_name.begin(), _server_name.end(), value);
243+
if(it != _server_name.end())
244+
ft_error(line, "duplicate server_name");
245+
_server_name.push_back(value);
246+
}
247+
c_server_name++;
259248
}
260249
else if (key == "host" && check_location)
261250
{
262251
if(c_host)
263252
ft_error(line, "Duplicated");
264253
is_empty_value(value, line);
265-
if (ft_numbers_value(iss) || (!isIpAddress(value) && value != "localhost"))
254+
if (ft_numbers_value(iss) || (!isip(value) && value != "localhost"))
266255
ft_error(line, "Error");
267256
c_host++;
268257
_host = value;
@@ -280,6 +269,9 @@ server::server(Data_config data, bool check_location)
280269
while (iss >> value)
281270
{
282271
int port = ft_number(value, line);
272+
std::vector<int>::iterator it = std::find(_listen.begin(), _listen.end(), port);
273+
if(it != _listen.end())
274+
ft_error(line, "duplicate port");
283275
if (port < 1024 || port > 65535)
284276
ft_error(line, "Error");
285277
_listen.push_back(port);
@@ -433,7 +425,7 @@ server::server(Data_config data, bool check_location)
433425
if(!c_allow_method && !check_location)
434426
_allow_methods.push_back("GET");
435427
if (!c_listen && check_location)
436-
_listen.push_back(80);
428+
_listen.push_back(8001);
437429
if (!c_host && check_location)
438430
_host = "127.0.0.1";
439431
if(!c_error_page && check_location)
@@ -450,7 +442,8 @@ server::server(Data_config data, bool check_location)
450442
_server_name.push_back("hostname");
451443
if (!c_root && check_location)
452444
_root = "www/html";
453-
445+
if (!c_client_max_body_size && check_location)
446+
_client_max_body_size = 1048576;
454447
if(data.location.size())
455448
{
456449
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

+11-7
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ std::vector<server> ft_fill_servers(char **av, int ac)
4545
int c = 0;
4646
int flag = 0;
4747
int kws = 0;
48-
size_t i = 0, j = 0;
48+
size_t i = 0, is_server = 0;
4949
if (ac == 2)
5050
config_file = av[1];
5151
else
@@ -65,7 +65,7 @@ std::vector<server> ft_fill_servers(char **av, int ac)
6565
c -= search_char(line, '}');
6666
if (is_world(&line[i], "server"))
6767
{
68-
j = 1;
68+
is_server = 1;
6969
if (!search_char(line, '{'))
7070
{
7171
while (!file.eof())
@@ -93,7 +93,7 @@ std::vector<server> ft_fill_servers(char **av, int ac)
9393
}
9494
if (is_world(&line[i], "location"))
9595
{
96-
if(!j)
96+
if(!is_server)
9797
{
9898
std::cerr << "error something outside of server\n";
9999
exit (1);
@@ -124,7 +124,7 @@ std::vector<server> ft_fill_servers(char **av, int ac)
124124
}
125125
}
126126
}
127-
else if (!j)
127+
else if (!is_server)
128128
{
129129
if (!line.empty())
130130
{
@@ -134,9 +134,9 @@ std::vector<server> ft_fill_servers(char **av, int ac)
134134
}
135135
else if (!line.empty())
136136
data.data_server += line + "\n";
137-
if (!c && j)
137+
if (!c && is_server)
138138
{
139-
j = 0;
139+
is_server = 0;
140140
kws = 0;
141141
v.push_back(data);
142142
data.data_server.clear();
@@ -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
@@ -282,8 +282,8 @@ std::string Respond::check_post_type()
282282

283283
void Respond::handle_delete_response(std::vector<server> server)
284284
{
285-
std::cout << "DKHLAAAAAAAAAAT" << std::endl;
286-
std::cout << "rooted path:" << _rooted_path << std::endl;
285+
//std::cout << "DKHLAAAAAAAAAAT" << std::endl;
286+
//std::cout << "rooted path:" << _rooted_path << std::endl;
287287
if (std::remove(_rooted_path.c_str()) == 0)
288288
{
289289
_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";

www/html/upload/upload.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ <h1>File Uploader</h1>
9393
<p>Select files to upload</p>
9494
</header>
9595
<main>
96-
<form action="http://localhost:8001/upload/success.html" method="post" enctype="multipart/form-data">
96+
<form action="http://localhost:8002/upload/success.html" method="post" enctype="multipart/form-data">
9797
<div class="form-group">
9898
<label for="file1">File 1</label>
9999
<input type="file" name="file1" id="file1" accept=".jpg, .jpeg, .png, .pdf, .mp4" required>

0 commit comments

Comments
 (0)