-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathKHttpContext.cpp
More file actions
141 lines (135 loc) · 3.95 KB
/
Copy pathKHttpContext.cpp
File metadata and controls
141 lines (135 loc) · 3.95 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
#ifdef USE_RINGBUFFER
#include "../tcp/KRingBuffer.h"
#else
#include "../tcp/KBuffer.h"
#endif
#include "KHttpContext.h"
#include <algorithm>
using namespace kback;
const char HttpContext::kCRLF[] = "\r\n"; // 回车换行
// 对请求行的处理
bool HttpContext::processRequestLine(const char *begin, const char *end) {
bool succeed = false;
const char *start = begin;
const char *space = std::find(start, end, ' ');
if (space != end && request_.setMethod(start, space)) {
start = space + 1;
space = std::find(start, end, ' ');
if (space != end) {
const char *question = std::find(start, space, '?');
if (question != space) {
request_.setPath(start, question);
request_.setQuery(question, space);
} else {
request_.setPath(start, space);
}
start = space + 1;
succeed = end - start == 8 && std::equal(start, end - 1, "HTTP/1.");
if (succeed) {
if (*(end - 1) == '1') {
request_.setVersion(HttpRequest::kHttp11);
} else if (*(end - 1) == '0') {
request_.setVersion(HttpRequest::kHttp10);
} else {
succeed = false;
}
}
}
}
return succeed;
}
#ifdef USE_RINGBUFFER
bool HttpContext::parseRequest(Buffer *buf, Timestamp receiveTime) {
string str_buf = buf->retrieveAsString();
bool ok = true;
bool hasMore = true;
// 利用状态机转移,分三部分对请求报文进行解析
while (hasMore) {
int start = 0;
if (state_ == kExpectRequestLine) {
const char *crlf =
std::search(str_buf.data() + start, str_buf.data() + str_buf.size(),
kCRLF, kCRLF + 2);
if (crlf) {
ok = processRequestLine(str_buf.data() + start, crlf);
if (ok) {
request_.setReceiveTime(receiveTime);
// buf->retrieveUntil(crlf + 2);
start = crlf + 2 - str_buf.data();
state_ = kExpectHeaders;
} else {
hasMore = false;
}
} else {
hasMore = false;
}
} else if (state_ == kExpectHeaders) {
const char *crlf =
std::search(str_buf.data() + start, str_buf.data() + str_buf.size(),
kCRLF, kCRLF + 2);
if (crlf) {
const char *colon = std::find(str_buf.data() + start, crlf, ':');
if (colon != crlf) {
request_.addHeader(str_buf.data() + start, colon, crlf);
} else {
// 空行,头部解析完毕
state_ = kGotAll;
hasMore = false;
}
// buf->retrieveUntil(crlf + 2);
start = crlf + 2 - str_buf.data();
} else {
hasMore = false;
}
} else if (state_ == kExpectBody) {
// 可以用于提取报文的主体部分
}
}
return ok;
}
#else
// 解析http请求
bool HttpContext::parseRequest(Buffer *buf, Timestamp receiveTime) {
bool ok = true;
bool hasMore = true;
// 利用状态机转移,分三部分对请求报文进行解析
while (hasMore) {
// 请求行解析
if (state_ == kExpectRequestLine) {
const char *crlf = buf->findCRLF();
if (crlf) {
ok = processRequestLine(buf->peek(), crlf);
if (ok) {
request_.setReceiveTime(receiveTime);
buf->retrieveUntil(crlf + 2);
state_ = kExpectHeaders;
} else {
hasMore = false;
}
} else {
hasMore = false;
}
}
// 请求头解析
else if (state_ == kExpectHeaders) {
const char *crlf = buf->findCRLF();
if (crlf) {
const char *colon = std::find(buf->peek(), crlf, ':');
if (colon != crlf) {
request_.addHeader(buf->peek(), colon, crlf);
} else {
// 空行,头部解析完毕
state_ = kGotAll;
hasMore = false;
}
buf->retrieveUntil(crlf + 2);
} else {
hasMore = false;
}
} else if (state_ == kExpectBody) {
// 可以用于提取报文的主体部分
}
}
return ok;
}
#endif