-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathHttpHandler.cpp
340 lines (320 loc) · 10.6 KB
/
HttpHandler.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
#include "HttpHandler.h"
#include "hbase.h"
#include "herr.h"
#include "hlog.h"
#include "hasync.h" // import hv::async for http_async_handler
#include "http_page.h"
int HttpHandler::customHttpHandler(const http_handler& handler) {
return invokeHttpHandler(&handler);
}
int HttpHandler::invokeHttpHandler(const http_handler* handler) {
int status_code = HTTP_STATUS_NOT_IMPLEMENTED;
if (handler->sync_handler) {
status_code = handler->sync_handler(req.get(), resp.get());
} else if (handler->async_handler) {
hv::async(std::bind(handler->async_handler, req, writer));
status_code = HTTP_STATUS_UNFINISHED;
} else if (handler->ctx_handler) {
HttpContextPtr ctx(new hv::HttpContext);
ctx->service = service;
ctx->request = req;
ctx->response = resp;
ctx->writer = writer;
status_code = handler->ctx_handler(ctx);
if (writer && writer->state != hv::HttpResponseWriter::SEND_BEGIN) {
status_code = HTTP_STATUS_UNFINISHED;
}
}
return status_code;
}
int HttpHandler::HandleHttpRequest() {
// preprocessor -> processor -> postprocessor
int status_code = HTTP_STATUS_OK;
HttpRequest* pReq = req.get();
HttpResponse* pResp = resp.get();
pReq->scheme = ssl ? "https" : "http";
pReq->client_addr.ip = ip;
pReq->client_addr.port = port;
pReq->Host();
pReq->ParseUrl();
// pReq->ParseBody();
preprocessor:
state = HANDLE_BEGIN;
if (service->preprocessor) {
status_code = customHttpHandler(service->preprocessor);
if (status_code != 0) {
goto postprocessor;
}
}
processor:
if (service->processor) {
status_code = customHttpHandler(service->processor);
} else {
status_code = defaultRequestHandler();
}
postprocessor:
if (status_code >= 100 && status_code < 600) {
pResp->status_code = (http_status)status_code;
}
if (pResp->status_code >= 400 && pResp->body.size() == 0 && pReq->method != HTTP_HEAD) {
if (service->errorHandler) {
customHttpHandler(service->errorHandler);
} else {
defaultErrorHandler();
}
}
if (fc) {
pResp->content = fc->filebuf.base;
pResp->content_length = fc->filebuf.len;
pResp->headers["Content-Type"] = fc->content_type;
pResp->headers["Last-Modified"] = fc->last_modified;
pResp->headers["Etag"] = fc->etag;
}
if (service->postprocessor) {
customHttpHandler(service->postprocessor);
}
if (status_code == 0) {
state = HANDLE_CONTINUE;
} else {
state = HANDLE_END;
parser->SubmitResponse(resp.get());
}
return status_code;
}
int HttpHandler::defaultRequestHandler() {
int status_code = HTTP_STATUS_OK;
http_handler* handler = NULL;
if (service->api_handlers.size() != 0) {
service->GetApi(req.get(), &handler);
}
if (handler) {
status_code = invokeHttpHandler(handler);
}
else if (req->method == HTTP_GET || req->method == HTTP_HEAD) {
// static handler
if (service->staticHandler) {
status_code = customHttpHandler(service->staticHandler);
}
else if (service->document_root.size() != 0) {
status_code = defaultStaticHandler();
}
else {
status_code = HTTP_STATUS_NOT_FOUND;
}
}
else {
// Not Implemented
status_code = HTTP_STATUS_NOT_IMPLEMENTED;
}
return status_code;
}
int HttpHandler::defaultStaticHandler() {
// file service
int status_code = HTTP_STATUS_OK;
std::string path = req->Path();
const char* req_path = path.c_str();
// path safe check
if (req_path[0] != '/' || strstr(req_path, "/../")) {
return HTTP_STATUS_BAD_REQUEST;
}
std::string filepath = service->document_root + path;
if (req_path[1] == '\0') {
filepath += service->home_page;
}
bool is_dir = filepath.c_str()[filepath.size()-1] == '/';
bool is_index_of = false;
if (service->index_of.size() != 0 && hv_strstartswith(req_path, service->index_of.c_str())) {
is_index_of = true;
}
if (!is_dir || is_index_of) {
FileCache::OpenParam param;
bool has_range = req->headers.find("Range") != req->headers.end();
param.need_read = req->method == HTTP_HEAD || has_range ? false : true;
param.path = req_path;
fc = files->Open(filepath.c_str(), ¶m);
if (fc == NULL) {
status_code = HTTP_STATUS_NOT_FOUND;
if (param.error == ERR_OVER_LIMIT) {
if (service->largeFileHandler) {
status_code = customHttpHandler(service->largeFileHandler);
}
}
}
} else {
status_code = HTTP_STATUS_NOT_FOUND;
}
if (fc) {
// Not Modified
auto iter = req->headers.find("if-not-match");
if (iter != req->headers.end() &&
strcmp(iter->second.c_str(), fc->etag) == 0) {
status_code = HTTP_STATUS_NOT_MODIFIED;
fc = NULL;
}
else {
iter = req->headers.find("if-modified-since");
if (iter != req->headers.end() &&
strcmp(iter->second.c_str(), fc->last_modified) == 0) {
status_code = HTTP_STATUS_NOT_MODIFIED;
fc = NULL;
}
}
}
return status_code;
}
int HttpHandler::defaultErrorHandler() {
// error page
if (service->error_page.size() != 0) {
std::string filepath = service->document_root;
filepath += '/';
filepath += service->error_page;
FileCache::OpenParam param;
fc = files->Open(filepath.c_str(), ¶m);
}
// status page
if (fc == NULL && resp->body.size() == 0) {
resp->content_type = TEXT_HTML;
make_http_status_page(resp->status_code, resp->body);
}
return 0;
}
int HttpHandler::FeedRecvData(const char* data, size_t len) {
int nfeed = 0;
if (protocol == HttpHandler::WEBSOCKET) {
nfeed = ws->parser->FeedRecvData(data, len);
if (nfeed != len) {
hloge("[%s:%d] websocket parse error!", ip, port);
}
} else {
if (state != WANT_RECV) {
Reset();
}
nfeed = parser->FeedRecvData(data, len);
if (nfeed != len) {
hloge("[%s:%d] http parse error: %s", ip, port, parser->StrError(parser->GetError()));
}
}
return nfeed;
}
int HttpHandler::GetSendData(char** data, size_t* len) {
if (state == HANDLE_CONTINUE) {
return 0;
}
HttpRequest* pReq = req.get();
HttpResponse* pResp = resp.get();
if (protocol == HTTP_V1) {
switch(state) {
case WANT_RECV:
if (parser->IsComplete()) state = WANT_SEND;
else return 0;
case HANDLE_END:
state = WANT_SEND;
case WANT_SEND:
state = SEND_HEADER;
case SEND_HEADER:
{
int content_length = 0;
const char* content = NULL;
// HEAD
if (pReq->method == HTTP_HEAD) {
if (fc) {
pResp->headers["Accept-Ranges"] = "bytes";
pResp->headers["Content-Length"] = hv::to_string(fc->st.st_size);
} else {
pResp->headers["Content-Type"] = "text/html";
pResp->headers["Content-Length"] = "0";
}
state = SEND_DONE;
goto return_nobody;
}
// File service
if (fc) {
long from, to, total;
int nread;
// Range:
if (pReq->GetRange(from, to)) {
HFile file;
if (file.open(fc->filepath.c_str(), "rb") != 0) {
pResp->status_code = HTTP_STATUS_NOT_FOUND;
state = SEND_DONE;
goto return_nobody;
}
total = file.size();
if (to == 0 || to >= total) to = total - 1;
pResp->content_length = to - from + 1;
nread = file.readrange(body, from, to);
if (nread != pResp->content_length) {
pResp->status_code = HTTP_STATUS_INTERNAL_SERVER_ERROR;
state = SEND_DONE;
goto return_nobody;
}
pResp->SetRange(from, to, total);
state = SEND_BODY;
goto return_header;
}
// FileCache
// NOTE: no copy filebuf, more efficient
header = pResp->Dump(true, false);
fc->prepend_header(header.c_str(), header.size());
*data = fc->httpbuf.base;
*len = fc->httpbuf.len;
state = SEND_DONE;
return *len;
}
// API service
content_length = pResp->ContentLength();
content = (const char*)pResp->Content();
if (content) {
if (content_length > (1 << 20)) {
state = SEND_BODY;
goto return_header;
} else {
// NOTE: header+body in one package if <= 1M
header = pResp->Dump(true, false);
header.append(content, content_length);
state = SEND_DONE;
goto return_header;
}
} else {
state = SEND_DONE;
goto return_header;
}
return_nobody:
pResp->content_length = 0;
return_header:
if (header.empty()) header = pResp->Dump(true, false);
*data = (char*)header.c_str();
*len = header.size();
return *len;
}
case SEND_BODY:
{
if (body.empty()) {
*data = (char*)pResp->Content();
*len = pResp->ContentLength();
} else {
*data = (char*)body.c_str();
*len = body.size();
}
state = SEND_DONE;
return *len;
}
case SEND_DONE:
{
// NOTE: remove file cache if > 16M
if (fc && fc->filebuf.len > (1 << 24)) {
files->Close(fc);
}
fc = NULL;
header.clear();
body.clear();
return 0;
}
default:
return 0;
}
} else if (protocol == HTTP_V2) {
return parser->GetSendData(data, len);
}
return 0;
}