-
Notifications
You must be signed in to change notification settings - Fork 0
/
HttpHandler.cpp
642 lines (532 loc) · 17.2 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
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
/*
* File: HttpHandler.cpp
* Author: try
*
* Created on 2011年5月28日, 下午3:55
*/
#include "HttpHandler.h"
#include "HttpProcess.h"
#include "SocketUtils.h"
#include <iostream>
#include <queue>
#include <deque>
#include "libev.h"
#include <errno.h>
#include "MemoryPool.h"
#include "HttpServletManager.h"
#include "HttpHandlerManager.h"
using namespace std;
HttpHandler::HttpHandler():status(NEW), fd(-1),
idleTimeout(false), httpProcess(NULL), readBuff(NULL), request(requestData), response(NULL), httpCode(0),
servlet(NULL),
bodyDataBytes(0), completeFlag(false)
{
}
HttpHandler::~HttpHandler() {
if(status != STOP){
stop();
}
if(readBuff){
MemoryPool::instance.free(readBuff);
}
if(response) delete response;
}
void HttpHandler::init(HttpProcess* p, FD fd){
if(!readBuff){
readBuff = (char*)MemoryPool::instance.malloc(p->conf->requestBufferSize);
}
if(!response){
response = new Response(*this, request, p->conf);
}
this->status = NEW;
this->httpProcess = p;
this->fd = fd;
this->servlet = NULL;
this->completeFlag = false;
this->setIdleTimeout(true);
}
void HttpHandler::callServletOnWriteQueueEmptyEvent(){
if(servlet){
servlet->onWriteQueueEmptyEvent();
}
}
HttpHandler::STATUS HttpHandler::getStatus(){
return status;
}
void HttpHandler::start(){
if(isClosed()){
LOG_ERROR("isClosed()");
return;
}
LOG_DEBUG("fd:%d", fd);
status = RUNNING;
//初始化IOWriter, 设置FD, 设置事件监听器
iowriter.setFD(fd);
iowriter.addListener(EVENT_TYPE_IO_WRITE_COMPLETION, this);
//初始化IOReader, 设置FD, 设置事件监听器
ioreader.setFD(fd);
ioreader.addListener(EVENT_TYPE_IO_READ_COMPLETION, this);
prepareRead(); //准备读Socket数据
evtimer.set<HttpHandler, &HttpHandler::timeoutCallback>(this);
}
bool HttpHandler::isRunning(){
return status == RUNNING;
}
bool HttpHandler::isClosed(){
return fd < 0;
}
void HttpHandler::setIdleTimeout(bool b){
if(idleTimeout != b){
if(idleTimeout && evtimer.is_active()){
evtimer.stop();
}
idleTimeout = b;
}
}
void HttpHandler::startTimeoutEv(){
if(idleTimeout){
if(evtimer.is_active()){
evtimer.stop();
}
evtimer.start(httpProcess->conf->idleTimeout, 0);
}
}
void HttpHandler::stopTimeoutEv(){
if(idleTimeout){
if(evtimer.is_active()){
evtimer.stop();
}
}
}
void HttpHandler::error(){
iowriter.close();
ioreader.close();
stop();
}
void HttpHandler::notifyClose(){
//Servlet不存在或onNotifiedEvent()返回true, 就停止服务
if(!servlet || servlet->onNotifiedCloseEvent()){
stop();
}
}
/**
* 强制关闭
*/
void HttpHandler::close(){
error();
}
void HttpHandler::stop(){
LOG_DEBUG("iowriter.waitingQueueSize():%d, status:%d", iowriter.waitingQueueSize(), status);
if(status == STOP){
LOG_WARN("already is STOP, fd:%d, status:%d", this->fd, status);
return;
}
//LOG_DEBUG("1 fd:%d, status:%d", this->fd, status);
if(iowriter.waitingQueueSize() > 0){
status = STOPPING;
//如果responseDataQueue不为空,说明还有没发送完的数据
return;
}
//LOG_DEBUG("2 fd:%d, status:%d", this->fd, status);
if(status != STOP){
status = STOP;
iowriter.close();
ioreader.close();
//必须在status = STOP;之后
clean();
// ::shutdown(fd, SHUT_RDWR);
::close(fd);
fd = -1;
//回收HttpHandler
HttpHandlerManager::instance.recycle(this);
stopTimeoutEv();
}
}
void HttpHandler::clean(){
LOG_DEBUG("iowriter.waitingQueueSize():%d, status:%d", iowriter.waitingQueueSize(), status);
if(iowriter.waitingQueueSize() > 0){
status = CLEANING;
//如果responseDataQueue不为空,说明还有没发送完的数据
return;
}
stopWrite();
if(servlet){
//发起请求完成事件
servlet->onCompletionEvent();
//回收HttpServlet
HttpServletManager::instance.recycle(servlet);
servlet = NULL;
}
// if(requestData.data.size() > 0 || httpCode > 0){
// //请求计数
// httpProcess->totalRequestCount++;
// }else{
// LOG_DEBUG("HttpHandler::clean, requestData.data.size():%d, httpCode:%d", requestData.data.size(), httpCode);
// }
httpProcess->totalRequestCount++;
//复位HTTP Code
httpCode = 0;
bodyDataBytes = 0;
idleTimeout = true;
completeFlag = false;
//清理请求数据
request.clean();
//清理响应数据
response->clean();
// LOG_DEBUG("HttpHandler::clean, 2 fd:%d, status:%d", fd, status);
if(status != STOP){
LOG_DEBUG("3 fd:%d, status:%d", fd, status);
status = RUNNING;
//开始超时事件
//startTimeoutEv();
prepareRead();
}
}
bool HttpHandler::prepareRequestData(){
//准备请求行
if(!requestData.existReqLine){
if(!prepareRequestLine()){
return false;
}
if(requestData.existReqLine){
//LOG_DEBUG("onRequestLineEvent start");
//发送请求行准备好事件
onRequestLineEvent();
//LOG_DEBUG("onRequestLineEvent end, httpCode:%d", httpCode);
if(httpCode != 0){
return false;
}
}
}
//准备请求头
if(requestData.existReqLine && !requestData.existReqHeaders){
if(!prepareRequestHeaders()){
return false;
}
if(requestData.existReqHeaders){
//发送消息头准备好事件
onRequestHeadersEvent();
if(httpCode != 0){
return false;
}
}
}
return true;
}
bool HttpHandler::prepareRequestLine(){
int size = requestData.data.size();
const char* data = requestData.data.data();
int begin = requestData.notTraversePos;
//准备请求方式位置
if(requestData.methodEndPos == 0){
for(; begin < size; begin++){
if(data[begin] == CHAR_SP){
requestData.methodEndPos = begin;
requestData.notTraversePos = ++begin;
requestData.pathStartPos = begin;
break;
}else if(data[begin] == CHAR_LF){
httpCode = HTTP_CODE_400; //请求错误
return false;
}
}
}
//准备请求URI路径
if(requestData.methodEndPos > 0 && requestData.pathEndPos == 0){
for(; begin < size; begin++){
if(data[begin] == CHAR_QUESTION){
requestData.pathEndPos = begin;
requestData.notTraversePos = ++begin;
requestData.parameterStartPos = begin;
break;
}else if(data[begin] == CHAR_SP){
requestData.pathEndPos = begin;
//没有请求参数, 设置开始位置和结束位置相等
requestData.parameterStartPos = begin;
requestData.parameterEndPos = begin;
requestData.notTraversePos = ++begin;
requestData.protocolStartPos = requestData.notTraversePos;
break;
}else if(data[begin] == CHAR_LF){
int pos = begin-1;
requestData.pathEndPos = pos;
requestData.parameterStartPos = pos;
requestData.parameterEndPos = pos;
requestData.protocolStartPos = pos;
requestData.protocolEndPos = pos;
requestData.notTraversePos = ++begin;
requestData.reqLineEndPos = pos;
requestData.headersStartPos = requestData.notTraversePos;
requestData.currentHeaderStartPos = requestData.notTraversePos;
requestData.existReqLine = true;
break;
}
}
}
//准备请求参数
if(requestData.pathEndPos > 0 && requestData.parameterEndPos == 0){
for(; begin < size; begin++){
if(data[begin] == CHAR_SP){
requestData.parameterEndPos = begin;
requestData.notTraversePos = ++begin;
requestData.protocolStartPos = requestData.notTraversePos;
break;
}else if(data[begin] == CHAR_LF){
int pos = begin-1;
requestData.parameterEndPos = pos;
requestData.protocolStartPos = pos;
requestData.protocolEndPos = pos;
requestData.notTraversePos = ++begin;
requestData.reqLineEndPos = pos;
requestData.headersStartPos = requestData.notTraversePos;
requestData.currentHeaderStartPos = requestData.notTraversePos;
requestData.existReqLine = true;
}
}
}
//准备请求协议信息
if(requestData.parameterEndPos > 0 && requestData.protocolEndPos == 0){
for(; begin < size; begin++){
if(data[begin] == CHAR_LF){
int pos = begin-1;
requestData.protocolEndPos = pos;
requestData.notTraversePos = ++begin;
requestData.reqLineEndPos = pos;
requestData.headersStartPos = requestData.notTraversePos;
requestData.currentHeaderStartPos = requestData.notTraversePos;
requestData.existReqLine = true;
break;
}
}
}
if(requestData.existReqLine){
//构建请求行
requestData.buildRequestLine();
}
return true;
}
bool HttpHandler::prepareRequestHeaders(){
int size = requestData.data.size();
const char* data = requestData.data.data();
int begin = requestData.currentHeaderStartPos;
int keyStartPos = 0;
int keyEndPos = 0;
int valueStartPos = 0;
int valueEndPos = 0;
char c;
char lc;
for(; begin < size; begin++){
lc = begin>requestData.headersStartPos?data[begin-1]:0;
c = data[begin];
if(c == CHAR_LF && lc == CHAR_CR && data[begin-2] == CHAR_LF){
//所有消息头处理完成
requestData.headersEndPos = begin-1;
requestData.existReqHeaders = true;
requestData.notTraversePos = begin+1;
requestData.bodyStartPos = requestData.notTraversePos;
//构建消息头
requestData.buildRequestHeaders();
break;
}
if(keyStartPos == 0){
keyStartPos = lc==0?begin:begin-1; //Key开始位置
}
if(keyEndPos == 0 && c == ':'){
keyEndPos = begin; //Key结束位置
continue;
}
if(keyEndPos > 0 && valueStartPos == 0){
valueStartPos = begin; //Value开始位置
}
//if(c == CHAR_LF){ //c == CHAR_CR
if(lc == CHAR_LF && (c != CHAR_SP && c != CHAR_HT)){
int pos = begin-2;
//一行消息头处理完成
valueEndPos = pos; //Value结束位置
if(keyEndPos == 0){
//说明只有key开始位置,没有=号和值
keyEndPos = pos;
valueStartPos = pos; //值开始位置和结束位置相等
}
//构建消息头结束
if(keyStartPos < keyEndPos){
requestData.saveHeaderPos(keyStartPos, keyEndPos, valueStartPos, valueEndPos);
}
// LOG_DEBUG("keyStartPos:%d, keyEndPos:%d, valueStartPos:%d, valueEndPos:%d", keyStartPos, keyEndPos, valueStartPos, valueEndPos);
//初始化下一个消息头位置信息
keyStartPos = 0;
keyEndPos = 0;
valueStartPos = 0;
valueEndPos = 0;
requestData.currentHeaderStartPos = begin;
}
}
return true;
}
bool HttpHandler::prepareRequestBody(){
size_t n = requestData.data.size()-requestData.bodyStartPos;//当前读到的body字节数量
if(servlet->onReadBodyEvent(&(requestData.data.data()[requestData.bodyStartPos]), n)){
requestData.existReqBody = true;
}else{
RequestData::DataIterator it = requestData.data.begin() + requestData.bodyStartPos;
requestData.data.erase(it, it + n);
}
return httpCode == 0;
}
bool HttpHandler::handleRequest(){
servlet->service(request, *response);
if(!response->isAsynAnswerMode()){
//回应客户端请求
answer();
return true;
}
return false;
}
//bool HttpHandler::service(Request& req, Response& resp){
// return true;
//}
void HttpHandler::answer(){
response->complete();
}
/**
* 处理读到的数据
*/
void HttpHandler::handleReadCompletionEvent(IOReadCompletionEvent* e){
LOG_DEBUG("isRunning:%d, readsize:%d", isRunning(), e->length);
//停止超时事件
stopTimeoutEv();
//读数据错误或读到EOF
if(e->length <= 0){
//LOG_DEBUG("read size:%d, errno:%d", e->length, errno);
error();
return;
}
//将读到的数据写入requestData.data
requestData.data.insert(requestData.data.end(), e->dataBuff, e->dataBuff + e->length);
//在成功读入数据后调用此方法
onReadEvent(e->length);
if(httpCode != 0){
//发生错误, 根据httpCode值进行处理
onHttpCodeEvent();
return;
}
//准备请求数据
if(!requestData.existReqHeaders){
if(!prepareRequestData()){
//发生错误, 根据httpCode值进行处理
onHttpCodeEvent();
return;
}
}
if(requestData.existReqHeaders){
//第一次进入handle时创建HttpServlet
LOG_DEBUG("servlet exist:%s", (servlet?"true":"false"));
if(!servlet){
servlet = HttpServletManager::instance.create(request.path);
servlet->init(this, request, *response);
}
completeFlag = handleRequest();
LOG_DEBUG("1, completeFlag:%d, httpCode:%d", completeFlag, httpCode);
if(httpCode != 0){
//发生错误, 根据httpCode值进行处理
onHttpCodeEvent();
return;
}
}
LOG_DEBUG("2, completeFlag:%d, httpCode:%d", completeFlag, httpCode);
if(!completeFlag){
LOG_DEBUG("prepareRead");
prepareRead();
}
}
/**
* 处理写完成事件
*/
void HttpHandler::handleWriteCompletionEvent(IOWriteCompletionEvent* e){
LOG_DEBUG("e.length:%d, iowriter.waitingQueueSize():%d, status:%d", e->length, iowriter.waitingQueueSize(), status);
if(e->length < 0){
//LOG_WARN("error, e->length:%d", e->length);
error();
}else{
// if(e->length == 0){
// LOG_DEBUG("e->length == 0, status:%d, iowriter.waitingQueueSize():%d", status, iowriter.waitingQueueSize());
// }
if(status == STOP){
LOG_WARN("status is STOP");
stopWrite();
return;
}
if(isClosed()){
LOG_WARN("isClosed()");
stopWrite();
return;
}
if(iowriter.waitingQueueSize() == 0){
//检查是否是STOPPING状态,如果是就停止服务
if(status == STOPPING){
stop();
}else if(status == CLEANING){
clean();
}else if(status == ANSWERING){
LOG_WARN("status is ANSWERING");
}else{
callServletOnWriteQueueEmptyEvent();
startTimeoutEv();
}
}
}
}
//提交读Socket数据请求
bool HttpHandler::prepareRead(){
if(isClosed())return false;
startTimeoutEv();
return ioreader.read(readBuff, httpProcess->conf->requestBufferSize);
}
//暂停读Socket数据
void HttpHandler::stopRead(){
if(isClosed())return;
ioreader.reset();
}
void HttpHandler::startWrite(){
if(isClosed())return;
iowriter.start();
}
void HttpHandler::stopWrite(){
if(isClosed())return;
iowriter.reset();
}
bool HttpHandler::handle(const Event* e){
LOG_DEBUG("e->getType():%d", e->getType());
switch(e->getType()){
case EVENT_TYPE_IO_READ_COMPLETION:
handleReadCompletionEvent((IOReadCompletionEvent*)e);
return true;
case EVENT_TYPE_IO_WRITE_COMPLETION:
handleWriteCompletionEvent((IOWriteCompletionEvent*)e);
return true;
}
return false;
}
void HttpHandler::timeoutCallback(ev::timer &ev, int revents){
//连接空闲超时, 停止服务
LOG_DEBUG("fd:%d, status:%d", fd, status);
error();
}
//--事件------------------------------------------------------------------------
bool HttpHandler::onReadEvent(unsigned int n){
return false;
}
bool HttpHandler::onRequestLineEvent(){
return false;
}
bool HttpHandler::onRequestHeadersEvent(){
return false;
}
bool HttpHandler::onHttpCodeEvent(){
return false;
}
bool HttpHandler::onCompleteEvent(){
//请求处理完成,停止Handler服务
stop();
return true;
}