Skip to content

Commit 0a8ab18

Browse files
author
casaserv
committed
Fixing build break I caused on Friday. I removed an unncessary unique_ptr from the linux http client request class. This saves a heap allocation and pointer per request, also allows us to reduce a bunch of bifricated code.
1 parent 33f9073 commit 0a8ab18

File tree

1 file changed

+45
-71
lines changed

1 file changed

+45
-71
lines changed

Release/src/http/client/http_linux.cpp

Lines changed: 45 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@
3232
#include "cpprest/http_client_impl.h"
3333
#include <limits>
3434

35+
using boost::asio::ip::tcp;
36+
3537
namespace web { namespace http
3638
{
3739
namespace client
3840
{
3941
namespace details
4042
{
41-
using boost::asio::ip::tcp;
42-
4343
enum class httpclient_errorcode_context
4444
{
4545
none = 0,
@@ -51,6 +51,9 @@ namespace web { namespace http
5151
readbody,
5252
close
5353
};
54+
55+
class linux_client;
56+
5457
class linux_request_context : public request_context
5558
{
5659
public:
@@ -92,66 +95,38 @@ namespace web { namespace http
9295
}
9396

9497
tcp::socket m_socket;
95-
std::unique_ptr<boost::asio::ssl::stream<tcp::socket>> m_ssl_stream;
98+
std::unique_ptr<boost::asio::ssl::stream<tcp::socket &>> m_ssl_stream;
9699
size_t m_known_size;
97100
size_t m_current_size;
98101
bool m_needChunked;
99102
bool m_timedout;
100103
boost::asio::streambuf m_body_buf;
101104
boost::asio::deadline_timer m_timer;
102105

103-
template <typename socket_type>
104-
void shutdown_socket(socket_type &socket)
105-
{
106-
boost::system::error_code ignore;
107-
socket.shutdown(tcp::socket::shutdown_both, ignore);
108-
socket.close();
109-
}
110-
111106
~linux_request_context()
112107
{
113108
m_timer.cancel();
114-
115-
shutdown_socket(m_socket);
116-
117-
if (m_ssl_stream)
118-
{
119-
shutdown_socket(m_ssl_stream->lowest_layer());
120-
m_ssl_stream.reset();
121-
}
109+
boost::system::error_code ignore;
110+
m_socket.shutdown(tcp::socket::shutdown_both, ignore);
111+
m_socket.close();
122112
}
123113

124114
void cancel(const boost::system::error_code& ec)
125115
{
126116
if (!ec)
127117
{
128118
m_timedout = true;
129-
if (m_ssl_stream)
130-
{
131-
boost::system::error_code error;
132-
m_ssl_stream->lowest_layer().cancel(error);
133119

134-
if (error)
135-
report_error("Failed to cancel the socket", error);
136-
}
137-
else
120+
boost::system::error_code error;
121+
m_socket.cancel(error);
122+
if (error)
138123
{
139-
m_socket.cancel();
124+
report_error("Failed to cancel the socket", error);
140125
}
141126
}
142127
}
143128

144-
public:
145-
linux_request_context(std::shared_ptr<_http_client_communicator> &client, http_request request)
146-
: request_context(client, request)
147-
, m_known_size(0)
148-
, m_needChunked(false)
149-
, m_timedout(false)
150-
, m_current_size(0)
151-
, m_timer(crossplat::threadpool::shared_instance().service())
152-
{
153-
}
154-
129+
linux_request_context(std::shared_ptr<_http_client_communicator> &client, http_request request);
155130
protected:
156131
virtual void cleanup()
157132
{
@@ -182,11 +157,7 @@ namespace web { namespace http
182157
{
183158
boost::asio::ssl::context context(boost::asio::ssl::context::sslv23);
184159
context.set_default_verify_paths();
185-
ctx->m_ssl_stream.reset(new boost::asio::ssl::stream<boost::asio::ip::tcp::socket>(m_io_service, context));
186-
}
187-
else
188-
{
189-
ctx->m_socket.reset(new tcp::socket(m_io_service));
160+
ctx->m_ssl_stream.reset(new boost::asio::ssl::stream<boost::asio::ip::tcp::socket &>(ctx->m_socket, context));
190161
}
191162

192163
auto encoded_resource = uri_builder(m_uri).append(ctx->m_request.relative_uri()).to_uri().resource().to_string();
@@ -263,8 +234,9 @@ namespace web { namespace http
263234
m_resolver.async_resolve(query, boost::bind(&linux_client::handle_resolve, this, boost::asio::placeholders::error, boost::asio::placeholders::iterator, ctx));
264235
}
265236

266-
private:
267237
boost::asio::io_service& m_io_service;
238+
239+
private:
268240
tcp::resolver m_resolver;
269241

270242
static bool _check_streambuf(std::shared_ptr<linux_request_context> ctx, concurrency::streams::streambuf<uint8_t> rdbuf, const utility::char_t* msg)
@@ -305,13 +277,9 @@ namespace web { namespace http
305277
{
306278
ctx->m_ssl_stream->set_verify_mode(boost::asio::ssl::context::verify_none);
307279
}
308-
309-
ctx->m_ssl_stream->lowest_layer().async_connect(endpoint, boost::bind(&linux_client::handle_connect, this, boost::asio::placeholders::error, ++endpoints, ctx));
310-
}
311-
else
312-
{
313-
ctx->m_socket->async_connect(endpoint, boost::bind(&linux_client::handle_connect, this, boost::asio::placeholders::error, ++endpoints, ctx));
314280
}
281+
282+
ctx->m_socket.async_connect(endpoint, boost::bind(&linux_client::handle_connect, this, boost::asio::placeholders::error, ++endpoints, ctx));
315283
}
316284
}
317285

@@ -325,7 +293,7 @@ namespace web { namespace http
325293
}
326294
else
327295
{
328-
boost::asio::async_write(*ctx->m_socket, ctx->m_body_buf, boost::bind(&linux_client::handle_write_request, this, boost::asio::placeholders::error, ctx));
296+
boost::asio::async_write(ctx->m_socket, ctx->m_body_buf, boost::bind(&linux_client::handle_write_request, this, boost::asio::placeholders::error, ctx));
329297
}
330298
}
331299
else if (endpoints == tcp::resolver::iterator())
@@ -335,14 +303,17 @@ namespace web { namespace http
335303
else
336304
{
337305
boost::system::error_code ignore;
306+
307+
ctx->m_socket.shutdown(tcp::socket::shutdown_both, ignore);
308+
ctx->m_socket.close();
309+
ctx->m_socket = tcp::socket(m_io_service);
310+
338311
auto endpoint = *endpoints;
339312
if (ctx->m_ssl_stream)
340313
{
341-
ctx->m_ssl_stream->lowest_layer().shutdown(tcp::socket::shutdown_both, ignore);
342-
ctx->m_ssl_stream->lowest_layer().close();
343314
boost::asio::ssl::context context(boost::asio::ssl::context::sslv23);
344315
context.set_default_verify_paths();
345-
ctx->m_ssl_stream.reset(new boost::asio::ssl::stream<boost::asio::ip::tcp::socket>(m_io_service, context));
316+
ctx->m_ssl_stream.reset(new boost::asio::ssl::stream<boost::asio::ip::tcp::socket &>(ctx->m_socket, context));
346317

347318
// Check to turn off server certificate verification.
348319
if(client_config().validate_certificates())
@@ -354,17 +325,9 @@ namespace web { namespace http
354325
{
355326
ctx->m_ssl_stream->set_verify_mode(boost::asio::ssl::context::verify_none);
356327
}
357-
358-
359-
ctx->m_ssl_stream->lowest_layer().async_connect(endpoint, boost::bind(&linux_client::handle_connect, this, boost::asio::placeholders::error, ++endpoints, ctx));
360-
}
361-
else
362-
{
363-
ctx->m_socket->shutdown(tcp::socket::shutdown_both, ignore);
364-
ctx->m_socket->close();
365-
ctx->m_socket.reset(new tcp::socket(m_io_service));
366-
ctx->m_socket->async_connect(endpoint, boost::bind(&linux_client::handle_connect, this, boost::asio::placeholders::error, ++endpoints, ctx));
367328
}
329+
330+
ctx->m_socket.async_connect(endpoint, boost::bind(&linux_client::handle_connect, this, boost::asio::placeholders::error, ++endpoints, ctx));
368331
}
369332
}
370333

@@ -411,7 +374,7 @@ namespace web { namespace http
411374
boost::asio::async_write(*ctx->m_ssl_stream, ctx->m_body_buf,
412375
boost::bind(readSize != 0 ? &linux_client::handle_write_chunked_body : &linux_client::handle_write_body, this, boost::asio::placeholders::error, ctx));
413376
else
414-
boost::asio::async_write(*ctx->m_socket, ctx->m_body_buf,
377+
boost::asio::async_write(ctx->m_socket, ctx->m_body_buf,
415378
boost::bind(readSize != 0 ? &linux_client::handle_write_chunked_body : &linux_client::handle_write_body, this, boost::asio::placeholders::error, ctx));
416379
});
417380
}
@@ -452,7 +415,7 @@ namespace web { namespace http
452415
boost::asio::async_write(*ctx->m_ssl_stream, ctx->m_body_buf,
453416
boost::bind(&linux_client::handle_write_large_body, this, boost::asio::placeholders::error, ctx));
454417
else
455-
boost::asio::async_write(*ctx->m_socket, ctx->m_body_buf,
418+
boost::asio::async_write(ctx->m_socket, ctx->m_body_buf,
456419
boost::bind(&linux_client::handle_write_large_body, this, boost::asio::placeholders::error, ctx));
457420
});
458421
}
@@ -493,7 +456,7 @@ namespace web { namespace http
493456
boost::asio::async_read_until(*ctx->m_ssl_stream, ctx->m_body_buf, CRLF+CRLF,
494457
boost::bind(&linux_client::handle_status_line, this, boost::asio::placeholders::error, ctx));
495458
else
496-
boost::asio::async_read_until(*ctx->m_socket, ctx->m_body_buf, CRLF+CRLF,
459+
boost::asio::async_read_until(ctx->m_socket, ctx->m_body_buf, CRLF+CRLF,
497460
boost::bind(&linux_client::handle_status_line, this, boost::asio::placeholders::error, ctx));
498461
}
499462
else
@@ -591,7 +554,7 @@ namespace web { namespace http
591554
boost::asio::async_read_until(*ctx->m_ssl_stream, ctx->m_body_buf, CRLF,
592555
boost::bind(&linux_client::handle_chunk_header, this, boost::asio::placeholders::error, ctx));
593556
else
594-
boost::asio::async_read_until(*ctx->m_socket, ctx->m_body_buf, CRLF,
557+
boost::asio::async_read_until(ctx->m_socket, ctx->m_body_buf, CRLF,
595558
boost::bind(&linux_client::handle_chunk_header, this, boost::asio::placeholders::error, ctx));
596559
}
597560
}
@@ -611,7 +574,7 @@ namespace web { namespace http
611574
{
612575
if (ctx->m_body_buf.size() < size)
613576
size_to_read = size - ctx->m_body_buf.size();
614-
boost::asio::async_read(*ctx->m_socket, ctx->m_body_buf, boost::asio::transfer_at_least(size_to_read), handler);
577+
boost::asio::async_read(ctx->m_socket, ctx->m_body_buf, boost::asio::transfer_at_least(size_to_read), handler);
615578
}
616579
}
617580

@@ -689,7 +652,7 @@ namespace web { namespace http
689652
boost::asio::async_read_until(*ctx->m_ssl_stream, ctx->m_body_buf, CRLF,
690653
boost::bind(&linux_client::handle_chunk_header, this, boost::asio::placeholders::error, ctx));
691654
else
692-
boost::asio::async_read_until(*ctx->m_socket, ctx->m_body_buf, CRLF,
655+
boost::asio::async_read_until(ctx->m_socket, ctx->m_body_buf, CRLF,
693656
boost::bind(&linux_client::handle_chunk_header, this, boost::asio::placeholders::error, ctx));
694657
});
695658
}
@@ -783,4 +746,15 @@ namespace web { namespace http
783746
return result_task;
784747
}
785748

749+
linux_request_context::linux_request_context(std::shared_ptr<_http_client_communicator> &client, http_request request)
750+
: request_context(client, request)
751+
, m_known_size(0)
752+
, m_needChunked(false)
753+
, m_timedout(false)
754+
, m_current_size(0)
755+
, m_timer(crossplat::threadpool::shared_instance().service())
756+
, m_socket(std::static_pointer_cast<linux_client>(client)->m_io_service)
757+
{
758+
}
759+
786760
}}}} // namespaces

0 commit comments

Comments
 (0)