Skip to content

Commit

Permalink
parser accepts elastic and sink bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
ashtum committed Jan 5, 2025
1 parent 0cb9598 commit 5e7b2e0
Show file tree
Hide file tree
Showing 6 changed files with 859 additions and 846 deletions.
10 changes: 8 additions & 2 deletions include/boost/http_proto/file_body.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include <boost/http_proto/detail/config.hpp>
#include <boost/http_proto/file.hpp>
#include <boost/http_proto/sink.hpp>
#include <boost/http_proto/source.hpp>
#include <cstdint>

Expand All @@ -20,7 +21,7 @@ namespace http_proto {

class BOOST_SYMBOL_VISIBLE
file_body
: public source
: public source, public sink
{
file f_;
std::uint64_t n_;
Expand All @@ -45,9 +46,14 @@ class BOOST_SYMBOL_VISIBLE
std::uint64_t(-1)) noexcept;

BOOST_HTTP_PROTO_DECL
results
source::results
on_read(
buffers::mutable_buffer b) override;

BOOST_HTTP_PROTO_DECL
sink::results
on_write(
buffers::const_buffer b, bool more) override;
};

} // http_proto
Expand Down
49 changes: 29 additions & 20 deletions include/boost/http_proto/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,23 @@
#define BOOST_HTTP_PROTO_PARSER_HPP

#include <boost/http_proto/detail/config.hpp>
#include <boost/http_proto/error.hpp>
#include <boost/http_proto/header_limits.hpp>
#include <boost/http_proto/sink.hpp>
#include <boost/http_proto/detail/header.hpp>
#include <boost/http_proto/detail/type_traits.hpp>
#include <boost/http_proto/detail/workspace.hpp>
#include <boost/http_proto/error.hpp>
#include <boost/http_proto/header_limits.hpp>
#include <boost/http_proto/sink.hpp>

#include <boost/buffers/any_dynamic_buffer.hpp>
#include <boost/buffers/circular_buffer.hpp>
#include <boost/buffers/flat_buffer.hpp>
#include <boost/buffers/mutable_buffer_pair.hpp>
#include <boost/buffers/mutable_buffer_span.hpp>
#include <boost/buffers/type_traits.hpp>
#include <boost/buffers/any_dynamic_buffer.hpp>
#include <boost/url/grammar/error.hpp>

#include <cstddef>
#include <cstdint>
#include <functional>
#include <memory>
#include <utility>

namespace boost {
namespace http_proto {
Expand Down Expand Up @@ -185,7 +184,7 @@ class BOOST_SYMBOL_VISIBLE
bool
is_complete() const noexcept
{
return st_ == state::complete;
return st_ >= state::complete_in_place;
}

/** Returns `true` if the end of the stream was reached.
Expand All @@ -209,8 +208,7 @@ class BOOST_SYMBOL_VISIBLE
{
return
st_ == state::reset ||
( st_ == state::complete &&
got_eof_);
(st_ >= state::complete_in_place && got_eof_);
}

//--------------------------------------------
Expand Down Expand Up @@ -366,38 +364,50 @@ class BOOST_SYMBOL_VISIBLE
friend class response_parser;

detail::header const*
safe_get_header() const;
bool is_plain() const noexcept;
void on_headers(system::error_code&);
BOOST_HTTP_PROTO_DECL void on_set_body();
void init_dynamic(system::error_code&);
safe_get_header() const;

bool
is_plain() const noexcept;

void
on_headers(system::error_code&);

BOOST_HTTP_PROTO_DECL
void
on_set_body();

std::size_t
apply_filter(
system::error_code&,
std::size_t,
bool);

static constexpr unsigned buffers_N = 8;

enum class state
{
// order matters
reset,
start,
header,
body,
set_body,
complete_in_place,
complete
};

enum class how
{
in_place,
sink,
elastic,
sink
};

context& ctx_;
parser_service& svc_;

detail::workspace ws_;
detail::header h_;
std::uint64_t body_avail_ = 0;
std::size_t body_avail_ = 0;
std::uint64_t body_total_ = 0;
std::uint64_t payload_remain_ = 0;
std::uint64_t chunk_remain_ = 0;
Expand All @@ -421,18 +431,17 @@ class BOOST_SYMBOL_VISIBLE
// `const_buffers_type` from relevant functions
buffers::const_buffer_pair cbp_;

buffers::circular_buffer* body_buf_ = nullptr;
buffers::any_dynamic_buffer* eb_ = nullptr;
detail::filter* filter_ = nullptr;
sink* sink_ = nullptr;

state st_ = state::start;
how how_ = how::in_place;
bool got_eof_ = false;
// bool need_more_;
bool head_response_ = false;
bool needs_chunk_close_ = false;
bool trailer_headers_ = false;
bool chunked_body_ended = false;
};

//------------------------------------------------
Expand Down
26 changes: 24 additions & 2 deletions src/file_body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ auto
file_body::
on_read(
buffers::mutable_buffer b) ->
results
source::results
{
results rv;
source::results rv;
if(n_ > 0)
{
std::size_t n;
Expand All @@ -53,5 +53,27 @@ on_read(
return rv;
}

auto
file_body::
on_write(
buffers::const_buffer b, bool) ->
sink::results
{
sink::results rv;
if(n_ > 0)
{
std::size_t n;
if( n_ >= b.size())
n = b.size();
else
n = static_cast<std::size_t>(n_);
n = f_.write(
b.data(), n, rv.ec);
rv.bytes = n;
n_ -= n;
}
return rv;
}

} // http_proto
} // boost
Loading

0 comments on commit 5e7b2e0

Please sign in to comment.