Skip to content

Commit 5b0c1ef

Browse files
author
Ognjen Sobajic
committed
Some changes that Artur suggested on the code review.
1 parent 0bb252b commit 5b0c1ef

File tree

1 file changed

+42
-19
lines changed

1 file changed

+42
-19
lines changed

Release/src/http/client/http_linux.cpp

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,21 @@ namespace web { namespace http
102102
if (!ec)
103103
{
104104
m_timedout = true;
105-
auto sock = m_socket.get();
106-
if (sock != nullptr)
105+
if (m_ssl_stream)
107106
{
108-
sock->cancel();
107+
boost::system::error_code error;
108+
m_ssl_stream->lowest_layer().cancel(error);
109+
110+
if (error)
111+
report_error("Failed to cancel the socket", error);
112+
}
113+
else
114+
{
115+
auto sock = m_socket.get();
116+
if (sock != nullptr)
117+
{
118+
sock->cancel();
119+
}
109120
}
110121
}
111122
}
@@ -270,11 +281,23 @@ namespace web { namespace http
270281
else
271282
{
272283
boost::system::error_code ignore;
273-
ctx->m_socket->shutdown(tcp::socket::shutdown_both, ignore);
274-
ctx->m_socket->close();
275-
ctx->m_socket.reset(new tcp::socket(m_io_service));
276284
auto endpoint = *endpoints;
277-
ctx->m_socket->async_connect(endpoint, boost::bind(&client::handle_connect, this, boost::asio::placeholders::error, ++endpoints, ctx));
285+
if (ctx->m_ssl_stream)
286+
{
287+
ctx->m_ssl_stream->lowest_layer().shutdown(tcp::socket::shutdown_both, ignore);
288+
ctx->m_ssl_stream->lowest_layer().close();
289+
boost::asio::ssl::context context(boost::asio::ssl::context::sslv23);
290+
context.set_verify_mode(boost::asio::ssl::context::verify_none);
291+
ctx->m_ssl_stream.reset(new boost::asio::ssl::stream<boost::asio::ip::tcp::socket>(m_io_service, context));
292+
ctx->m_ssl_stream->lowest_layer().async_connect(endpoint, boost::bind(&client::handle_connect, this, boost::asio::placeholders::error, ++endpoints, ctx));
293+
}
294+
else
295+
{
296+
ctx->m_socket->shutdown(tcp::socket::shutdown_both, ignore);
297+
ctx->m_socket->close();
298+
ctx->m_socket.reset(new tcp::socket(m_io_service));
299+
ctx->m_socket->async_connect(endpoint, boost::bind(&client::handle_connect, this, boost::asio::placeholders::error, ++endpoints, ctx));
300+
}
278301
}
279302
}
280303

@@ -351,11 +374,11 @@ namespace web { namespace http
351374
ctx->m_current_size += actualSize;
352375
ctx->m_request_buf.commit(actualSize);
353376
if (ctx->m_ssl_stream)
354-
boost::asio::async_write(*ctx->m_ssl_stream, ctx->m_request_buf,
355-
boost::bind(&client::handle_write_large_body, this, boost::asio::placeholders::error, ctx));
377+
boost::asio::async_write(*ctx->m_ssl_stream, ctx->m_request_buf,
378+
boost::bind(&client::handle_write_large_body, this, boost::asio::placeholders::error, ctx));
356379
else
357-
boost::asio::async_write(*ctx->m_socket, ctx->m_request_buf,
358-
boost::bind(&client::handle_write_large_body, this, boost::asio::placeholders::error, ctx));
380+
boost::asio::async_write(*ctx->m_socket, ctx->m_request_buf,
381+
boost::bind(&client::handle_write_large_body, this, boost::asio::placeholders::error, ctx));
359382
});
360383
}
361384

@@ -497,17 +520,17 @@ namespace web { namespace http
497520
{
498521
if (ctx->m_ssl_stream)
499522
{
500-
if (ctx->m_response_buf.size() >= size)
501-
boost::asio::async_read(*ctx->m_ssl_stream, ctx->m_response_buf, boost::asio::transfer_at_least(0), handler);
502-
else
503-
boost::asio::async_read(*ctx->m_ssl_stream, ctx->m_response_buf, boost::asio::transfer_at_least(size - ctx->m_response_buf.size()), handler);
523+
int size_to_read = 0;
524+
if (ctx->m_response_buf.size() < size)
525+
size_to_read = size - ctx->m_response_buf.size();
526+
boost::asio::async_read(*ctx->m_ssl_stream, ctx->m_response_buf, boost::asio::transfer_at_least(size_to_read), handler);
504527
}
505528
else
506529
{
507-
if (ctx->m_response_buf.size() >= size)
508-
boost::asio::async_read(*ctx->m_socket, ctx->m_response_buf, boost::asio::transfer_at_least(0), handler);
509-
else
510-
boost::asio::async_read(*ctx->m_socket, ctx->m_response_buf, boost::asio::transfer_at_least(size - ctx->m_response_buf.size()), handler);
530+
int size_to_read = 0;
531+
if (ctx->m_response_buf.size() < size)
532+
size_to_read = size - ctx->m_response_buf.size();
533+
boost::asio::async_read(*ctx->m_socket, ctx->m_response_buf, boost::asio::transfer_at_least(size_to_read), handler);
511534
}
512535
}
513536

0 commit comments

Comments
 (0)