1515
1616#include " stdafx.h"
1717
18- #undef min
19- #undef max
20-
2118#include " cpprest/asyncrt_utils.h"
2219#include " ../common/internal_http_helpers.h"
2320
@@ -83,7 +80,7 @@ using utility::conversions::details::to_string;
8380using std::to_string;
8481#endif
8582
86- # define CRLF std::string (" \r\n " )
83+ static const std::string CRLF (" \r\n " );
8784
8885namespace web { namespace http
8986{
@@ -213,14 +210,15 @@ class asio_connection
213210 template <typename Iterator, typename Handler>
214211 void async_connect (const Iterator &begin, const Handler &handler)
215212 {
216- std::unique_lock<std::mutex> lock (m_socket_lock);
217- if (!m_closed)
218- m_socket.async_connect (begin, handler);
219- else
220213 {
221- lock.unlock ();
222- handler (boost::asio::error::operation_aborted);
223- }
214+ std::lock_guard<std::mutex> lock (m_socket_lock);
215+ if (!m_closed) {
216+ m_socket.async_connect (begin, handler);
217+ return ;
218+ }
219+ } // unlock
220+
221+ handler (boost::asio::error::operation_aborted);
224222 }
225223
226224 template <typename HandshakeHandler, typename CertificateHandler>
@@ -339,7 +337,7 @@ class asio_connection
339337// / }
340338// / </code>
341339// / </remarks>
342- class asio_connection_pool : public std ::enable_shared_from_this<asio_connection_pool>
340+ class asio_connection_pool final : public std::enable_shared_from_this<asio_connection_pool>
343341{
344342public:
345343 asio_connection_pool () : m_pool_epoch_timer(crossplat::threadpool::shared_instance().service())
@@ -396,24 +394,25 @@ class asio_connection_pool : public std::enable_shared_from_this<asio_connection
396394 if (!pool)
397395 return ;
398396 auto & self = *pool;
397+ auto & connections = self.m_connections ;
399398
400399 std::lock_guard<std::mutex> lock (self.m_lock );
401400 if (self.m_prev_epoch == self.m_epoch )
402401 {
403- self. m_connections .clear ();
402+ connections .clear ();
404403 self.is_timer_running = false ;
405404 return ;
406405 }
407406 else
408407 {
409408 auto prev_epoch = self.m_prev_epoch ;
410- auto erase_end = std::find_if (self. m_connections . begin (), self. m_connections .end (),
409+ auto erase_end = std::find_if (connections. begin (), connections .end (),
411410 [prev_epoch](std::pair<uint64_t , std::shared_ptr<asio_connection>>& p)
412411 {
413412 return p.first > prev_epoch;
414413 });
415414
416- self. m_connections . erase (self. m_connections .begin (), erase_end);
415+ connections. erase (connections .begin (), erase_end);
417416 start_epoch_interval (pool);
418417 }
419418 });
@@ -438,7 +437,7 @@ class asio_client final : public _http_client_communicator
438437 , m_start_with_ssl(base_uri().scheme() == U(" https" ) && !this ->client_config ().proxy().is_specified())
439438 {}
440439
441- void send_request (const std::shared_ptr<request_context> &request_ctx) override ;
440+ virtual void send_request (const std::shared_ptr<request_context> &request_ctx) override ;
442441
443442 void release_connection (std::shared_ptr<asio_connection>& conn)
444443 {
@@ -468,7 +467,7 @@ class asio_client final : public _http_client_communicator
468467 const bool m_start_with_ssl;
469468};
470469
471- class asio_context : public request_context , public std ::enable_shared_from_this<asio_context>
470+ class asio_context final : public request_context, public std::enable_shared_from_this<asio_context>
472471{
473472 friend class asio_client ;
474473public:
@@ -501,7 +500,7 @@ class asio_context : public request_context, public std::enable_shared_from_this
501500 return ctx;
502501 }
503502
504- class ssl_proxy_tunnel : public std ::enable_shared_from_this<ssl_proxy_tunnel>
503+ class ssl_proxy_tunnel final : public std::enable_shared_from_this<ssl_proxy_tunnel>
505504 {
506505 public:
507506 ssl_proxy_tunnel (std::shared_ptr<asio_context> context, std::function<void (std::shared_ptr<asio_context>)> ssl_tunnel_established)
@@ -518,14 +517,15 @@ class asio_context : public request_context, public std::enable_shared_from_this
518517
519518 const auto &base_uri = m_context->m_http_client ->base_uri ();
520519 const auto &host = utility::conversions::to_utf8string (base_uri.host ());
521- const auto &port = base_uri.port ();
520+ const int portRaw = base_uri.port ();
521+ const int port = (portRaw != 0 ) ? portRaw : 443 ;
522522
523523 std::ostream request_stream (&m_request);
524524 request_stream.imbue (std::locale::classic ());
525525
526- request_stream << " CONNECT " << host << " :" << (( port != 0 ) ? port : 443 ) << " HTTP/1.1" << CRLF ;
527- request_stream << " Host: " << host << " :" << (( port != 0 ) ? port : 443 ) << CRLF;
528- request_stream << " Proxy-Connection: Keep-Alive" << CRLF ;
526+ request_stream << " CONNECT " << host << " :" << port << " HTTP/1.1\r\n " ;
527+ request_stream << " Host: " << host << " :" << port << CRLF;
528+ request_stream << " Proxy-Connection: Keep-Alive\r\n " ;
529529
530530 if (m_context->m_http_client ->client_config ().proxy ().credentials ().is_set ())
531531 {
@@ -698,7 +698,7 @@ class asio_context : public request_context, public std::enable_shared_from_this
698698 request_stream.imbue (std::locale::classic ());
699699 const auto &host = utility::conversions::to_utf8string (base_uri.host ());
700700
701- request_stream << utility::conversions::to_utf8string (method) << " " << utility::conversions::to_utf8string (encoded_resource) << " " << " HTTP/1.1" << CRLF ;
701+ request_stream << utility::conversions::to_utf8string (method) << " " << utility::conversions::to_utf8string (encoded_resource) << " " << " HTTP/1.1\r\n " ;
702702
703703 int port = base_uri.port ();
704704
@@ -766,7 +766,7 @@ class asio_context : public request_context, public std::enable_shared_from_this
766766 request_stream << utility::conversions::to_utf8string (::web::http::details::flatten_http_headers (ctx->m_request .headers ()));
767767 request_stream << extra_headers;
768768 // Enforce HTTP connection keep alive (even for the old HTTP/1.0 protocol).
769- request_stream << " Connection: Keep-Alive" << CRLF << CRLF ;
769+ request_stream << " Connection: Keep-Alive\r\n\r\n " ;
770770
771771 // Start connection timeout timer.
772772 if (!ctx->m_timer .has_started ())
@@ -1345,7 +1345,7 @@ class asio_context : public request_context, public std::enable_shared_from_this
13451345 }
13461346 else
13471347 {
1348- async_read_until_buffersize (octets + CRLF.size (), // + 2 for crlf
1348+ async_read_until_buffersize (octets + CRLF.size (),
13491349 boost::bind (&asio_context::handle_chunk, shared_from_this (), boost::asio::placeholders::error, octets));
13501350 }
13511351 }
0 commit comments