-
Notifications
You must be signed in to change notification settings - Fork 1
/
HttpChannel.cpp
404 lines (368 loc) · 14.4 KB
/
HttpChannel.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
//
// Created by tong on 19-2-22.
//
#include <unistd.h>
#include <sys/socket.h>
#include <functional>
#include "HttpChannel.h"
#include "Log.h"
#include "Response.h"
using namespace std::placeholders;
HttpChannel::HttpChannel(int fd, int timeout, std::weak_ptr<EventLoop> loop) :
Channel(fd, EPOLLIN, timeout), __eventLoop(loop){
__parser.callback = std::bind<bool>(&HttpChannel::__parserCallback, this, _1, _2, _3, _4, _5);
}
HttpChannel::~HttpChannel() {
LOG << "Thread id: " << std::this_thread::get_id() << " Disconnected. fd " << __fd << '\n';
}
void HttpChannel::__handleReadEvent() {
vector<char> recvData;
int readRet = __readn(__fd, recvData);
switch(__curState){
case S_IN:
if(readRet < 0){ // ERROR
__nextState = S_CLOSE;
}else{
if(recvData.empty()){
__nextState = readRet == 0 ? S_CLOSE : S_IN;
}else{
// parse data
int n = 1, offset = 0;
while(n > 0 && recvData.size() > offset){
n = __parser(recvData.data()+offset, recvData.size()-offset);
if(n <= 0){ // 报文格式错误 或者 未遇到报文结尾
vector<char> response = Response::make_xxx_response(BAD_REQUEST);
__sendBuf.insert(__sendBuf.end(), response.begin(), response.end());
}else{
// 此时报文已经在buffer中
}
// 此时,发送__sendBuf中的数据
int sendLen = __writen(__fd, __sendBuf);
if(sendLen < 0){ // write遇到错误
__nextState = S_CLOSE;
}else if(sendLen == __sendBuf.size()){
// 发完了就要关闭连接,但是如果未解析完,仍会继续解析,后面如果未发送完,那么就会更改状态
__nextState = readRet == 0 ? S_CLOSE : S_IN;
__sendBuf.clear();
}else{ // 未发完response
__nextState = readRet == 0 ? S_OUT : S_IN_OUT;
__sendBuf.erase(__sendBuf.begin(), __sendBuf.begin()+sendLen);
}
offset += n;
}
}
}
break;
case S_IN_OUT:
if(readRet < 0){
__nextState = S_CLOSE;
}else{
if(recvData.empty()){
__nextState = readRet == 0 ? S_OUT : S_IN_OUT;
}else{
int n = 1, offset = 0;
while(n > 0 && recvData.size() > offset){
n = __parser(recvData.data()+offset, recvData.size()-offset);
if(n <= 0){ // 报文格式错误
vector<char> response = Response::make_xxx_response(BAD_REQUEST);
__sendBuf.insert(__sendBuf.end(), response.begin(), response.end());
}else{ // 已解析完一条报文
// do nothing
}
// 此时,发送__sendBuf中的报文
int sendLen = __writen(__fd, __sendBuf);
if(sendLen < 0){ // write遇到错误
__nextState = S_CLOSE;
}else if(sendLen == __sendBuf.size()){
__nextState = readRet == 0 ? S_CLOSE : S_IN;
__sendBuf.clear();
}else{
__nextState = readRet == 0 ? S_OUT : S_IN_OUT;
__sendBuf.erase(__sendBuf.begin(), __sendBuf.begin()+sendLen);
}
offset += n;
}
}
}
break;
default:
LOG << "HttpChannel::__handleReadEvent: Wrong __curState.\n";
exit(0);
break;
}
shared_ptr<EventLoop> p = __eventLoop.lock();
if(!p){
LOG << "HttpChannel::__eventLoop is nullptr.\n";
exit(0);
}
if(__nextState == S_CLOSE){
p->delChannel(shared_from_this());
}else if(__nextState != __curState){
uint32_t events = 0;
if(__nextState == S_IN){
events = EPOLLIN;
}else if(__nextState == S_OUT){
events = EPOLLOUT;
}else if(__nextState == S_IN_OUT){
events = EPOLLIN | EPOLLOUT;
}
setEvent(events);
p->modChannel(shared_from_this());
}
__curState = __nextState;
//////////////////////////////////////////////////////////////////////////////////////////////////////
// vector<char> data;
//
// int ret = __readn(__fd, data);
//
// if(ret < 0){ // 发生错误
// __state = NO_RDWR;
// }else if(ret == 0){ // 收到FIN
// __state = NO_RD;
// }else{
// __state = NORMAL;
// }
//
// // 解析报文,填入__sendBuf,然后尝试发送。该过程中__state状态可能发生改变。
// if(__state != NO_RDWR && !data.empty()){ // 需要解析报文
// // parse() 返回-1,表示解析失败;返回0,表示未读到request结束;返回>0,表示解析成功一次request,返回的是已读字符数目。
// int n = 1, offset = 0;
// while(n > 0 && data.size() > offset){
// n = __parser(data.data()+offset, data.size()-offset);
// if(n < 0){ // 报文格式错误
// __state = NO_RD;
// vector<char> response = make_response(BAD_REQUEST);
// __sendBuf.insert(__sendBuf.end(), response.begin(), response.end());
// }else if(n == 0){ // 未遇到报文的结尾
// if(__state == NO_RD){
// vector<char> response = make_response(BAD_REQUEST);
// __sendBuf.insert(__sendBuf.end(), response.begin(), response.end());
// }else{
// // do nothing
// }
// }else{ // 已解析完一条报文
// // do nothing
// }
//
// // 此时,发送__sendBuf中的报文
// int sendLen = __writen(__fd, __sendBuf);
// if(sendLen < 0){ // write遇到错误
// __state = NO_RDWR;
// }else{
// __sendBuf.erase(__sendBuf.begin(), __sendBuf.begin()+sendLen);
// // do nothing
// }
// offset += n;
// }
// }
//
// shared_ptr<EventLoop> p = __eventLoop.lock();
// if(!p){
// throw logic_error("HttpChannel::__eventLoop is nullptr.");
// }
// // 根据写缓冲区是否有数据,以及__state的值,来判断下一步的动作
// if(__state == NO_RDWR){
// p->delChannel(shared_from_this());
// }else if(__state == NO_RD){
// if(__sendBuf.empty()){
// p->delChannel(shared_from_this());
// __state = NO_RDWR;
// }else{
// shutdown(__fd, SHUT_RD);
// if(getEvent() != EPOLLOUT){ // 如果之前关注IN 或者 之前 没关注OUT
// setEvent(EPOLLOUT);
// p->modChannel(shared_from_this());
// }
// }
// }else {
// if(__sendBuf.empty()){ // 仅关注可读
// if(getEvent() != EPOLLIN){
// setEvent(EPOLLIN);
// p->modChannel(shared_from_this());
// }
// }else{ // 继关注可读又关注可写
// if(getEvent() != EPOLLIN|EPOLLOUT){
// setEvent(EPOLLIN|EPOLLOUT);
// p->modChannel(shared_from_this());
// }
// }
// }
}
void HttpChannel::__handleWriteEvent() {
shared_ptr<EventLoop> p = __eventLoop.lock();
if(!p){
LOG << "HttpChannel::__eventLoop is nullptr.\n";
exit(0);
}
switch(__curState){
case S_OUT:
if(__sendBuf.empty()){
__nextState = S_CLOSE;
}else{
int sendLen = __writen(__fd, __sendBuf);
if(sendLen < 0 || sendLen == __sendBuf.size()){ // 发送错误 或者 发送完
__nextState = S_CLOSE;
__sendBuf.clear();
}else{ // 发送未完
__nextState = S_OUT;
__sendBuf.erase(__sendBuf.begin(), __sendBuf.begin()+sendLen);
}
}
break;
case S_IN_OUT:
if(__sendBuf.empty()){
__nextState = S_IN;
}else{
int sendLen = __writen(__fd, __sendBuf);
if(sendLen < 0){ // write发生错误
__nextState = S_CLOSE;
p->delChannel(shared_from_this());
}else if(sendLen == __sendBuf.size()){ // 发送完
__nextState = S_IN;
__sendBuf.clear();
}else{ // 没发送完
__nextState = S_IN_OUT;
__sendBuf.erase(__sendBuf.begin(), __sendBuf.begin()+sendLen);
}
}
break;
case S_IN: // 说明__handleWriteEvent需要发送的数据,在__handleReadEvent里面就已经做完了
case S_CLOSE: // 说明之前执行过__handleReadEvent,并且已经delChannnel。
return;
default:
break;
}
if(__nextState == S_CLOSE){
p->delChannel(shared_from_this());
}else if(__nextState != __curState){
uint32_t events = 0;
if(__nextState == S_IN){
events = EPOLLIN;
}else if(__nextState == S_OUT){
events = EPOLLOUT;
}else if(__nextState == S_IN_OUT){
events = EPOLLIN | EPOLLOUT;
}
setEvent(events);
p->modChannel(shared_from_this());
}
__curState = __nextState;
// shared_ptr<EventLoop> p = __eventLoop.lock();
// if(!p){
// throw logic_error("HttpChannel::__eventLoop is nullptr.");
// }
//
// if(__state == NO_RDWR){
// // 说明handleRead里面已经delChannel了。
// return;
// }else if(__state == NO_RD){
// if(__sendBuf.empty()/*缓冲区没有数据*/){
// __state = NO_RDWR;
// p->delChannel(shared_from_this());
// }else{
// int sendLen = __writen(__fd, __sendBuf);
//
// if(sendLen < 0 || sendLen == __sendBuf.size()){ // 发送错误 或者 发送完
// __state = NO_RDWR;
// p->delChannel(shared_from_this());
// }else{ // 发送未完
// if(getEvent() != EPOLLOUT){
// setEvent(EPOLLOUT);
// p->modChannel(shared_from_this());
// }
// __sendBuf.erase(__sendBuf.begin(), __sendBuf.begin()+sendLen);
// }
// }
// }else{
// if(__sendBuf.empty()){
// if(getEvent() != EPOLLIN){
// setEvent(EPOLLIN);
// p->modChannel(shared_from_this());
// }
// }else{
// int sendLen = __writen(__fd, __sendBuf);
//
// if(sendLen < 0){ // write发生错误
// __state = NO_RDWR;
// p->delChannel(shared_from_this());
// }else if(sendLen == __sendBuf.size()){ // 发送完
// if(getEvent() != EPOLLIN){
// setEvent(EPOLLIN);
// p->modChannel(shared_from_this());
// }
// __sendBuf.erase(__sendBuf.begin(), __sendBuf.begin()+sendLen);
// }else{ // 没发送完
// if(getEvent() != EPOLLIN|EPOLLOUT){
// setEvent(EPOLLIN|EPOLLOUT);
// p->modChannel(shared_from_this());
// }
// __sendBuf.erase(__sendBuf.begin(), __sendBuf.begin()+sendLen);
// }
// }
// }
}
bool HttpChannel::__parserCallback(string &method, string &url, string &version, std::vector<Header> &requestHeaders,
std::vector<char> &messageBody) {
LOG << "Thread id: " << std::this_thread::get_id() << " Received. " << method << ' ' << url << ' ' << version << " fd " << getFd() << '\n';
// 构造报文,添进buffer,返回值应该使得没调用一次parser()返回一次。
vector<char> response;
if(version != "HTTP/1.1"){
response = std::move(Response::make_xxx_response(HTTP_VERSION_NOT_SUPPORTED));
}else{
if(method == "GET"){
response = Response::make_get_response(url, requestHeaders, messageBody);
}else if(method == "POST"){
response = std::move(Response::make_post_response(url, requestHeaders, messageBody));
}else if(method == "HEAD"){
response = std::move(Response::make_head_response(url, requestHeaders, messageBody));
}else{
response = std::move(Response::make_xxx_response(METHOD_NOT_ALLOWD));
}
}
__sendBuf.insert(__sendBuf.end(), response.begin(), response.end());
bool keepGoing = false;
return keepGoing;
}
// 最好还是,parser每调用一次callback,parser()就返回一次。这样可以根据每个报文的发送是否成功,来控制连接。
/**
* 返回-1,表示fd发生错误
* 返回0,表示收到fin
* 返回1,表示正常读到EAGAIN为止,但是可能未读到数据
* **/
int HttpChannel::__readn(int fd, vector<char> &buf) const {
// 读数据,一直到EAGAIN(读完),或者0(断开),或者其他错误。EINTR错误不会发生,因为EINTR只发生在会blocked system call上。
char tmp[1024];
while(true){
ssize_t n = read(fd, tmp, sizeof(tmp));
if(n > 0){
buf.insert(buf.end(), tmp, tmp+n);
}else if(n == 0){ // 对端关闭连接
return 0;
}else{
if(errno == EAGAIN){
return 1;
}else{ // 极大可能是Connection reset by peer
return -1;
}
}
}
}
/**
* 返回-1,表示发生错误;
* 返回>=0,表示写进去的字节数;
* **/
int HttpChannel::__writen(int fd, const vector<char> &buf) const {
if(buf.empty()){
return 0;
}
ssize_t n = write(fd, buf.data(), buf.size());
if(n < 0){
if(errno == EAGAIN){
return 0;
}else{
return -1;
}
}else{
return int(n);
}
}