-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathKHttpContext.cpp
More file actions
102 lines (96 loc) · 2.97 KB
/
Copy pathKHttpContext.cpp
File metadata and controls
102 lines (96 loc) · 2.97 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
#include "KHttpContext.h"
#include "webserver/tcp/KSelectedBuffer.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 + 1, 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;
}
// 解析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) {
const char *lineBegin = buf->peek();
const char *lineEnd = crlf;
std::string lineStorage;
if (!buf->isSpanContiguous(crlf)) {
lineStorage = buf->readableStringUntil(crlf);
lineBegin = lineStorage.data();
lineEnd = lineBegin + lineStorage.size();
}
ok = processRequestLine(lineBegin, lineEnd);
if (ok) {
request_.setReceiveTime(receiveTime);
buf->retrieveLineAndCRLF(crlf);
state_ = kExpectHeaders;
} else {
hasMore = false;
}
} else {
hasMore = false;
}
}
// 请求头解析
else if (state_ == kExpectHeaders) {
const char *crlf = buf->findCRLF();
if (crlf) {
const char *lineBegin = buf->peek();
const char *lineEnd = crlf;
std::string lineStorage;
if (!buf->isSpanContiguous(crlf)) {
lineStorage = buf->readableStringUntil(crlf);
lineBegin = lineStorage.data();
lineEnd = lineBegin + lineStorage.size();
}
const char *colon = std::find(lineBegin, lineEnd, ':');
if (colon != lineEnd) {
request_.addHeader(lineBegin, colon, lineEnd);
} else {
// 空行,头部解析完毕
state_ = kGotAll;
hasMore = false;
}
buf->retrieveLineAndCRLF(crlf);
} else {
hasMore = false;
}
} else if (state_ == kExpectBody) {
// 可以用于提取报文的主体部分
}
}
return ok;
}