Skip to content

Commit b5aa910

Browse files
committed
done
1 parent 053699d commit b5aa910

File tree

16 files changed

+272
-581
lines changed

16 files changed

+272
-581
lines changed

config/server.conf

+38-15
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
server
1+
server
22
{
3-
listen 8001;
3+
listen 8002;
44

55
host 127.0.0.1;
6-
client_max_body_size 10000000;
7-
root ./www/html;
8-
server_name localhost;
9-
server_name listen;
10-
6+
client_max_body_size 100;
7+
8+
119
error_page 404 www/html/error_pages/404.html;
1210
error_page 403 www/html/error_pages/403.html;
1311
error_page 409 www/html/error_pages/409.html;
@@ -19,14 +17,13 @@ server
1917
index index.html;
2018

2119
location / {
22-
autoindex on;
20+
autoindex off;
2321
}
2422

2523
location /cgi_bin
2624
{
27-
2825
root ./www/html/cgi_bin;
29-
allow_Methods GET POST;
26+
allow_Methods GET;
3027
index hello.html;
3128
path_info .py /usr/bin/python3;
3229
path_info .php /php-cgi;
@@ -36,7 +33,7 @@ server
3633

3734
location /red
3835
{
39-
return 400 https://www.youtube.com/watch?v=fhdX3Wcxwas&t=2768s&ab_channel=BGMGhibliStudio;
36+
return 301 http://localhost:8002/;
4037
}
4138

4239
location /delete {
@@ -48,22 +45,23 @@ server
4845

4946
location /upload{
5047
root ./www/html/upload;
51-
index upload.html;
48+
# index upload.html;
5249
allow_methods POST GET;
5350
upload on;
5451
upload_store ./www/html/upload;
52+
autoindex on;
5553
}
5654

5755
location /getto {
5856
root ./www/html/upload;
59-
index video.mp4;
57+
# index video.mp4;
6058
allow_methods GET;
61-
autoindex off;
59+
autoindex on;
6260
}
6361
}
6462
# server
6563
# {
66-
# listen 8001;
64+
# listen 8002;
6765

6866
# host 127.0.0.1;
6967
# client_max_body_size 10000000;
@@ -186,4 +184,29 @@ server
186184
# allow_methods GET;
187185
# autoindex off;
188186
# }
187+
# }
188+
189+
190+
191+
# server
192+
# {
193+
# listen 8001 8001 5000 5000;
194+
195+
# client_max_body_size 100;
196+
197+
198+
# server_name localhost;
199+
200+
201+
202+
# index index.html;
203+
# location / {
204+
205+
206+
# }
207+
208+
209+
210+
211+
189212
# }

include/http_server.hpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: yismaili <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/03/22 14:57:52 by yismaili #+# #+# */
9-
/* Updated: 2023/05/22 22:15:41 by yismaili ### ########.fr */
9+
/* Updated: 2023/05/23 21:03:42 by yismaili ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -35,6 +35,7 @@
3535
#include <iomanip>
3636
#include "../request/request.hpp"
3737
#include "../respond/respond.hpp"
38+
#include "../prs_rsc/server.hpp"
3839
#include <csignal>
3940

4041

@@ -83,6 +84,7 @@ namespace http{
8384
std::vector<std::string> servers_names;
8485
std::vector<int> port;
8586
std::vector<std::string> host;
87+
std::size_t connection;
8688
bool flag;
8789
};
8890
}

index.html

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Your Website Title</title>
5+
<style>
6+
body {
7+
font-family: Arial, sans-serif;
8+
margin: 0;
9+
padding: 0;
10+
background-color: #f1f1f1;
11+
}
12+
.container {
13+
max-width: 800px;
14+
margin: 0 auto;
15+
padding: 20px;
16+
background-color: #ffffff;
17+
border-radius: 10px;
18+
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
19+
}
20+
h1 {
21+
text-align: center;
22+
color: #333333;
23+
}
24+
p {
25+
color: #555555;
26+
line-height: 1.5;
27+
}
28+
.cta-button {
29+
display: inline-block;
30+
padding: 12px 24px;
31+
background-color: #007bff;
32+
color: #ffffff;
33+
font-size: 16px;
34+
text-decoration: none;
35+
border-radius: 5px;
36+
transition: background-color 0.3s ease;
37+
}
38+
.cta-button:hover {
39+
background-color: #0056b3;
40+
}
41+
</style>
42+
</head>
43+
<body>
44+
<div class="container">
45+
<h1>Welcome to our WEBSERVER!</h1>
46+
<p>This is the main page of our web server.</p>
47+
<p class="message">This is the default page for my website. Start exploring by clicking the button below.</p>
48+
<a class="cta-button" href="/start.html">Get Started</a>
49+
</div>
50+
51+
</body>
52+
</html>
53+

prs_rsc/main.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ 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";
1819
// for (size_t i = 0; i < servers.size(); i++)
1920
// {
2021
// servers[i].display_sever();

prs_rsc/server.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ int is_world(std::string str, std::string tmp)
200200

201201

202202
server::server(Data_config data, bool check_location)
203-
: _root("./www/html"), _client_max_body_size(1048576)
203+
: _client_max_body_size(1048576)
204204
{
205205
std::istringstream ss(data.data_server);
206206
std::string line;
@@ -448,6 +448,8 @@ server::server(Data_config data, bool check_location)
448448
}
449449
if (!c_server_name && check_location)
450450
_server_name.push_back("hostname");
451+
if (!c_root && check_location)
452+
_root = "www/html";
451453

452454
if(data.location.size())
453455
{

request/request.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: aoumad <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/03/26 23:05:21 by aoumad #+# #+# */
9-
/* Updated: 2023/05/22 20:41:03 by aoumad ### ########.fr */
9+
/* Updated: 2023/05/24 00:23:21 by aoumad ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

request/task.txt

-2
This file was deleted.

respond/method_handling.cpp

+6-7
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/23 15:08:57 by aoumad ### ########.fr */
9+
/* Updated: 2023/05/23 20:21:54 by aoumad ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -155,12 +155,11 @@ void Respond::handle_form_data(std::vector<server> server)
155155
break;
156156
// Read the data between the boundaries
157157
FormData formData = read_form_data(server, pos); // escape /r/n
158-
// if (_file_too_large == true)
159-
// {
160-
// std::cout << "hahahahaahah" << std::endl;
161-
// handle_error_response(server, 413);
162-
// return ;
163-
// }
158+
if (_file_too_large == true)
159+
{
160+
handle_error_response(server, 413);
161+
return ;
162+
}
164163
if (formData.isValid())
165164
_form_data.push_back(formData); // Add the form data to the list
166165
// std::cout << "pos before: " << pos << std::endl;

respond/method_utils.cpp

+10-6
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/23 14:54:00 by aoumad ### ########.fr */
9+
/* Updated: 2023/05/24 01:02:19 by aoumad ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -202,6 +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;
205206
std::string index_html = "<!DOCTYPE html>\n<html>\n<head>\n";
206207
index_html += "<meta charset=\"UTF-8\">\n";
207208
index_html += "<title>Index of " + _rooted_path + "</title>\n";
@@ -228,18 +229,18 @@ void Respond::ft_show_autoindex(std::vector<server> server)
228229
{
229230
if (entry->d_name[0] != '.')
230231
{
231-
file_name = std::string(entry->d_name);
232+
file_name = std::string(entry->d_name); // cgi_bin
232233
std::string file_path;
233234
if (_path_found[_path_found.size() - 1] == '/')
234-
file_path = _path_found + file_name;
235+
file_path = _path_found + file_name; // /cgi_bin
235236
else
236237
file_path = _path_found + "/" + file_name;
238+
// std::cout << "file path: " << file_path << std::endl;
237239
std::string match_path;
238240
if (_rooted_path[_rooted_path.size() - 1] == '/')
239-
match_path = _rooted_path + file_name;
241+
match_path = _rooted_path + file_name; // www/html/cgi_bin
240242
else
241243
match_path = _rooted_path + "/" + file_name;
242-
243244
if (stat(match_path.c_str(), &file_stat) < 0)
244245
{
245246
handle_error_response(server, 403);
@@ -252,7 +253,6 @@ void Respond::ft_show_autoindex(std::vector<server> server)
252253
std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", timeInfo);
253254
std::string fileTime(buffer);
254255

255-
// Now you can use 'fileTime' in your HTML code
256256
index_html += "<p><a href=\"http://" + r.get_header("Host") + file_path + "\"><b><i><font size=\"5\">" + file_name + "</font></i></b></a>";
257257
index_html += "\t\t <b><i><font size=\"5\">" + file_size + "\t\t" + fileTime + "</font></i></b></p>\n";
258258
}
@@ -273,9 +273,12 @@ void Respond::handle_error_response(std::vector<server> server, int error_cod
273273
file.open(error_path.c_str());
274274
if (file.is_open())
275275
{
276+
set_status_code(error_code);
277+
set_status_message(get_response_status(error_code));
276278
_response_body = std::string((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
277279
set_header("Content-Type", get_mime_type("html"));
278280
set_header("Content-Length", std::to_string(_response_body.length()));
281+
set_date();
279282
set_cache_control("no cache");
280283
}
281284
// file.close();
@@ -289,6 +292,7 @@ void Respond::handle_error_response(std::vector<server> server, int error_cod
289292
set_date();
290293
_response_body = "<html><head><title>" + std::to_string(error_code) + " " + _status_message + "</title></head><body><h1>" + std::to_string(error_code) + " " + _status_message + "</h1><p>You don't have permission to access " + r.get_uri() + " on this server.</p></body></html>";
291294
set_header("Content-Length", std::to_string(_response_body.length()));
295+
set_date();
292296
set_cache_control("no cache");
293297
}
294298
}

respond/respond.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ std::string Respond::rtn_response()
161161
response = _http_version + " " + std::to_string(_status_code) + " " + _status_message + "\r\n";
162162
for (std::map<std::string, std::string>::iterator it = _headers.begin(); it != _headers.end(); it++)
163163
response += it->first + ": " + it->second + "\r\n";
164+
if (_cache_control != "")
165+
response += "Cache-Control: " + _cache_control + "\r\n";
164166
response += "\r\n";
165167
response += _response_body;
166168
return (response);

respond/respond_root.cpp

+14-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: aoumad <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/04/29 14:53:31 by aoumad #+# #+# */
9-
/* Updated: 2023/05/23 12:59:51 by aoumad ### ########.fr */
9+
/* Updated: 2023/05/24 01:25:32 by aoumad ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -37,6 +37,17 @@ std::string Respond::response_root(std::vector<server> servers)
3737
handle_error_response(servers, _status_code);
3838
return (rtn_response());
3939
}
40+
char buff_root[1024];
41+
realpath(servers[_server_index].get_root().c_str(), buff_root);
42+
char buff_uri[1024];
43+
realpath(_rooted_path.c_str(), buff_uri);
44+
std::string buf = buff_uri;
45+
size_t pos = buf.find(buff_root);
46+
if(pos != 0)
47+
{
48+
handle_error_response(servers, 403);
49+
return (rtn_response());
50+
}
4051
// step 4 : check the allowed methods
4152
if (ft_check_allowed_methods(servers))
4253
{
@@ -189,7 +200,7 @@ int Respond::ft_parse_url_forwarding(std::vector<server> server)
189200
set_status_code(status_code);
190201
set_status_message(get_response_status(status_code));
191202
set_header("Location", server[_server_index]._location[j].get_redirection().second);
192-
set_cache_control("no cache");
203+
set_cache_control("no-cache");
193204
_is_redirection = true;
194205
return (0);
195206
}
@@ -219,7 +230,7 @@ int Respond::ft_check_allowed_methods(std::vector<server> server)
219230
}
220231
}
221232
}
222-
set_status_code(404);
233+
set_status_code(405);
223234
set_status_message(get_response_status(get_status_code()));
224235
return (1);
225236
}

0 commit comments

Comments
 (0)