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