src/serializer.cpp

79.8% Lines (317 / 397) 90.4% Functions (47 / 52)
serializer.cpp
f(x) Functions (52)
Function Calls Lines Blocks
void boost::http::(anonymous namespace)::trim_prefix_pair<boost::capy::mutable_buffer>(std::array<boost::capy::mutable_buffer, 2ul>&, unsigned long) :48 71x 36.4% 26.0% void boost::http::(anonymous namespace)::trim_suffix_pair<boost::capy::mutable_buffer>(std::array<boost::capy::mutable_buffer, 2ul>&, unsigned long) :71 71x 77.8% 64.0% boost::http::(anonymous namespace)::chunk_header_len(unsigned long) :102 161x 100.0% 100.0% boost::http::(anonymous namespace)::write_chunk_header(std::array<boost::capy::mutable_buffer, 2ul> const&, unsigned long) :112 70x 100.0% 90.0% boost::http::(anonymous namespace)::zlib_filter::zlib_filter(boost::http::zlib::deflate_service&, int, int, int) :141 2x 87.5% 62.0% boost::http::(anonymous namespace)::zlib_filter::~zlib_filter() :159 4x 100.0% 100.0% boost::http::(anonymous namespace)::zlib_filter::min_out_buffer() const :167 2x 100.0% 100.0% boost::http::(anonymous namespace)::zlib_filter::do_process(boost::capy::mutable_buffer, boost::capy::const_buffer, bool) :174 2x 92.9% 81.0% boost::http::(anonymous namespace)::brotli_filter::brotli_filter(boost::http::brotli::encode_service&, unsigned int, unsigned int) :208 0 0.0% 0.0% boost::http::(anonymous namespace)::brotli_filter::~brotli_filter() :222 0 0.0% 0.0% boost::http::(anonymous namespace)::brotli_filter::do_process(boost::capy::mutable_buffer, boost::capy::const_buffer, bool) :230 0 0.0% 0.0% boost::http::serializer::impl::impl(std::shared_ptr<boost::http::serializer_config_impl const>) :318 160x 100.0% 92.0% boost::http::serializer::impl::reset() :334 56x 100.0% 100.0% boost::http::serializer::impl::prepare() :342 391x 87.3% 80.0% boost::http::serializer::impl::consume(unsigned long) :450 1904x 95.5% 94.0% boost::http::serializer::impl::start_init(boost::http::message_base const&) :493 161x 47.6% 33.0% boost::http::serializer::impl::start_empty(boost::http::message_base const&) :567 7x 100.0% 100.0% boost::http::serializer::impl::start_stream(boost::http::message_base const&) :587 79x 100.0% 93.0% boost::http::serializer::impl::start_buffers_direct(boost::http::message_base const&) :612 75x 100.0% 100.0% boost::http::serializer::impl::stream_capacity() const :628 183x 100.0% 100.0% boost::http::serializer::impl::stream_prepare() :636 130x 62.5% 67.0% boost::http::serializer::impl::stream_commit(unsigned long) :650 166x 100.0% 100.0% boost::http::serializer::impl::stream_close() :662 76x 100.0% 100.0% boost::http::serializer::impl::is_done() const :671 470x 100.0% 100.0% boost::http::serializer::impl::is_start() const :677 249x 100.0% 100.0% boost::http::serializer::impl::ws() :683 0 0.0% 0.0% boost::http::serializer::impl::is_header_done() const :690 2388x 100.0% 100.0% boost::http::serializer::impl::make_array(unsigned long) :696 161x 80.0% 80.0% boost::http::serializer::impl::out_init() :707 161x 87.5% 75.0% boost::http::serializer::impl::out_prepare() :719 131x 100.0% 100.0% boost::http::serializer::impl::out_commit(unsigned long) :733 166x 100.0% 100.0% boost::http::serializer::impl::out_capacity() const :757 345x 100.0% 100.0% boost::http::serializer::impl::out_finish() :771 83x 100.0% 100.0% boost::http::serializer::~serializer() :784 165x 100.0% 100.0% boost::http::serializer::serializer(boost::http::serializer&&) :790 1x 100.0% 100.0% boost::http::serializer::operator=(boost::http::serializer&&) :798 2x 100.0% 71.0% boost::http::serializer::serializer(std::shared_ptr<boost::http::serializer_config_impl const>) :810 160x 100.0% 58.0% boost::http::serializer::reset() :818 4x 100.0% 75.0% boost::http::serializer::set_message(boost::http::message_base const&) :826 161x 100.0% 67.0% boost::http::serializer::start() :834 7x 80.0% 71.0% boost::http::serializer::start_stream() :843 1x 80.0% 71.0% boost::http::serializer::start_writes() :852 78x 80.0% 71.0% boost::http::serializer::start_buffers() :861 75x 80.0% 71.0% boost::http::serializer::prepare() :870 391x 100.0% 75.0% boost::http::serializer::consume(unsigned long) :879 1904x 100.0% 75.0% boost::http::serializer::is_done() const :887 470x 100.0% 75.0% boost::http::serializer::is_start() const :895 249x 100.0% 75.0% boost::http::serializer::ws() :905 0 0.0% 0.0% boost::http::serializer::stream_capacity() const :915 17x 100.0% 75.0% boost::http::serializer::stream_prepare() :923 130x 100.0% 75.0% boost::http::serializer::stream_commit(unsigned long) :932 166x 100.0% 75.0% boost::http::serializer::stream_close() :940 76x 100.0% 75.0%
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3 // Copyright (c) 2024 Christian Mazakas
4 // Copyright (c) 2024 Mohammad Nejati
5 //
6 // Distributed under the Boost Software License, Version 1.0. (See accompanying
7 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 //
9 // Official repository: https://github.com/cppalliance/http
10 //
11
12 #include <boost/http/detail/circular_buffer.hpp>
13 #include <boost/http/detail/except.hpp>
14 #include <boost/http/detail/header.hpp>
15 #include <boost/http/message_base.hpp>
16 #include <boost/http/serializer.hpp>
17
18 #include "src/detail/array_of_const_buffers.hpp"
19 #include "src/detail/brotli_filter_base.hpp"
20 #include "src/detail/buffer_utils.hpp"
21 #include "src/detail/zlib_filter_base.hpp"
22
23 #include <boost/capy/buffers/buffer_copy.hpp>
24 #include <boost/capy/ex/system_context.hpp>
25 #include <boost/core/bit.hpp>
26 #include <boost/core/ignore_unused.hpp>
27 #include <boost/http/brotli/encode.hpp>
28 #include <boost/http/zlib/compression_method.hpp>
29 #include <boost/http/zlib/compression_strategy.hpp>
30 #include <boost/http/zlib/deflate.hpp>
31 #include <boost/http/zlib/error.hpp>
32 #include <boost/http/zlib/flush.hpp>
33
34 #include <array>
35 #include <memory>
36 #include <stddef.h>
37
38 namespace boost {
39 namespace http {
40
41 namespace {
42
43 // Trim n bytes from the front of a 2-element buffer pair, in place.
44 // Replaces the pre-#262 `capy::remove_prefix(pair, n)` idiom on a
45 // 2-element buffer sequence.
46 template<class Buf>
47 inline void
48 71x trim_prefix_pair(std::array<Buf, 2>& a, std::size_t n) noexcept
49 {
50 71x if(n >= a[0].size())
51 {
52 ✗ n -= a[0].size();
53 ✗ a[0] = Buf();
54 ✗ if(n >= a[1].size())
55 ✗ a[1] = Buf();
56 else
57 ✗ a[1] = Buf(
58 static_cast<char*>(const_cast<void*>(
59 ✗ static_cast<void const*>(a[1].data()))) + n,
60 ✗ a[1].size() - n);
61 }
62 else
63 {
64 71x a[0] += n;
65 }
66 71x }
67
68 // Trim n bytes from the back of a 2-element buffer pair, in place.
69 template<class Buf>
70 inline void
71 71x trim_suffix_pair(std::array<Buf, 2>& a, std::size_t n) noexcept
72 {
73 71x if(n >= a[1].size())
74 {
75 71x n -= a[1].size();
76 71x a[1] = Buf();
77 71x if(n >= a[0].size())
78 ✗ a[0] = Buf();
79 else
80 71x a[0] = Buf(a[0].data(), a[0].size() - n);
81 }
82 else
83 {
84 ✗ a[1] = Buf(a[1].data(), a[1].size() - n);
85 }
86 71x }
87
88 const
89 capy::const_buffer
90 crlf_and_final_chunk = {"\r\n0\r\n\r\n", 7};
91
92 const
93 capy::const_buffer
94 crlf = {"\r\n", 2};
95
96 const
97 capy::const_buffer
98 final_chunk = {"0\r\n\r\n", 5};
99
100 constexpr
101 std::uint8_t
102 161x chunk_header_len(
103 std::size_t max_chunk_size) noexcept
104 {
105 return
106 static_cast<uint8_t>(
107 161x (core::bit_width(max_chunk_size) + 3) / 4 +
108 161x 2); // crlf
109 };
110
111 void
112 70x write_chunk_header(
113 const std::array<capy::mutable_buffer, 2>& mbs,
114 std::size_t size) noexcept
115 {
116 static constexpr char hexdig[] =
117 "0123456789ABCDEF";
118 char buf[18];
119 70x auto p = buf + 16;
120 70x auto const n = capy::buffer_size(mbs);
121 349x for(std::size_t i = n - 2; i--;)
122 {
123 279x *--p = hexdig[size & 0xf];
124 279x size >>= 4;
125 }
126 70x buf[16] = '\r';
127 70x buf[17] = '\n';
128 70x auto copied = capy::buffer_copy(
129 mbs,
130 140x capy::const_buffer(p, n));
131 ignore_unused(copied);
132 70x BOOST_ASSERT(copied == n);
133 70x }
134
135 class zlib_filter
136 : public detail::zlib_filter_base
137 {
138 http::zlib::deflate_service& svc_;
139
140 public:
141 2x zlib_filter(
142 http::zlib::deflate_service& svc,
143 int comp_level,
144 int window_bits,
145 int mem_level)
146 2x : svc_(svc)
147 {
148 4x std::error_code ec = static_cast<http::zlib::error>(svc_.init2(
149 2x strm_,
150 comp_level,
151 http::zlib::deflated,
152 window_bits,
153 mem_level,
154 2x http::zlib::default_strategy));
155 2x if(ec != http::zlib::error::ok)
156 ✗ detail::throw_system_error(ec);
157 2x }
158
159 4x ~zlib_filter()
160 2x {
161 2x svc_.deflate_end(strm_);
162 4x }
163
164 private:
165 virtual
166 std::size_t
167 2x min_out_buffer() const noexcept override
168 {
169 2x return 8;
170 }
171
172 virtual
173 results
174 2x do_process(
175 capy::mutable_buffer out,
176 capy::const_buffer in,
177 bool more) noexcept override
178 {
179 2x strm_.next_out = static_cast<unsigned char*>(out.data());
180 2x strm_.avail_out = saturate_cast(out.size());
181 2x strm_.next_in = static_cast<unsigned char*>(const_cast<void *>(in.data()));
182 2x strm_.avail_in = saturate_cast(in.size());
183
184 auto rs = static_cast<http::zlib::error>(
185 2x svc_.deflate(
186 2x strm_,
187 more ? http::zlib::no_flush : http::zlib::finish));
188
189 2x results rv;
190 2x rv.out_bytes = saturate_cast(out.size()) - strm_.avail_out;
191 2x rv.in_bytes = saturate_cast(in.size()) - strm_.avail_in;
192 2x rv.finished = (rs == http::zlib::error::stream_end);
193
194 2x if(rs < http::zlib::error::ok && rs != http::zlib::error::buf_err)
195 ✗ rv.ec = rs;
196
197 2x return rv;
198 }
199 };
200
201 class brotli_filter
202 : public detail::brotli_filter_base
203 {
204 http::brotli::encode_service& svc_;
205 http::brotli::encoder_state* state_;
206
207 public:
208 ✗ brotli_filter(
209 http::brotli::encode_service& svc,
210 std::uint32_t comp_quality,
211 std::uint32_t comp_window)
212 ✗ : svc_(svc)
213 {
214 ✗ state_ = svc_.create_instance(nullptr, nullptr, nullptr);
215 ✗ if(!state_)
216 ✗ detail::throw_bad_alloc();
217 using encoder_parameter = http::brotli::encoder_parameter;
218 ✗ svc_.set_parameter(state_, encoder_parameter::quality, comp_quality);
219 ✗ svc_.set_parameter(state_, encoder_parameter::lgwin, comp_window);
220 ✗ }
221
222 ✗ ~brotli_filter()
223 ✗ {
224 ✗ svc_.destroy_instance(state_);
225 ✗ }
226
227 private:
228 virtual
229 results
230 ✗ do_process(
231 capy::mutable_buffer out,
232 capy::const_buffer in,
233 bool more) noexcept override
234 {
235 ✗ auto* next_in = reinterpret_cast<const std::uint8_t*>(in.data());
236 ✗ auto available_in = in.size();
237 ✗ auto* next_out = reinterpret_cast<std::uint8_t*>(out.data());
238 ✗ auto available_out = out.size();
239
240 using encoder_operation =
241 http::brotli::encoder_operation;
242
243 ✗ bool rs = svc_.compress_stream(
244 state_,
245 more ? encoder_operation::process : encoder_operation::finish,
246 &available_in,
247 &next_in,
248 &available_out,
249 &next_out,
250 nullptr);
251
252 ✗ results rv;
253 ✗ rv.in_bytes = in.size() - available_in;
254 ✗ rv.out_bytes = out.size() - available_out;
255 ✗ rv.finished = svc_.is_finished(state_);
256
257 ✗ if(rs == false)
258 ✗ rv.ec = error::bad_payload;
259
260 ✗ return rv;
261 }
262 };
263
264 template<class UInt>
265 std::size_t
266 clamp(
267 UInt x,
268 std::size_t limit = (std::numeric_limits<
269 std::size_t>::max)()) noexcept
270 {
271 if(x >= limit)
272 return limit;
273 return static_cast<std::size_t>(x);
274 }
275
276 } // namespace
277
278 //------------------------------------------------
279
280 class serializer::impl
281 {
282 enum class state
283 {
284 reset,
285 start,
286 header,
287 body
288 };
289
290 enum class style
291 {
292 empty,
293 stream
294 };
295
296 std::shared_ptr<serializer_config_impl const> cfg_;
297 detail::workspace ws_;
298
299 std::unique_ptr<detail::filter> filter_;
300
301 detail::circular_buffer out_;
302 detail::circular_buffer in_;
303 detail::array_of_const_buffers prepped_;
304 capy::const_buffer tmp_;
305
306 state state_ = state::start;
307 style style_ = style::empty;
308 uint8_t chunk_header_len_ = 0;
309 bool more_input_ = false;
310 bool is_chunked_ = false;
311 bool needs_exp100_continue_ = false;
312 bool filter_done_ = false;
313
314 public:
315 message_base const* msg_ = nullptr;
316
317 explicit
318 160x impl(std::shared_ptr<serializer_config_impl const> cfg)
319 160x : cfg_(std::move(cfg))
320 160x , ws_(cfg_->space_needed)
321 {
322 160x }
323
324 impl(
325 std::shared_ptr<serializer_config_impl const> cfg,
326 message_base const& msg)
327 : cfg_(std::move(cfg))
328 , ws_(cfg_->space_needed)
329 , msg_(&msg)
330 {
331 }
332
333 void
334 56x reset() noexcept
335 {
336 56x filter_.reset();
337 56x ws_.clear();
338 56x state_ = state::start;
339 56x }
340
341 auto
342 391x prepare() ->
343 system::result<const_buffers_type>
344 {
345 // Precondition violation
346 391x if(state_ < state::header)
347 1x detail::throw_logic_error();
348
349 // Expect: 100-continue
350 390x if(needs_exp100_continue_)
351 {
352 4x if(!is_header_done())
353 4x return const_buffers_type(
354 prepped_.begin(),
355 2x 1); // limit to header
356
357 2x needs_exp100_continue_ = false;
358
359 2x return error::expect_100_continue;
360 }
361
362 386x if(!filter_)
363 {
364 384x switch(style_)
365 {
366 6x case style::empty:
367 6x break;
368
369 378x case style::stream:
370 378x if(out_.size() == 0 && is_header_done() && more_input_)
371 118x return error::need_data;
372 260x break;
373 }
374 }
375 else // filter
376 {
377 2x switch(style_)
378 {
379 1x case style::empty:
380 {
381 1x if(out_capacity() == 0 || filter_done_)
382 1x break;
383
384 2x const auto rs = filter_->process(
385 1x detail::make_span(out_prepare()),
386 {}, // empty input
387 false);
388
389 1x if(rs.ec)
390 {
391 ✗ ws_.clear();
392 ✗ state_ = state::reset;
393 ✗ return rs.ec;
394 }
395
396 1x out_commit(rs.out_bytes);
397
398 1x if(rs.finished)
399 {
400 1x filter_done_ = true;
401 1x out_finish();
402 }
403
404 1x break;
405 }
406
407 1x case style::stream:
408 {
409 1x if(out_capacity() == 0 || filter_done_)
410 1x break;
411
412 2x const auto rs = filter_->process(
413 1x detail::make_span(out_prepare()),
414 in_.data(),
415 1x more_input_);
416
417 1x if(rs.ec)
418 {
419 ✗ ws_.clear();
420 ✗ state_ = state::reset;
421 ✗ return rs.ec;
422 }
423
424 1x in_.consume(rs.in_bytes);
425 1x out_commit(rs.out_bytes);
426
427 1x if(rs.finished)
428 {
429 1x filter_done_ = true;
430 1x out_finish();
431 }
432
433 1x if(out_.size() == 0 && is_header_done() && more_input_)
434 ✗ return error::need_data;
435 1x break;
436 }
437 }
438 }
439
440 268x prepped_.reset(!is_header_done());
441 804x for(auto const& cb : out_.data())
442 {
443 536x if(cb.size() != 0)
444 170x prepped_.append(cb);
445 }
446 268x return detail::make_span(prepped_);
447 }
448
449 void
450 1904x consume(
451 std::size_t n)
452 {
453 // Precondition violation
454 1904x if(state_ < state::header)
455 1x detail::throw_logic_error();
456
457 1903x if(!is_header_done())
458 {
459 const auto header_remain =
460 134x prepped_[0].size();
461 134x if(n < header_remain)
462 {
463 48x prepped_.consume(n);
464 48x return;
465 }
466 86x n -= header_remain;
467 86x prepped_.consume(header_remain);
468 86x state_ = state::body;
469 }
470
471 1855x prepped_.consume(n);
472
473 // no-op when out_ is not in use
474 1855x out_.consume(n);
475
476 1855x if(!prepped_.empty())
477 1692x return;
478
479 163x if(more_input_)
480 109x return;
481
482 54x if(filter_ && !filter_done_)
483 ✗ return;
484
485 54x if(needs_exp100_continue_)
486 2x return;
487
488 // ready for next message
489 52x reset();
490 }
491
492 void
493 161x start_init(
494 message_base const& m)
495 {
496 // Precondition violation
497 161x if(state_ != state::start)
498 ✗ detail::throw_logic_error();
499
500 // TODO: To uphold the strong exception guarantee,
501 // `state_` must be reset to `state::start` if an
502 // exception is thrown during the start operation.
503 161x state_ = state::header;
504
505 // VFALCO what do we do with
506 // metadata error code failures?
507 // m.h_.md.maybe_throw();
508
509 161x auto const& md = m.metadata();
510 161x needs_exp100_continue_ = md.expect.is_100_continue;
511
512 // Transfer-Encoding
513 161x is_chunked_ = md.transfer_encoding.is_chunked;
514
515 // Content-Encoding
516 161x switch (md.content_encoding.coding)
517 {
518 ✗ case content_coding::deflate:
519 ✗ if(!cfg_->apply_deflate_encoder)
520 ✗ goto no_filter;
521 ✗ if(auto* svc = capy::get_system_context().find_service<http::zlib::deflate_service>())
522 {
523 ✗ filter_.reset(new zlib_filter(
524 *svc,
525 ✗ cfg_->zlib_comp_level,
526 ✗ cfg_->zlib_window_bits,
527 ✗ cfg_->zlib_mem_level));
528 ✗ filter_done_ = false;
529 }
530 ✗ break;
531
532 2x case content_coding::gzip:
533 2x if(!cfg_->apply_gzip_encoder)
534 ✗ goto no_filter;
535 2x if(auto* svc = capy::get_system_context().find_service<http::zlib::deflate_service>())
536 {
537 2x filter_.reset(new zlib_filter(
538 *svc,
539 4x cfg_->zlib_comp_level,
540 4x cfg_->zlib_window_bits + 16,
541 2x cfg_->zlib_mem_level));
542 2x filter_done_ = false;
543 }
544 2x break;
545
546 ✗ case content_coding::br:
547 ✗ if(!cfg_->apply_brotli_encoder)
548 ✗ goto no_filter;
549 ✗ if(auto* svc = capy::get_system_context().find_service<http::brotli::encode_service>())
550 {
551 ✗ filter_.reset(new brotli_filter(
552 *svc,
553 ✗ cfg_->brotli_comp_quality,
554 ✗ cfg_->brotli_comp_window));
555 ✗ filter_done_ = false;
556 }
557 ✗ break;
558
559 ✗ no_filter:
560 159x default:
561 159x filter_.reset();
562 159x break;
563 }
564 161x }
565
566 void
567 7x start_empty(
568 message_base const& m)
569 {
570 7x start_init(m);
571 7x style_ = style::empty;
572
573 7x prepped_ = make_array(
574 1 + // header
575 2); // out buffer pairs
576
577 7x out_init();
578
579 7x if(!filter_)
580 6x out_finish();
581
582 7x prepped_.append({ m.h_.cbuf, m.h_.size });
583 7x more_input_ = false;
584 7x }
585
586 void
587 79x start_stream(message_base const& m)
588 {
589 79x start_init(m);
590 79x style_ = style::stream;
591
592 79x prepped_ = make_array(
593 1 + // header
594 2); // out buffer pairs
595
596 79x if(filter_)
597 {
598 // TODO: smarter buffer distribution
599 1x auto const n = (ws_.size() - 1) / 2;
600 1x in_ = { ws_.reserve_front(n), n };
601 }
602
603 79x out_init();
604
605 79x prepped_.append({ m.h_.cbuf, m.h_.size });
606 79x more_input_ = true;
607 79x }
608
609 // Like start_stream but without in_ allocation.
610 // Entire workspace is used for output buffering.
611 void
612 75x start_buffers_direct(message_base const& m)
613 {
614 75x start_init(m);
615 75x style_ = style::stream;
616
617 75x prepped_ = make_array(
618 1 + // header
619 2); // out buffer pairs
620
621 75x out_init();
622
623 75x prepped_.append({ m.h_.cbuf, m.h_.size });
624 75x more_input_ = true;
625 75x }
626
627 std::size_t
628 183x stream_capacity() const
629 {
630 183x if(filter_)
631 1x return in_.capacity();
632 182x return out_capacity();
633 }
634
635 std::array<capy::mutable_buffer, 2>
636 130x stream_prepare()
637 {
638 130x if(state_ == state::start)
639 {
640 ✗ if(!msg_)
641 ✗ detail::throw_logic_error();
642 ✗ start_stream(*msg_);
643 }
644 130x if(filter_)
645 1x return in_.prepare(in_.capacity());
646 129x return out_prepare();
647 }
648
649 void
650 166x stream_commit(std::size_t n)
651 {
652 166x if(n > stream_capacity())
653 1x detail::throw_invalid_argument();
654
655 165x if(filter_)
656 1x return in_.commit(n);
657
658 164x out_commit(n);
659 }
660
661 void
662 76x stream_close() noexcept
663 {
664 76x if(!filter_)
665 75x out_finish();
666
667 76x more_input_ = false;
668 76x }
669
670 bool
671 470x is_done() const noexcept
672 {
673 470x return state_ == state::start;
674 }
675
676 bool
677 249x is_start() const noexcept
678 {
679 249x return state_ == state::start;
680 }
681
682 detail::workspace&
683 ✗ ws() noexcept
684 {
685 ✗ return ws_;
686 }
687
688 private:
689 bool
690 2388x is_header_done() const noexcept
691 {
692 2388x return state_ == state::body;
693 }
694
695 detail::array_of_const_buffers
696 161x make_array(std::size_t n)
697 {
698 161x BOOST_ASSERT(n <= std::uint16_t(-1));
699
700 return {
701 161x ws_.push_array(n,
702 ✗ capy::const_buffer{}),
703 161x static_cast<std::uint16_t>(n) };
704 }
705
706 void
707 161x out_init()
708 {
709 // use all the remaining buffer
710 161x auto const n = ws_.size() - 1;
711 161x out_ = { ws_.reserve_front(n), n };
712 161x chunk_header_len_ =
713 161x chunk_header_len(out_.capacity());
714 161x if(out_capacity() == 0)
715 ✗ detail::throw_length_error();
716 161x }
717
718 std::array<capy::mutable_buffer, 2>
719 131x out_prepare() noexcept
720 {
721 131x auto mbp = out_.prepare(out_.capacity());
722 131x if(is_chunked_)
723 {
724 71x trim_prefix_pair(
725 71x mbp, chunk_header_len_);
726 71x trim_suffix_pair(
727 mbp, crlf_and_final_chunk.size());
728 }
729 131x return mbp;
730 }
731
732 void
733 166x out_commit(
734 std::size_t n) noexcept
735 {
736 166x if(is_chunked_)
737 {
738 89x if(n == 0)
739 19x return;
740
741 70x write_chunk_header(out_.prepare(chunk_header_len_), n);
742 70x out_.commit(chunk_header_len_);
743
744 70x out_.prepare(n);
745 70x out_.commit(n);
746
747 70x capy::buffer_copy(out_.prepare(crlf.size()), crlf);
748 70x out_.commit(crlf.size());
749 }
750 else
751 {
752 77x out_.commit(n);
753 }
754 }
755
756 std::size_t
757 345x out_capacity() const noexcept
758 {
759 345x if(is_chunked_)
760 {
761 178x auto const overhead = chunk_header_len_ +
762 178x crlf_and_final_chunk.size();
763 178x if(out_.capacity() < overhead)
764 1x return 0;
765 177x return out_.capacity() - overhead;
766 }
767 167x return out_.capacity();
768 }
769
770 void
771 83x out_finish() noexcept
772 {
773 83x if(is_chunked_)
774 {
775 43x capy::buffer_copy(
776 43x out_.prepare(final_chunk.size()), final_chunk);
777 43x out_.commit(final_chunk.size());
778 }
779 83x }
780 };
781
782 //------------------------------------------------
783
784 165x serializer::
785 ~serializer()
786 {
787 165x delete impl_;
788 165x }
789
790 1x serializer::
791 1x serializer(serializer&& other) noexcept
792 1x : impl_(other.impl_)
793 {
794 1x other.impl_ = nullptr;
795 1x }
796
797 serializer&
798 2x serializer::
799 operator=(serializer&& other) noexcept
800 {
801 2x if(this != &other)
802 {
803 2x delete impl_;
804 2x impl_ = other.impl_;
805 2x other.impl_ = nullptr;
806 }
807 2x return *this;
808 }
809
810 160x serializer::
811 serializer(
812 160x std::shared_ptr<serializer_config_impl const> cfg)
813 160x : impl_(new impl(std::move(cfg)))
814 {
815 160x }
816
817 void
818 4x serializer::
819 reset() noexcept
820 {
821 4x BOOST_ASSERT(impl_);
822 4x impl_->reset();
823 4x }
824
825 void
826 161x serializer::
827 set_message(message_base const& m) noexcept
828 {
829 161x BOOST_ASSERT(impl_);
830 161x impl_->msg_ = &m;
831 161x }
832
833 void
834 7x serializer::
835 start()
836 {
837 7x if(!impl_ || !impl_->msg_)
838 ✗ detail::throw_logic_error();
839 7x impl_->start_empty(*impl_->msg_);
840 7x }
841
842 void
843 1x serializer::
844 start_stream()
845 {
846 1x if(!impl_ || !impl_->msg_)
847 ✗ detail::throw_logic_error();
848 1x impl_->start_stream(*impl_->msg_);
849 1x }
850
851 void
852 78x serializer::
853 start_writes()
854 {
855 78x if(!impl_ || !impl_->msg_)
856 ✗ detail::throw_logic_error();
857 78x impl_->start_stream(*impl_->msg_);
858 78x }
859
860 void
861 75x serializer::
862 start_buffers()
863 {
864 75x if(!impl_ || !impl_->msg_)
865 ✗ detail::throw_logic_error();
866 75x impl_->start_buffers_direct(*impl_->msg_);
867 75x }
868
869 auto
870 391x serializer::
871 prepare() ->
872 system::result<const_buffers_type>
873 {
874 391x BOOST_ASSERT(impl_);
875 391x return impl_->prepare();
876 }
877
878 void
879 1904x serializer::
880 consume(std::size_t n)
881 {
882 1904x BOOST_ASSERT(impl_);
883 1904x impl_->consume(n);
884 1903x }
885
886 bool
887 470x serializer::
888 is_done() const noexcept
889 {
890 470x BOOST_ASSERT(impl_);
891 470x return impl_->is_done();
892 }
893
894 bool
895 249x serializer::
896 is_start() const noexcept
897 {
898 249x BOOST_ASSERT(impl_);
899 249x return impl_->is_start();
900 }
901
902 //------------------------------------------------
903
904 detail::workspace&
905 ✗ serializer::
906 ws()
907 {
908 ✗ BOOST_ASSERT(impl_);
909 ✗ return impl_->ws();
910 }
911
912 //------------------------------------------------
913
914 std::size_t
915 17x serializer::
916 stream_capacity() const
917 {
918 17x BOOST_ASSERT(impl_);
919 17x return impl_->stream_capacity();
920 }
921
922 auto
923 130x serializer::
924 stream_prepare() ->
925 mutable_buffers_type
926 {
927 130x BOOST_ASSERT(impl_);
928 130x return impl_->stream_prepare();
929 }
930
931 void
932 166x serializer::
933 stream_commit(std::size_t n)
934 {
935 166x BOOST_ASSERT(impl_);
936 166x impl_->stream_commit(n);
937 165x }
938
939 void
940 76x serializer::
941 stream_close() noexcept
942 {
943 76x BOOST_ASSERT(impl_);
944 76x impl_->stream_close();
945 76x }
946
947 } // http
948 } // boost
949