Skip to content

Commit defb4f2

Browse files
author
casaserv
committed
Our linux http_client was using a separate streambuf for both the request and the response. This was entirely unnecessary since they are only used one at a time so the same one can be re-used. This saves a streambuf per request.
1 parent 136c653 commit defb4f2

File tree

1 file changed

+36
-36
lines changed

1 file changed

+36
-36
lines changed

Release/src/http/client/http_linux.cpp

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,7 @@ namespace web { namespace http
9797
size_t m_current_size;
9898
bool m_needChunked;
9999
bool m_timedout;
100-
boost::asio::streambuf m_request_buf;
101-
boost::asio::streambuf m_response_buf;
100+
boost::asio::streambuf m_body_buf;
102101
std::unique_ptr<boost::asio::deadline_timer> m_timer;
103102

104103
template <typename socket_type>
@@ -216,7 +215,7 @@ namespace web { namespace http
216215
}
217216

218217
const auto &host = m_uri.host();
219-
std::ostream request_stream(&ctx->m_request_buf);
218+
std::ostream request_stream(&ctx->m_body_buf);
220219

221220
request_stream << method << " " << encoded_resource << " " << "HTTP/1.1" << CRLF << "Host: " << host;
222221

@@ -338,7 +337,7 @@ namespace web { namespace http
338337
}
339338
else
340339
{
341-
boost::asio::async_write(*ctx->m_socket, ctx->m_request_buf, boost::bind(&linux_client::handle_write_request, this, boost::asio::placeholders::error, ctx));
340+
boost::asio::async_write(*ctx->m_socket, ctx->m_body_buf, boost::bind(&linux_client::handle_write_request, this, boost::asio::placeholders::error, ctx));
342341
}
343342
}
344343
else if (endpoints == tcp::resolver::iterator())
@@ -384,10 +383,11 @@ namespace web { namespace http
384383
void handle_handshake(const boost::system::error_code& ec, std::shared_ptr<linux_request_context> ctx)
385384
{
386385
if (!ec)
387-
boost::asio::async_write(*ctx->m_ssl_stream, ctx->m_request_buf, boost::bind(&linux_client::handle_write_request, this, boost::asio::placeholders::error, ctx));
386+
boost::asio::async_write(*ctx->m_ssl_stream, ctx->m_body_buf, boost::bind(&linux_client::handle_write_request, this, boost::asio::placeholders::error, ctx));
388387
else
389388
ctx->report_error("Error code in handle_handshake is ", ec, httpclient_errorcode_context::handshake);
390389
}
390+
391391
void handle_write_chunked_body(const boost::system::error_code& ec, std::shared_ptr<linux_request_context> ctx)
392392
{
393393
if (ec)
@@ -404,7 +404,7 @@ namespace web { namespace http
404404
}
405405

406406
auto readbuf = ctx->_get_readbuffer();
407-
uint8_t *buf = boost::asio::buffer_cast<uint8_t *>(ctx->m_request_buf.prepare(client_config().chunksize() + http::details::chunked_encoding::additional_encoding_space));
407+
uint8_t *buf = boost::asio::buffer_cast<uint8_t *>(ctx->m_body_buf.prepare(client_config().chunksize() + http::details::chunked_encoding::additional_encoding_space));
408408
readbuf.getn(buf + http::details::chunked_encoding::data_offset, client_config().chunksize()).then([=](pplx::task<size_t> op)
409409
{
410410
size_t readSize = 0;
@@ -415,15 +415,15 @@ namespace web { namespace http
415415
return;
416416
}
417417
size_t offset = http::details::chunked_encoding::add_chunked_delimiters(buf, client_config().chunksize() + http::details::chunked_encoding::additional_encoding_space, readSize);
418-
ctx->m_request_buf.commit(readSize + http::details::chunked_encoding::additional_encoding_space);
419-
ctx->m_request_buf.consume(offset);
418+
ctx->m_body_buf.commit(readSize + http::details::chunked_encoding::additional_encoding_space);
419+
ctx->m_body_buf.consume(offset);
420420
ctx->m_current_size += readSize;
421421
ctx->m_uploaded += (size64_t)readSize;
422422
if (ctx->m_ssl_stream)
423-
boost::asio::async_write(*ctx->m_ssl_stream, ctx->m_request_buf,
423+
boost::asio::async_write(*ctx->m_ssl_stream, ctx->m_body_buf,
424424
boost::bind(readSize != 0 ? &linux_client::handle_write_chunked_body : &linux_client::handle_write_body, this, boost::asio::placeholders::error, ctx));
425425
else
426-
boost::asio::async_write(*ctx->m_socket, ctx->m_request_buf,
426+
boost::asio::async_write(*ctx->m_socket, ctx->m_body_buf,
427427
boost::bind(readSize != 0 ? &linux_client::handle_write_chunked_body : &linux_client::handle_write_body, this, boost::asio::placeholders::error, ctx));
428428
});
429429
}
@@ -448,7 +448,7 @@ namespace web { namespace http
448448
auto readbuf = ctx->_get_readbuffer();
449449
size_t readSize = std::min(client_config().chunksize(), ctx->m_known_size - ctx->m_current_size);
450450

451-
readbuf.getn(boost::asio::buffer_cast<uint8_t *>(ctx->m_request_buf.prepare(readSize)), readSize).then([=](pplx::task<size_t> op)
451+
readbuf.getn(boost::asio::buffer_cast<uint8_t *>(ctx->m_body_buf.prepare(readSize)), readSize).then([=](pplx::task<size_t> op)
452452
{
453453
size_t actualSize = 0;
454454
try { actualSize = op.get(); }
@@ -459,12 +459,12 @@ namespace web { namespace http
459459
}
460460
ctx->m_uploaded += (size64_t)actualSize;
461461
ctx->m_current_size += actualSize;
462-
ctx->m_request_buf.commit(actualSize);
462+
ctx->m_body_buf.commit(actualSize);
463463
if (ctx->m_ssl_stream)
464-
boost::asio::async_write(*ctx->m_ssl_stream, ctx->m_request_buf,
464+
boost::asio::async_write(*ctx->m_ssl_stream, ctx->m_body_buf,
465465
boost::bind(&linux_client::handle_write_large_body, this, boost::asio::placeholders::error, ctx));
466466
else
467-
boost::asio::async_write(*ctx->m_socket, ctx->m_request_buf,
467+
boost::asio::async_write(*ctx->m_socket, ctx->m_body_buf,
468468
boost::bind(&linux_client::handle_write_large_body, this, boost::asio::placeholders::error, ctx));
469469
});
470470
}
@@ -502,10 +502,10 @@ namespace web { namespace http
502502

503503
// Read until the end of entire headers
504504
if (ctx->m_ssl_stream)
505-
boost::asio::async_read_until(*ctx->m_ssl_stream, ctx->m_response_buf, CRLF+CRLF,
505+
boost::asio::async_read_until(*ctx->m_ssl_stream, ctx->m_body_buf, CRLF+CRLF,
506506
boost::bind(&linux_client::handle_status_line, this, boost::asio::placeholders::error, ctx));
507507
else
508-
boost::asio::async_read_until(*ctx->m_socket, ctx->m_response_buf, CRLF+CRLF,
508+
boost::asio::async_read_until(*ctx->m_socket, ctx->m_body_buf, CRLF+CRLF,
509509
boost::bind(&linux_client::handle_status_line, this, boost::asio::placeholders::error, ctx));
510510
}
511511
else
@@ -518,7 +518,7 @@ namespace web { namespace http
518518
{
519519
if (!ec)
520520
{
521-
std::istream response_stream(&ctx->m_response_buf);
521+
std::istream response_stream(&ctx->m_body_buf);
522522
std::string http_version;
523523
response_stream >> http_version;
524524
status_code status_code;
@@ -549,7 +549,7 @@ namespace web { namespace http
549549
void read_headers(std::shared_ptr<linux_request_context> ctx)
550550
{
551551
ctx->m_needChunked = false;
552-
std::istream response_stream(&ctx->m_response_buf);
552+
std::istream response_stream(&ctx->m_body_buf);
553553
std::string header;
554554
while (std::getline(response_stream, header) && header != "\r")
555555
{
@@ -600,10 +600,10 @@ namespace web { namespace http
600600
else
601601
{
602602
if (ctx->m_ssl_stream)
603-
boost::asio::async_read_until(*ctx->m_ssl_stream, ctx->m_response_buf, CRLF,
603+
boost::asio::async_read_until(*ctx->m_ssl_stream, ctx->m_body_buf, CRLF,
604604
boost::bind(&linux_client::handle_chunk_header, this, boost::asio::placeholders::error, ctx));
605605
else
606-
boost::asio::async_read_until(*ctx->m_socket, ctx->m_response_buf, CRLF,
606+
boost::asio::async_read_until(*ctx->m_socket, ctx->m_body_buf, CRLF,
607607
boost::bind(&linux_client::handle_chunk_header, this, boost::asio::placeholders::error, ctx));
608608
}
609609
}
@@ -615,23 +615,23 @@ namespace web { namespace http
615615
size_t size_to_read = 0;
616616
if (ctx->m_ssl_stream)
617617
{
618-
if (ctx->m_response_buf.size() < size)
619-
size_to_read = size - ctx->m_response_buf.size();
620-
boost::asio::async_read(*ctx->m_ssl_stream, ctx->m_response_buf, boost::asio::transfer_at_least(size_to_read), handler);
618+
if (ctx->m_body_buf.size() < size)
619+
size_to_read = size - ctx->m_body_buf.size();
620+
boost::asio::async_read(*ctx->m_ssl_stream, ctx->m_body_buf, boost::asio::transfer_at_least(size_to_read), handler);
621621
}
622622
else
623623
{
624-
if (ctx->m_response_buf.size() < size)
625-
size_to_read = size - ctx->m_response_buf.size();
626-
boost::asio::async_read(*ctx->m_socket, ctx->m_response_buf, boost::asio::transfer_at_least(size_to_read), handler);
624+
if (ctx->m_body_buf.size() < size)
625+
size_to_read = size - ctx->m_body_buf.size();
626+
boost::asio::async_read(*ctx->m_socket, ctx->m_body_buf, boost::asio::transfer_at_least(size_to_read), handler);
627627
}
628628
}
629629

630630
void handle_chunk_header(const boost::system::error_code& ec, std::shared_ptr<linux_request_context> ctx)
631631
{
632632
if (!ec)
633633
{
634-
std::istream response_stream(&ctx->m_response_buf);
634+
std::istream response_stream(&ctx->m_body_buf);
635635
std::string line;
636636
std::getline(response_stream, line);
637637

@@ -671,7 +671,7 @@ namespace web { namespace http
671671

672672
if (to_read == 0)
673673
{
674-
ctx->m_response_buf.consume(CRLF.size());
674+
ctx->m_body_buf.consume(CRLF.size());
675675
ctx->_get_writebuffer().sync().then([ctx](pplx::task<void> op)
676676
{
677677
try {
@@ -687,21 +687,21 @@ namespace web { namespace http
687687
else
688688
{
689689
auto writeBuffer = ctx->_get_writebuffer();
690-
writeBuffer.putn(boost::asio::buffer_cast<const uint8_t *>(ctx->m_response_buf.data()), to_read).then([=](pplx::task<size_t> op)
690+
writeBuffer.putn(boost::asio::buffer_cast<const uint8_t *>(ctx->m_body_buf.data()), to_read).then([=](pplx::task<size_t> op)
691691
{
692692
try { op.wait(); }
693693
catch (...)
694694
{
695695
ctx->report_exception(std::current_exception());
696696
return;
697697
}
698-
ctx->m_response_buf.consume(to_read + CRLF.size()); // consume crlf
698+
ctx->m_body_buf.consume(to_read + CRLF.size()); // consume crlf
699699

700700
if (ctx->m_ssl_stream)
701-
boost::asio::async_read_until(*ctx->m_ssl_stream, ctx->m_response_buf, CRLF,
701+
boost::asio::async_read_until(*ctx->m_ssl_stream, ctx->m_body_buf, CRLF,
702702
boost::bind(&linux_client::handle_chunk_header, this, boost::asio::placeholders::error, ctx));
703703
else
704-
boost::asio::async_read_until(*ctx->m_socket, ctx->m_response_buf, CRLF,
704+
boost::asio::async_read_until(*ctx->m_socket, ctx->m_body_buf, CRLF,
705705
boost::bind(&linux_client::handle_chunk_header, this, boost::asio::placeholders::error, ctx));
706706
});
707707
}
@@ -719,7 +719,7 @@ namespace web { namespace http
719719
if (ec)
720720
{
721721
if (ec == boost::asio::error::eof && ctx->m_known_size == std::numeric_limits<size_t>::max())
722-
ctx->m_known_size = ctx->m_current_size + ctx->m_response_buf.size();
722+
ctx->m_known_size = ctx->m_current_size + ctx->m_body_buf.size();
723723
else
724724
{
725725
ctx->report_error("Failed to read response body", ec, httpclient_errorcode_context::readbody);
@@ -739,16 +739,16 @@ namespace web { namespace http
739739
if (ctx->m_current_size < ctx->m_known_size)
740740
{
741741
// more data need to be read
742-
writeBuffer.putn(boost::asio::buffer_cast<const uint8_t *>(ctx->m_response_buf.data()),
743-
std::min(ctx->m_response_buf.size(), ctx->m_known_size - ctx->m_current_size)).then([=](pplx::task<size_t> op)
742+
writeBuffer.putn(boost::asio::buffer_cast<const uint8_t *>(ctx->m_body_buf.data()),
743+
std::min(ctx->m_body_buf.size(), ctx->m_known_size - ctx->m_current_size)).then([=](pplx::task<size_t> op)
744744
{
745745
size_t writtenSize = 0;
746746
try
747747
{
748748
writtenSize = op.get();
749749
ctx->m_downloaded += (size64_t)writtenSize;
750750
ctx->m_current_size += writtenSize;
751-
ctx->m_response_buf.consume(writtenSize);
751+
ctx->m_body_buf.consume(writtenSize);
752752
async_read_until_buffersize(std::min(client_config().chunksize(), ctx->m_known_size - ctx->m_current_size),
753753
boost::bind(&linux_client::handle_read_content, this, boost::asio::placeholders::error, ctx), ctx);
754754
}

0 commit comments

Comments
 (0)