Skip to content

Commit 7b48277

Browse files
committed
serializer does not partial flush
1 parent fc3a245 commit 7b48277

File tree

8 files changed

+188
-195
lines changed

8 files changed

+188
-195
lines changed

include/boost/http_proto/serializer.hpp

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -434,32 +434,36 @@ class serializer::stream
434434
return *this;
435435
}
436436

437-
/** Returns `true` if the stream is open
437+
/** Return true if the stream is open
438438
*/
439439
BOOST_HTTP_PROTO_DECL
440440
bool
441441
is_open() const noexcept;
442442

443-
/** Returns the available capacity
443+
/** Return the available capacity
444+
445+
@throw std::logic_error if `!is_open()`.
444446
*/
445447
BOOST_HTTP_PROTO_DECL
446448
std::size_t
447-
capacity() const noexcept;
449+
capacity() const;
448450

449-
/** Prepares a buffer for writing
451+
/** Prepare a buffer for writing
450452
451453
Use @ref commit to make the written data available
452454
to the serializer.
453455
454456
@return An object of type @ref mutable_buffers_type
455457
that satisfies MutableBufferSequence requirements,
456458
the underlying memory is owned by the serializer.
459+
460+
@throw std::logic_error if `!is_open()`.
457461
*/
458462
BOOST_HTTP_PROTO_DECL
459463
mutable_buffers_type
460-
prepare() noexcept;
464+
prepare();
461465

462-
/** Commits data to the serializer
466+
/** Commit data to the serializer
463467
464468
@param n Number of bytes to commit.
465469
@@ -470,12 +474,19 @@ class serializer::stream
470474
void
471475
commit(std::size_t n);
472476

473-
/** Closes the stream
477+
/** Close the stream if open
474478
*/
475479
BOOST_HTTP_PROTO_DECL
476480
void
477481
close();
478482

483+
/** Destructor
484+
485+
Closes the stream if open.
486+
*/
487+
BOOST_HTTP_PROTO_DECL
488+
~stream();
489+
479490
private:
480491
friend class serializer;
481492

@@ -494,7 +505,7 @@ class serializer::stream
494505
class serializer::const_buf_gen_base
495506
{
496507
public:
497-
// Returns the next non-empty buffer,
508+
// Return the next non-empty buffer,
498509
// or an empty buffer if none remain.
499510
virtual
500511
buffers::const_buffer
@@ -510,7 +521,7 @@ class serializer::const_buf_gen_base
510521
std::size_t
511522
count() const = 0;
512523

513-
// Returns true when there is no buffer or
524+
// Return true when there is no buffer or
514525
// the remaining buffers are empty
515526
virtual
516527
bool
@@ -526,6 +537,7 @@ class serializer::const_buf_gen
526537

527538
ConstBufferSequence cbs_;
528539
it_t current_;
540+
529541
public:
530542
using const_buffer =
531543
buffers::const_buffer;
@@ -556,8 +568,10 @@ class serializer::const_buf_gen
556568
current_,
557569
buffers::end(cbs_),
558570
std::size_t{},
559-
[](std::size_t sum, const_buffer cb) {
560-
return sum + cb.size(); });
571+
[](std::size_t sum, const_buffer cb)
572+
{
573+
return sum + cb.size();
574+
});
561575
}
562576

563577
std::size_t
@@ -566,8 +580,10 @@ class serializer::const_buf_gen
566580
return std::count_if(
567581
current_,
568582
buffers::end(cbs_),
569-
[](const_buffer cb) {
570-
return cb.size() != 0; });
583+
[](const_buffer cb)
584+
{
585+
return cb.size() != 0;
586+
});
571587
}
572588

573589
bool
@@ -576,8 +592,10 @@ class serializer::const_buf_gen
576592
return std::all_of(
577593
current_,
578594
buffers::end(cbs_),
579-
[](const_buffer cb) {
580-
return cb.size() == 0; });
595+
[](const_buffer cb)
596+
{
597+
return cb.size() == 0;
598+
});
581599
}
582600
};
583601

