Skip to content

Commit b4db8fa

Browse files
committed
parser accepts elastic and sink bodies
1 parent ef6753a commit b4db8fa

File tree

6 files changed

+859
-846
lines changed

6 files changed

+859
-846
lines changed

include/boost/http_proto/file_body.hpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include <boost/http_proto/detail/config.hpp>
1414
#include <boost/http_proto/file.hpp>
15+
#include <boost/http_proto/sink.hpp>
1516
#include <boost/http_proto/source.hpp>
1617
#include <cstdint>
1718

@@ -20,7 +21,7 @@ namespace http_proto {
2021

2122
class BOOST_SYMBOL_VISIBLE
2223
file_body
23-
: public source
24+
: public source, public sink
2425
{
2526
file f_;
2627
std::uint64_t n_;
@@ -45,9 +46,14 @@ class BOOST_SYMBOL_VISIBLE
4546
std::uint64_t(-1)) noexcept;
4647

4748
BOOST_HTTP_PROTO_DECL
48-
results
49+
source::results
4950
on_read(
5051
buffers::mutable_buffer b) override;
52+
53+
BOOST_HTTP_PROTO_DECL
54+
sink::results
55+
on_write(
56+
buffers::const_buffer b, bool more) override;
5157
};
5258

5359
} // http_proto

include/boost/http_proto/parser.hpp

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,23 @@
1212
#define BOOST_HTTP_PROTO_PARSER_HPP
1313

1414
#include <boost/http_proto/detail/config.hpp>
15-
#include <boost/http_proto/error.hpp>
16-
#include <boost/http_proto/header_limits.hpp>
17-
#include <boost/http_proto/sink.hpp>
1815
#include <boost/http_proto/detail/header.hpp>
1916
#include <boost/http_proto/detail/type_traits.hpp>
2017
#include <boost/http_proto/detail/workspace.hpp>
18+
#include <boost/http_proto/error.hpp>
19+
#include <boost/http_proto/header_limits.hpp>
20+
#include <boost/http_proto/sink.hpp>
21+
22+
#include <boost/buffers/any_dynamic_buffer.hpp>
2123
#include <boost/buffers/circular_buffer.hpp>
2224
#include <boost/buffers/flat_buffer.hpp>
2325
#include <boost/buffers/mutable_buffer_pair.hpp>
2426
#include <boost/buffers/mutable_buffer_span.hpp>
2527
#include <boost/buffers/type_traits.hpp>
26-
#include <boost/buffers/any_dynamic_buffer.hpp>
2728
#include <boost/url/grammar/error.hpp>
29+
2830
#include <cstddef>
2931
#include <cstdint>
30-
#include <functional>
31-
#include <memory>
32-
#include <utility>
3332

3433
namespace boost {
3534
namespace http_proto {
@@ -185,7 +184,7 @@ class BOOST_SYMBOL_VISIBLE
185184
bool
186185
is_complete() const noexcept
187186
{
188-
return st_ == state::complete;
187+
return st_ >= state::complete_in_place;
189188
}
190189

191190
/** Returns `true` if the end of the stream was reached.
@@ -209,8 +208,7 @@ class BOOST_SYMBOL_VISIBLE
209208
{
210209
return
211210
st_ == state::reset ||
212-
( st_ == state::complete &&
213-
got_eof_);
211+
(st_ >= state::complete_in_place && got_eof_);
214212
}
215213

216214
//--------------------------------------------
@@ -366,38 +364,50 @@ class BOOST_SYMBOL_VISIBLE
366364
friend class response_parser;
367365

368366
detail::header const*
369-
safe_get_header() const;
370-
bool is_plain() const noexcept;
371-
void on_headers(system::error_code&);
372-
BOOST_HTTP_PROTO_DECL void on_set_body();
373-
void init_dynamic(system::error_code&);
367+
safe_get_header() const;
368+
369+
bool
370+
is_plain() const noexcept;
371+
372+
void
373+
on_headers(system::error_code&);
374+
375+
BOOST_HTTP_PROTO_DECL
376+
void
377+
on_set_body();
378+
379+
std::size_t
380+
apply_filter(
381+
system::error_code&,
382+
std::size_t,
383+
bool);
374384

375385
static constexpr unsigned buffers_N = 8;
376386

377387
enum class state
378388
{
379-
// order matters
380389
reset,
381390
start,
382391
header,
383392
body,
384393
set_body,
394+
complete_in_place,
385395
complete
386396
};
387397

388398
enum class how
389399
{
390400
in_place,
401+
sink,
391402
elastic,
392-
sink
393403
};
394404

395405
context& ctx_;
396406
parser_service& svc_;
397407

398408
detail::workspace ws_;
399409
detail::header h_;
400-
std::uint64_t body_avail_ = 0;
410+
std::size_t body_avail_ = 0;
401411
std::uint64_t body_total_ = 0;
402412
std::uint64_t payload_remain_ = 0;
403413
std::uint64_t chunk_remain_ = 0;
@@ -421,18 +431,17 @@ class BOOST_SYMBOL_VISIBLE
421431
// `const_buffers_type` from relevant functions
422432
buffers::const_buffer_pair cbp_;
423433

424-
buffers::circular_buffer* body_buf_ = nullptr;
425434
buffers::any_dynamic_buffer* eb_ = nullptr;
426435
detail::filter* filter_ = nullptr;
427436
sink* sink_ = nullptr;
428437

429438
state st_ = state::start;
430439
how how_ = how::in_place;
431440
bool got_eof_ = false;
432-
// bool need_more_;
433441
bool head_response_ = false;
434442
bool needs_chunk_close_ = false;
435443
bool trailer_headers_ = false;
444+
bool chunked_body_ended = false;
436445
};
437446

438447
//------------------------------------------------

src/file_body.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ auto
3434
file_body::
3535
on_read(
3636
buffers::mutable_buffer b) ->
37-
results
37+
source::results
3838
{
39-
results rv;
39+
source::results rv;
4040
if(n_ > 0)
4141
{
4242
std::size_t n;
@@ -53,5 +53,27 @@ on_read(
5353
return rv;
5454
}
5555

56+
auto
57+
file_body::
58+
on_write(
59+
buffers::const_buffer b, bool) ->
60+
sink::results
61+
{
62+
sink::results rv;
63+
if(n_ > 0)
64+
{
65+
std::size_t n;
66+
if( n_ >= b.size())
67+
n = b.size();
68+
else
69+
n = static_cast<std::size_t>(n_);
70+
n = f_.write(
71+
b.data(), n, rv.ec);
72+
rv.bytes = n;
73+
n_ -= n;
74+
}
75+
return rv;
76+
}
77+
5678
} // http_proto
5779
} // boost

0 commit comments

Comments
 (0)