@@ -169,20 +169,20 @@ namespace web { namespace http
169
169
170
170
struct client
171
171
{
172
- client (boost::asio::io_service& io_service, size_t chunk_size )
172
+ client (boost::asio::io_service& io_service, const http_client_config &config )
173
173
: m_resolver(io_service)
174
174
, m_io_service(io_service)
175
- , m_chunksize(chunk_size ) {}
175
+ , m_config(config ) {}
176
176
177
- void send_request (linux_request_context* ctx, int timeout )
177
+ void send_request (linux_request_context* ctx)
178
178
{
179
179
auto what = ctx->m_what ;
180
180
auto resource = what.resource ().to_string ();
181
181
182
182
if (what.scheme () == " https" )
183
183
{
184
184
boost::asio::ssl::context context (boost::asio::ssl::context::sslv23);
185
- context.set_verify_mode (boost::asio::ssl::context::verify_none );
185
+ context.set_default_verify_paths ( );
186
186
ctx->m_ssl_stream .reset (new boost::asio::ssl::stream<boost::asio::ip::tcp::socket>(m_io_service, context));
187
187
}
188
188
else
@@ -250,7 +250,9 @@ namespace web { namespace http
250
250
tcp::resolver::query query (host, utility::conversions::print_string (port));
251
251
252
252
ctx->m_timer .reset (new boost::asio::deadline_timer (m_io_service));
253
- ctx->m_timer ->expires_from_now (boost::posix_time::milliseconds (timeout));
253
+ auto timeout = m_config.timeout ();
254
+ int secs = static_cast <int >(timeout.count ());
255
+ ctx->m_timer ->expires_from_now (boost::posix_time::milliseconds (secs * 1000 ));
254
256
ctx->m_timer ->async_wait (boost::bind (&linux_request_context::cancel, ctx, boost::asio::placeholders::error));
255
257
256
258
m_resolver.async_resolve (query, boost::bind (&client::handle_resolve, this , boost::asio::placeholders::error, boost::asio::placeholders::iterator, ctx));
@@ -259,7 +261,7 @@ namespace web { namespace http
259
261
private:
260
262
boost::asio::io_service& m_io_service;
261
263
tcp::resolver m_resolver;
262
- size_t m_chunksize ;
264
+ http_client_config m_config ;
263
265
264
266
static bool _check_streambuf (linux_request_context * ctx, concurrency::streams::streambuf<uint8_t > rdbuf, const utility::char_t * msg)
265
267
{
@@ -288,7 +290,20 @@ namespace web { namespace http
288
290
{
289
291
auto endpoint = *endpoints;
290
292
if (ctx->m_ssl_stream )
293
+ {
294
+ // Check to turn off server certificate verification.
295
+ if (m_config.validate_certificates ())
296
+ {
297
+ ctx->m_ssl_stream ->set_verify_mode (boost::asio::ssl::context::verify_peer);
298
+ ctx->m_ssl_stream ->set_verify_callback (boost::asio::ssl::rfc2818_verification (ctx->m_what .host ()));
299
+ }
300
+ else
301
+ {
302
+ ctx->m_ssl_stream ->set_verify_mode (boost::asio::ssl::context::verify_none);
303
+ }
304
+
291
305
ctx->m_ssl_stream ->lowest_layer ().async_connect (endpoint, boost::bind (&client::handle_connect, this , boost::asio::placeholders::error, ++endpoints, ctx));
306
+ }
292
307
else
293
308
ctx->m_socket ->async_connect (endpoint, boost::bind (&client::handle_connect, this , boost::asio::placeholders::error, ++endpoints, ctx));
294
309
}
@@ -316,8 +331,21 @@ namespace web { namespace http
316
331
ctx->m_ssl_stream ->lowest_layer ().shutdown (tcp::socket::shutdown_both, ignore);
317
332
ctx->m_ssl_stream ->lowest_layer ().close ();
318
333
boost::asio::ssl::context context (boost::asio::ssl::context::sslv23);
319
- context.set_verify_mode (boost::asio::ssl::context::verify_none );
334
+ context.set_default_verify_paths ( );
320
335
ctx->m_ssl_stream .reset (new boost::asio::ssl::stream<boost::asio::ip::tcp::socket>(m_io_service, context));
336
+
337
+ // Check to turn off server certificate verification.
338
+ if (m_config.validate_certificates ())
339
+ {
340
+ ctx->m_ssl_stream ->set_verify_mode (boost::asio::ssl::context::verify_peer);
341
+ ctx->m_ssl_stream ->set_verify_callback (boost::asio::ssl::rfc2818_verification (ctx->m_what .host ()));
342
+ }
343
+ else
344
+ {
345
+ ctx->m_ssl_stream ->set_verify_mode (boost::asio::ssl::context::verify_none);
346
+ }
347
+
348
+
321
349
ctx->m_ssl_stream ->lowest_layer ().async_connect (endpoint, boost::bind (&client::handle_connect, this , boost::asio::placeholders::error, ++endpoints, ctx));
322
350
}
323
351
else
@@ -350,8 +378,8 @@ namespace web { namespace http
350
378
}
351
379
352
380
auto readbuf = ctx->_get_readbuffer ();
353
- uint8_t *buf = boost::asio::buffer_cast<uint8_t *>(ctx->m_request_buf .prepare (m_chunksize + http::details::chunked_encoding::additional_encoding_space));
354
- readbuf.getn (buf + http::details::chunked_encoding::data_offset, m_chunksize ).then ([=](pplx::task<size_t > op)
381
+ uint8_t *buf = boost::asio::buffer_cast<uint8_t *>(ctx->m_request_buf .prepare (m_config. chunksize () + http::details::chunked_encoding::additional_encoding_space));
382
+ readbuf.getn (buf + http::details::chunked_encoding::data_offset, m_config. chunksize () ).then ([=](pplx::task<size_t > op)
355
383
{
356
384
size_t readSize = 0 ;
357
385
try { readSize = op.get (); }
@@ -360,7 +388,7 @@ namespace web { namespace http
360
388
ctx->report_exception (std::current_exception ());
361
389
return ;
362
390
}
363
- size_t offset = http::details::chunked_encoding::add_chunked_delimiters (buf, m_chunksize+ http::details::chunked_encoding::additional_encoding_space, readSize);
391
+ size_t offset = http::details::chunked_encoding::add_chunked_delimiters (buf, m_config. chunksize () + http::details::chunked_encoding::additional_encoding_space, readSize);
364
392
ctx->m_request_buf .commit (readSize + http::details::chunked_encoding::additional_encoding_space);
365
393
ctx->m_request_buf .consume (offset);
366
394
ctx->m_current_size += readSize;
@@ -388,7 +416,7 @@ namespace web { namespace http
388
416
}
389
417
390
418
auto readbuf = ctx->_get_readbuffer ();
391
- size_t readSize = std::min (m_chunksize , ctx->m_known_size - ctx->m_current_size );
419
+ size_t readSize = std::min (m_config. chunksize () , ctx->m_known_size - ctx->m_current_size );
392
420
393
421
readbuf.getn (boost::asio::buffer_cast<uint8_t *>(ctx->m_request_buf .prepare (readSize)), readSize).then ([=](pplx::task<size_t > op)
394
422
{
@@ -529,7 +557,7 @@ namespace web { namespace http
529
557
{
530
558
ctx->m_current_size = 0 ;
531
559
if (!ctx->m_needChunked )
532
- async_read_until_buffersize (std::min (ctx->m_known_size , m_chunksize ),
560
+ async_read_until_buffersize (std::min (ctx->m_known_size , m_config. chunksize () ),
533
561
boost::bind (&client::handle_read_content, this , boost::asio::placeholders::error, ctx), ctx);
534
562
else
535
563
{
@@ -675,7 +703,7 @@ namespace web { namespace http
675
703
ctx->m_downloaded += (size64_t )writtenSize;
676
704
ctx->m_current_size += writtenSize;
677
705
ctx->m_response_buf .consume (writtenSize);
678
- async_read_until_buffersize (std::min (m_chunksize , ctx->m_known_size - ctx->m_current_size ),
706
+ async_read_until_buffersize (std::min (m_config. chunksize () , ctx->m_known_size - ctx->m_current_size ),
679
707
boost::bind (&client::handle_read_content, this , boost::asio::placeholders::error, ctx), ctx);
680
708
}
681
709
catch (...)
@@ -716,7 +744,7 @@ namespace web { namespace http
716
744
717
745
unsigned long open ()
718
746
{
719
- m_client.reset (new client (crossplat::threadpool::shared_instance ().service (), client_config (). chunksize () ));
747
+ m_client.reset (new client (crossplat::threadpool::shared_instance ().service (), client_config ()));
720
748
return 0 ;
721
749
}
722
750
@@ -725,15 +753,9 @@ namespace web { namespace http
725
753
auto linux_ctx = static_cast <linux_request_context*>(request_ctx);
726
754
727
755
auto encoded_resource = uri_builder (m_address).append (linux_ctx->m_request .relative_uri ()).to_uri ();
728
-
729
756
linux_ctx->m_what = encoded_resource;
730
757
731
- auto & config = client_config ();
732
-
733
- auto timeout = config.timeout ();
734
- int secs = static_cast <int >(timeout.count ());
735
-
736
- m_client->send_request (linux_ctx, secs * 1000 );
758
+ m_client->send_request (linux_ctx);
737
759
}
738
760
};
739
761
} // namespace details
0 commit comments