Skip to content

Commit

Permalink
parser constructs sink in-place
Browse files Browse the repository at this point in the history
  • Loading branch information
ashtum committed Jan 7, 2025
1 parent 72b6a95 commit cfc59eb
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 40 deletions.
15 changes: 7 additions & 8 deletions include/boost/http_proto/impl/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,13 @@ set_body(

//------------------------------------------------

template<class Sink>
typename std::enable_if<
is_sink<Sink>::value,
typename std::decay<Sink>::type
>::type&
template<
class Sink,
class... Args,
class>
Sink&
parser::
set_body(
Sink&& sink)
set_body(Args&&... args)
{
// body must not be set already
if(how_ != how::in_place)
Expand All @@ -109,7 +108,7 @@ set_body(
detail::throw_logic_error();

auto& s = ws_.emplace<Sink>(
std::forward<Sink>(sink));
std::forward<Args>(args)...);
sink_ = &s;
how_ = how::sink;
on_set_body();
Expand Down
18 changes: 9 additions & 9 deletions include/boost/http_proto/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,16 +312,16 @@ class BOOST_SYMBOL_VISIBLE

/** Attach a body
*/
template<class Sink>
template<
class Sink,
class... Args
#ifndef BOOST_HTTP_PROTO_DOCS
typename std::enable_if<
is_sink<Sink>::value,
typename std::decay<Sink>::type
>::type&
#else
typename std::decay<Sink>::type&
,class = typename std::enable_if<
is_sink<Sink>::value>::type
#endif
set_body(Sink&& sink);
>
Sink&
set_body(Args&&... args);

/** Return the available body data.
Expand Down Expand Up @@ -374,7 +374,7 @@ class BOOST_SYMBOL_VISIBLE

BOOST_HTTP_PROTO_DECL
void
on_set_body();
on_set_body() noexcept;

std::size_t
apply_filter(
Expand Down
1 change: 0 additions & 1 deletion include/boost/http_proto/sink.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ namespace http_proto {
*/
struct BOOST_HTTP_PROTO_DECL
sink
: buffered_base
{
/** The results of consuming data.
*/
Expand Down
16 changes: 4 additions & 12 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1492,24 +1492,16 @@ on_headers(
// Called at the end of set_body
void
parser::
on_set_body()
on_set_body() noexcept
{
// This function is called after all
// limit checking and calculation of
// chunked or filter.

BOOST_ASSERT(got_header());
BOOST_ASSERT(
st_ == state::complete_in_place ||
st_ == state::body);

nprepare_ = 0; // invalidate

if(st_ == state::body)
{
st_ = state::set_body;
return;
}

BOOST_ASSERT(
st_ == state::complete_in_place);
}

std::size_t
Expand Down
2 changes: 1 addition & 1 deletion test/unit/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1336,7 +1336,7 @@ struct parser_test
BOOST_TEST_EQ(ec, ex);
return;
}
auto& ts = pr_->set_body(test_sink{});
auto& ts = pr_->set_body<test_sink>();
pr_->parse(ec);
BOOST_TEST(pr_->body().empty());
if(! pr_->is_complete())
Expand Down
17 changes: 8 additions & 9 deletions test/unit/zlib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,6 @@ struct zlib_test
response_parser& pr,
buffers::const_buffer input)
{
std::string rs;
std::size_t n1 = buffers::buffer_copy(
pr.prepare(), input);
input = buffers::sans_prefix(input, n1);
Expand All @@ -627,30 +626,30 @@ struct zlib_test

class sink_t : public sink
{
std::string* body_;
std::string body_;

public:
sink_t(std::string* body)
: body_{ body }
std::string
get_body()
{
return body_;
}

results
on_write(
buffers::const_buffer b,
bool) override
{
body_->append(
static_cast<
const char*>(b.data()),
body_.append(
static_cast<const char*>(b.data()),
b.size());
results rv;
rv.bytes = b.size();
return rv;
}
};

pr.set_body<sink_t>(&rs);
auto& sink = pr.set_body<sink_t>();
pr.parse(ec);

while(ec == error::need_data)
Expand All @@ -667,7 +666,7 @@ struct zlib_test
break;
}
}
return rs;
return sink.get_body();
}

void
Expand Down

0 comments on commit cfc59eb

Please sign in to comment.