32
32
#include " cpprest/http_client_impl.h"
33
33
#include < limits>
34
34
35
+ using boost::asio::ip::tcp;
36
+
35
37
namespace web { namespace http
36
38
{
37
39
namespace client
38
40
{
39
41
namespace details
40
42
{
41
- using boost::asio::ip::tcp;
42
-
43
43
enum class httpclient_errorcode_context
44
44
{
45
45
none = 0 ,
@@ -51,6 +51,9 @@ namespace web { namespace http
51
51
readbody,
52
52
close
53
53
};
54
+
55
+ class linux_client ;
56
+
54
57
class linux_request_context : public request_context
55
58
{
56
59
public:
@@ -92,66 +95,38 @@ namespace web { namespace http
92
95
}
93
96
94
97
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;
96
99
size_t m_known_size;
97
100
size_t m_current_size;
98
101
bool m_needChunked;
99
102
bool m_timedout;
100
103
boost::asio::streambuf m_body_buf;
101
104
boost::asio::deadline_timer m_timer;
102
105
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
-
111
106
~linux_request_context ()
112
107
{
113
108
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 ();
122
112
}
123
113
124
114
void cancel (const boost::system::error_code& ec)
125
115
{
126
116
if (!ec)
127
117
{
128
118
m_timedout = true ;
129
- if (m_ssl_stream)
130
- {
131
- boost::system::error_code error;
132
- m_ssl_stream->lowest_layer ().cancel (error);
133
119
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)
138
123
{
139
- m_socket. cancel ( );
124
+ report_error ( " Failed to cancel the socket " , error );
140
125
}
141
126
}
142
127
}
143
128
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);
155
130
protected:
156
131
virtual void cleanup ()
157
132
{
@@ -182,11 +157,7 @@ namespace web { namespace http
182
157
{
183
158
boost::asio::ssl::context context (boost::asio::ssl::context::sslv23);
184
159
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));
190
161
}
191
162
192
163
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
263
234
m_resolver.async_resolve (query, boost::bind (&linux_client::handle_resolve, this , boost::asio::placeholders::error, boost::asio::placeholders::iterator, ctx));
264
235
}
265
236
266
- private:
267
237
boost::asio::io_service& m_io_service;
238
+
239
+ private:
268
240
tcp::resolver m_resolver;
269
241
270
242
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
305
277
{
306
278
ctx->m_ssl_stream ->set_verify_mode (boost::asio::ssl::context::verify_none);
307
279
}
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));
314
280
}
281
+
282
+ ctx->m_socket .async_connect (endpoint, boost::bind (&linux_client::handle_connect, this , boost::asio::placeholders::error, ++endpoints, ctx));
315
283
}
316
284
}
317
285
@@ -325,7 +293,7 @@ namespace web { namespace http
325
293
}
326
294
else
327
295
{
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));
329
297
}
330
298
}
331
299
else if (endpoints == tcp::resolver::iterator ())
@@ -335,14 +303,17 @@ namespace web { namespace http
335
303
else
336
304
{
337
305
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
+
338
311
auto endpoint = *endpoints;
339
312
if (ctx->m_ssl_stream )
340
313
{
341
- ctx->m_ssl_stream ->lowest_layer ().shutdown (tcp::socket::shutdown_both, ignore);
342
- ctx->m_ssl_stream ->lowest_layer ().close ();
343
314
boost::asio::ssl::context context (boost::asio::ssl::context::sslv23);
344
315
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));
346
317
347
318
// Check to turn off server certificate verification.
348
319
if (client_config ().validate_certificates ())
@@ -354,17 +325,9 @@ namespace web { namespace http
354
325
{
355
326
ctx->m_ssl_stream ->set_verify_mode (boost::asio::ssl::context::verify_none);
356
327
}
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));
367
328
}
329
+
330
+ ctx->m_socket .async_connect (endpoint, boost::bind (&linux_client::handle_connect, this , boost::asio::placeholders::error, ++endpoints, ctx));
368
331
}
369
332
}
370
333
@@ -411,7 +374,7 @@ namespace web { namespace http
411
374
boost::asio::async_write (*ctx->m_ssl_stream , ctx->m_body_buf ,
412
375
boost::bind (readSize != 0 ? &linux_client::handle_write_chunked_body : &linux_client::handle_write_body, this , boost::asio::placeholders::error, ctx));
413
376
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 ,
415
378
boost::bind (readSize != 0 ? &linux_client::handle_write_chunked_body : &linux_client::handle_write_body, this , boost::asio::placeholders::error, ctx));
416
379
});
417
380
}
@@ -452,7 +415,7 @@ namespace web { namespace http
452
415
boost::asio::async_write (*ctx->m_ssl_stream , ctx->m_body_buf ,
453
416
boost::bind (&linux_client::handle_write_large_body, this , boost::asio::placeholders::error, ctx));
454
417
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 ,
456
419
boost::bind (&linux_client::handle_write_large_body, this , boost::asio::placeholders::error, ctx));
457
420
});
458
421
}
@@ -493,7 +456,7 @@ namespace web { namespace http
493
456
boost::asio::async_read_until (*ctx->m_ssl_stream , ctx->m_body_buf , CRLF+CRLF,
494
457
boost::bind (&linux_client::handle_status_line, this , boost::asio::placeholders::error, ctx));
495
458
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,
497
460
boost::bind (&linux_client::handle_status_line, this , boost::asio::placeholders::error, ctx));
498
461
}
499
462
else
@@ -591,7 +554,7 @@ namespace web { namespace http
591
554
boost::asio::async_read_until (*ctx->m_ssl_stream , ctx->m_body_buf , CRLF,
592
555
boost::bind (&linux_client::handle_chunk_header, this , boost::asio::placeholders::error, ctx));
593
556
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,
595
558
boost::bind (&linux_client::handle_chunk_header, this , boost::asio::placeholders::error, ctx));
596
559
}
597
560
}
@@ -611,7 +574,7 @@ namespace web { namespace http
611
574
{
612
575
if (ctx->m_body_buf .size () < size)
613
576
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);
615
578
}
616
579
}
617
580
@@ -689,7 +652,7 @@ namespace web { namespace http
689
652
boost::asio::async_read_until (*ctx->m_ssl_stream , ctx->m_body_buf , CRLF,
690
653
boost::bind (&linux_client::handle_chunk_header, this , boost::asio::placeholders::error, ctx));
691
654
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,
693
656
boost::bind (&linux_client::handle_chunk_header, this , boost::asio::placeholders::error, ctx));
694
657
});
695
658
}
@@ -783,4 +746,15 @@ namespace web { namespace http
783
746
return result_task;
784
747
}
785
748
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
+
786
760
}}}} // namespaces
0 commit comments