Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 33 additions & 15 deletions include/boost/http_proto/serializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,32 +434,36 @@ class serializer::stream
return *this;
}

/** Returns `true` if the stream is open
/** Return true if the stream is open
*/
BOOST_HTTP_PROTO_DECL
bool
is_open() const noexcept;

/** Returns the available capacity
/** Return the available capacity

@throw std::logic_error if `!is_open()`.
*/
BOOST_HTTP_PROTO_DECL
std::size_t
capacity() const noexcept;
capacity() const;

/** Prepares a buffer for writing
/** Prepare a buffer for writing

Use @ref commit to make the written data available
to the serializer.

@return An object of type @ref mutable_buffers_type
that satisfies MutableBufferSequence requirements,
the underlying memory is owned by the serializer.

@throw std::logic_error if `!is_open()`.
*/
BOOST_HTTP_PROTO_DECL
mutable_buffers_type
prepare() noexcept;
prepare();

/** Commits data to the serializer
/** Commit data to the serializer

@param n Number of bytes to commit.

Expand All @@ -470,12 +474,19 @@ class serializer::stream
void
commit(std::size_t n);

/** Closes the stream
/** Close the stream if open
*/
BOOST_HTTP_PROTO_DECL
void
close();

/** Destructor

Closes the stream if open.
*/
BOOST_HTTP_PROTO_DECL
~stream();

private:
friend class serializer;

Expand All @@ -494,7 +505,7 @@ class serializer::stream
class serializer::const_buf_gen_base
{
public:
// Returns the next non-empty buffer,
// Return the next non-empty buffer,
// or an empty buffer if none remain.
virtual
buffers::const_buffer
Expand All @@ -510,7 +521,7 @@ class serializer::const_buf_gen_base
std::size_t
count() const = 0;

// Returns true when there is no buffer or
// Return true when there is no buffer or
// the remaining buffers are empty
virtual
bool
Expand All @@ -526,6 +537,7 @@ class serializer::const_buf_gen

ConstBufferSequence cbs_;
it_t current_;

public:
using const_buffer =
buffers::const_buffer;
Expand Down Expand Up @@ -556,8 +568,10 @@ class serializer::const_buf_gen
current_,
buffers::end(cbs_),
std::size_t{},
[](std::size_t sum, const_buffer cb) {
return sum + cb.size(); });
[](std::size_t sum, const_buffer cb)
{
return sum + cb.size();
});
}

std::size_t
Expand All @@ -566,8 +580,10 @@ class serializer::const_buf_gen
return std::count_if(
current_,
buffers::end(cbs_),
[](const_buffer cb) {
return cb.size() != 0; });
[](const_buffer cb)
{
return cb.size() != 0;
});
}

bool
Expand All @@ -576,8 +592,10 @@ class serializer::const_buf_gen
return std::all_of(
current_,
buffers::end(cbs_),
[](const_buffer cb) {
return cb.size() == 0; });
[](const_buffer cb)
{
return cb.size() == 0;
});
}
};

Expand Down
18 changes: 5 additions & 13 deletions src/detail/filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,25 @@ filter::
process(
buffers::mutable_buffer_subspan out,
buffers::const_buffer_pair in,
bool more,
bool partial_flush) -> results
bool more) -> results
{
results rv;
auto flush = filter::flush::none;
bool p_more = true;
for(;;)
{
if(!more && flush != filter::flush::finish && in[1].size() == 0)
if(!more && p_more && in[1].size() == 0)
{
if(buffers::size(out) < min_out_buffer())
{
rv.out_short = true;
return rv;
}
flush = filter::flush::finish;
p_more = false;
}

auto ob = buffers::front(out);
auto ib = buffers::front(in);
auto rs = do_process(ob, ib, flush);
auto rs = do_process(ob, ib, p_more);

rv.in_bytes += rs.in_bytes;
rv.out_bytes += rs.out_bytes;
Expand All @@ -66,14 +65,7 @@ process(
return rv;

if(buffers::size(in) == 0 && rs.out_bytes < ob.size())
{
if(partial_flush && rv.out_bytes == 0)
{
flush = filter::flush::partial;
continue;
}
return rv;
}
}
}

Expand Down
12 changes: 2 additions & 10 deletions src/detail/filter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,9 @@ class filter
process(
buffers::mutable_buffer_subspan out,
buffers::const_buffer_pair in,
bool more,
bool partial_flush = false);
bool more);

protected:
enum class flush
{
none,
partial,
finish
};

virtual
std::size_t
min_out_buffer() const noexcept
Expand All @@ -87,7 +79,7 @@ class filter
do_process(
buffers::mutable_buffer,
buffers::const_buffer,
flush) noexcept = 0;
bool) noexcept = 0;
};

} // detail
Expand Down
18 changes: 0 additions & 18 deletions src/detail/zlib_filter_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,6 @@ class zlib_filter_base : public filter
protected:
rts::zlib::stream strm_;

static
rts::zlib::flush
translate(filter::flush flush) noexcept
{
switch(flush)
{
case filter::flush::none:
return rts::zlib::flush::no_flush;

case filter::flush::partial:
return rts::zlib::flush::block;

case filter::flush::finish:
default:
return rts::zlib::flush::finish;
}
}

static
unsigned int
saturate_cast(std::size_t n) noexcept
Expand Down
10 changes: 6 additions & 4 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,15 +322,17 @@ class zlib_filter
do_process(
buffers::mutable_buffer out,
buffers::const_buffer in,
filter::flush flush) noexcept override
bool more) noexcept override
{
strm_.next_out = static_cast<unsigned char*>(out.data());
strm_.avail_out = saturate_cast(out.size());
strm_.next_in = static_cast<unsigned char*>(const_cast<void *>(in.data()));
strm_.avail_in = saturate_cast(in.size());

auto rs = static_cast<rts::zlib::error>(
svc_.inflate(strm_, translate(flush)));
svc_.inflate(
strm_,
more ? rts::zlib::no_flush : rts::zlib::finish));

results rv;
rv.out_bytes = saturate_cast(out.size()) - strm_.avail_out;
Expand Down Expand Up @@ -374,7 +376,7 @@ class brotli_filter
do_process(
buffers::mutable_buffer out,
buffers::const_buffer in,
filter::flush flush) noexcept override
bool more) noexcept override
{
auto* next_in = reinterpret_cast<const std::uint8_t*>(in.data());
auto available_in = in.size();
Expand All @@ -394,7 +396,7 @@ class brotli_filter
rv.out_bytes = out.size() - available_out;
rv.finished = svc_.is_finished(state_);

if(rs == rts::brotli::decoder_result::needs_more_input && flush == filter::flush::finish)
if(!more && rs == rts::brotli::decoder_result::needs_more_input)
rv.ec = BOOST_HTTP_PROTO_ERR(error::bad_payload);

if(rs == rts::brotli::decoder_result::error)
Expand Down
Loading
Loading