src/parser.cpp

77.2% Lines (533 / 690) 84.1% Functions (53 / 63)
parser.cpp
f(x) Functions (63)
Function Calls Lines Blocks
boost::http::(anonymous namespace)::prefix_pair(std::array<boost::capy::const_buffer, 2ul> const&, unsigned long) :131 41598x 90.0% 92.0% boost::http::(anonymous namespace)::chained_sequence::chained_sequence(std::array<boost::capy::const_buffer, 2ul> const&) :159 71617x 100.0% 100.0% boost::http::(anonymous namespace)::chained_sequence::next() :168 319930x 63.6% 83.0% boost::http::(anonymous namespace)::chained_sequence::is_empty() const :190 212674x 100.0% 100.0% boost::http::(anonymous namespace)::chained_sequence::value() const :196 305475x 100.0% 100.0% boost::http::(anonymous namespace)::chained_sequence::size() const :202 226936x 100.0% 100.0% boost::http::(anonymous namespace)::parse_hex(boost::http::(anonymous namespace)::chained_sequence&, std::error_code&) :209 66939x 100.0% 100.0% boost::http::(anonymous namespace)::find_eol(boost::http::(anonymous namespace)::chained_sequence&, std::error_code&) :243 47341x 100.0% 100.0% boost::http::(anonymous namespace)::parse_eol(boost::http::(anonymous namespace)::chained_sequence&, std::error_code&) :267 62239x 100.0% 100.0% boost::http::(anonymous namespace)::skip_trailer_headers(boost::http::(anonymous namespace)::chained_sequence&, std::error_code&) :286 4243x 100.0% 100.0% unsigned long boost::http::(anonymous namespace)::clamp<unsigned long>(unsigned long, unsigned long) :314 193635x 100.0% 100.0% boost::http::(anonymous namespace)::zlib_filter::zlib_filter(boost::http::zlib::inflate_service&, int) :330 0 0.0% 0.0% boost::http::(anonymous namespace)::zlib_filter::~zlib_filter() :341 0 0.0% 0.0% boost::http::(anonymous namespace)::zlib_filter::do_process(boost::capy::mutable_buffer, boost::capy::const_buffer, bool) :349 0 0.0% 0.0% boost::http::(anonymous namespace)::brotli_filter::brotli_filter(boost::http::brotli::decode_service&) :383 0 0.0% 0.0% boost::http::(anonymous namespace)::brotli_filter::~brotli_filter() :391 0 0.0% 0.0% boost::http::(anonymous namespace)::brotli_filter::do_process(boost::capy::mutable_buffer, boost::capy::const_buffer, bool) :399 0 0.0% 0.0% boost::http::parser::impl::impl(std::shared_ptr<boost::http::parser_config_impl const>, boost::http::detail::kind) :477 2181x 100.0% 94.0% boost::http::parser::impl::got_header() const :488 36139x 100.0% 100.0% boost::http::parser::impl::is_complete() const :494 59150x 100.0% 100.0% boost::http::parser::impl::safe_get_request() const :500 316x 75.0% 67.0% boost::http::parser::impl::safe_get_response() const :510 3x 75.0% 67.0% boost::http::parser::impl::reset() :521 2728x 100.0% 100.0% boost::http::parser::impl::start(bool) :530 10657x 98.3% 88.0% boost::http::parser::impl::prepare() :649 81921x 93.3% 85.0% boost::http::parser::impl::commit(unsigned long) :742 80860x 81.5% 76.0% boost::http::parser::impl::commit_eof() :815 134x 88.9% 81.0% boost::http::parser::impl::parse(std::error_code&) :849 98779x 76.9% 71.0% boost::http::parser::impl::parse(std::error_code&)::{lambda()#1}::operator()() const :1032 8366x 100.0% 100.0% boost::http::parser::impl::parse(std::error_code&)::{lambda()#2}::operator()() const :1045 20849x 83.3% 70.0% boost::http::parser::impl::parse(std::error_code&)::{lambda()#3}::operator()() const :1167 24654x 100.0% 100.0% boost::http::parser::impl::parse(std::error_code&)::{lambda()#4}::operator()() const :1178 24654x 87.5% 100.0% boost::http::parser::impl::pull_body() :1244 41630x 91.7% 89.0% boost::http::parser::impl::consume_body(unsigned long) :1267 39610x 53.8% 67.0% boost::http::parser::impl::body() const :1289 712x 80.0% 70.0% boost::http::parser::impl::has_buffered_data() const :1307 9x 100.0% 100.0% boost::http::parser::impl::set_body_limit(unsigned long) :1318 5x 100.0% 100.0% boost::http::parser::impl::is_plain() const :1339 140727x 100.0% 100.0% boost::http::parser::impl::body_limit_remain() const :1346 47967x 100.0% 100.0% boost::http::parser::impl::apply_filter(std::error_code&, unsigned long, bool) :1352 0 0.0% 0.0% boost::http::parser::impl::apply_filter(std::error_code&, unsigned long, bool)::{lambda()#1}::operator()() const :1363 0 0.0% 0.0% boost::http::parser::~parser() :1422 2196x 100.0% 100.0% boost::http::parser::parser() :1428 12x 100.0% 100.0% boost::http::parser::parser(boost::http::parser&&) :1434 3x 100.0% 100.0% boost::http::parser::parser(std::shared_ptr<boost::http::parser_config_impl const>, boost::http::detail::kind) :1441 2181x 100.0% 58.0% boost::http::parser::assign(boost::http::parser&&) :1452 4x 83.3% 57.0% boost::http::parser::got_header() const :1469 36139x 100.0% 75.0% boost::http::parser::is_complete() const :1476 59150x 100.0% 75.0% boost::http::parser::reset() :1489 2728x 100.0% 75.0% boost::http::parser::start() :1497 10657x 100.0% 75.0% boost::http::parser::prepare() :1504 81921x 100.0% 75.0% boost::http::parser::commit(unsigned long) :1513 80860x 100.0% 75.0% boost::http::parser::commit_eof() :1522 134x 100.0% 75.0% boost::http::parser::parse(std::error_code&) :1530 98779x 100.0% 75.0% boost::http::parser::pull_body() :1539 41630x 100.0% 75.0% boost::http::parser::consume_body(unsigned long) :1548 39610x 100.0% 75.0% boost::http::parser::body() const :1556 712x 100.0% 75.0% boost::http::parser::release_buffered_data() :1564 0 0.0% 0.0% boost::http::parser::has_buffered_data() const :1572 9x 100.0% 75.0% boost::http::parser::set_body_limit(unsigned long) :1580 5x 100.0% 75.0% boost::http::parser::start_impl(bool) :1594 0 0.0% 0.0% boost::http::parser::safe_get_request() const :1602 316x 100.0% 80.0% boost::http::parser::safe_get_response() const :1610 3x 100.0% 80.0%
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3 // Copyright (c) 2024 Mohammad Nejati
4 //
5 // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 //
8 // Official repository: https://github.com/cppalliance/http
9 //
10
11 #include <boost/http/detail/except.hpp>
12 #include <boost/http/detail/workspace.hpp>
13 #include <boost/http/error.hpp>
14 #include <boost/http/parser.hpp>
15 #include <boost/http/static_request.hpp>
16 #include <boost/http/static_response.hpp>
17
18 #include <boost/http/detail/circular_buffer.hpp>
19 #include <boost/http/detail/flat_buffer.hpp>
20
21 #include <boost/assert.hpp>
22 #include <boost/capy/buffers/buffer_copy.hpp>
23 #include <boost/capy/buffers/buffers_front.hpp>
24 #include <boost/capy/ex/system_context.hpp>
25 #include <boost/http/brotli/decode.hpp>
26 #include <boost/http/zlib/error.hpp>
27 #include <boost/http/zlib/inflate.hpp>
28 #include <boost/url/grammar/ci_string.hpp>
29 #include <boost/url/grammar/error.hpp>
30 #include <boost/url/grammar/hexdig_chars.hpp>
31
32 #include "src/detail/brotli_filter_base.hpp"
33 #include "src/detail/buffer_utils.hpp"
34 #include "src/detail/zlib_filter_base.hpp"
35
36 #include <array>
37 #include <memory>
38
39 namespace boost {
40 namespace http {
41
42 /*
43 Principles for fixed-size buffer design
44
45 axiom 1:
46 To read data you must have a buffer.
47
48 axiom 2:
49 The size of the HTTP header is not
50 known in advance.
51
52 conclusion 3:
53 A single I/O can produce a complete
54 HTTP header and additional payload
55 data.
56
57 conclusion 4:
58 A single I/O can produce multiple
59 complete HTTP headers, complete
60 payloads, and a partial header or
61 payload.
62
63 axiom 5:
64 A process is in one of two states:
65 1. at or below capacity
66 2. above capacity
67
68 axiom 6:
69 A program which can allocate an
70 unbounded number of resources can
71 go above capacity.
72
73 conclusion 7:
74 A program can guarantee never going
75 above capacity if all resources are
76 provisioned at program startup.
77
78 corollary 8:
79 `parser` and `serializer` should each
80 allocate a single buffer of calculated
81 size, and never resize it.
82
83 axiom #:
84 A parser and a serializer are always
85 used in pairs.
86
87 Buffer Usage
88
89 | | begin
90 | H | p | | f | read headers
91 | H | p | | T | f | set T body
92 | H | p | | C | T | f | make codec C
93 | H | p | b | C | T | f | decode p into b
94 | H | p | b | C | T | f | read/parse loop
95 | H | | T | f | destroy codec
96 | H | | T | f | finished
97
98 H headers
99 C codec
100 T body
101 f table
102 p partial payload
103 b body data
104
105 "payload" is the bytes coming in from
106 the stream.
107
108 "body" is the logical body, after transfer
109 encoding is removed. This can be the
110 same as the payload.
111
112 A "plain payload" is when the payload and
113 body are identical (no transfer encodings).
114
115 A "buffered payload" is any payload which is
116 not plain. A second buffer is required
117 for reading.
118
119 "overread" is additional data received past
120 the end of the headers when reading headers,
121 or additional data received past the end of
122 the message payload.
123 */
124
125 namespace {
126
127 // Construct a 2-element const_buffer pair representing the first
128 // `n` bytes of `src`. Replaces the pre-#262 `capy::prefix(src, n)`
129 // idiom which yielded a slice convertible to std::array.
130 inline std::array<capy::const_buffer, 2>
131 41598x prefix_pair(
132 std::array<capy::const_buffer, 2> const& src,
133 std::size_t n) noexcept
134 {
135 41598x std::array<capy::const_buffer, 2> result{};
136 41598x if(n <= src[0].size())
137 {
138 41099x result[0] = capy::const_buffer(src[0].data(), n);
139 }
140 else
141 {
142 499x result[0] = src[0];
143 499x std::size_t remaining = n - src[0].size();
144 499x if(remaining > src[1].size())
145 ✗ remaining = src[1].size();
146 499x result[1] = capy::const_buffer(src[1].data(), remaining);
147 }
148 41598x return result;
149 }
150
151 class chained_sequence
152 {
153 char const* pos_;
154 char const* end_;
155 char const* begin_b_;
156 char const* end_b_;
157
158 public:
159 71617x chained_sequence(std::array<capy::const_buffer, 2> const& cbp)
160 71617x : pos_(static_cast<char const*>(cbp[0].data()))
161 71617x , end_(pos_ + cbp[0].size())
162 71617x , begin_b_(static_cast<char const*>(cbp[1].data()))
163 71617x , end_b_(begin_b_ + cbp[1].size())
164 {
165 71617x }
166
167 char const*
168 319930x next() noexcept
169 {
170 319930x ++pos_;
171 // most frequently taken branch
172 319930x if(pos_ < end_)
173 297556x return pos_;
174
175 // bring the second range
176 22374x if(begin_b_ != end_b_)
177 {
178 ✗ pos_ = begin_b_;
179 ✗ end_ = end_b_;
180 ✗ begin_b_ = end_b_;
181 ✗ return pos_;
182 }
183
184 // undo the increament
185 22374x pos_ = end_;
186 22374x return nullptr;
187 }
188
189 bool
190 212674x is_empty() const noexcept
191 {
192 212674x return pos_ == end_;
193 }
194
195 char
196 305475x value() const noexcept
197 {
198 305475x return *pos_;
199 }
200
201 std::size_t
202 226936x size() const noexcept
203 {
204 226936x return (end_ - pos_) + (end_b_ - begin_b_);
205 }
206 };
207
208 std::uint64_t
209 66939x parse_hex(
210 chained_sequence& cs,
211 std::error_code& ec) noexcept
212 {
213 66939x std::uint64_t v = 0;
214 66939x std::size_t init_size = cs.size();
215 154117x while(!cs.is_empty())
216 {
217 134169x auto n = grammar::hexdig_value(cs.value());
218 134169x if(n < 0)
219 {
220 46990x if(init_size == cs.size())
221 {
222 1x ec = error::bad_payload;
223 1x return 0;
224 }
225 46989x return v;
226 }
227
228 // at least 4 significant bits are free
229 87179x if(v > (std::numeric_limits<std::uint64_t>::max)() >> 4)
230 {
231 1x ec = error::bad_payload;
232 1x return 0;
233 }
234
235 87178x v = (v << 4) | static_cast<std::uint64_t>(n);
236 87178x cs.next();
237 }
238 19948x ec = error::need_data;
239 19948x return 0;
240 }
241
242 void
243 47341x find_eol(
244 chained_sequence& cs,
245 std::error_code& ec) noexcept
246 {
247 54030x while(!cs.is_empty())
248 {
249 53942x if(cs.value() == '\r')
250 {
251 47253x if(!cs.next())
252 330x break;
253 46923x if(cs.value() != '\n')
254 {
255 2x ec = error::bad_payload;
256 2x return;
257 }
258 46921x cs.next();
259 46921x return;
260 }
261 6689x cs.next();
262 }
263 418x ec = error::need_data;
264 }
265
266 void
267 62239x parse_eol(
268 chained_sequence& cs,
269 std::error_code& ec) noexcept
270 {
271 62239x if(cs.size() >= 2)
272 {
273 // we are sure size is at least 2
274 61807x if(cs.value() == '\r' && *cs.next() == '\n')
275 {
276 61804x cs.next();
277 61804x return;
278 }
279 3x ec = error::bad_payload;
280 3x return;
281 }
282 432x ec = error::need_data;
283 }
284
285 void
286 4243x skip_trailer_headers(
287 chained_sequence& cs,
288 std::error_code& ec) noexcept
289 {
290 4527x while(!cs.is_empty())
291 {
292 4501x if(cs.value() == '\r')
293 {
294 4149x if(!cs.next())
295 16x break;
296 4133x if(cs.value() != '\n')
297 {
298 2x ec = error::bad_payload;
299 2x return;
300 }
301 4131x cs.next();
302 4131x return;
303 }
304 // skip to the end of field
305 352x find_eol(cs, ec);
306 352x if(ec)
307 68x return;
308 }
309 42x ec = error::need_data;
310 }
311
312 template<class UInt>
313 std::size_t
314 193635x clamp(
315 UInt x,
316 std::size_t limit = (std::numeric_limits<
317 std::size_t>::max)()) noexcept
318 {
319 193635x if(x >= limit)
320 46531x return limit;
321 147104x return static_cast<std::size_t>(x);
322 }
323
324 class zlib_filter
325 : public detail::zlib_filter_base
326 {
327 http::zlib::inflate_service& svc_;
328
329 public:
330 ✗ zlib_filter(
331 http::zlib::inflate_service& svc,
332 int window_bits)
333 ✗ : svc_(svc)
334 {
335 std::error_code ec = static_cast<http::zlib::error>(
336 ✗ svc_.init2(strm_, window_bits));
337 ✗ if(ec != http::zlib::error::ok)
338 ✗ detail::throw_system_error(ec);
339 ✗ }
340
341 ✗ ~zlib_filter()
342 ✗ {
343 ✗ svc_.inflate_end(strm_);
344 ✗ }
345
346 private:
347 virtual
348 results
349 ✗ do_process(
350 capy::mutable_buffer out,
351 capy::const_buffer in,
352 bool more) noexcept override
353 {
354 ✗ strm_.next_out = static_cast<unsigned char*>(out.data());
355 ✗ strm_.avail_out = saturate_cast(out.size());
356 ✗ strm_.next_in = static_cast<unsigned char*>(const_cast<void *>(in.data()));
357 ✗ strm_.avail_in = saturate_cast(in.size());
358
359 auto rs = static_cast<http::zlib::error>(
360 ✗ svc_.inflate(
361 ✗ strm_,
362 more ? http::zlib::no_flush : http::zlib::finish));
363
364 ✗ results rv;
365 ✗ rv.out_bytes = saturate_cast(out.size()) - strm_.avail_out;
366 ✗ rv.in_bytes = saturate_cast(in.size()) - strm_.avail_in;
367 ✗ rv.finished = (rs == http::zlib::error::stream_end);
368
369 ✗ if(rs < http::zlib::error::ok && rs != http::zlib::error::buf_err)
370 ✗ rv.ec = rs;
371
372 ✗ return rv;
373 }
374 };
375
376 class brotli_filter
377 : public detail::brotli_filter_base
378 {
379 http::brotli::decode_service& svc_;
380 http::brotli::decoder_state* state_;
381
382 public:
383 ✗ brotli_filter(http::brotli::decode_service& svc)
384 ✗ : svc_(svc)
385 {
386 ✗ state_ = svc_.create_instance(nullptr, nullptr, nullptr);
387 ✗ if(!state_)
388 ✗ detail::throw_bad_alloc();
389 ✗ }
390
391 ✗ ~brotli_filter()
392 ✗ {
393 ✗ svc_.destroy_instance(state_);
394 ✗ }
395
396 private:
397 virtual
398 results
399 ✗ do_process(
400 capy::mutable_buffer out,
401 capy::const_buffer in,
402 bool more) noexcept override
403 {
404 ✗ auto* next_in = reinterpret_cast<const std::uint8_t*>(in.data());
405 ✗ auto available_in = in.size();
406 ✗ auto* next_out = reinterpret_cast<std::uint8_t*>(out.data());
407 ✗ auto available_out = out.size();
408
409 ✗ auto rs = svc_.decompress_stream(
410 state_,
411 &available_in,
412 &next_in,
413 &available_out,
414 &next_out,
415 nullptr);
416
417 ✗ results rv;
418 ✗ rv.in_bytes = in.size() - available_in;
419 ✗ rv.out_bytes = out.size() - available_out;
420 ✗ rv.finished = svc_.is_finished(state_);
421
422 ✗ if(!more && rs == http::brotli::decoder_result::needs_more_input)
423 ✗ rv.ec = error::bad_payload;
424
425 ✗ if(rs == http::brotli::decoder_result::error)
426 ✗ rv.ec = svc_.get_error_code(state_);
427
428 ✗ return rv;
429 }
430 };
431
432 } // namespace
433
434 //------------------------------------------------
435
436 class parser::impl
437 {
438 enum class state
439 {
440 reset,
441 start,
442 header,
443 header_done,
444 body,
445 complete,
446 };
447
448 std::shared_ptr<parser_config_impl const> cfg_;
449
450 detail::workspace ws_;
451 static_request m_;
452 std::uint64_t body_limit_;
453 std::uint64_t body_total_;
454 std::uint64_t payload_remain_;
455 std::uint64_t chunk_remain_;
456 std::size_t body_avail_;
457 std::size_t nprepare_;
458
459 detail::flat_buffer fb_;
460 detail::circular_buffer cb0_;
461 detail::circular_buffer cb1_;
462
463 std::array<capy::mutable_buffer, 2> mbp_;
464 std::array<capy::const_buffer, 2> cbp_;
465
466 std::unique_ptr<detail::filter> filter_;
467
468 state state_;
469 bool got_header_;
470 bool got_eof_;
471 bool head_response_;
472 bool needs_chunk_close_;
473 bool trailer_headers_;
474 bool chunked_body_ended;
475
476 public:
477 2181x impl(std::shared_ptr<parser_config_impl const> cfg, detail::kind k)
478 2181x : cfg_(std::move(cfg))
479 2181x , ws_(cfg_->space_needed)
480 2181x , m_(ws_.data(), ws_.size())
481 2181x , state_(state::reset)
482 2181x , got_header_(false)
483 {
484 2181x m_.h_ = detail::header(detail::empty{ k });
485 2181x }
486
487 bool
488 36139x got_header() const noexcept
489 {
490 36139x return got_header_;
491 }
492
493 bool
494 59150x is_complete() const noexcept
495 {
496 59150x return state_ == state::complete;
497 }
498
499 static_request const&
500 316x safe_get_request() const
501 {
502 // headers must be received
503 316x if(! got_header_)
504 ✗ detail::throw_logic_error();
505
506 316x return m_;
507 }
508
509 static_response const&
510 3x safe_get_response() const
511 {
512 // headers must be received
513 3x if(! got_header_)
514 ✗ detail::throw_logic_error();
515
516 // TODO: use a union
517 3x return reinterpret_cast<static_response const&>(m_);
518 }
519
520 void
521 2728x reset() noexcept
522 {
523 2728x ws_.clear();
524 2728x state_ = state::start;
525 2728x got_header_ = false;
526 2728x got_eof_ = false;
527 2728x }
528
529 void
530 10657x start(
531 bool head_response)
532 {
533 10657x std::size_t leftover = 0;
534 10657x switch(state_)
535 {
536 1x default:
537 case state::reset:
538 // reset must be called first
539 1x detail::throw_logic_error();
540
541 2653x case state::start:
542 // reset required on eof
543 2653x if(got_eof_)
544 ✗ detail::throw_logic_error();
545 2653x break;
546
547 3x case state::header:
548 3x if(fb_.size() == 0)
549 {
550 // start() called twice
551 2x detail::throw_logic_error();
552 }
553 BOOST_FALLTHROUGH;
554
555 case state::header_done:
556 case state::body:
557 // current message is incomplete
558 2x detail::throw_logic_error();
559
560 7999x case state::complete:
561 {
562 // remove available body.
563 7999x if(is_plain())
564 4000x cb0_.consume(body_avail_);
565 // move leftovers to front
566
567 7999x ws_.clear();
568 7999x leftover = cb0_.size();
569
570 7999x auto* dest = reinterpret_cast<char*>(ws_.data());
571 7999x auto cbp = cb0_.data();
572 7999x auto* a = static_cast<char const*>(cbp[0].data());
573 7999x auto* b = static_cast<char const*>(cbp[1].data());
574 7999x auto an = cbp[0].size();
575 7999x auto bn = cbp[1].size();
576
577 7999x if(bn == 0)
578 {
579 7561x std::memmove(dest, a, an);
580 }
581 else
582 {
583 // if `a` can fit between `dest` and `b`, shift `b` to the left
584 // and copy `a` to its position. if `a` fits perfectly, the
585 // shift will be of size 0.
586 // if `a` requires more space, shift `b` to the right and
587 // copy `a` to its position. this process may require multiple
588 // iterations and should be done chunk by chunk to prevent `b`
589 // from overlapping with `a`.
590 do
591 {
592 // clamp right shifts to prevent overlap with `a`
593 438x auto* bp = (std::min)(dest + an, const_cast<char*>(a) - bn);
594 438x b = static_cast<char const*>(std::memmove(bp, b, bn));
595
596 // a chunk or all of `a` based on available space
597 438x auto chunk_a = static_cast<std::size_t>(b - dest);
598 438x std::memcpy(dest, a, chunk_a); // never overlap
599 438x an -= chunk_a;
600 438x dest += chunk_a;
601 438x a += chunk_a;
602 438x } while(an);
603 }
604
605 7999x break;
606 }
607 }
608
609 10652x ws_.clear();
610
611 21304x fb_ = {
612 10652x ws_.data(),
613 10652x cfg_->headers.max_size + cfg_->min_buffer,
614 leftover };
615
616 10652x BOOST_ASSERT(
617 fb_.capacity() == cfg_->max_overread() - leftover);
618
619 10652x BOOST_ASSERT(
620 head_response == false ||
621 m_.h_.kind == detail::kind::response);
622
623 10652x m_.h_ = detail::header(detail::empty{m_.h_.kind});
624 10652x m_.h_.buf = reinterpret_cast<char*>(ws_.data());
625 10652x m_.h_.cbuf = m_.h_.buf;
626 10652x m_.h_.cap = ws_.size();
627
628 10652x state_ = state::header;
629
630 // reset to the configured default
631 10652x body_limit_ = cfg_->body_limit;
632
633 10652x body_total_ = 0;
634 10652x payload_remain_ = 0;
635 10652x chunk_remain_ = 0;
636 10652x body_avail_ = 0;
637 10652x nprepare_ = 0;
638
639 10652x filter_.reset();
640
641 10652x got_header_ = false;
642 10652x head_response_ = head_response;
643 10652x needs_chunk_close_ = false;
644 10652x trailer_headers_ = false;
645 10652x chunked_body_ended = false;
646 10652x }
647
648 auto
649 81921x prepare() ->
650 mutable_buffers_type
651 {
652 81921x nprepare_ = 0;
653
654 81921x switch(state_)
655 {
656 1x default:
657 case state::reset:
658 // reset must be called first
659 1x detail::throw_logic_error();
660
661 1x case state::start:
662 // start must be called first
663 1x detail::throw_logic_error();
664
665 39834x case state::header:
666 {
667 39834x BOOST_ASSERT(
668 m_.h_.size < cfg_->headers.max_size);
669 39834x std::size_t n = fb_.capacity();
670 39834x BOOST_ASSERT(n <= cfg_->max_overread());
671 39834x n = clamp(n, cfg_->max_prepare);
672 39834x mbp_[0] = fb_.prepare(n);
673 39834x nprepare_ = n;
674 39834x return mutable_buffers_type(&mbp_[0], 1);
675 }
676
677 ✗ case state::header_done:
678 // forgot to call parse()
679 ✗ detail::throw_logic_error();
680
681 42084x case state::body:
682 {
683 42084x if(got_eof_)
684 {
685 // forgot to call parse()
686 ✗ detail::throw_logic_error();
687 }
688
689 42084x if(! is_plain())
690 {
691 // buffered payload
692 22017x std::size_t n = cb0_.capacity();
693 22017x n = clamp(n, cfg_->max_prepare);
694 22017x nprepare_ = n;
695 22017x mbp_ = cb0_.prepare(n);
696 22017x return detail::make_span(mbp_);
697 }
698 else
699 {
700 // plain payload
701 20067x std::size_t n = cb0_.capacity();
702 20067x n = clamp(n, cfg_->max_prepare);
703
704 20067x if(m_.payload() == payload::size)
705 {
706 20053x if(n > payload_remain_)
707 {
708 18836x std::size_t overread =
709 18836x n - static_cast<std::size_t>(payload_remain_);
710 18836x if(overread > cfg_->max_overread())
711 8920x n = static_cast<std::size_t>(payload_remain_) +
712 8920x cfg_->max_overread();
713 }
714 }
715 else
716 {
717 14x BOOST_ASSERT(
718 m_.payload() == payload::to_eof);
719 // No more messages can be pipelined, so
720 // limit the output buffer to the remaining
721 // body limit plus one byte to detect
722 // exhaustion.
723 14x std::uint64_t r = body_limit_remain();
724 14x if(r != std::uint64_t(-1))
725 14x r += 1;
726 14x n = clamp(r, n);
727 }
728
729 20067x nprepare_ = n;
730 20067x mbp_ = cb0_.prepare(n);
731 20067x return detail::make_span(mbp_);
732 }
733 }
734
735 1x case state::complete:
736 // already complete
737 1x detail::throw_logic_error();
738 }
739 }
740
741 void
742 80860x commit(
743 std::size_t n)
744 {
745 80860x switch(state_)
746 {
747 1x default:
748 case state::reset:
749 {
750 // reset must be called first
751 1x detail::throw_logic_error();
752 }
753
754 1x case state::start:
755 {
756 // forgot to call start()
757 1x detail::throw_logic_error();
758 }
759
760 39048x case state::header:
761 {
762 39048x if(n > nprepare_)
763 {
764 // n can't be greater than size of
765 // the buffers returned by prepare()
766 1x detail::throw_invalid_argument();
767 }
768
769 39047x if(got_eof_)
770 {
771 // can't commit after EOF
772 1x detail::throw_logic_error();
773 }
774
775 39046x nprepare_ = 0; // invalidate
776 39046x fb_.commit(n);
777 39046x break;
778 }
779
780 ✗ case state::header_done:
781 {
782 // forgot to call parse()
783 ✗ detail::throw_logic_error();
784 }
785
786 41810x case state::body:
787 {
788 41810x if(n > nprepare_)
789 {
790 // n can't be greater than size of
791 // the buffers returned by prepare()
792 2x detail::throw_invalid_argument();
793 }
794
795 41808x if(got_eof_)
796 {
797 // can't commit after EOF
798 ✗ detail::throw_logic_error();
799 }
800
801 41808x nprepare_ = 0; // invalidate
802 41808x cb0_.commit(n);
803 41808x break;
804 }
805
806 ✗ case state::complete:
807 {
808 // already complete
809 ✗ detail::throw_logic_error();
810 }
811 }
812 80854x }
813
814 void
815 134x commit_eof()
816 {
817 134x nprepare_ = 0; // invalidate
818
819 134x switch(state_)
820 {
821 1x default:
822 case state::reset:
823 // reset must be called first
824 1x detail::throw_logic_error();
825
826 1x case state::start:
827 // forgot to call start()
828 1x detail::throw_logic_error();
829
830 14x case state::header:
831 14x got_eof_ = true;
832 14x break;
833
834 ✗ case state::header_done:
835 // forgot to call parse()
836 ✗ detail::throw_logic_error();
837
838 117x case state::body:
839 117x got_eof_ = true;
840 117x break;
841
842 1x case state::complete:
843 // can't commit eof when complete
844 1x detail::throw_logic_error();
845 }
846 131x }
847
848 void
849 98779x parse(
850 std::error_code& ec)
851 {
852 98779x ec = {};
853 98779x switch(state_)
854 {
855 1x default:
856 case state::reset:
857 // reset must be called first
858 1x detail::throw_logic_error();
859
860 1x case state::start:
861 // start must be called first
862 1x detail::throw_logic_error();
863
864 45037x case state::header:
865 {
866 45037x BOOST_ASSERT(m_.h_.buf == static_cast<
867 void const*>(ws_.data()));
868 45037x BOOST_ASSERT(m_.h_.cbuf == static_cast<
869 void const*>(ws_.data()));
870
871 45037x m_.h_.parse(fb_.size(), cfg_->headers, ec);
872
873 45037x if(ec == condition::need_more_input)
874 {
875 35191x if(! got_eof_)
876 {
877 // headers incomplete
878 35180x return;
879 }
880
881 11x if(fb_.size() == 0)
882 {
883 // stream closed cleanly
884 6x state_ = state::reset;
885 6x ec = error::end_of_stream;
886 6x return;
887 }
888
889 // stream closed with a
890 // partial message received
891 5x state_ = state::reset;
892 5x ec = error::incomplete;
893 5x return;
894 }
895 9846x else if(ec)
896 {
897 // other error,
898 //
899 // VFALCO map this to a bad
900 // request or bad response error?
901 //
902 259x state_ = state::reset; // unrecoverable
903 259x return;
904 }
905
906 9587x got_header_ = true;
907
908 // reserve headers + table
909 9587x ws_.reserve_front(m_.h_.size);
910 9587x ws_.reserve_back(m_.h_.table_space());
911
912 // no payload
913 18366x if(m_.payload() == payload::none ||
914 8779x head_response_)
915 {
916 // octets of the next message
917 808x auto overread = fb_.size() - m_.h_.size;
918 808x cb0_ = { ws_.data(), overread, overread };
919 808x ws_.reserve_front(overread);
920 808x state_ = state::complete;
921 808x return;
922 }
923
924 8779x state_ = state::header_done;
925 8779x break;
926 }
927
928 8776x case state::header_done:
929 {
930 // metadata error
931 8776x if(m_.payload() == payload::error)
932 {
933 // VFALCO This needs looking at
934 60x ec = error::bad_payload;
935 60x state_ = state::reset; // unrecoverable
936 60x return;
937 }
938
939 // overread currently includes any and all octets that
940 // extend beyond the current end of the header
941 // this can include associated body octets for the
942 // current message or octets of the next message in the
943 // stream, e.g. pipelining is being used
944 8716x auto const overread = fb_.size() - m_.h_.size;
945 8716x BOOST_ASSERT(overread <= cfg_->max_overread());
946
947 8716x auto cap = fb_.capacity() + overread +
948 8716x cfg_->min_buffer;
949
950 // reserve body buffers first, as the decoder
951 // must be installed after them.
952 8716x auto const p = ws_.reserve_front(cap);
953
954 // Content-Encoding
955 8716x switch(m_.metadata().content_encoding.coding)
956 {
957 ✗ case content_coding::deflate:
958 ✗ if(!cfg_->apply_deflate_decoder)
959 ✗ goto no_filter;
960 ✗ if(auto* svc = capy::get_system_context().find_service<http::zlib::inflate_service>())
961 {
962 ✗ filter_.reset(new zlib_filter(
963 *svc,
964 ✗ cfg_->zlib_window_bits));
965 }
966 ✗ break;
967
968 ✗ case content_coding::gzip:
969 ✗ if(!cfg_->apply_gzip_decoder)
970 ✗ goto no_filter;
971 ✗ if(auto* svc = capy::get_system_context().find_service<http::zlib::inflate_service>())
972 {
973 ✗ filter_.reset(new zlib_filter(
974 *svc,
975 ✗ cfg_->zlib_window_bits + 16));
976 }
977 ✗ break;
978
979 ✗ case content_coding::br:
980 ✗ if(!cfg_->apply_brotli_decoder)
981 ✗ goto no_filter;
982 ✗ if(auto* svc = capy::get_system_context().find_service<http::brotli::decode_service>())
983 {
984 ✗ filter_.reset(new brotli_filter(*svc));
985 }
986 ✗ break;
987
988 ✗ no_filter:
989 8716x default:
990 8716x break;
991 }
992
993 8716x if(is_plain())
994 {
995 4387x cb0_ = { p, cap, overread };
996 4387x cb1_ = {};
997 }
998 else
999 {
1000 // buffered payload
1001 4329x std::size_t n0 = (overread > cfg_->min_buffer)
1002 8658x ? overread
1003 4329x : cfg_->min_buffer;
1004 4329x std::size_t n1 = cfg_->min_buffer;
1005
1006 4329x cb0_ = { p , n0, overread };
1007 4329x cb1_ = { p + n0 , n1 };
1008 }
1009
1010 8716x if(m_.payload() == payload::size)
1011 {
1012 8540x if(!filter_ &&
1013 4270x body_limit_ < m_.payload_size())
1014 {
1015 3x ec = error::body_too_large;
1016 3x state_ = state::reset;
1017 3x return;
1018 }
1019 4267x payload_remain_ = m_.payload_size();
1020 }
1021
1022 8713x state_ = state::body;
1023 BOOST_FALLTHROUGH;
1024 }
1025
1026 51464x case state::body:
1027 {
1028 51464x BOOST_ASSERT(state_ == state::body);
1029 51464x BOOST_ASSERT(m_.payload() != payload::none);
1030 51464x BOOST_ASSERT(m_.payload() != payload::error);
1031
1032 8366x auto set_state_to_complete = [&]()
1033 {
1034 8366x state_ = state::complete;
1035 59830x };
1036
1037 51464x if(m_.payload() == payload::chunked)
1038 {
1039 for(;;)
1040 {
1041 78651x if(chunk_remain_ == 0
1042 75748x && !chunked_body_ended)
1043 {
1044 71617x auto cs = chained_sequence(cb0_.data());
1045 20849x auto check_ec = [&]()
1046 {
1047 20849x if(ec == condition::need_more_input && got_eof_)
1048 {
1049 ✗ ec = error::incomplete;
1050 ✗ state_ = state::reset;
1051 }
1052 92466x };
1053
1054 71617x if(needs_chunk_close_)
1055 {
1056 62239x parse_eol(cs, ec);
1057 62239x if(ec)
1058 {
1059 435x check_ec();
1060 20849x return;
1061 }
1062 }
1063 9378x else if(trailer_headers_)
1064 {
1065 4243x skip_trailer_headers(cs, ec);
1066 4243x if(ec)
1067 {
1068 112x check_ec();
1069 112x return;
1070 }
1071 4131x cb0_.consume(cb0_.size() - cs.size());
1072 4131x chunked_body_ended = true;
1073 8276x continue;
1074 }
1075
1076 66939x auto chunk_size = parse_hex(cs, ec);
1077 66939x if(ec)
1078 {
1079 19950x check_ec();
1080 19950x return;
1081 }
1082
1083 // skip chunk extensions
1084 46989x find_eol(cs, ec);
1085 46989x if(ec)
1086 {
1087 352x check_ec();
1088 352x return;
1089 }
1090
1091 46637x cb0_.consume(cb0_.size() - cs.size());
1092 46637x chunk_remain_ = chunk_size;
1093
1094 46637x needs_chunk_close_ = true;
1095 46637x if(chunk_remain_ == 0)
1096 {
1097 4145x needs_chunk_close_ = false;
1098 4145x trailer_headers_ = true;
1099 4145x continue;
1100 }
1101 }
1102
1103 49526x if(cb0_.size() == 0 && !chunked_body_ended)
1104 {
1105 1830x if(got_eof_)
1106 {
1107 1x ec = error::incomplete;
1108 1x state_ = state::reset;
1109 1x return;
1110 }
1111
1112 1829x ec = error::need_data;
1113 1829x return;
1114 }
1115
1116 47696x if(filter_)
1117 {
1118 ✗ chunk_remain_ -= apply_filter(
1119 ec,
1120 clamp(chunk_remain_, cb0_.size()),
1121 ✗ !chunked_body_ended);
1122
1123 ✗ if(ec || chunked_body_ended)
1124 ✗ return;
1125 }
1126 else
1127 {
1128 const std::size_t chunk_avail =
1129 47696x clamp(chunk_remain_, cb0_.size());
1130
1131 47696x if(body_limit_remain() < chunk_avail)
1132 {
1133 ✗ ec = error::body_too_large;
1134 ✗ state_ = state::reset;
1135 ✗ return;
1136 }
1137
1138 // in_place style
1139 47696x auto copied = capy::buffer_copy(
1140 47696x cb1_.prepare(cb1_.capacity()),
1141 47696x cb0_.data(),
1142 chunk_avail);
1143 47696x chunk_remain_ -= copied;
1144 47696x body_avail_ += copied;
1145 47696x body_total_ += copied;
1146 47696x cb0_.consume(copied);
1147 47696x cb1_.commit(copied);
1148 47696x if(cb1_.capacity() == 0
1149 47696x && !chunked_body_ended)
1150 {
1151 ✗ ec = error::in_place_overflow;
1152 ✗ return;
1153 }
1154
1155 47696x if(chunked_body_ended)
1156 {
1157 4131x set_state_to_complete();
1158 4131x return;
1159 }
1160 }
1161 51841x }
1162 }
1163 else
1164 {
1165 // non-chunked payload
1166
1167 73962x const std::size_t payload_avail = [&]()
1168 {
1169 24654x auto ret = cb0_.size();
1170 24654x if(!filter_)
1171 24654x ret -= body_avail_;
1172 24654x if(m_.payload() == payload::size)
1173 24397x return clamp(payload_remain_, ret);
1174 // payload::eof
1175 257x return ret;
1176 24654x }();
1177
1178 73962x const bool is_complete = [&]()
1179 {
1180 24654x if(m_.payload() == payload::size)
1181 24397x return payload_avail == payload_remain_;
1182 // payload::eof
1183 257x return got_eof_;
1184 24654x }();
1185
1186 24654x if(filter_)
1187 {
1188 ✗ payload_remain_ -= apply_filter(
1189 ✗ ec, payload_avail, !is_complete);
1190 ✗ if(ec || is_complete)
1191 ✗ return;
1192 }
1193 else
1194 {
1195 // plain body
1196
1197 24654x if(m_.payload() == payload::to_eof)
1198 {
1199 257x if(body_limit_remain() < payload_avail)
1200 {
1201 1x ec = error::body_too_large;
1202 1x state_ = state::reset;
1203 1x return;
1204 }
1205 }
1206
1207 // in_place style
1208 24653x payload_remain_ -= payload_avail;
1209 24653x body_avail_ += payload_avail;
1210 24653x body_total_ += payload_avail;
1211 24653x if(cb0_.capacity() == 0 && !is_complete)
1212 {
1213 7x ec = error::in_place_overflow;
1214 7x return;
1215 }
1216
1217 24646x if(is_complete)
1218 {
1219 4235x set_state_to_complete();
1220 4235x return;
1221 }
1222 }
1223
1224 20411x if(m_.payload() == payload::size && got_eof_)
1225 {
1226 1x ec = error::incomplete;
1227 1x state_ = state::reset;
1228 1x return;
1229 }
1230
1231 20410x ec = error::need_data;
1232 20410x return;
1233 }
1234
1235 break;
1236 }
1237
1238 2213x case state::complete:
1239 2213x break;
1240 }
1241 }
1242
1243 auto
1244 41630x pull_body() ->
1245 const_buffers_type
1246 {
1247 41630x switch(state_)
1248 {
1249 30x case state::header_done:
1250 30x return {};
1251 41598x case state::body:
1252 case state::complete:
1253 41598x cbp_ = prefix_pair(
1254 41598x (is_plain() ? cb0_ : cb1_).data(),
1255 body_avail_);
1256 41598x return detail::make_span(cbp_);
1257 2x case state::reset:
1258 2x if(got_header_)
1259 2x return {};
1260 BOOST_FALLTHROUGH;
1261 default:
1262 ✗ detail::throw_logic_error();
1263 }
1264 }
1265
1266 void
1267 39610x consume_body(std::size_t n)
1268 {
1269 39610x switch(state_)
1270 {
1271 ✗ case state::header_done:
1272 ✗ return;
1273 39610x case state::body:
1274 case state::complete:
1275 39610x n = clamp(n, body_avail_);
1276 39610x (is_plain() ? cb0_ : cb1_).consume(n);
1277 39610x body_avail_ -= n;
1278 39610x return;
1279 ✗ case state::reset:
1280 ✗ if(got_header_)
1281 ✗ return;
1282 BOOST_FALLTHROUGH;
1283 default:
1284 ✗ detail::throw_logic_error();
1285 }
1286 }
1287
1288 core::string_view
1289 712x body() const
1290 {
1291 // Precondition violation
1292 712x if(state_ != state::complete)
1293 ✗ detail::throw_logic_error();
1294
1295 // Precondition violation
1296 712x if(body_avail_ != body_total_)
1297 ✗ detail::throw_logic_error();
1298
1299 712x auto cbp = (is_plain() ? cb0_ : cb1_).data();
1300 712x BOOST_ASSERT(body_avail_ <= cbp[0].size());
1301 712x return core::string_view(
1302 712x static_cast<char const*>(cbp[0].data()),
1303 1424x body_avail_);
1304 }
1305
1306 bool
1307 9x has_buffered_data() const noexcept
1308 {
1309 9x if(state_ != state::complete)
1310 1x return false;
1311
1312 8x if(is_plain())
1313 6x return cb0_.size() > body_avail_;
1314 2x return cb0_.size() > 0;
1315 }
1316
1317 void
1318 5x set_body_limit(std::uint64_t n)
1319 {
1320 5x switch(state_)
1321 {
1322 1x case state::header:
1323 case state::header_done:
1324 1x body_limit_ = n;
1325 1x break;
1326 2x case state::complete:
1327 // only allowed for empty bodies
1328 2x if(body_total_ == 0)
1329 1x break;
1330 BOOST_FALLTHROUGH;
1331 default:
1332 // set body_limit before parsing the body
1333 3x detail::throw_logic_error();
1334 }
1335 2x }
1336
1337 private:
1338 bool
1339 140727x is_plain() const noexcept
1340 {
1341 281454x return ! filter_ &&
1342 281454x m_.payload() != payload::chunked;
1343 }
1344
1345 std::uint64_t
1346 47967x body_limit_remain() const noexcept
1347 {
1348 47967x return body_limit_ - body_total_;
1349 }
1350
1351 std::size_t
1352 ✗ apply_filter(
1353 std::error_code& ec,
1354 std::size_t payload_avail,
1355 bool more)
1356 {
1357 ✗ std::size_t p0 = payload_avail;
1358 for(;;)
1359 {
1360 ✗ if(payload_avail == 0 && more)
1361 ✗ break;
1362
1363 ✗ auto f_rs = [&](){
1364 ✗ BOOST_ASSERT(filter_ != nullptr);
1365 ✗ std::size_t n = clamp(body_limit_remain());
1366 ✗ n = clamp(n, cb1_.capacity());
1367
1368 ✗ return filter_->process(
1369 ✗ detail::make_span(cb1_.prepare(n)),
1370 ✗ prefix_pair(cb0_.data(), payload_avail),
1371 ✗ more);
1372 ✗ }();
1373
1374 ✗ cb0_.consume(f_rs.in_bytes);
1375 ✗ payload_avail -= f_rs.in_bytes;
1376 ✗ body_total_ += f_rs.out_bytes;
1377
1378 // in_place style
1379 ✗ cb1_.commit(f_rs.out_bytes);
1380 ✗ body_avail_ += f_rs.out_bytes;
1381 ✗ if(cb1_.capacity() == 0 &&
1382 ✗ !f_rs.finished && f_rs.in_bytes == 0)
1383 {
1384 ✗ ec = error::in_place_overflow;
1385 ✗ goto done;
1386 }
1387
1388 ✗ if(f_rs.ec)
1389 {
1390 ✗ ec = f_rs.ec;
1391 ✗ state_ = state::reset;
1392 ✗ break;
1393 }
1394
1395 ✗ if(body_limit_remain() == 0 &&
1396 ✗ !f_rs.finished && f_rs.in_bytes == 0)
1397 {
1398 ✗ ec = error::body_too_large;
1399 ✗ state_ = state::reset;
1400 ✗ break;
1401 }
1402
1403 ✗ if(f_rs.finished)
1404 {
1405 ✗ if(!more)
1406 ✗ state_ = state::complete;
1407 ✗ break;
1408 }
1409 ✗ }
1410
1411 ✗ done:
1412 ✗ return p0 - payload_avail;
1413 }
1414 };
1415
1416 //------------------------------------------------
1417 //
1418 // Special Members
1419 //
1420 //------------------------------------------------
1421
1422 2196x parser::
1423 ~parser()
1424 {
1425 2196x delete impl_;
1426 2196x }
1427
1428 12x parser::
1429 12x parser() noexcept
1430 12x : impl_(nullptr)
1431 {
1432 12x }
1433
1434 3x parser::
1435 3x parser(parser&& other) noexcept
1436 3x : impl_(other.impl_)
1437 {
1438 3x other.impl_ = nullptr;
1439 3x }
1440
1441 2181x parser::
1442 parser(
1443 std::shared_ptr<parser_config_impl const> cfg,
1444 2181x detail::kind k)
1445 2181x : impl_(new impl(std::move(cfg), k))
1446 {
1447 // TODO: use a single allocation for
1448 // impl and workspace buffer.
1449 2181x }
1450
1451 void
1452 4x parser::
1453 assign(parser&& other) noexcept
1454 {
1455 4x if(this == &other)
1456 ✗ return;
1457 4x delete impl_;
1458 4x impl_ = other.impl_;
1459 4x other.impl_ = nullptr;
1460 }
1461
1462 //--------------------------------------------
1463 //
1464 // Observers
1465 //
1466 //--------------------------------------------
1467
1468 bool
1469 36139x parser::got_header() const noexcept
1470 {
1471 36139x BOOST_ASSERT(impl_);
1472 36139x return impl_->got_header();
1473 }
1474
1475 bool
1476 59150x parser::is_complete() const noexcept
1477 {
1478 59150x BOOST_ASSERT(impl_);
1479 59150x return impl_->is_complete();
1480 }
1481
1482 //------------------------------------------------
1483 //
1484 // Modifiers
1485 //
1486 //------------------------------------------------
1487
1488 void
1489 2728x parser::
1490 reset() noexcept
1491 {
1492 2728x BOOST_ASSERT(impl_);
1493 2728x impl_->reset();
1494 2728x }
1495
1496 void
1497 10657x parser::start()
1498 {
1499 10657x BOOST_ASSERT(impl_);
1500 10657x impl_->start(false);
1501 10652x }
1502
1503 auto
1504 81921x parser::
1505 prepare() ->
1506 mutable_buffers_type
1507 {
1508 81921x BOOST_ASSERT(impl_);
1509 81921x return impl_->prepare();
1510 }
1511
1512 void
1513 80860x parser::
1514 commit(
1515 std::size_t n)
1516 {
1517 80860x BOOST_ASSERT(impl_);
1518 80860x impl_->commit(n);
1519 80854x }
1520
1521 void
1522 134x parser::
1523 commit_eof()
1524 {
1525 134x BOOST_ASSERT(impl_);
1526 134x impl_->commit_eof();
1527 131x }
1528
1529 void
1530 98779x parser::
1531 parse(
1532 std::error_code& ec)
1533 {
1534 98779x BOOST_ASSERT(impl_);
1535 98779x impl_->parse(ec);
1536 98777x }
1537
1538 auto
1539 41630x parser::
1540 pull_body() ->
1541 const_buffers_type
1542 {
1543 41630x BOOST_ASSERT(impl_);
1544 41630x return impl_->pull_body();
1545 }
1546
1547 void
1548 39610x parser::
1549 consume_body(std::size_t n)
1550 {
1551 39610x BOOST_ASSERT(impl_);
1552 39610x impl_->consume_body(n);
1553 39610x }
1554
1555 core::string_view
1556 712x parser::
1557 body() const
1558 {
1559 712x BOOST_ASSERT(impl_);
1560 712x return impl_->body();
1561 }
1562
1563 core::string_view
1564 ✗ parser::
1565 release_buffered_data() noexcept
1566 {
1567 // TODO
1568 ✗ return {};
1569 }
1570
1571 bool
1572 9x parser::
1573 has_buffered_data() const noexcept
1574 {
1575 9x BOOST_ASSERT(impl_);
1576 9x return impl_->has_buffered_data();
1577 }
1578
1579 void
1580 5x parser::
1581 set_body_limit(std::uint64_t n)
1582 {
1583 5x BOOST_ASSERT(impl_);
1584 5x impl_->set_body_limit(n);
1585 2x }
1586
1587 //------------------------------------------------
1588 //
1589 // Implementation
1590 //
1591 //------------------------------------------------
1592
1593 void
1594 ✗ parser::
1595 start_impl(bool head_response)
1596 {
1597 ✗ BOOST_ASSERT(impl_);
1598 ✗ impl_->start(head_response);
1599 ✗ }
1600
1601 static_request const&
1602 316x parser::
1603 safe_get_request() const
1604 {
1605 316x BOOST_ASSERT(impl_);
1606 316x return impl_->safe_get_request();
1607 }
1608
1609 static_response const&
1610 3x parser::
1611 safe_get_response() const
1612 {
1613 3x BOOST_ASSERT(impl_);
1614 3x return impl_->safe_get_response();
1615 }
1616
1617 } // http
1618 } // boost
1619