LCOV - code coverage report
Current view: top level - src - serializer.cpp (source / functions) Coverage Total Hit Missed
Test: coverage_remapped.info Lines: 79.8 % 397 317 80
Test Date: 2026-11-22 21:03:06 Functions: 88.9 % 54 48 6

           TLA  Line data    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 HIT          71 : trim_prefix_pair(std::array<Buf, 2>& a, std::size_t n) noexcept
      49                 : {
      50              71 :     if(n >= a[0].size())
      51                 :     {
      52 MIS           0 :         n -= a[0].size();
      53               0 :         a[0] = Buf();
      54               0 :         if(n >= a[1].size())
      55               0 :             a[1] = Buf();
      56                 :         else
      57               0 :             a[1] = Buf(
      58                 :                 static_cast<char*>(const_cast<void*>(
      59               0 :                     static_cast<void const*>(a[1].data()))) + n,
      60               0 :                 a[1].size() - n);
      61                 :     }
      62                 :     else
      63                 :     {
      64 HIT          71 :         a[0] += n;
      65                 :     }
      66              71 : }
      67                 : 
      68                 : // Trim n bytes from the back of a 2-element buffer pair, in place.
      69                 : template<class Buf>
      70                 : inline void
      71              71 : trim_suffix_pair(std::array<Buf, 2>& a, std::size_t n) noexcept
      72                 : {
      73              71 :     if(n >= a[1].size())
      74                 :     {
      75              71 :         n -= a[1].size();
      76              71 :         a[1] = Buf();
      77              71 :         if(n >= a[0].size())
      78 MIS           0 :             a[0] = Buf();
      79                 :         else
      80 HIT          71 :             a[0] = Buf(a[0].data(), a[0].size() - n);
      81                 :     }
      82                 :     else
      83                 :     {
      84 MIS           0 :         a[1] = Buf(a[1].data(), a[1].size() - n);
      85                 :     }
      86 HIT          71 : }
      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             161 : chunk_header_len(
     103                 :     std::size_t max_chunk_size) noexcept
     104                 : {
     105                 :     return
     106                 :         static_cast<uint8_t>(
     107             161 :             (core::bit_width(max_chunk_size) + 3) / 4 +
     108             161 :             2); // crlf
     109                 : };
     110                 : 
     111                 : void
     112              70 : 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              70 :     auto p = buf + 16;
     120              70 :     auto const n = capy::buffer_size(mbs);
     121             349 :     for(std::size_t i = n - 2; i--;)
     122                 :     {
     123             279 :         *--p = hexdig[size & 0xf];
     124             279 :         size >>= 4;
     125                 :     }
     126              70 :     buf[16] = '\r';
     127              70 :     buf[17] = '\n';
     128              70 :     auto copied = capy::buffer_copy(
     129                 :         mbs,
     130             140 :         capy::const_buffer(p, n));
     131                 :     ignore_unused(copied);
     132              70 :     BOOST_ASSERT(copied == n);
     133              70 : }
     134                 : 
     135                 : class zlib_filter
     136                 :     : public detail::zlib_filter_base
     137                 : {
     138                 :     http::zlib::deflate_service& svc_;
     139                 : 
     140                 : public:
     141               2 :     zlib_filter(
     142                 :         http::zlib::deflate_service& svc,
     143                 :         int comp_level,
     144                 :         int window_bits,
     145                 :         int mem_level)
     146               2 :         : svc_(svc)
     147                 :     {
     148               4 :         std::error_code ec = static_cast<http::zlib::error>(svc_.init2(
     149               2 :             strm_,
     150                 :             comp_level,
     151                 :             http::zlib::deflated,
     152                 :             window_bits,
     153                 :             mem_level,
     154               2 :             http::zlib::default_strategy));
     155               2 :         if(ec != http::zlib::error::ok)
     156 MIS           0 :             detail::throw_system_error(ec);
     157 HIT           2 :     }
     158                 : 
     159               4 :     ~zlib_filter()
     160               2 :     {
     161               2 :         svc_.deflate_end(strm_);
     162               4 :     }
     163                 : 
     164                 : private:
     165                 :     virtual
     166                 :     std::size_t
     167               2 :     min_out_buffer() const noexcept override
     168                 :     {
     169               2 :         return 8;
     170                 :     }
     171                 : 
     172                 :     virtual
     173                 :     results
     174               2 :     do_process(
     175                 :         capy::mutable_buffer out,
     176                 :         capy::const_buffer in,
     177                 :         bool more) noexcept override
     178                 :     {
     179               2 :         strm_.next_out  = static_cast<unsigned char*>(out.data());
     180               2 :         strm_.avail_out = saturate_cast(out.size());
     181               2 :         strm_.next_in   = static_cast<unsigned char*>(const_cast<void *>(in.data()));
     182               2 :         strm_.avail_in  = saturate_cast(in.size());
     183                 : 
     184                 :         auto rs = static_cast<http::zlib::error>(
     185               2 :             svc_.deflate(
     186               2 :                 strm_,
     187                 :                 more ? http::zlib::no_flush : http::zlib::finish));
     188                 : 
     189               2 :         results rv;
     190               2 :         rv.out_bytes = saturate_cast(out.size()) - strm_.avail_out;
     191               2 :         rv.in_bytes  = saturate_cast(in.size()) - strm_.avail_in;
     192               2 :         rv.finished  = (rs == http::zlib::error::stream_end);
     193                 : 
     194               2 :         if(rs < http::zlib::error::ok && rs != http::zlib::error::buf_err)
     195 MIS           0 :             rv.ec = rs;
     196                 : 
     197 HIT           2 :         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 MIS           0 :     brotli_filter(
     209                 :         http::brotli::encode_service& svc,
     210                 :         std::uint32_t comp_quality,
     211                 :         std::uint32_t comp_window)
     212               0 :         : svc_(svc)
     213                 :     {
     214               0 :         state_ = svc_.create_instance(nullptr, nullptr, nullptr);
     215               0 :         if(!state_)
     216               0 :             detail::throw_bad_alloc();
     217                 :         using encoder_parameter = http::brotli::encoder_parameter;
     218               0 :         svc_.set_parameter(state_, encoder_parameter::quality, comp_quality);
     219               0 :         svc_.set_parameter(state_, encoder_parameter::lgwin, comp_window);
     220               0 :     }
     221                 : 
     222               0 :     ~brotli_filter()
     223               0 :     {
     224               0 :         svc_.destroy_instance(state_);
     225               0 :     }
     226                 : 
     227                 : private:
     228                 :     virtual
     229                 :     results
     230               0 :     do_process(
     231                 :         capy::mutable_buffer out,
     232                 :         capy::const_buffer in,
     233                 :         bool more) noexcept override
     234                 :     {
     235               0 :         auto* next_in = reinterpret_cast<const std::uint8_t*>(in.data());
     236               0 :         auto available_in = in.size();
     237               0 :         auto* next_out = reinterpret_cast<std::uint8_t*>(out.data());
     238               0 :         auto available_out = out.size();
     239                 : 
     240                 :         using encoder_operation = 
     241                 :             http::brotli::encoder_operation;
     242                 : 
     243               0 :         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               0 :         results rv;
     253               0 :         rv.in_bytes  = in.size()  - available_in;
     254               0 :         rv.out_bytes = out.size() - available_out;
     255               0 :         rv.finished  = svc_.is_finished(state_);
     256                 : 
     257               0 :         if(rs == false)
     258               0 :             rv.ec = error::bad_payload;
     259                 : 
     260               0 :         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 HIT         160 :     impl(std::shared_ptr<serializer_config_impl const> cfg)
     319             160 :         : cfg_(std::move(cfg))
     320             160 :         , ws_(cfg_->space_needed)
     321                 :     {
     322             160 :     }
     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              56 :     reset() noexcept
     335                 :     {
     336              56 :         filter_.reset();
     337              56 :         ws_.clear();
     338              56 :         state_ = state::start;
     339              56 :     }
     340                 : 
     341                 :     auto
     342             391 :     prepare() ->
     343                 :         system::result<const_buffers_type>
     344                 :     {
     345                 :         // Precondition violation
     346             391 :         if(state_ < state::header)
     347               1 :             detail::throw_logic_error();
     348                 : 
     349                 :         // Expect: 100-continue
     350             390 :         if(needs_exp100_continue_)
     351                 :         {
     352               4 :             if(!is_header_done())
     353               4 :                 return const_buffers_type(
     354                 :                     prepped_.begin(),
     355               2 :                     1); // limit to header
     356                 : 
     357               2 :             needs_exp100_continue_ = false;
     358                 : 
     359               2 :             return error::expect_100_continue;
     360                 :         }
     361                 : 
     362             386 :         if(!filter_)
     363                 :         {
     364             384 :             switch(style_)
     365                 :             {
     366               6 :             case style::empty:
     367               6 :                 break;
     368                 : 
     369             378 :             case style::stream:
     370             378 :                 if(out_.size() == 0 && is_header_done() && more_input_)
     371             118 :                     return error::need_data;
     372             260 :                 break;
     373                 :             }
     374                 :         }
     375                 :         else // filter
     376                 :         {
     377               2 :             switch(style_)
     378                 :             {
     379               1 :             case style::empty:
     380                 :             {
     381               1 :                 if(out_capacity() == 0 || filter_done_)
     382               1 :                     break;
     383                 : 
     384               2 :                 const auto rs = filter_->process(
     385               1 :                     detail::make_span(out_prepare()),
     386                 :                     {}, // empty input
     387                 :                     false);
     388                 : 
     389               1 :                 if(rs.ec)
     390                 :                 {
     391 MIS           0 :                     ws_.clear();
     392               0 :                     state_ = state::reset;
     393               0 :                     return rs.ec;
     394                 :                 }
     395                 : 
     396 HIT           1 :                 out_commit(rs.out_bytes);
     397                 : 
     398               1 :                 if(rs.finished)
     399                 :                 {
     400               1 :                     filter_done_ = true;
     401               1 :                     out_finish();
     402                 :                 }
     403                 : 
     404               1 :                 break;
     405                 :             }
     406                 : 
     407               1 :             case style::stream:
     408                 :             {
     409               1 :                 if(out_capacity() == 0 || filter_done_)
     410               1 :                     break;
     411                 : 
     412               2 :                 const auto rs = filter_->process(
     413               1 :                     detail::make_span(out_prepare()),
     414                 :                     in_.data(),
     415               1 :                     more_input_);
     416                 : 
     417               1 :                 if(rs.ec)
     418                 :                 {
     419 MIS           0 :                     ws_.clear();
     420               0 :                     state_ = state::reset;
     421               0 :                     return rs.ec;
     422                 :                 }
     423                 : 
     424 HIT           1 :                 in_.consume(rs.in_bytes);
     425               1 :                 out_commit(rs.out_bytes);
     426                 : 
     427               1 :                 if(rs.finished)
     428                 :                 {
     429               1 :                     filter_done_ = true;
     430               1 :                     out_finish();
     431                 :                 }
     432                 : 
     433               1 :                 if(out_.size() == 0 && is_header_done() && more_input_)
     434 MIS           0 :                     return error::need_data;
     435 HIT           1 :                 break;
     436                 :             }
     437                 :             }
     438                 :         }
     439                 : 
     440             268 :         prepped_.reset(!is_header_done());
     441             804 :         for(auto const& cb : out_.data())
     442                 :         {
     443             536 :             if(cb.size() != 0)
     444             170 :                 prepped_.append(cb);
     445                 :         }
     446             268 :         return detail::make_span(prepped_);
     447                 :     }
     448                 : 
     449                 :     void
     450            1904 :     consume(
     451                 :         std::size_t n)
     452                 :     {
     453                 :         // Precondition violation
     454            1904 :         if(state_ < state::header)
     455               1 :             detail::throw_logic_error();
     456                 : 
     457            1903 :         if(!is_header_done())
     458                 :         {
     459                 :             const auto header_remain =
     460             134 :                 prepped_[0].size();
     461             134 :             if(n < header_remain)
     462                 :             {
     463              48 :                 prepped_.consume(n);
     464              48 :                 return;
     465                 :             }
     466              86 :             n -= header_remain;
     467              86 :             prepped_.consume(header_remain);
     468              86 :             state_ = state::body;
     469                 :         }
     470                 : 
     471            1855 :         prepped_.consume(n);
     472                 : 
     473                 :         // no-op when out_ is not in use
     474            1855 :         out_.consume(n);
     475                 : 
     476            1855 :         if(!prepped_.empty())
     477            1692 :             return;
     478                 : 
     479             163 :         if(more_input_)
     480             109 :             return;
     481                 : 
     482              54 :         if(filter_ && !filter_done_)
     483 MIS           0 :             return;
     484                 : 
     485 HIT          54 :         if(needs_exp100_continue_)
     486               2 :             return;
     487                 : 
     488                 :         // ready for next message
     489              52 :         reset();
     490                 :     }
     491                 : 
     492                 :     void
     493             161 :     start_init(
     494                 :         message_base const& m)
     495                 :     {
     496                 :         // Precondition violation
     497             161 :         if(state_ != state::start)
     498 MIS           0 :             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 HIT         161 :         state_ = state::header;
     504                 : 
     505                 :         // VFALCO what do we do with
     506                 :         // metadata error code failures?
     507                 :         // m.h_.md.maybe_throw();
     508                 : 
     509             161 :         auto const& md = m.metadata();
     510             161 :         needs_exp100_continue_ = md.expect.is_100_continue;
     511                 : 
     512                 :         // Transfer-Encoding
     513             161 :         is_chunked_ = md.transfer_encoding.is_chunked;
     514                 : 
     515                 :         // Content-Encoding
     516             161 :         switch (md.content_encoding.coding)
     517                 :         {
     518 MIS           0 :         case content_coding::deflate:
     519               0 :             if(!cfg_->apply_deflate_encoder)
     520               0 :                 goto no_filter;
     521               0 :             if(auto* svc = capy::get_system_context().find_service<http::zlib::deflate_service>())
     522                 :             {
     523               0 :                 filter_.reset(new zlib_filter(
     524                 :                     *svc,
     525               0 :                     cfg_->zlib_comp_level,
     526               0 :                     cfg_->zlib_window_bits,
     527               0 :                     cfg_->zlib_mem_level));
     528               0 :                 filter_done_ = false;
     529                 :             }
     530               0 :             break;
     531                 : 
     532 HIT           2 :         case content_coding::gzip:
     533               2 :             if(!cfg_->apply_gzip_encoder)
     534 MIS           0 :                 goto no_filter;
     535 HIT           2 :             if(auto* svc = capy::get_system_context().find_service<http::zlib::deflate_service>())
     536                 :             {
     537               2 :                 filter_.reset(new zlib_filter(
     538                 :                     *svc,
     539               4 :                     cfg_->zlib_comp_level,
     540               4 :                     cfg_->zlib_window_bits + 16,
     541               2 :                     cfg_->zlib_mem_level));
     542               2 :                 filter_done_ = false;
     543                 :             }
     544               2 :             break;
     545                 : 
     546 MIS           0 :         case content_coding::br:
     547               0 :             if(!cfg_->apply_brotli_encoder)
     548               0 :                 goto no_filter;
     549               0 :             if(auto* svc = capy::get_system_context().find_service<http::brotli::encode_service>())
     550                 :             {
     551               0 :                 filter_.reset(new brotli_filter(
     552                 :                     *svc,
     553               0 :                     cfg_->brotli_comp_quality,
     554               0 :                     cfg_->brotli_comp_window));
     555               0 :                 filter_done_ = false;
     556                 :             }
     557               0 :             break;
     558                 : 
     559               0 :         no_filter:
     560 HIT         159 :         default:
     561             159 :             filter_.reset();
     562             159 :             break;
     563                 :         }
     564             161 :     }
     565                 : 
     566                 :     void
     567               7 :     start_empty(
     568                 :         message_base const& m)
     569                 :     {
     570               7 :         start_init(m);
     571               7 :         style_ = style::empty;
     572                 : 
     573               7 :         prepped_ = make_array(
     574                 :             1 + // header
     575                 :             2); // out buffer pairs
     576                 : 
     577               7 :         out_init();
     578                 : 
     579               7 :         if(!filter_)
     580               6 :             out_finish();
     581                 : 
     582               7 :         prepped_.append({ m.h_.cbuf, m.h_.size });
     583               7 :         more_input_ = false;
     584               7 :     }
     585                 : 
     586                 :     void
     587              79 :     start_stream(message_base const& m)
     588                 :     {
     589              79 :         start_init(m);
     590              79 :         style_ = style::stream;
     591                 : 
     592              79 :         prepped_ = make_array(
     593                 :             1 + // header
     594                 :             2); // out buffer pairs
     595                 : 
     596              79 :         if(filter_)
     597                 :         {
     598                 :             // TODO: smarter buffer distribution
     599               1 :             auto const n = (ws_.size() - 1) / 2;
     600               1 :             in_ = { ws_.reserve_front(n), n };
     601                 :         }
     602                 : 
     603              79 :         out_init();
     604                 : 
     605              79 :         prepped_.append({ m.h_.cbuf, m.h_.size });
     606              79 :         more_input_ = true;
     607              79 :     }
     608                 : 
     609                 :     // Like start_stream but without in_ allocation.
     610                 :     // Entire workspace is used for output buffering.
     611                 :     void
     612              75 :     start_buffers_direct(message_base const& m)
     613                 :     {
     614              75 :         start_init(m);
     615              75 :         style_ = style::stream;
     616                 : 
     617              75 :         prepped_ = make_array(
     618                 :             1 + // header
     619                 :             2); // out buffer pairs
     620                 : 
     621              75 :         out_init();
     622                 : 
     623              75 :         prepped_.append({ m.h_.cbuf, m.h_.size });
     624              75 :         more_input_ = true;
     625              75 :     }
     626                 : 
     627                 :     std::size_t
     628             183 :     stream_capacity() const
     629                 :     {
     630             183 :         if(filter_)
     631               1 :             return in_.capacity();
     632             182 :         return out_capacity();
     633                 :     }
     634                 : 
     635                 :     std::array<capy::mutable_buffer, 2>
     636             130 :     stream_prepare()
     637                 :     {
     638             130 :         if(state_ == state::start)
     639                 :         {
     640 MIS           0 :             if(!msg_)
     641               0 :                 detail::throw_logic_error();
     642               0 :             start_stream(*msg_);
     643                 :         }
     644 HIT         130 :         if(filter_)
     645               1 :             return in_.prepare(in_.capacity());
     646             129 :         return out_prepare();
     647                 :     }
     648                 : 
     649                 :     void
     650             166 :     stream_commit(std::size_t n)
     651                 :     {
     652             166 :         if(n > stream_capacity())
     653               1 :             detail::throw_invalid_argument();
     654                 : 
     655             165 :         if(filter_)
     656               1 :             return in_.commit(n);
     657                 : 
     658             164 :         out_commit(n);
     659                 :     }
     660                 : 
     661                 :     void
     662              76 :     stream_close() noexcept
     663                 :     {
     664              76 :         if(!filter_)
     665              75 :             out_finish();
     666                 : 
     667              76 :         more_input_ = false;
     668              76 :     }
     669                 : 
     670                 :     bool
     671             470 :     is_done() const noexcept
     672                 :     {
     673             470 :         return state_ == state::start;
     674                 :     }
     675                 : 
     676                 :     bool
     677             249 :     is_start() const noexcept
     678                 :     {
     679             249 :         return state_ == state::start;
     680                 :     }
     681                 : 
     682                 :     detail::workspace&
     683 MIS           0 :     ws() noexcept
     684                 :     {
     685               0 :         return ws_;
     686                 :     }
     687                 : 
     688                 : private:
     689                 :     bool
     690 HIT        2388 :     is_header_done() const noexcept
     691                 :     {
     692            2388 :         return state_ == state::body;
     693                 :     }
     694                 : 
     695                 :     detail::array_of_const_buffers
     696             161 :     make_array(std::size_t n)
     697                 :     {
     698             161 :         BOOST_ASSERT(n <= std::uint16_t(-1));
     699                 : 
     700                 :         return {
     701             161 :             ws_.push_array(n,
     702 MIS           0 :                 capy::const_buffer{}),
     703 HIT         161 :             static_cast<std::uint16_t>(n) };
     704                 :     }
     705                 : 
     706                 :     void
     707             161 :     out_init()
     708                 :     {
     709                 :         // use all the remaining buffer
     710             161 :         auto const n = ws_.size() - 1;
     711             161 :         out_ = { ws_.reserve_front(n), n };
     712             161 :         chunk_header_len_ =
     713             161 :             chunk_header_len(out_.capacity());
     714             161 :         if(out_capacity() == 0)
     715 MIS           0 :             detail::throw_length_error();
     716 HIT         161 :     }
     717                 : 
     718                 :     std::array<capy::mutable_buffer, 2>
     719             131 :     out_prepare() noexcept
     720                 :     {
     721             131 :         auto mbp = out_.prepare(out_.capacity());
     722             131 :         if(is_chunked_)
     723                 :         {
     724              71 :             trim_prefix_pair(
     725              71 :                 mbp, chunk_header_len_);
     726              71 :             trim_suffix_pair(
     727                 :                 mbp, crlf_and_final_chunk.size());
     728                 :         }
     729             131 :         return mbp;
     730                 :     }
     731                 : 
     732                 :     void
     733             166 :     out_commit(
     734                 :         std::size_t n) noexcept
     735                 :     {
     736             166 :         if(is_chunked_)
     737                 :         {
     738              89 :             if(n == 0)
     739              19 :                 return;
     740                 : 
     741              70 :             write_chunk_header(out_.prepare(chunk_header_len_), n);
     742              70 :             out_.commit(chunk_header_len_);
     743                 : 
     744              70 :             out_.prepare(n);
     745              70 :             out_.commit(n);
     746                 : 
     747              70 :             capy::buffer_copy(out_.prepare(crlf.size()), crlf);
     748              70 :             out_.commit(crlf.size());
     749                 :         }
     750                 :         else
     751                 :         {
     752              77 :             out_.commit(n);
     753                 :         }
     754                 :     }
     755                 : 
     756                 :     std::size_t
     757             345 :     out_capacity() const noexcept
     758                 :     {
     759             345 :         if(is_chunked_)
     760                 :         {
     761             178 :             auto const overhead = chunk_header_len_ +
     762             178 :                 crlf_and_final_chunk.size();
     763             178 :             if(out_.capacity() < overhead)
     764               1 :                 return 0;
     765             177 :             return out_.capacity() - overhead;
     766                 :         }
     767             167 :         return out_.capacity();
     768                 :     }
     769                 : 
     770                 :     void
     771              83 :     out_finish() noexcept
     772                 :     {
     773              83 :         if(is_chunked_)
     774                 :         {
     775              43 :             capy::buffer_copy(
     776              43 :                 out_.prepare(final_chunk.size()), final_chunk);
     777              43 :             out_.commit(final_chunk.size());
     778                 :         }
     779              83 :     }
     780                 : };
     781                 : 
     782                 : //------------------------------------------------
     783                 : 
     784             165 : serializer::
     785                 : ~serializer()
     786                 : {
     787             165 :     delete impl_;
     788             165 : }
     789                 : 
     790               1 : serializer::
     791               1 : serializer(serializer&& other) noexcept
     792               1 :     : impl_(other.impl_)
     793                 : {
     794               1 :     other.impl_ = nullptr;
     795               1 : }
     796                 : 
     797                 : serializer&
     798               2 : serializer::
     799                 : operator=(serializer&& other) noexcept
     800                 : {
     801               2 :     if(this != &other)
     802                 :     {
     803               2 :         delete impl_;
     804               2 :         impl_ = other.impl_;
     805               2 :         other.impl_ = nullptr;
     806                 :     }
     807               2 :     return *this;
     808                 : }
     809                 : 
     810             160 : serializer::
     811                 : serializer(
     812             160 :     std::shared_ptr<serializer_config_impl const> cfg)
     813             160 :     : impl_(new impl(std::move(cfg)))
     814                 : {
     815             160 : }
     816                 : 
     817                 : void
     818               4 : serializer::
     819                 : reset() noexcept
     820                 : {
     821               4 :     BOOST_ASSERT(impl_);
     822               4 :     impl_->reset();
     823               4 : }
     824                 : 
     825                 : void
     826             161 : serializer::
     827                 : set_message(message_base const& m) noexcept
     828                 : {
     829             161 :     BOOST_ASSERT(impl_);
     830             161 :     impl_->msg_ = &m;
     831             161 : }
     832                 : 
     833                 : void
     834               7 : serializer::
     835                 : start()
     836                 : {
     837               7 :     if(!impl_ || !impl_->msg_)
     838 MIS           0 :         detail::throw_logic_error();
     839 HIT           7 :     impl_->start_empty(*impl_->msg_);
     840               7 : }
     841                 : 
     842                 : void
     843               1 : serializer::
     844                 : start_stream()
     845                 : {
     846               1 :     if(!impl_ || !impl_->msg_)
     847 MIS           0 :         detail::throw_logic_error();
     848 HIT           1 :     impl_->start_stream(*impl_->msg_);
     849               1 : }
     850                 : 
     851                 : void
     852              78 : serializer::
     853                 : start_writes()
     854                 : {
     855              78 :     if(!impl_ || !impl_->msg_)
     856 MIS           0 :         detail::throw_logic_error();
     857 HIT          78 :     impl_->start_stream(*impl_->msg_);
     858              78 : }
     859                 : 
     860                 : void
     861              75 : serializer::
     862                 : start_buffers()
     863                 : {
     864              75 :     if(!impl_ || !impl_->msg_)
     865 MIS           0 :         detail::throw_logic_error();
     866 HIT          75 :     impl_->start_buffers_direct(*impl_->msg_);
     867              75 : }
     868                 : 
     869                 : auto
     870             391 : serializer::
     871                 : prepare() ->
     872                 :     system::result<const_buffers_type>
     873                 : {
     874             391 :     BOOST_ASSERT(impl_);
     875             391 :     return impl_->prepare();
     876                 : }
     877                 : 
     878                 : void
     879            1904 : serializer::
     880                 : consume(std::size_t n)
     881                 : {
     882            1904 :     BOOST_ASSERT(impl_);
     883            1904 :     impl_->consume(n);
     884            1903 : }
     885                 : 
     886                 : bool
     887             470 : serializer::
     888                 : is_done() const noexcept
     889                 : {
     890             470 :     BOOST_ASSERT(impl_);
     891             470 :     return impl_->is_done();
     892                 : }
     893                 : 
     894                 : bool
     895             249 : serializer::
     896                 : is_start() const noexcept
     897                 : {
     898             249 :     BOOST_ASSERT(impl_);
     899             249 :     return impl_->is_start();
     900                 : }
     901                 : 
     902                 : //------------------------------------------------
     903                 : 
     904                 : detail::workspace&
     905 MIS           0 : serializer::
     906                 : ws()
     907                 : {
     908               0 :     BOOST_ASSERT(impl_);
     909               0 :     return impl_->ws();
     910                 : }
     911                 : 
     912                 : //------------------------------------------------
     913                 : 
     914                 : std::size_t
     915 HIT          17 : serializer::
     916                 : stream_capacity() const
     917                 : {
     918              17 :     BOOST_ASSERT(impl_);
     919              17 :     return impl_->stream_capacity();
     920                 : }
     921                 : 
     922                 : auto
     923             130 : serializer::
     924                 : stream_prepare() ->
     925                 :     mutable_buffers_type
     926                 : {
     927             130 :     BOOST_ASSERT(impl_);
     928             130 :     return impl_->stream_prepare();
     929                 : }
     930                 : 
     931                 : void
     932             166 : serializer::
     933                 : stream_commit(std::size_t n)
     934                 : {
     935             166 :     BOOST_ASSERT(impl_);
     936             166 :     impl_->stream_commit(n);
     937             165 : }
     938                 : 
     939                 : void
     940              76 : serializer::
     941                 : stream_close() noexcept
     942                 : {
     943              76 :     BOOST_ASSERT(impl_);
     944              76 :     impl_->stream_close();
     945              76 : }
     946                 : 
     947                 : } // http
     948                 : } // boost
        

Generated by: LCOV version 2.3