Skip to content

Commit abe3ece

Browse files
committed
added some functionalities into response
1 parent 7c6b879 commit abe3ece

File tree

5 files changed

+61
-50
lines changed

5 files changed

+61
-50
lines changed

request/request.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212

1313
#include "request.hpp"
1414

15-
request::request()
15+
request::request() : _method(""), _uri(""), _version(""),
16+
_body(""), _port(80), _query("")
1617
{
1718
return ;
1819
}
1920

20-
request::request(std::string request)
21+
request::request(std::string request) : _method(""), _uri(""), _version(""),
22+
_body(""), _port(80), _query("")
2123
{
2224
this->parse_request(request);
2325
return ;
@@ -43,6 +45,8 @@ request &request::operator=(const request &src)
4345
this->_version = src._version;
4446
this->_headers = src._headers;
4547
this->_body = src._body;
48+
this->_port = src._port;
49+
this->_query = src._query;
4650
}
4751
return (*this);
4852
}

respond/method_handling.cpp

+7-3
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@
1414

1515
void Respond::handle_get_response(std::vector<server> servers)
1616
{
17-
1817
// step 2: check if it's a CGI or not (like if `index` of the configuration file has .py or .php...etc)
18+
if (_is_cgi == true)
19+
{
20+
run_cgi(r, *this);
21+
return ;
22+
}
1923
// step 3: check if it's a file or not
2024
if (ft_check_file() == true)
21-
ft_handle_file();
25+
return (ft_handle_file());
2226
else
23-
handle_error_response(404);
27+
return (handle_error_response(404));
2428
// step 4 : check the index in the configuration file and render it
2529
ft_handle_index(servers);
2630

respond/method_utils.cpp

+42-41
Original file line numberDiff line numberDiff line change
@@ -23,32 +23,27 @@ int Respond::ft_check_file()
2323
return (0);
2424
}
2525

