Skip to content

Commit c584084

Browse files
committed
serializer handles empty buffer sequence in chunked bodies
1 parent 6eac235 commit c584084

2 files changed

Lines changed: 31 additions & 17 deletions

File tree

src/serializer.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ write_crlf(
8686
{
8787
auto n = buffers::copy(
8888
mbs,
89-
buffers::const_buffer(
90-
"\r\n", 2));
89+
buffers::const_buffer("\r\n", 2));
9190
ignore_unused(n);
9291
BOOST_ASSERT(n == 2);
9392
}
@@ -794,19 +793,21 @@ start_buffers(
794793
// start_init() already called
795794
style_ = style::buffers;
796795

797-
const auto buffers_max = (std::min)(
798-
std::size_t{ 16 },
799-
buf_gen_->count());
800-
801796
if(!filter_)
802797
{
798+
// limit the batch size. any remaining buffers will
799+
// be appended to incrementally in subsequent calls
800+
// to the prepare function.
801+
const auto batch_size = (std::min)(
802+
std::size_t{ 16 },
803+
buf_gen_->count());
804+
805+
// none-chunked
803806
if(!is_chunked_)
804807
{
805-
// no filter and no chunked
806-
807808
prepped_ = make_array(
808-
1 + // header
809-
buffers_max ); // buffers
809+
1 + // header
810+
batch_size); // buffers
810811

811812
prepped_[0] = { m.ph_->cbuf, m.ph_->size };
812813
std::generate(
@@ -817,9 +818,8 @@ start_buffers(
817818
return;
818819
}
819820

820-
// no filter and chunked
821-
822-
if(buf_gen_->is_empty())
821+
// chunked with length 0
822+
if(batch_size == 0)
823823
{
824824
prepped_ = make_array(
825825
1 + // header
@@ -838,8 +838,8 @@ start_buffers(
838838
return;
839839
}
840840

841-
// Write entire buffers as a single chunk
842-
// since total size is known
841+
// write all buffers as a single chunk, since
842+
// the total size is known in advance.
843843

844844
auto const buf_size = buf_gen_->size();
845845
auto const header_len =
@@ -871,7 +871,7 @@ start_buffers(
871871
prepped_ = make_array(
872872
1 + // header
873873
1 + // chunk header
874-
buffers_max + // buffers
874+
batch_size + // buffers
875875
1); // buffer or (crlf and final chunk)
876876

877877
prepped_[0] = { m.ph_->cbuf, m.ph_->size };
@@ -882,7 +882,7 @@ start_buffers(
882882
[this](){ return buf_gen_->next(); });
883883

884884
more_input_ = !buf_gen_->is_empty();
885-
// assigning the last slot
885+
// assign the last slot
886886
if(more_input_)
887887
{
888888
prepped_[prepped_.size() - 1] =

test/unit/serializer.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,20 @@ struct serializer_test
535535
std::string(2048, '*') +
536536
std::string("\r\n0\r\n\r\n"));
537537

538+
// buffers chunked empty
539+
check_buffers(
540+
"HTTP/1.1 200 OK\r\n"
541+
"Server: test\r\n"
542+
"Transfer-Encoding: chunked\r\n"
543+
"\r\n",
544+
"",
545+
//--------------------------
546+
"HTTP/1.1 200 OK\r\n"
547+
"Server: test\r\n"
548+
"Transfer-Encoding: chunked\r\n"
549+
"\r\n",
550+
"0\r\n\r\n");
551+
538552
// source
539553
check_src(
540554
"HTTP/1.1 200 OK\r\n"

0 commit comments

Comments
 (0)