src/detail/filter.cpp

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,25 @@ filter::
2323
process(
2424
buffers::mutable_buffer_subspan out,
2525
buffers::const_buffer_pair in,
26-
bool more,
27-
bool partial_flush) -> results
26+
bool more) -> results
2827
{
2928
results rv;
30-
auto flush = filter::flush::none;
29+
bool p_more = true;
3130
for(;;)
3231
{
33-
if(!more && flush != filter::flush::finish && in[1].size() == 0)
32+
if(!more && p_more && in[1].size() == 0)
3433
{
3534
if(buffers::size(out) < min_out_buffer())
3635
{
3736
rv.out_short = true;
3837
return rv;
3938
}
40-
flush = filter::flush::finish;
39+
p_more = false;
4140
}
4241

4342
auto ob = buffers::front(out);
4443
auto ib = buffers::front(in);
45-
auto rs = do_process(ob, ib, flush);
44+
auto rs = do_process(ob, ib, p_more);
4645

4746
rv.in_bytes += rs.in_bytes;
4847
rv.out_bytes += rs.out_bytes;
@@ -66,14 +65,7 @@ process(
6665
return rv;
6766

6867
if(buffers::size(in) == 0 && rs.out_bytes < ob.size())
69-
{
70-
if(partial_flush && rv.out_bytes == 0)
71-
{
72-
flush = filter::flush::partial;
73-
continue;
74-
}
7568
return rv;
76-
}
7769
}
7870
}
7971

src/detail/filter.hpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,9 @@ class filter
6464
process(
6565
buffers::mutable_buffer_subspan out,
6666
buffers::const_buffer_pair in,
67-
bool more,
68-
bool partial_flush = false);
67+
bool more);
6968

7069
protected:
71-
enum class flush
72-
{
73-
none,
74-
partial,
75-
finish
76-
};
77-
7870
virtual
7971
std::size_t
8072
min_out_buffer() const noexcept
@@ -87,7 +79,7 @@ class filter
8779
do_process(
8880
buffers::mutable_buffer,
8981
buffers::const_buffer,
90-
flush) noexcept = 0;
82+
bool) noexcept = 0;
9183
};
9284

9385
} // detail

src/detail/zlib_filter_base.hpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,6 @@ class zlib_filter_base : public filter
3737
protected:
3838
rts::zlib::stream strm_;
3939

40-
static
41-
rts::zlib::flush
42-
translate(filter::flush flush) noexcept
43-
{
44-
switch(flush)
45-
{
46-
case filter::flush::none:
47-
return rts::zlib::flush::no_flush;
48-
49-
case filter::flush::partial:
50-
return rts::zlib::flush::block;
51-
52-
case filter::flush::finish:
53-
default:
54-
return rts::zlib::flush::finish;
55-
}
56-
}
57-
5840
static
5941
unsigned int
6042
saturate_cast(std::size_t n) noexcept

src/parser.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,15 +322,17 @@ class zlib_filter
322322
do_process(
323323
buffers::mutable_buffer out,
324324
buffers::const_buffer in,
325-
filter::flush flush) noexcept override
325+
bool more) noexcept override
326326
{
327327
strm_.next_out = static_cast<unsigned char*>(out.data());
328328
strm_.avail_out = saturate_cast(out.size());
329329
strm_.next_in = static_cast<unsigned char*>(const_cast<void *>(in.data()));
330330
strm_.avail_in = saturate_cast(in.size());
331331

332332
auto rs = static_cast<rts::zlib::error>(
333-
svc_.inflate(strm_, translate(flush)));
333+
svc_.inflate(
334+
strm_,
335+
more ? rts::zlib::no_flush : rts::zlib::finish));
334336

335337
results rv;
336338
rv.out_bytes = saturate_cast(out.size()) - strm_.avail_out;
@@ -374,7 +376,7 @@ class brotli_filter
374376
do_process(
375377
buffers::mutable_buffer out,
376378
buffers::const_buffer in,
377-
filter::flush flush) noexcept override
379+
bool more) noexcept override
378380
{
379381
auto* next_in = reinterpret_cast<const std::uint8_t*>(in.data());
380382
auto available_in = in.size();
@@ -394,7 +396,7 @@ class brotli_filter
394396
rv.out_bytes = out.size() - available_out;
395397
rv.finished = svc_.is_finished(state_);
396398

397-
if(rs == rts::brotli::decoder_result::needs_more_input && flush == filter::flush::finish)
399+
if(!more && rs == rts::brotli::decoder_result::needs_more_input)
398400
rv.ec = BOOST_HTTP_PROTO_ERR(error::bad_payload);
399401

400402
if(rs == rts::brotli::decoder_result::error)

0 commit comments

Comments
 (0)