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
12 changes: 6 additions & 6 deletions include/boost/http/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
#include <boost/http/error.hpp>

#include <boost/capy/buffers/buffer_copy.hpp>
#include <boost/capy/buffers/buffer_pair.hpp>
#include <boost/capy/buffers/slice.hpp>
#include <boost/capy/buffers/buffer_slice.hpp>
#include <boost/capy/concept/read_stream.hpp>
#include <boost/capy/concept/write_sink.hpp>
#include <boost/capy/cond.hpp>
#include <boost/capy/error.hpp>
#include <boost/capy/io_task.hpp>
#include <boost/core/span.hpp>

#include <array>
#include <cstddef>
#include <cstdint>
#include <memory>
Expand Down Expand Up @@ -526,7 +526,7 @@ read(Stream& stream, MB buffers)
co_return {{}, 0};

std::size_t total = 0;
auto dest = capy::sans_prefix(buffers, 0);
auto dest = capy::buffer_slice(buffers);

for(;;)
{
Expand All @@ -538,12 +538,12 @@ read(Stream& stream, MB buffers)
auto body_data = pull_body();
if(capy::buffer_size(body_data) > 0)
{
std::size_t copied = capy::buffer_copy(dest, body_data);
std::size_t copied = capy::buffer_copy(dest.data(), body_data);
consume_body(copied);
total += copied;
dest = capy::sans_prefix(dest, copied);
dest.remove_prefix(copied);

if(capy::buffer_empty(dest))
if(capy::buffer_empty(dest.data()))
co_return {{}, total};
}

Expand Down
4 changes: 2 additions & 2 deletions include/boost/http/serializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
#include <boost/http/error.hpp>

#include <boost/capy/buffers.hpp>
#include <boost/capy/buffers/buffer_pair.hpp>
#include <boost/capy/concept/buffer_sink.hpp>
#include <boost/capy/concept/write_stream.hpp>
#include <boost/capy/io_task.hpp>
#include <boost/core/span.hpp>
#include <boost/system/result.hpp>

#include <array>
#include <cstddef>
#include <cstring>
#include <type_traits>
Expand Down Expand Up @@ -82,7 +82,7 @@ class serializer
of mutable buffers for streaming.
*/
using mutable_buffers_type =
capy::mutable_buffer_pair;
std::array<capy::mutable_buffer, 2>;

/** The type used to represent a sequence of
constant buffers that refers to the output
Expand Down
4 changes: 1 addition & 3 deletions src/detail/array_of_const_buffers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

#include <boost/http/detail/except.hpp>

#include <boost/capy/buffers/slice.hpp>

namespace boost {
namespace http {
namespace detail {
Expand All @@ -38,7 +36,7 @@ consume(std::size_t n)
auto* p = base_ + pos_;
if(n < p->size())
{
capy::remove_prefix(*p, n);
*p += n;
return;
}
n -= p->size();
Expand Down
43 changes: 32 additions & 11 deletions src/detail/filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,57 @@

#include "src/detail/filter.hpp"

#include <boost/capy/buffers/buffer_slice.hpp>
#include <boost/capy/buffers/front.hpp>

namespace boost {
namespace http {
namespace detail {

namespace {

// Returns true if the slice's current data view contains at most one
// non-empty buffer (i.e., we are processing the last logical chunk).
template<class Slice>
bool single_or_empty(Slice const& s)
{
auto d = s.data();
auto it = d.begin();
auto const end_it = d.end();
if(it == end_it)
return true;
++it;
return it == end_it;
}

} // anonymous

auto
filter::
process(
capy::slice_of<
boost::span<const capy::mutable_buffer>> out,
capy::const_buffer_pair in,
boost::span<const capy::mutable_buffer> out_seq,
std::array<capy::const_buffer, 2> in_seq,
bool more) -> results
{
auto out = capy::buffer_slice(out_seq);
auto in = capy::buffer_slice(in_seq);

results rv;
bool p_more = true;
for(;;)
{
if(!more && p_more && in[1].size() == 0)
if(!more && p_more && single_or_empty(in))
{
if(capy::buffer_size(out) < min_out_buffer())
if(capy::buffer_size(out.data()) < min_out_buffer())
{
rv.out_short = true;
return rv;
}
p_more = false;
}

auto ob = capy::front(out);
auto ib = capy::front(in);
auto ob = capy::front(out.data());
auto ib = capy::front(in.data());
auto rs = do_process(ob, ib, p_more);

rv.in_bytes += rs.in_bytes;
Expand All @@ -57,13 +78,13 @@ process(
return rv;
}

capy::remove_prefix(out, rs.out_bytes);
capy::remove_prefix(in, rs.in_bytes);
out.remove_prefix(rs.out_bytes);
in.remove_prefix(rs.in_bytes);

if(capy::buffer_empty(out))
if(capy::buffer_size(out.data()) == 0)
return rv;

if(capy::buffer_empty(in) && rs.out_bytes < ob.size())
if(capy::buffer_size(in.data()) == 0 && rs.out_bytes < ob.size())
return rv;
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/detail/filter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
#ifndef BOOST_HTTP_DETAIL_FILTER_HPP
#define BOOST_HTTP_DETAIL_FILTER_HPP

#include <boost/capy/buffers/buffer_pair.hpp>
#include <boost/capy/buffers/slice.hpp>
#include <boost/capy/buffers.hpp>
#include <boost/core/span.hpp>
#include <boost/system/error_code.hpp>

#include <array>

namespace boost {
namespace http {
namespace detail {
Expand Down Expand Up @@ -65,9 +66,8 @@ class filter

results
process(
capy::slice_of<
boost::span<const capy::mutable_buffer>> out,
capy::const_buffer_pair in,
boost::span<const capy::mutable_buffer> out,
std::array<capy::const_buffer, 2> in,
bool more);

protected:
Expand Down
44 changes: 35 additions & 9 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include <boost/capy/buffers/buffer_copy.hpp>
#include <boost/capy/buffers/flat_dynamic_buffer.hpp>
#include <boost/capy/buffers/front.hpp>
#include <boost/capy/buffers/slice.hpp>
#include <boost/capy/buffers/buffer_slice.hpp>
#include <boost/capy/ex/system_context.hpp>
#include <boost/http/brotli/decode.hpp>
#include <boost/http/zlib/error.hpp>
Expand All @@ -33,6 +33,7 @@
#include "src/detail/buffer_utils.hpp"
#include "src/detail/zlib_filter_base.hpp"

#include <array>
#include <memory>

namespace boost {
Expand Down Expand Up @@ -123,6 +124,30 @@ Buffer Usage

namespace {

// Construct a 2-element const_buffer pair representing the first
// `n` bytes of `src`. Replaces the pre-#262 `capy::prefix(src, n)`
// idiom which yielded a slice convertible to std::array.
inline std::array<capy::const_buffer, 2>
prefix_pair(
std::array<capy::const_buffer, 2> const& src,
std::size_t n) noexcept
{
std::array<capy::const_buffer, 2> result{};
if(n <= src[0].size())
{
result[0] = capy::const_buffer(src[0].data(), n);
}
else
{
result[0] = src[0];
std::size_t remaining = n - src[0].size();
if(remaining > src[1].size())
remaining = src[1].size();
result[1] = capy::const_buffer(src[1].data(), remaining);
}
return result;
}

class chained_sequence
{
char const* pos_;
Expand All @@ -131,7 +156,7 @@ class chained_sequence
char const* end_b_;

public:
chained_sequence(capy::const_buffer_pair const& cbp)
chained_sequence(std::array<capy::const_buffer, 2> const& cbp)
: pos_(static_cast<char const*>(cbp[0].data()))
, end_(pos_ + cbp[0].size())
, begin_b_(static_cast<char const*>(cbp[1].data()))
Expand Down Expand Up @@ -440,8 +465,8 @@ class parser::impl
capy::circular_dynamic_buffer cb0_;
capy::circular_dynamic_buffer cb1_;

capy::mutable_buffer_pair mbp_;
capy::const_buffer_pair cbp_;
std::array<capy::mutable_buffer, 2> mbp_;
std::array<capy::const_buffer, 2> cbp_;

std::unique_ptr<detail::filter> filter_;

Expand Down Expand Up @@ -1113,8 +1138,9 @@ class parser::impl
{
const std::size_t chunk_avail =
clamp(chunk_remain_, cb0_.size());
const auto chunk =
capy::prefix(cb0_.data(), chunk_avail);
auto cb0_data = cb0_.data();
auto chunk = capy::buffer_slice(
cb0_data, 0, chunk_avail);

if(body_limit_remain() < chunk_avail)
{
Expand All @@ -1127,7 +1153,7 @@ class parser::impl
// in_place style
auto copied = capy::buffer_copy(
cb1_.prepare(cb1_.capacity()),
chunk);
chunk.data());
chunk_remain_ -= copied;
body_avail_ += copied;
body_total_ += copied;
Expand Down Expand Up @@ -1243,7 +1269,7 @@ class parser::impl
return {};
case state::body:
case state::complete:
cbp_ = capy::prefix(
cbp_ = prefix_pair(
(is_plain() ? cb0_ : cb1_).data(),
body_avail_);
return detail::make_span(cbp_);
Expand Down Expand Up @@ -1342,7 +1368,7 @@ class parser::impl

return filter_->process(
detail::make_span(cb1_.prepare(n)),
capy::prefix(cb0_.data(), payload_avail),
prefix_pair(cb0_.data(), payload_avail),
more);
}();

Expand Down
56 changes: 51 additions & 5 deletions src/serializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <boost/http/zlib/error.hpp>
#include <boost/http/zlib/flush.hpp>

#include <array>
#include <memory>
#include <stddef.h>

Expand All @@ -39,6 +40,51 @@ namespace http {

namespace {

// Trim n bytes from the front of a 2-element buffer pair, in place.
// Replaces the pre-#262 `capy::remove_prefix(pair, n)` idiom on a
// 2-element buffer sequence.
template<class Buf>
inline void
trim_prefix_pair(std::array<Buf, 2>& a, std::size_t n) noexcept
{
if(n >= a[0].size())
{
n -= a[0].size();
a[0] = Buf();
if(n >= a[1].size())
a[1] = Buf();
else
a[1] = Buf(
static_cast<char*>(const_cast<void*>(
static_cast<void const*>(a[1].data()))) + n,
a[1].size() - n);
}
else
{
a[0] += n;
}
}

// Trim n bytes from the back of a 2-element buffer pair, in place.
template<class Buf>
inline void
trim_suffix_pair(std::array<Buf, 2>& a, std::size_t n) noexcept
{
if(n >= a[1].size())
{
n -= a[1].size();
a[1] = Buf();
if(n >= a[0].size())
a[0] = Buf();
else
a[0] = Buf(a[0].data(), a[0].size() - n);
}
else
{
a[1] = Buf(a[1].data(), a[1].size() - n);
}
}

const
capy::const_buffer
crlf_and_final_chunk = {"\r\n0\r\n\r\n", 7};
Expand All @@ -64,7 +110,7 @@ chunk_header_len(

void
write_chunk_header(
const capy::mutable_buffer_pair& mbs,
const std::array<capy::mutable_buffer, 2>& mbs,
std::size_t size) noexcept
{
static constexpr char hexdig[] =
Expand Down Expand Up @@ -584,7 +630,7 @@ class serializer::impl
return out_capacity();
}

capy::mutable_buffer_pair
std::array<capy::mutable_buffer, 2>
stream_prepare()
{
if(state_ == state::start)
Expand Down Expand Up @@ -667,15 +713,15 @@ class serializer::impl
detail::throw_length_error();
}

capy::mutable_buffer_pair
std::array<capy::mutable_buffer, 2>
out_prepare() noexcept
{
auto mbp = out_.prepare(out_.capacity());
if(is_chunked_)
{
capy::remove_prefix(
trim_prefix_pair(
mbp, chunk_header_len_);
capy::remove_suffix(
trim_suffix_pair(
mbp, crlf_and_final_chunk.size());
}
return mbp;
Expand Down
Loading
Loading