@@ -87,12 +87,11 @@ namespace
87
87
{
88
88
const std::string CRLF (" \r\n " );
89
89
90
- std::string calc_cn_host (const bool secure,
91
- const web::http::uri& baseUri,
90
+ std::string calc_cn_host (const web::http::uri& baseUri,
92
91
const web::http::http_headers& requestHeaders)
93
92
{
94
93
std::string result;
95
- if (secure )
94
+ if (baseUri. scheme () == U ( " https " ) )
96
95
{
97
96
const utility::string_t * encResult;
98
97
const auto hostHeader = requestHeaders.find (_XPLATSTR (" Host" ));
@@ -472,7 +471,6 @@ class asio_client final : public _http_client_communicator
472
471
: _http_client_communicator(std::move(address), std::move(client_config))
473
472
, m_resolver(crossplat::threadpool::shared_instance().service())
474
473
, m_pool(std::make_shared<asio_connection_pool>())
475
- , m_start_with_ssl(base_uri().scheme() == U(" https" ) && !this ->client_config ().proxy().is_specified())
476
474
{
477
475
}
478
476
@@ -482,13 +480,13 @@ class asio_client final : public _http_client_communicator
482
480
483
481
std::shared_ptr<asio_connection> obtain_connection (const http_request& req)
484
482
{
485
- std::string cn_host = calc_cn_host (m_start_with_ssl, base_uri (), req.headers ());
483
+ std::string cn_host = calc_cn_host (base_uri (), req.headers ());
486
484
std::shared_ptr<asio_connection> conn = m_pool->try_acquire (cn_host);
487
485
if (conn == nullptr )
488
486
{
489
487
// Pool was empty. Create a new connection
490
488
conn = std::make_shared<asio_connection>(crossplat::threadpool::shared_instance ().service ());
491
- if (m_start_with_ssl )
489
+ if (base_uri (). scheme () == U ( " https " ) && ! this -> client_config (). proxy (). is_specified () )
492
490
{
493
491
conn->upgrade_to_ssl (std::move (cn_host), this ->client_config ().get_ssl_context_callback ());
494
492
}
@@ -499,13 +497,10 @@ class asio_client final : public _http_client_communicator
499
497
500
498
virtual pplx::task<http_response> propagate (http_request request) override ;
501
499
502
- bool start_with_ssl () const CPPREST_NOEXCEPT { return m_start_with_ssl; }
503
-
504
500
tcp::resolver m_resolver;
505
501
506
502
private:
507
503
const std::shared_ptr<asio_connection_pool> m_pool;
508
- const bool m_start_with_ssl;
509
504
};
510
505
511
506
class asio_context final : public request_context, public std::enable_shared_from_this<asio_context>
@@ -631,7 +626,15 @@ class asio_context final : public request_context, public std::enable_shared_fro
631
626
m_context->m_timer .reset ();
632
627
// // Replace the connection. This causes old connection object to go out of scope.
633
628
auto client = std::static_pointer_cast<asio_client>(m_context->m_http_client );
634
- m_context->m_connection = client->obtain_connection (m_context->m_request );
629
+ try
630
+ {
631
+ m_context->m_connection = client->obtain_connection (m_context->m_request );
632
+ }
633
+ catch (...)
634
+ {
635
+ m_context->report_exception (std::current_exception ());
636
+ return ;
637
+ }
635
638
636
639
auto endpoint = *endpoints;
637
640
m_context->m_connection ->async_connect (endpoint,
@@ -688,7 +691,15 @@ class asio_context final : public request_context, public std::enable_shared_fro
688
691
return ;
689
692
}
690
693
691
- m_context->upgrade_to_ssl ();
694
+ try
695
+ {
696
+ m_context->upgrade_to_ssl ();
697
+ }
698
+ catch (...)
699
+ {
700
+ m_context->report_exception (std::current_exception ());
701
+ return ;
702
+ }
692
703
693
704
m_ssl_tunnel_established (m_context);
694
705
}
@@ -925,7 +936,7 @@ class asio_context final : public request_context, public std::enable_shared_fro
925
936
void upgrade_to_ssl ()
926
937
{
927
938
auto & client = static_cast <asio_client&>(*m_http_client);
928
- m_connection->upgrade_to_ssl (calc_cn_host (client.start_with_ssl (), client. base_uri (), m_request.headers ()),
939
+ m_connection->upgrade_to_ssl (calc_cn_host (client.base_uri (), m_request.headers ()),
929
940
client.client_config ().get_ssl_context_callback ());
930
941
}
931
942
@@ -1009,7 +1020,15 @@ class asio_context final : public request_context, public std::enable_shared_fro
1009
1020
{
1010
1021
// Replace the connection. This causes old connection object to go out of scope.
1011
1022
auto client = std::static_pointer_cast<asio_client>(m_http_client);
1012
- m_connection = client->obtain_connection (m_request);
1023
+ try
1024
+ {
1025
+ m_connection = client->obtain_connection (m_request);
1026
+ }
1027
+ catch (...)
1028
+ {
1029
+ request_context::report_exception (std::current_exception ());
1030
+ return ;
1031
+ }
1013
1032
1014
1033
auto endpoint = *endpoints;
1015
1034
m_connection->async_connect (
@@ -1330,7 +1349,16 @@ class asio_context final : public request_context, public std::enable_shared_fro
1330
1349
// Create a new context and copy the request object, completion event and
1331
1350
// cancellation registration to maintain the old state.
1332
1351
// This also obtains a new connection from pool.
1333
- auto new_ctx = create_request_context (m_http_client, m_request);
1352
+ std::shared_ptr<request_context> new_ctx;
1353
+ try
1354
+ {
1355
+ new_ctx = create_request_context (m_http_client, m_request);
1356
+ }
1357
+ catch (...)
1358
+ {
1359
+ report_exception (std::current_exception ());
1360
+ return ;
1361
+ }
1334
1362
1335
1363
// If the request contains a valid instream, we try to rewind it to
1336
1364
// replay the just-failed request. Otherwise we assume that no data
@@ -1940,7 +1968,15 @@ void asio_client::send_request(const std::shared_ptr<request_context>& request_c
1940
1968
pplx::task<http_response> asio_client::propagate (http_request request)
1941
1969
{
1942
1970
auto self = std::static_pointer_cast<_http_client_communicator>(shared_from_this ());
1943
- auto context = details::asio_context::create_request_context (self, request);
1971
+ std::shared_ptr<request_context> context;
1972
+ try
1973
+ {
1974
+ context = details::asio_context::create_request_context (self, request);
1975
+ }
1976
+ catch (...)
1977
+ {
1978
+ return pplx::task_from_exception<http_response>(std::current_exception ());
1979
+ }
1944
1980
1945
1981
// Use a task to externally signal the final result and completion of the task.
1946
1982
auto result_task = pplx::create_task (context->m_request_completion );
0 commit comments