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