-
Notifications
You must be signed in to change notification settings - Fork 12
/
http_codec.hpp
328 lines (285 loc) · 9.2 KB
/
http_codec.hpp
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
#pragma once
#include "bytes_buffer.hpp"
#include "string_map.hpp"
#include "enum_magic.hpp"
struct http11_header_parser {
bytes_buffer m_header; // "GET / HTTP/1.1\nHost: 142857.red\r\nAccept:
// */*\r\nConnection: close"
std::string m_headline; // "GET / HTTP/1.1"
string_map m_header_keys; // {"Host": "142857.red", "Accept": "*/*",
// "Connection: close"}
std::string m_body; // 不小心超量读取的正文(如果有的话)
bool m_header_finished = false;
void reset_state() {
m_header.clear();
m_headline.clear();
m_header_keys.clear();
m_body.clear();
m_header_finished = 0;
}
[[nodiscard]] bool header_finished() {
return m_header_finished; // 如果正文都结束了,就不再需要更多数据
}
void _extract_headers() {
std::string_view header = m_header;
size_t pos = header.find("\r\n", 0, 2);
m_headline = std::string(header.substr(0, pos));
while (pos != std::string::npos) {
// 跳过 "\r\n"
pos += 2;
// 从当前位置开始找,先找到下一行位置(可能为 npos)
size_t next_pos = header.find("\r\n", pos, 2);
size_t line_len = std::string::npos;
if (next_pos != std::string::npos) {
// 如果下一行还不是结束,那么 line_len
// 设为本行开始到下一行之间的距离
line_len = next_pos - pos;
}
// 就能切下本行
std::string_view line = header.substr(pos, line_len);
size_t colon = line.find(": ", 0, 2);
if (colon != std::string::npos) {
// 每一行都是 "键: 值"
std::string key = std::string(line.substr(0, colon));
std::string_view value = line.substr(colon + 2);
// 键统一转成小写,实现大小写不敏感的判断
for (char &c: key) {
if ('A' <= c && c <= 'Z') {
c += 'a' - 'A';
}
}
// 古代 C++ 过时的写法:m_header_keys[key] = value;
// 现代 C++17 的高效写法:
m_header_keys.insert_or_assign(std::move(key), value);
}
pos = next_pos;
}
}
void push_chunk(bytes_const_view chunk) {
assert(!m_header_finished);
size_t old_size = m_header.size();
m_header.append(chunk);
std::string_view header = m_header;
// 如果还在解析头部的话,尝试判断头部是否结束
if (old_size < 4) {
old_size = 4;
}
old_size -= 4;
size_t header_len = header.find("\r\n\r\n", old_size, 4);
if (header_len != std::string::npos) {
// 头部已经结束
m_header_finished = true;
// 把不小心多读取的正文留下
m_body = header.substr(header_len + 4);
m_header.resize(header_len);
// 开始分析头部,尝试提取 Content-length 字段
_extract_headers();
}
}
std::string &headline() {
return m_headline;
}
string_map &headers() {
return m_header_keys;
}
bytes_buffer &headers_raw() {
return m_header;
}
std::string &extra_body() {
return m_body;
}
};
template <class HeaderParser = http11_header_parser>
struct _http_base_parser {
HeaderParser m_header_parser;
size_t m_content_length = 0;
size_t body_accumulated_size = 0;
bool m_body_finished = false;
void reset_state() {
m_header_parser.reset_state();
m_content_length = 0;
body_accumulated_size = 0;
m_body_finished = false;
}
[[nodiscard]] bool header_finished() {
return m_header_parser.header_finished();
}
[[nodiscard]] bool request_finished() {
return m_body_finished;
}
std::string &headers_raw() {
return m_header_parser.headers_raw();
}
std::string &headline() {
return m_header_parser.headline();
}
string_map &headers() {
return m_header_parser.headers();
}
std::string _headline_first() {
// "GET / HTTP/1.1" request
// "HTTP/1.1 200 OK" response
auto &line = headline();
size_t space = line.find(' ');
if (space == std::string::npos) {
return "";
}
return line.substr(0, space);
}
std::string _headline_second() {
// "GET / HTTP/1.1"
auto &line = headline();
size_t space1 = line.find(' ');
if (space1 == std::string::npos) {
return "";
}
size_t space2 = line.find(' ', space1 + 1);
if (space2 == std::string::npos) {
return "";
}
return line.substr(space1 + 1, space2 - (space1 + 1));
}
std::string _headline_third() {
// "GET / HTTP/1.1"
auto &line = headline();
size_t space1 = line.find(' ');
if (space1 == std::string::npos) {
return "";
}
size_t space2 = line.find(' ', space1 + 1);
if (space2 == std::string::npos) {
return "";
}
return line.substr(space2);
}
std::string &body() {
return m_header_parser.extra_body();
}
size_t _extract_content_length() {
auto &headers = m_header_parser.headers();
auto it = headers.find("content-length");
if (it == headers.end()) {
return 0;
}
try {
return std::stoi(it->second);
} catch (std::logic_error const &) {
return 0;
}
}
void push_chunk(bytes_const_view chunk) {
assert(!m_body_finished);
if (!m_header_parser.header_finished()) {
m_header_parser.push_chunk(chunk);
if (m_header_parser.header_finished()) {
body_accumulated_size = body().size();
m_content_length = _extract_content_length();
if (body_accumulated_size >= m_content_length) {
m_body_finished = true;
}
}
} else {
body().append(chunk);
body_accumulated_size += chunk.size();
if (body_accumulated_size >= m_content_length) {
m_body_finished = true;
}
}
}
std::string read_some_body() {
return std::move(body());
}
};
enum class http_method {
UNKNOWN = -1,
GET,
POST,
PUT,
DELETE,
HEAD,
OPTIONS,
PATCH,
TRACE,
CONNECT,
};
template <class HeaderParser = http11_header_parser>
struct http_request_parser : _http_base_parser<HeaderParser> {
http_method method() {
return parse_enum<http_method>(this->_headline_first());
}
std::string url() {
return this->_headline_second();
}
};
template <class HeaderParser = http11_header_parser>
struct http_response_parser : _http_base_parser<HeaderParser> {
int status() {
auto s = this->_headline_second();
try {
return std::stoi(s);
} catch (std::logic_error const &) {
return -1;
}
}
};
struct http11_header_writer {
bytes_buffer m_buffer;
void reset_state() {
m_buffer.clear();
}
bytes_buffer &buffer() {
return m_buffer;
}
void begin_header(std::string_view first, std::string_view second,
std::string_view third) {
m_buffer.append(first);
m_buffer.append_literial(" ");
m_buffer.append(second);
m_buffer.append_literial(" ");
m_buffer.append(third);
}
void write_header(std::string_view key, std::string_view value) {
m_buffer.append_literial("\r\n");
m_buffer.append(key);
m_buffer.append_literial(": ");
m_buffer.append(value);
}
void end_header() {
m_buffer.append_literial("\r\n\r\n");
}
};
template <class HeaderWriter = http11_header_writer>
struct _http_base_writer {
HeaderWriter m_header_writer;
void _begin_header(std::string_view first, std::string_view second,
std::string_view third) {
m_header_writer.begin_header(first, second, third);
}
void reset_state() {
m_header_writer.reset_state();
}
bytes_buffer &buffer() {
return m_header_writer.buffer();
}
void write_header(std::string_view key, std::string_view value) {
m_header_writer.write_header(key, value);
}
void end_header() {
m_header_writer.end_header();
}
void write_body(std::string_view body) {
m_header_writer.buffer().append(body);
}
};
template <class HeaderWriter = http11_header_writer>
struct http_request_writer : _http_base_writer<HeaderWriter> {
void begin_header(std::string_view method, std::string_view url) {
this->_begin_header(method, url, "HTTP/1.1");
}
};
template <class HeaderWriter = http11_header_writer>
struct http_response_writer : _http_base_writer<HeaderWriter> {
void begin_header(int status) {
this->_begin_header("HTTP/1.1", std::to_string(status), "OK");
}
};