Skip to content

Commit 1cad17f

Browse files
author
casaserv
committed
Minor improvements to http linux client. Added const in places, a few std::move calls, fixed some indentation.
In general this code could be significantly improved if we fixed all the duplicated socket/ssl code paths.
1 parent 0a8ab18 commit 1cad17f

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

Release/src/http/client/http_linux.cpp

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ namespace web { namespace http
5252
close
5353
};
5454

55-
class linux_client;
56-
5755
class linux_request_context : public request_context
5856
{
5957
public:
@@ -127,6 +125,7 @@ namespace web { namespace http
127125
}
128126

129127
linux_request_context(std::shared_ptr<_http_client_communicator> &client, http_request request);
128+
130129
protected:
131130
virtual void cleanup()
132131
{
@@ -365,7 +364,7 @@ namespace web { namespace http
365364
ctx->report_exception(std::current_exception());
366365
return;
367366
}
368-
size_t offset = http::details::chunked_encoding::add_chunked_delimiters(buf, client_config().chunksize() + http::details::chunked_encoding::additional_encoding_space, readSize);
367+
const size_t offset = http::details::chunked_encoding::add_chunked_delimiters(buf, client_config().chunksize() + http::details::chunked_encoding::additional_encoding_space, readSize);
369368
ctx->m_body_buf.commit(readSize + http::details::chunked_encoding::additional_encoding_space);
370369
ctx->m_body_buf.consume(offset);
371370
ctx->m_current_size += readSize;
@@ -397,7 +396,7 @@ namespace web { namespace http
397396
}
398397

399398
auto readbuf = ctx->_get_readbuffer();
400-
size_t readSize = std::min(client_config().chunksize(), ctx->m_known_size - ctx->m_current_size);
399+
const size_t readSize = std::min(client_config().chunksize(), ctx->m_known_size - ctx->m_current_size);
401400

402401
readbuf.getn(boost::asio::buffer_cast<uint8_t *>(ctx->m_body_buf.prepare(readSize)), readSize).then([=](pplx::task<size_t> op)
403402
{
@@ -451,14 +450,14 @@ namespace web { namespace http
451450
}
452451
}
453452

454-
// Read until the end of entire headers
455-
if (ctx->m_ssl_stream)
456-
boost::asio::async_read_until(*ctx->m_ssl_stream, ctx->m_body_buf, CRLF+CRLF,
457-
boost::bind(&linux_client::handle_status_line, this, boost::asio::placeholders::error, ctx));
458-
else
459-
boost::asio::async_read_until(ctx->m_socket, ctx->m_body_buf, CRLF+CRLF,
460-
boost::bind(&linux_client::handle_status_line, this, boost::asio::placeholders::error, ctx));
461-
}
453+
// Read until the end of entire headers
454+
if (ctx->m_ssl_stream)
455+
boost::asio::async_read_until(*ctx->m_ssl_stream, ctx->m_body_buf, CRLF+CRLF,
456+
boost::bind(&linux_client::handle_status_line, this, boost::asio::placeholders::error, ctx));
457+
else
458+
boost::asio::async_read_until(ctx->m_socket, ctx->m_body_buf, CRLF+CRLF,
459+
boost::bind(&linux_client::handle_status_line, this, boost::asio::placeholders::error, ctx));
460+
}
462461
else
463462
{
464463
ctx->report_error("Failed to write request body", ec, httpclient_errorcode_context::writebody);
@@ -481,7 +480,7 @@ namespace web { namespace http
481480
ctx->m_response.set_status_code(status_code);
482481

483482
trim_whitespace(status_message);
484-
ctx->m_response.set_reason_phrase(status_message);
483+
ctx->m_response.set_reason_phrase(std::move(status_message));
485484

486485
if (!response_stream || http_version.substr(0, 5) != "HTTP/")
487486
{
@@ -504,20 +503,20 @@ namespace web { namespace http
504503
std::string header;
505504
while (std::getline(response_stream, header) && header != "\r")
506505
{
507-
auto colon = header.find(':');
506+
const auto colon = header.find(':');
508507
if (colon != std::string::npos)
509508
{
510509
auto name = header.substr(0, colon);
511510
auto value = header.substr(colon+2, header.size()-(colon+3)); // also exclude '\r'
512511
boost::algorithm::trim(name);
513512
boost::algorithm::trim(value);
514513

515-
ctx->m_response.headers().add(name, value);
516-
517514
if (boost::iequals(name, header_names::transfer_encoding))
518515
{
519516
ctx->m_needChunked = boost::iequals(value, U("chunked"));
520517
}
518+
519+
ctx->m_response.headers().add(std::move(name), std::move(value));
521520
}
522521
}
523522
ctx->complete_headers();
@@ -546,8 +545,10 @@ namespace web { namespace http
546545
{
547546
ctx->m_current_size = 0;
548547
if (!ctx->m_needChunked)
548+
{
549549
async_read_until_buffersize(std::min(ctx->m_known_size, client_config().chunksize()),
550-
boost::bind(&linux_client::handle_read_content, this, boost::asio::placeholders::error, ctx), ctx);
550+
boost::bind(&linux_client::handle_read_content, this, boost::asio::placeholders::error, ctx), ctx);
551+
}
551552
else
552553
{
553554
if (ctx->m_ssl_stream)
@@ -567,13 +568,17 @@ namespace web { namespace http
567568
if (ctx->m_ssl_stream)
568569
{
569570
if (ctx->m_body_buf.size() < size)
571+
{
570572
size_to_read = size - ctx->m_body_buf.size();
573+
}
571574
boost::asio::async_read(*ctx->m_ssl_stream, ctx->m_body_buf, boost::asio::transfer_at_least(size_to_read), handler);
572575
}
573576
else
574577
{
575578
if (ctx->m_body_buf.size() < size)
579+
{
576580
size_to_read = size - ctx->m_body_buf.size();
581+
}
577582
boost::asio::async_read(ctx->m_socket, ctx->m_body_buf, boost::asio::transfer_at_least(size_to_read), handler);
578583
}
579584
}
@@ -586,7 +591,7 @@ namespace web { namespace http
586591
std::string line;
587592
std::getline(response_stream, line);
588593

589-
std::istringstream octetLine(line);
594+
std::istringstream octetLine(std::move(line));
590595
int octets = 0;
591596
octetLine >> std::hex >> octets;
592597

@@ -625,7 +630,8 @@ namespace web { namespace http
625630
ctx->m_body_buf.consume(CRLF.size());
626631
ctx->_get_writebuffer().sync().then([ctx](pplx::task<void> op)
627632
{
628-
try {
633+
try
634+
{
629635
op.wait();
630636
ctx->complete_request(ctx->m_current_size);
631637
}

0 commit comments

Comments
 (0)