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