Skip to content

Commit 56ffd3b

Browse files
committed
parser constructs sink in-place
1 parent 72b6a95 commit 56ffd3b

File tree

6 files changed

+29
-40
lines changed

6 files changed

+29
-40
lines changed

include/boost/http_proto/impl/parser.hpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,13 @@ set_body(
9191

9292
//------------------------------------------------
9393

94-
template<class Sink>
95-
typename std::enable_if<
96-
is_sink<Sink>::value,
97-
typename std::decay<Sink>::type
98-
>::type&
94+
template<
95+
class Sink,
96+
class... Args,
97+
class>
98+
Sink&
9999
parser::
100-
set_body(
101-
Sink&& sink)
100+
set_body(Args&&... args)
102101
{
103102
// body must not be set already
104103
if(how_ != how::in_place)
@@ -109,7 +108,7 @@ set_body(
109108
detail::throw_logic_error();
110109

111110
auto& s = ws_.emplace<Sink>(
112-
std::forward<Sink>(sink));
111+
std::forward<Args>(args)...);
113112
sink_ = &s;
114113
how_ = how::sink;
115114
on_set_body();

include/boost/http_proto/parser.hpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -312,16 +312,16 @@ class BOOST_SYMBOL_VISIBLE
312312

313313
/** Attach a body
314314
*/
315-
template<class Sink>
315+
template<
316+
class Sink,
317+
class... Args
316318
#ifndef BOOST_HTTP_PROTO_DOCS
317-
typename std::enable_if<
318-
is_sink<Sink>::value,
319-
typename std::decay<Sink>::type
320-
>::type&
321-
#else
322-
typename std::decay<Sink>::type&
319+
,class = typename std::enable_if<
320+
is_sink<Sink>::value>::type
323321
#endif
324-
set_body(Sink&& sink);
322+
>
323+
Sink&
324+
set_body(Args&&... args);
325325

326326
/** Return the available body data.
327327
@@ -374,7 +374,7 @@ class BOOST_SYMBOL_VISIBLE
374374

375375
BOOST_HTTP_PROTO_DECL
376376
void
377-
on_set_body();
377+
on_set_body() noexcept;
378378

379379
std::size_t
380380
apply_filter(

include/boost/http_proto/sink.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ namespace http_proto {
3434
*/
3535
struct BOOST_HTTP_PROTO_DECL
3636
sink
37-
: buffered_base
3837
{
3938
/** The results of consuming data.
4039
*/

src/parser.cpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,24 +1492,16 @@ on_headers(
14921492
// Called at the end of set_body
14931493
void
14941494
parser::
1495-
on_set_body()
1495+
on_set_body() noexcept
14961496
{
1497-
// This function is called after all
1498-
// limit checking and calculation of
1499-
// chunked or filter.
1500-
1501-
BOOST_ASSERT(got_header());
1497+
BOOST_ASSERT(
1498+
st_ == state::complete_in_place ||
1499+
st_ == state::body);
15021500

15031501
nprepare_ = 0; // invalidate
15041502

15051503
if(st_ == state::body)
1506-
{
15071504
st_ = state::set_body;
1508-
return;
1509-
}
1510-
1511-
BOOST_ASSERT(
1512-
st_ == state::complete_in_place);
15131505
}
15141506

15151507
std::size_t

test/unit/parser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1336,7 +1336,7 @@ struct parser_test
13361336
BOOST_TEST_EQ(ec, ex);
13371337
return;
13381338
}
1339-
auto& ts = pr_->set_body(test_sink{});
1339+
auto& ts = pr_->set_body<test_sink>();
13401340
pr_->parse(ec);
13411341
BOOST_TEST(pr_->body().empty());
13421342
if(! pr_->is_complete())

test/unit/zlib.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,6 @@ struct zlib_test
616616
response_parser& pr,
617617
buffers::const_buffer input)
618618
{
619-
std::string rs;
620619
std::size_t n1 = buffers::buffer_copy(
621620
pr.prepare(), input);
622621
input = buffers::sans_prefix(input, n1);
@@ -627,30 +626,30 @@ struct zlib_test
627626

628627
class sink_t : public sink
629628
{
630-
std::string* body_;
629+
std::string body_;
631630

632631
public:
633-
sink_t(std::string* body)
634-
: body_{ body }
632+
std::string
633+
get_body()
635634
{
635+
return body_;
636636
}
637637

638638
results
639639
on_write(
640640
buffers::const_buffer b,
641641
bool) override
642642
{
643-
body_->append(
644-
static_cast<
645-
const char*>(b.data()),
643+
body_.append(
644+
static_cast<const char*>(b.data()),
646645
b.size());
647646
results rv;
648647
rv.bytes = b.size();
649648
return rv;
650649
}
651650
};
652651

653-
pr.set_body<sink_t>(&rs);
652+
auto& sink = pr.set_body<sink_t>();
654653
pr.parse(ec);
655654

656655
while(ec == error::need_data)
@@ -667,7 +666,7 @@ struct zlib_test
667666
break;
668667
}
669668
}
670-
return rs;
669+
return sink.get_body();
671670
}
672671

673672
void

0 commit comments

Comments
 (0)