forked from boostorg/beast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
376 lines (358 loc) · 11 KB
/
Copy pathparser.cpp
File metadata and controls
376 lines (358 loc) · 11 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
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
//
// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/boostorg/beast
//
// Test that header file is self-contained.
#include <boost/beast/http/parser.hpp>
#include "test_parser.hpp"
#include <boost/beast/_experimental/unit_test/suite.hpp>
#include <boost/beast/core/buffer_traits.hpp>
#include <boost/beast/core/buffers_suffix.hpp>
#include <boost/beast/core/flat_buffer.hpp>
#include <boost/beast/core/multi_buffer.hpp>
#include <boost/beast/core/ostream.hpp>
#include <boost/beast/http/read.hpp>
#include <boost/beast/http/string_body.hpp>
#include <boost/system/system_error.hpp>
#include <algorithm>
namespace boost {
namespace beast {
namespace http {
class parser_test
: public beast::unit_test::suite
{
public:
template<bool isRequest>
using parser_type =
parser<isRequest, string_body>;
static
net::const_buffer
buf(string_view s)
{
return {s.data(), s.size()};
}
template<class ConstBufferSequence,
bool isRequest>
static
void
put(ConstBufferSequence const& buffers,
basic_parser<isRequest>& p,
error_code& ec)
{
buffers_suffix<ConstBufferSequence> cb{buffers};
for(;;)
{
auto const used = p.put(cb, ec);
cb.consume(used);
if(ec)
return;
if(p.need_eof() &&
buffer_bytes(cb) == 0)
{
p.put_eof(ec);
if(ec)
return;
}
if(p.is_done())
break;
}
}
template<bool isRequest, class F>
void
doMatrix(string_view s0, F const& f)
{
// parse a single buffer
{
auto s = s0;
error_code ec;
parser_type<isRequest> p;
put(net::buffer(s.data(), s.size()), p, ec);
if(! BEAST_EXPECTS(! ec, ec.message()))
return;
f(p);
}
// parse two buffers
for(auto n = s0.size() - 1; n >= 1; --n)
{
auto s = s0;
error_code ec;
parser_type<isRequest> p;
p.eager(true);
auto used =
p.put(net::buffer(s.data(), n), ec);
s.remove_prefix(used);
if(ec == error::need_more)
ec = {};
if(! BEAST_EXPECTS(! ec, ec.message()))
continue;
BEAST_EXPECT(! p.is_done());
used = p.put(
net::buffer(s.data(), s.size()), ec);
s.remove_prefix(used);
if(! BEAST_EXPECTS(! ec, ec.message()))
continue;
BEAST_EXPECT(s.empty());
if(p.need_eof())
{
p.put_eof(ec);
if(! BEAST_EXPECTS(! ec, ec.message()))
continue;
}
if(BEAST_EXPECT(p.is_done()))
f(p);
}
}
void
testParse()
{
doMatrix<false>(
"HTTP/1.0 200 OK\r\n"
"Server: test\r\n"
"\r\n"
"Hello, world!",
[&](parser_type<false> const& p)
{
auto const& m = p.get();
BEAST_EXPECT(! p.chunked());
BEAST_EXPECT(p.need_eof());
BEAST_EXPECT(p.content_length() == boost::none);
BEAST_EXPECT(m.version() == 10);
BEAST_EXPECT(m.result() == status::ok);
BEAST_EXPECT(m.reason() == "OK");
BEAST_EXPECT(m["Server"] == "test");
BEAST_EXPECT(m.body() == "Hello, world!");
}
);
doMatrix<false>(
"HTTP/1.1 200 OK\r\n"
"Server: test\r\n"
"Expect: Expires, MD5-Fingerprint\r\n"
"Transfer-Encoding: chunked\r\n"
"\r\n"
"5\r\n"
"*****\r\n"
"2;a;b=1;c=\"2\"\r\n"
"--\r\n"
"0;d;e=3;f=\"4\"\r\n"
"Expires: never\r\n"
"MD5-Fingerprint: -\r\n"
"\r\n",
[&](parser_type<false> const& p)
{
auto const& m = p.get();
BEAST_EXPECT(! p.need_eof());
BEAST_EXPECT(p.chunked());
BEAST_EXPECT(p.content_length() == boost::none);
BEAST_EXPECT(m.version() == 11);
BEAST_EXPECT(m.result() == status::ok);
BEAST_EXPECT(m.reason() == "OK");
BEAST_EXPECT(m["Server"] == "test");
BEAST_EXPECT(m["Transfer-Encoding"] == "chunked");
BEAST_EXPECT(m["Expires"] == "never");
BEAST_EXPECT(m["MD5-Fingerprint"] == "-");
BEAST_EXPECT(m.body() == "*****--");
}
);
doMatrix<false>(
"HTTP/1.0 200 OK\r\n"
"Server: test\r\n"
"Content-Length: 5\r\n"
"\r\n"
"*****",
[&](parser_type<false> const& p)
{
auto const& m = p.get();
BEAST_EXPECT(m.body() == "*****");
}
);
doMatrix<true>(
"GET / HTTP/1.1\r\n"
"User-Agent: test\r\n"
"\r\n",
[&](parser_type<true> const& p)
{
auto const& m = p.get();
BEAST_EXPECT(m.method() == verb::get);
BEAST_EXPECT(m.target() == "/");
BEAST_EXPECT(m.version() == 11);
BEAST_EXPECT(! p.need_eof());
BEAST_EXPECT(! p.chunked());
BEAST_EXPECT(p.content_length() == boost::none);
}
);
doMatrix<true>(
"GET / HTTP/1.1\r\n"
"User-Agent: test\r\n"
"X: \t x \t \r\n"
"\r\n",
[&](parser_type<true> const& p)
{
auto const& m = p.get();
BEAST_EXPECT(m["X"] == "x");
}
);
// test eager(true)
{
error_code ec;
parser_type<true> p;
p.eager(true);
p.put(buf(
"GET / HTTP/1.1\r\n"
"User-Agent: test\r\n"
"Content-Length: 1\r\n"
"\r\n"
"*")
, ec);
auto const& m = p.get();
BEAST_EXPECT(! ec);
BEAST_EXPECT(p.is_done());
BEAST_EXPECT(p.is_header_done());
BEAST_EXPECT(! p.need_eof());
BEAST_EXPECT(m.method() == verb::get);
BEAST_EXPECT(m.target() == "/");
BEAST_EXPECT(m.version() == 11);
BEAST_EXPECT(m["User-Agent"] == "test");
BEAST_EXPECT(m.body() == "*");
}
{
// test partial parsing of final chunk
// parse through the chunk body
error_code ec;
flat_buffer b;
parser_type<true> p;
p.eager(true);
ostream(b) <<
"PUT / HTTP/1.1\r\n"
"Transfer-Encoding: chunked\r\n"
"\r\n"
"1\r\n"
"*";
auto used = p.put(b.data(), ec);
b.consume(used);
BEAST_EXPECT(! ec);
BEAST_EXPECT(! p.is_done());
BEAST_EXPECT(p.get().body() == "*");
ostream(b) <<
"\r\n"
"0;d;e=3;f=\"4\"\r\n"
"Expires: never\r\n"
"MD5-Fingerprint: -\r\n";
// incomplete parse, missing the final crlf
used = p.put(b.data(), ec);
b.consume(used);
BEAST_EXPECT(ec == error::need_more);
ec = {};
BEAST_EXPECT(! p.is_done());
ostream(b) <<
"\r\n"; // final crlf to end message
used = p.put(b.data(), ec);
b.consume(used);
BEAST_EXPECTS(! ec, ec.message());
BEAST_EXPECT(p.is_done());
}
// skip body
{
error_code ec;
response_parser<string_body> p;
p.skip(true);
p.put(buf(
"HTTP/1.1 200 OK\r\n"
"Content-Length: 5\r\n"
"\r\n"
"*****")
, ec);
BEAST_EXPECTS(! ec, ec.message());
BEAST_EXPECT(p.is_done());
BEAST_EXPECT(p.is_header_done());
BEAST_EXPECT(p.content_length() &&
*p.content_length() == 5);
}
}
//--------------------------------------------------------------------------
template<class DynamicBuffer>
void
testNeedMore()
{
error_code ec;
std::size_t used;
{
DynamicBuffer b;
parser_type<true> p;
ostream(b) <<
"GET / HTTP/1.1\r\n";
used = p.put(b.data(), ec);
BEAST_EXPECTS(ec == error::need_more, ec.message());
b.consume(used);
ec = {};
ostream(b) <<
"User-Agent: test\r\n"
"\r\n";
used = p.put(b.data(), ec);
BEAST_EXPECTS(! ec, ec.message());
b.consume(used);
BEAST_EXPECT(p.is_done());
BEAST_EXPECT(p.is_header_done());
}
}
void
testGotSome()
{
error_code ec;
parser_type<true> p;
auto used = p.put(buf(""), ec);
BEAST_EXPECT(ec == error::need_more);
BEAST_EXPECT(! p.got_some());
BEAST_EXPECT(used == 0);
ec = {};
used = p.put(buf("G"), ec);
BEAST_EXPECT(ec == error::need_more);
BEAST_EXPECT(p.got_some());
BEAST_EXPECT(used == 0);
}
void
testIssue818()
{
// Make sure that the parser clears pre-existing fields
request<string_body> m;
m.set(field::accept, "html/text");
BEAST_EXPECT(std::distance(m.begin(), m.end()) == 1);
request_parser<string_body> p{std::move(m)};
BEAST_EXPECT(std::distance(m.begin(), m.end()) == 0);
auto& m1 = p.get();
BEAST_EXPECT(std::distance(m1.begin(), m1.end()) == 0);
}
void
testIssue1187()
{
// make sure parser finishes on redirect
error_code ec;
parser_type<false> p;
p.eager(true);
p.put(buf(
"HTTP/1.1 301 Moved Permanently\r\n"
"Location: https://www.ebay.com\r\n"
"\r\n\r\n"), ec);
BEAST_EXPECTS(! ec, ec.message());
BEAST_EXPECT(p.is_header_done());
BEAST_EXPECT(! p.is_done());
BEAST_EXPECT(p.need_eof());
}
void
run() override
{
testParse();
testNeedMore<flat_buffer>();
testNeedMore<multi_buffer>();
testGotSome();
testIssue818();
testIssue1187();
}
};
BEAST_DEFINE_TESTSUITE(beast,http,parser);
} // http
} // beast
} // boost