diff options
Diffstat (limited to 'server/src/http/ext/raw_deflate.cppm')
| -rw-r--r-- | server/src/http/ext/raw_deflate.cppm | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/server/src/http/ext/raw_deflate.cppm b/server/src/http/ext/raw_deflate.cppm new file mode 100644 index 0000000..ee7da2f --- /dev/null +++ b/server/src/http/ext/raw_deflate.cppm | |||
| @@ -0,0 +1,134 @@ | |||
| 1 | module; | ||
| 2 | |||
| 3 | #include <boost/asio/buffer.hpp> | ||
| 4 | #include <boost/beast/core.hpp> | ||
| 5 | #include <boost/beast/http.hpp> | ||
| 6 | #include <boost/beast/zlib.hpp> | ||
| 7 | |||
| 8 | export module routemon:http.ext.raw_deflate; | ||
| 9 | |||
| 10 | import :http.core.common; | ||
| 11 | |||
| 12 | namespace routemon::http::ext::raw_deflate { | ||
| 13 | |||
| 14 | namespace { | ||
| 15 | |||
| 16 | namespace net = boost::asio; | ||
| 17 | |||
| 18 | } | ||
| 19 | |||
| 20 | template <bhttp::concepts::body_reader Inner> | ||
| 21 | struct body | ||
| 22 | { | ||
| 23 | using value_type = Inner::value_type; | ||
| 24 | |||
| 25 | class reader | ||
| 26 | { | ||
| 27 | static constexpr auto const chunk_size = 16384; | ||
| 28 | |||
| 29 | Inner::reader r_; | ||
| 30 | bool stream_ended_ = false; | ||
| 31 | beast::zlib::z_params zs_ = {}; | ||
| 32 | beast::zlib::inflate_stream inflate_strm_ = {}; | ||
| 33 | std::unique_ptr<std::array<char, chunk_size>> in_buf_ = nullptr; | ||
| 34 | std::unique_ptr<std::array<char, chunk_size>> out_buf_ = nullptr; | ||
| 35 | |||
| 36 | auto inflate_chunk(beast::error_code& ec) -> void | ||
| 37 | { | ||
| 38 | assert(zs_.avail_in > 0); | ||
| 39 | zs_.next_in = in_buf_->data(); | ||
| 40 | |||
| 41 | do | ||
| 42 | { | ||
| 43 | zs_.avail_out = out_buf_->size(); | ||
| 44 | zs_.next_out = out_buf_->data(); | ||
| 45 | |||
| 46 | inflate_strm_.write(zs_, beast::zlib::Flush::none, ec); | ||
| 47 | assert(ec != beast::zlib::error::stream_error); | ||
| 48 | if (ec == beast::zlib::error::need_buffers) | ||
| 49 | ec = {}; | ||
| 50 | if (ec == beast::zlib::error::end_of_stream) | ||
| 51 | { | ||
| 52 | ec = {}; | ||
| 53 | stream_ended_ = true; | ||
| 54 | } | ||
| 55 | if (ec) | ||
| 56 | break; | ||
| 57 | |||
| 58 | auto have = chunk_size - zs_.avail_out; | ||
| 59 | auto cbuf = | ||
| 60 | net::buffer(static_cast<void const*>(out_buf_->data()), have); | ||
| 61 | |||
| 62 | auto inner_ec = beast::error_code{}; | ||
| 63 | do | ||
| 64 | cbuf += r_.put(cbuf, inner_ec); | ||
| 65 | while (cbuf.size() > 0 && !inner_ec); | ||
| 66 | if (inner_ec) | ||
| 67 | { | ||
| 68 | ec = inner_ec; | ||
| 69 | break; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | while (zs_.avail_out == 0); | ||
| 73 | |||
| 74 | // In the last case (stream_ended_), the user has sent too much data | ||
| 75 | assert(zs_.avail_in == 0 || ec || stream_ended_); | ||
| 76 | } | ||
| 77 | |||
| 78 | public: | ||
| 79 | template <bool isRequest, bhttp::concepts::fields Fields> | ||
| 80 | explicit reader(bhttp::header<isRequest, Fields>& hdr, value_type& v) | ||
| 81 | : r_{hdr, v} | ||
| 82 | { | ||
| 83 | } | ||
| 84 | |||
| 85 | auto | ||
| 86 | init(boost::optional<std::uint64_t> /* n */, beast::error_code& ec) noexcept | ||
| 87 | -> void | ||
| 88 | { | ||
| 89 | in_buf_.reset(new std::array<char, chunk_size>{}); | ||
| 90 | out_buf_.reset(new std::array<char, chunk_size>{}); | ||
| 91 | ec = {}; | ||
| 92 | } | ||
| 93 | |||
| 94 | auto | ||
| 95 | put(beast::concepts::const_buffer_sequence auto b, | ||
| 96 | beast::error_code& ec) noexcept -> std::size_t | ||
| 97 | { | ||
| 98 | auto in_buf = net::buffer(in_buf_->data(), in_buf_->size()); | ||
| 99 | in_buf += zs_.avail_in; | ||
| 100 | |||
| 101 | auto consumed_from_b = net::buffer_copy(in_buf, b); | ||
| 102 | zs_.avail_in += consumed_from_b; | ||
| 103 | |||
| 104 | if (zs_.avail_in == in_buf_->size()) | ||
| 105 | inflate_chunk(ec); | ||
| 106 | else | ||
| 107 | ec = {}; | ||
| 108 | |||
| 109 | return consumed_from_b; | ||
| 110 | } | ||
| 111 | |||
| 112 | auto finish(beast::error_code& ec) noexcept -> void | ||
| 113 | { | ||
| 114 | ec = {}; | ||
| 115 | if (zs_.avail_in > 0) | ||
| 116 | inflate_chunk(ec); | ||
| 117 | if (!ec && !stream_ended_) | ||
| 118 | ec = beast::zlib::error::stream_error; | ||
| 119 | if (stream_ended_ && zs_.avail_in > 0) | ||
| 120 | // client sent data past deflate stream end | ||
| 121 | ec = beast::zlib::error::stream_error; | ||
| 122 | |||
| 123 | in_buf_.reset(); | ||
| 124 | out_buf_.reset(); | ||
| 125 | inflate_strm_.reset(); | ||
| 126 | if (!ec) | ||
| 127 | { | ||
| 128 | r_.finish(ec); | ||
| 129 | } | ||
| 130 | } | ||
| 131 | }; | ||
| 132 | }; | ||
| 133 | |||
| 134 | } // namespace routemon::http::ext::raw_deflate | ||