-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResponse.cpp
426 lines (379 loc) · 10.7 KB
/
Response.cpp
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#include <sys/stat.h>
#include <cerrno>
#include <fcntl.h>
#include <unistd.h>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <dirent.h>
#include "Response.hpp"
#include "ConnectionSocket.hpp"
#include "Config_unit.hpp"
#include "Config_parser.hpp"
#include "CGI_unit.hpp"
#include "utils.hpp"
#define BUF_SIZE 8096
Response::Response()
{
request = new Request();
cgi = new CGI_unit();
}
Response::Response(ConnectionSocket *socket)
: socket(socket), responseData_(), state_(PREPARE_FOR_GENERATE), needHeaders(true)
{
request = new Request();
cgi = new CGI_unit();
errorMap_[200] = "OK";
errorMap_[201] = "Created";
errorMap_[204] = "No Content";
errorMap_[301] = "Moved Permanently";
errorMap_[307] = "Temporary Redirect";
errorMap_[308] = "Permanent Redirect";
errorMap_[400] = "Bad Request";
errorMap_[403] = "Forbidden";
errorMap_[404] = "Not Found";
errorMap_[405] = "Method Not Allowed";
errorMap_[413] = "Request Entity Too Large";
errorMap_[500] = "Internal Server Error";
errorMap_[501] = "Not Implemented";
}
Response::Response(Response const &) {}
Response::~Response()
{
delete request;
delete cgi;
}
void Response::initGenerateResponse()
{
bool validRequest = request->parseRequest(socket->getBuffer());
config = socket->getConfig()->getServerConf(request->getHost(), socket->getPort());
cgi->setPhpLoc(config->getPHPexec());
cgi->setPythonLoc(config->getPythonExec());
if (!validRequest) {
return _handleInvalidRequest(400);
}
if (isPayloadTooLarge()) {
return _handleInvalidRequest(RequestTooLarge);
}
if (!config->checkMethod(request->getMethod(), request->getPath())) {
return _handleInvalidRequest(MethodNotAllowed);
}
if (config->checkRedirect(request->getPath())) {
return _handleRedirect();
}
if (request->getMethod() == "GET") {
_handleMethodGET();
} else if (request->getMethod() == "HEAD") {
_handleMethodHEAD();
} else if (request->getMethod() == "POST") {
_handleMethodPOST();
} else if (request->getMethod() == "DELETE") {
_handleMethodDELETE();
} else {
_handleInvalidRequest(NotImplemented);
}
}
void Response::generateResponse()
{
if (state_ == READ_FILE) {
_readContent();
} else if (state_ == WRITE_FILE) {
_writeContent();
} else if (state_ == PROCESSING_CGI) {
_processCGI();
}
}
void Response::_handleMethodHEAD()
{
this->_handleMethodGET();
if (state_ == READ_FILE) {
close(responseData_.fd);
state_ = READY_FOR_SEND;
}
}
void Response::_handleMethodGET()
{
responseData_.status = OK;
if (isAutoIndex()) {
return _handleAutoindex();
}
if (isCGI()) {
return _handleCGI();
}
responseData_.file = config->getServerPath(request->getPath());
_openContent();
}
void Response::_handleMethodPUT()
{
responseData_.status = Created;
responseData_.file = config->getUploadPath(request->getPath());
responseData_.fd = open(responseData_.file.c_str(),
O_CREAT | O_WRONLY | O_TRUNC, S_IRWXU);
fcntl(responseData_.fd, F_SETFL, O_NONBLOCK);
if (responseData_.fd < 0) {
return _handleInvalidRequest(Forbidden);
}
responseData_.content = request->getBody();
state_ = WRITE_FILE;
}
void Response::_handleMethodPOST()
{
responseData_.status = OK;
if (isCGI()) {
return _handleCGI();
}
_handleMethodPUT();
}
void Response::_handleMethodDELETE()
{
responseData_.file = config->getServerPath(request->getPath());
responseData_.status = NoContent;
int ret = std::remove(responseData_.file.c_str());
if (ret < 0) {
if (errno == ENOENT) {
return _handleInvalidRequest(NotFound);
} else {
return _handleInvalidRequest(Forbidden);
}
} else {
state_ = READY_FOR_SEND;
}
}
void Response::_handleInvalidRequest(int code)
{
responseData_.file = config->searchError_page(code);
responseData_.status = code;
if (isFileExists(responseData_.file)) {
_openContent();
} else {
responseData_.content = generateErrorPage(code);
responseData_.contentLength = responseData_.content.size();
responseData_.contentType = "text/html";
state_ = READY_FOR_SEND;
}
}
std::string Response::getResponse()
{
std::string result = responseData_.content;
if (needHeaders) {
result = getHeaders() + result;
}
return result;
}
bool Response::isGenerated() const
{
return state_ == READY_FOR_SEND;
}
void Response::_openContent()
{
if (isDirectory(responseData_.file))
return _handleInvalidRequest(Forbidden);
responseData_.fd = open(responseData_.file.c_str(), O_RDONLY);
if (responseData_.fd == -1) {
if (errno == ENOENT) {
return _handleInvalidRequest(NotFound);
} else {
return _handleInvalidRequest(Forbidden);
}
}
struct stat info = {};
stat(responseData_.file.c_str(), &info);
responseData_.contentLength = info.st_size;
responseData_.contentType = _getContentType(responseData_.file);
responseData_.lastModified = convertTime(&info.st_mtime);
fcntl(responseData_.fd, F_SETFL, O_NONBLOCK);
state_ = READ_FILE;
}
void Response::_readContent()
{
char buf[BUF_SIZE] = {};
long ret = read(responseData_.fd, buf, BUF_SIZE);
if (ret < 0) {
close(responseData_.fd);
responseData_ = response_data();
return _handleInvalidRequest(InternalError);
} else {
responseData_.content.append(std::string(buf, BUF_SIZE));
}
if (ret < BUF_SIZE || responseData_.content.size() == responseData_.contentLength) {
state_ = READY_FOR_SEND;
close(responseData_.fd);
}
}
void Response::_writeContent()
{
long ret = write(responseData_.fd, responseData_.content.c_str(), responseData_.content.size());
if (ret < 0) {
close(responseData_.fd);
responseData_ = response_data();
return _handleInvalidRequest(InternalError);
} else {
responseData_.content.erase(0, ret);
if (responseData_.content.empty()) {
close(responseData_.fd);
state_ = READY_FOR_SEND;
}
}
}
std::string Response::_getContentType(const std::string &file)
{
std::string endWith = file.substr(file.find_last_of('.') + 1);
if (endWith == "html")
return "text/html";
if (endWith == "jpg")
return "image/jpeg";
if (endWith == "png")
return "image/png";
if (endWith == "css")
return "text/css";
if (endWith == "js")
return "application/javascript";
return "text/plain";
}
bool Response::isReadyGenerate(fd_set *readfds, fd_set *writefds) const
{
if (FD_ISSET(responseData_.fd, writefds) ||
FD_ISSET(responseData_.fd, readfds)) {
return true;
}
return false;
}
int Response::fillFdSet(fd_set *readfds, fd_set *writefds) const
{
if (state_ == READ_FILE || cgi->checkRead()) {
FD_SET(responseData_.fd, readfds);
return responseData_.fd;
} else if (state_ == WRITE_FILE || cgi->checkWrite()) {
FD_SET(responseData_.fd, writefds);
return responseData_.fd;
}
return -1;
}
std::string Response::getHeaders()
{
std::stringstream headers;
time_t t;
time(&t);
headers << "HTTP/1.1 " << responseData_.status << " " << errorMap_[responseData_.status] << "\r\n";
if (!responseData_.location.empty()) {
headers << "Location: " << responseData_.location << "\r\n";
}
if (responseData_.status == MethodNotAllowed || responseData_.status == NotImplemented) {
headers << "Allow: ";
for (std::list<std::string>::const_iterator i = config->getMethods().begin();
i != config->getMethods().end();) {
headers << *i;
if (++i != config->getMethods().end()) {
headers << ", ";
}
}
headers << "\r\n";
}
headers << "Date: " << convertTime(&t) << "\r\n";
if (!responseData_.content.empty() || request->getMethod() == "HEAD") {
headers << "Content-Type: " << responseData_.contentType << "\r\n";
headers << "Content-Length: " << responseData_.contentLength << "\r\n";
headers << "Last-Modified: " << responseData_.lastModified << "\r\n";
}
headers << "\r\n";
return headers.str();
}
bool Response::isPayloadTooLarge() const
{
size_t maxClientBody = config->getMax_client_body(request->getPath());
return request->getContentLength() > maxClientBody ||
request->getBody().size() > maxClientBody;
}
std::string Response::generateErrorPage(int code)
{
std::stringstream page;
page << "<!DOCTYPE html>\n"
"<html lang=\"en\">\n"
"<head>\n"
" <meta charset=\"UTF-8\">\n"
" <title>" << errorMap_[code] << "</title>\n"
"</head>\n"
"<body>\n"
"<h1>" << code << " " << errorMap_[code] << "</h1>\n"
"</body>\n"
"</html>";
return page.str();
}
std::string Response::getDirListing(std::string const &path, std::string req)
{
DIR *folder;
FILE_INFO *file;
std::string page;
if (req[req.size() - 1] != '/')
req += "/";
folder = opendir(path.c_str());
if (folder == 0)
return (page);
file = readdir(folder);
page.append(
"<html>\n"
"<head><title>Index of " + req + "</title></head>\n"
"<body bgcolor=\"white\">\n"
" <h1>Index of "
" <a href=\"" + req + "\">" + req + "</a></h1>");
while (file != 0) {
page.append("<a href = \"");
page.append(req);
page.append(file->d_name);
if (file->d_type == 4)
page.append("/");
page.append("\">");
page.append(file->d_name);
if (file->d_type == 4)
page.append("/");
page.append("</a><br>");
file = readdir(folder);
}
closedir(folder);
page.append("</body>");
return page;
}
bool Response::isAutoIndex()
{
bool flag = config->checkAutoindex(request->getPath());
bool dir = isDirectory(config->getPathFromLocation(request->getPath()));
return flag && dir;
}
void Response::_handleRedirect()
{
responseData_.status = config->getRedirectPath(request->getPath()).first;
responseData_.location = config->getRedirectPath(request->getPath()).second;
state_ = READY_FOR_SEND;
}
bool Response::isCGI()
{
std::string path = config->getServerPath(request->getPath());
std::string endWith = path.substr(path.find_last_of('.') + 1);
return (endWith == "php" || endWith == "py" || endWith == "out");
}
void Response::_processCGI()
{
int ret = cgi->work();
if (ret <= -1) {
_handleInvalidRequest(500);
} else if (ret > 0) {
responseData_.fd = ret;
} else if (cgi->checkDone()) {
responseData_.content = cgi->getAnswer();
state_ = READY_FOR_SEND;
needHeaders = false;
}
}
void Response::_handleAutoindex()
{
responseData_.file = config->getPathFromLocation(request->getPath());
responseData_.content = getDirListing(responseData_.file, request->getPath());
responseData_.contentLength = responseData_.content.size();
responseData_.contentType = "text/html";
state_ = READY_FOR_SEND;
}
void Response::_handleCGI()
{
responseData_.fd = cgi->init(*request, socket->getPort(), config->getCGI_Path(request->getPath()));
state_ = PROCESSING_CGI;
}