forked from oulhafiane/HTTP-Web-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResponse.cpp
More file actions
320 lines (304 loc) · 8.53 KB
/
Response.cpp
File metadata and controls
320 lines (304 loc) · 8.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include "Response.hpp"
#include "CGI.hpp"
#include <algorithm>
#include "chunked.hpp"
int isDirectory(const char *path)
{
struct stat statbuf;
if (stat(path, &statbuf) != 0)
return 0;
return S_ISDIR(statbuf.st_mode);
}
void Response::init_response_code_message()
{
_response_message[200] = "OK";
_response_message[403] = "Forbidden";
_response_message[404] = "Not Found";
_response_message[405] = "Method Not Allowed";
_response_message[500] = "Internal Server Error";
_response_message[502] = "Bad Gateway";
}
void Response::set_status_code(std::string &path, std::map<std::string, std::string> const &_location)
{
std::string method = this->_request.get_method();
_directory_listing = false;
try {
std::string allowed_methods = _location.at("allowed_methods");
if (allowed_methods.find(method) == std::string::npos) {
this->_status_code = 405;
return;
}
}
catch (std::exception &e) {
std::string allowed_methods = _vserver->get_allowed_methods();
if (allowed_methods == "")
allowed_methods = "GET, POST, DELETE";
if (allowed_methods.find(method) == std::string::npos) {
this->_status_code = 405;
return;
}
}
if (access(path.c_str(), F_OK) == -1)
_status_code = 404;
else if (access(path.c_str(), R_OK) == -1 || isDirectory(path.c_str()))
{
std::string index;
try {
index = _location.at("index");
path += index;
if (access(path.c_str(), F_OK) == -1)
_status_code = 404;
else if (access(path.c_str(), R_OK) == -1)
_status_code = 403;
else
_status_code = 200;
}
catch (std::exception &e) {
index = _vserver->get_index();
if (index == "")
{
if (isDirectory(path.c_str()))
{
try
{
std::string directory_listing = _location.at("directory_listing");
if (directory_listing != "on")
_status_code = 403;
else
{
_status_code = 200;
this->_directory_listing = true;
}
}
catch (std::exception &e)
{
std::string directory_listing = _vserver->get_directory_listing();
if (directory_listing != "on")
_status_code = 403;
else
{
_status_code = 200;
this->_directory_listing = true;
}
}
}
else
_status_code = 403;
}
else
{
path += index;
if (access(path.c_str(), F_OK) == -1)
_status_code = 404;
else if (access(path.c_str(), R_OK) == -1)
_status_code = 403;
else
_status_code = 200;
}
}
}
else
_status_code = 200;
}
std::string Response::get_html_of_directory_listing(std::string const &path)
{
DIR *dir;
struct dirent *ent;
std::string html = "<!DOCTYPE html><html><head><meta charset=\"UTF-8\"><title>Index of " + path + "</title></head><body><h1>Index of " + _request.get_path() + "</h1><hr><pre>";
if ((dir = opendir(path.c_str())) != NULL)
{
while ((ent = readdir(dir)) != NULL)
{
if (ent->d_name[0] != '.')
{
std::string file_path = path + "/" + ent->d_name;
if (isDirectory(file_path.c_str()))
html += "<a href=\"" + _request.get_path() + ent->d_name + "/\">" + ent->d_name + "/</a><br>";
else
html += "<a href=\"" + _request.get_path() + ent->d_name + "\">" + ent->d_name + "</a>\n";
}
}
closedir(dir);
}
html += "</pre></body></html>";
return html;
}
std::string Response::get_content_of_path(std::string path, std::map<std::string, std::string> const &location)
{
std::string content;
if (_status_code == 404)
return "<h1>404 Not found</h1>";
else if (_status_code == 403)
return "<h1>403 Forbidden</h1>";
else if (_status_code == 405)
return "<h1>405 Method Not Allowed</h1>";
else if (_directory_listing == true)
return get_html_of_directory_listing(path);
try {
std::string cgi_path = location.at("fastcgi_pass");
CGI gci(_request, path, cgi_path);
if (gci._get_status() == 500) {
_status_code = 500;
return "<h1>500 Internal Server Error</h1>";
}
content = gci._get_content();
std::string headers = content.substr(0, content.find("\r\n\r\n")) + "\r\n";
std::string body = content.substr(content.find("\r\n\r\n")+4);
size_t status_idx = headers.find("Status: ");
if (status_idx != std::string::npos) {
int status = atoi(headers.substr(status_idx+7, 4).c_str());
if (status)
_status_code = status;
}
format_response(body, headers);
return "\r\rnone\r\r";// return value to go back without reforamting
}
catch (std::exception &e) {
std::ifstream file(path);
std::getline(file, content, '\0');
file.close();
}
return content;
}
void Response::format_response(std::string content, std::string headers)
{
if (_status_code != 200 && _status_code != 301)
content = _error_pages[_status_code];
_response = "HTTP/1.1 " + std::to_string(_status_code) + " " + _response_message[_status_code] + "\r\n";
_response += "Content-Type: text/html\r\n";
if (content.size() < 400) {
_response += "Content-Length: " + std::to_string(content.size()) + "\r\n";
}
else {
_response += "Transfer-Encoding: chunked\r\n";
content = encode_body(content);
}
if (_status_code == 301)
_response += "Location: " + _location + "\r\n";
if (this->_request.get_connection() == "close")
_response += "Connection: close\r\n";
_response += headers;
_response += "\r\n";
_response += content;
}
void Response::handle_response(Request &request)
{
std::string content = "";
std::string path = request.get_path();
std::string location = _vserver->location_match(path);
std::string root;
if (location == "none") {
_status_code = 404;
content = "<h1>404 Not found</h1>";
format_response(content);
return ;
}
std::map<std::string, std::string> const & \
location_map = _vserver->get_locations().at(location);
_status_code = 200;
if (location_map.find("body_size_limit") != location_map.end()) {
size_t max_size;
try {
max_size = std::stoi(location_map.at("body_size_limit"));
if (request.get_body().size() > max_size) {
_status_code = 400;
format_response("Default bad request page");
return;
}
} catch (std::exception & e) {
std::cerr << e.what() << std::endl;
}
}
location = location_map.at("location");
if (location_map.find("redirect") != location_map.end())
{
_status_code = 301;
_location = location_map.at("redirect") + path.erase(0, location.length() - 1);
content = "<h1>301 Moved Permanently</h1>";
format_response(content);
return ;
}
if (location_map.find("upload_pass") != location_map.end())
{
std::string upload_path = location_map.at("upload_pass");
upload_path += path.erase(0, location.length() - 2);
if (_request.get_method() == "GET")
{
std::ifstream file(upload_path);
std::getline(file, content, '\0');
file.close();
format_response(content);
return ;
}
std::ofstream file(upload_path);
file << request.get_body();
if (!file.good())
{
_status_code = 500;
content = "<h1>500 Internal Server Error</h1>";
format_response(content);
return ;
}
file.close();
content = "<h1>Upload Success</h1>";
format_response(content);
return ;
}
if (location_map.find("root") != location_map.end())
root = location_map.at("root");
else
root = _vserver->get_root();
if (location[0] == '~')
path = root + path;
else
path = root + path.erase(0, location.length() - 2);
set_status_code(path, location_map);
content = get_content_of_path(path, location_map);
if (content == "\r\rnone\r\r")
return ;
format_response(content);
}
std::string get_host(Request &request)
{
std::vector<std::pair<std::string, std::string> > const &heades = request.get_headers();
std::vector<std::pair<std::string, std::string> >::const_iterator it;
for (it = heades.begin(); it != heades.end(); ++it)
{
if (it->first == "Host") {
std::string host = it->second;
host.resize(host.length() - 1);
return host;
}
}
return "none";
}
void Response::match_virtual_server() {
std::vector<VirtualServer>::const_iterator it;
std::vector<std::string>::iterator it2;
std::string host = get_host(_request);
for (it = _vservers.begin(); it < _vservers.end(); it++)
{
std::vector<std::string> server_names = it->get_server_names();
for (it2 = server_names.begin(); it2 < server_names.end(); it2++)
{
if (*it2 == host)
{
_vserver = &(*it);
return;
}
}
}
_vserver = &(*_vservers.begin());
}
Response::Response(Request &request, std::vector<VirtualServer> const &vservers, std::map<int, std::string> &error_pages)
: _request(request), _vservers(vservers), _error_pages(error_pages)
{
(void) _error_pages;
match_virtual_server();
init_response_code_message();
handle_response(request);
}
std::string Response::operator*() const
{
return this->_response;
}