26-
void Respond::ft_handle_file()
26+
void Respond::ft_handle_file()
2727
{
2828
std::ifstream file;
29-
if (strcmp(_rooted_path.c_str(), ""))
29+
if (!strcmp(_rooted_path.c_str(), ""))
3030
{
31-
file.open(_rooted_path.c_str());
32-
if (file.is_open())
33-
{
34-
_response_body = std::string((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
35-
_status_code = 200;
36-
_status_message = "OK";
37-
_headers["Content-Type"] = "text/html";
38-
_headers["Content-Length"] = std::to_string(_response_body.length());
39-
_headers["Connection"] = "keep-alive";
40-
}
41-
else
42-
{
43-
_status_code = 404;
44-
_status_message = "Not Found";
45-
}
31+
handle_error_response(404);
32+
return;
4633
}
47-
else
34+
35+
file.open(_rooted_path.c_str(), std::ifstream::in);
36+
if (file.is_open())
4837
{
49-
_status_code = 404;
50-
_status_message = "Not Found";
38+
std::stringstream buffer;
39+
buffer << file.rdbuf();
40+
_response_body = buffer.str();
41+
file.close();
42+
set_status_code(200);
43+
set_status_message(get_response_status(200));
5144
}
45+
else
46+
handle_error_response(403);
5247
}
5348
/*
5449
Here's how it works:
@@ -58,32 +53,34 @@ Here's how it works:
5853
In other words, the std::string constructor reads the entire contents of the file into a std::string object. The resulting std::string object contains all the characters from the file, including any whitespace characters and newline characters.
5954
*/
6055

61-
void Respond::ft_handle_index(std::vector<server> server)
56+
int Respond::ft_handle_index(std::vector<server> server)
6257
{
6358
std::string index;
64-
65-
for (size_t i = 0; i < server.size(); i++)
66-
{
67-
for (size_t j = 0; j < server[i]._location.size(); j++)
68-
{
69-
if (_path_found == server[i]._location[j].location_name)
59+
60+
if (_path_found == server[_server_index]._location[_location_index].location_name)
7061
{
71-
if (server[i]._location[j].get_index().empty())
62+
if (server[_server_index]._location[_location_index].get_index().empty())
7263
{
73-
if (server[i].get_index().empty())
74-
handle_error_response(403);
64+
if (server[_server_index].get_index().empty())
65+
return (handle_error_response(403));
7566
else
7667
{
77-
index = server[i].get_index();
78-
_rooted_path = server[i].get_root() + _removed_path + index;
79-
ft_handle_index_2();
68+
index = server[_server_index].get_index();
69+
_rooted_path = server[_server_index].get_root() + _removed_path + index;
70+
if (ft_handle_index_2())
71+
return (1);
72+
else
73+
return (0);
8074
}
8175
}
8276
else
8377
{
84-
index = server[i]._location[j].get_index();
85-
_rooted_path = server[i].get_root() + _removed_path + index;
86-
ft_handle_index_2();
78+
index = server[_server_index]._location[_location_index].get_index();
79+
_rooted_path = server[_location_index].get_root() + _removed_path + index;
80+
if (ft_handle_index_2())
81+
return (1);
82+
else
83+
return (0);
8784
}
8885
}
8986
}
@@ -100,21 +97,23 @@ void Respond::ft_handle_index_2()
10097
{
10198
_response_body = std::string((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
10299
set_status_code(200);
103-
set_status_message(get_response_status(get_status_code()));
100+
set_status_message(get_response_status(200));
104101
_headers["Content-Type"] = "text/html";
105102
_headers["Content-Length"] = std::to_string(_response_body.length());
106103
_headers["Connection"] = "keep-alive";
104+
set_date();
105+
set_cache_control("no cache");
107106
}
108107
else
109108
{
110-
_status_code = 404;
111-
_status_message = "Not Found";
109+
handle_error_response(403);
110+
return (1);
112111
}
113112
}
114113
else
115114
{
116-
_status_code = 404;
117-
_status_message = "Not Found";
115+
handle_error_response(404);
116+
return (1);
118117
}
119118
}
120119

@@ -151,6 +150,8 @@ void Respond::ft_handle_error(int error_code)
151150
_headers["Content-Type"] = "text/html";
152151
_headers["Content-Length"] = _response_body.length();
153152
_headers["Connection"] = "keep-alive";
153+
set_date();
154+
set_cache_control("no cache");
154155
}
155156

156157
void Respond::ft_show_autoindex()

respond/respond.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class Respond
8787
void ft_handle_autoindex(std::vector<server> servers);
8888
void ft_check_cgi();
8989
int ft_check_file();
90-
void ft_handle_index(std::vector<server> servers);
90+
int ft_handle_index(std::vector<server> servers);
9191
void ft_handle_index_2();
9292
void ft_show_autoindex();
9393

respond/respond_root.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ std::string Respond::response_root(std::vector<server> servers)
2020
// considering the location of the root directory is indeed the final step in handling requests when no specific location matches the requested URI.
2121
// if (root_location(servers) == 0)
2222
if (root_location(servers) == 1)
23+
{
2324
handle_error_response(404);
24-
return (rtn_response());
25+
return (rtn_response());
26+
}
2527
// direct it to the GET response and see if i can read the file concatenated with the root using `stat`
2628
}
2729
// step 2 : check the redirectation
@@ -212,10 +214,10 @@ int Respond::ft_parse_url_forwarding(std::vector<server> server)
212214
{
213215
size_t status_code = server[_server_index]._location[j].get_redirection().first;
214216
// search for message of the status_code
215-
std::string message = get_response_status(status_code);
216217
set_status_code(status_code);
217-
set_status_message(get_response_status(get_status_code()));
218+
set_status_message(get_response_status(status_code));
218219
set_header("Location", server[_server_index]._location[j].get_redirection().second);
220+
set_cache_control("cache");
219221
_is_redirection = true;
220222
return (0);
221223
}

0 commit comments

Comments
 (0)