12
12
#include < iterator>
13
13
#include < cstdint>
14
14
#include < boost/algorithm/string/trim.hpp>
15
- #include < boost/array.hpp>
16
15
#include < boost/asio/deadline_timer.hpp>
17
16
#include < boost/asio/placeholders.hpp>
18
17
#include < boost/asio/strand.hpp>
19
18
#include < boost/asio/streambuf.hpp>
20
19
#include < boost/assert.hpp>
21
- #include < boost/bind/protect.hpp>
22
20
#include < boost/logic/tribool.hpp>
23
21
#include < boost/network/constants.hpp>
24
22
#include < boost/network/detail/debug.hpp>
@@ -102,16 +100,19 @@ struct http_async_connection
102
100
string_type host_ = host (request);
103
101
std::uint16_t source_port = request.source_port ();
104
102
103
+ auto self = this ->shared_from_this ();
105
104
resolve_ (resolver_, host_, port_,
106
- request_strand_.wrap (boost::bind (
107
- &this_type::handle_resolved, this_type::shared_from_this (),
108
- host_, port_, source_port, get_body, callback, generator,
109
- boost::arg<1 >(), boost::arg<2 >())));
105
+ request_strand_.wrap (
106
+ [=] (boost::system ::error_code const &ec,
107
+ resolver_iterator_pair endpoint_range) {
108
+ self->handle_resolved (host_, port_, source_port, get_body,
109
+ callback, generator, ec, endpoint_range);
110
+ }));
110
111
if (timeout_ > 0 ) {
111
112
timer_.expires_from_now (boost::posix_time::seconds (timeout_));
112
- timer_.async_wait (request_strand_.wrap (
113
- boost::bind (&this_type::handle_timeout, this_type::shared_from_this (),
114
- boost::arg< 1 >()) ));
113
+ timer_.async_wait (request_strand_.wrap ([=] (boost:: system ::error_code const &ec) {
114
+ self-> handle_timeout (ec);
115
+ } ));
115
116
}
116
117
return response_;
117
118
}
@@ -145,13 +146,14 @@ struct http_async_connection
145
146
// that there's still more endpoints to try connecting to.
146
147
resolver_iterator iter = boost::begin (endpoint_range);
147
148
asio::ip::tcp::endpoint endpoint (iter->endpoint ().address (), port);
149
+ auto self = this ->shared_from_this ();
148
150
delegate_->connect (
149
151
endpoint, host, source_port,
150
- request_strand_.wrap (boost::bind (
151
- &this_type::handle_connected, this_type::shared_from_this (), host,
152
- port, source_port, get_body, callback, generator ,
153
- std::make_pair (++iter , resolver_iterator ()),
154
- placeholders::error) ));
152
+ request_strand_.wrap ([=] ( boost::system ::error_code const &ec) {
153
+ auto iter_copy = iter;
154
+ self-> handle_connected (host, port, source_port, get_body, callback,
155
+ generator, std::make_pair (++iter_copy , resolver_iterator ()), ec);
156
+ } ));
155
157
} else {
156
158
set_errors (ec ? ec : boost::asio::error::host_not_found);
157
159
boost::iterator_range<const char *> range;
@@ -169,23 +171,27 @@ struct http_async_connection
169
171
set_errors (asio::error::timed_out);
170
172
} else if (!ec) {
171
173
BOOST_ASSERT (delegate_.get () != 0 );
174
+ auto self = this ->shared_from_this ();
172
175
delegate_->write (
173
176
command_streambuf,
174
- request_strand_.wrap (boost::bind (
175
- &this_type::handle_sent_request, this_type::shared_from_this (),
176
- get_body, callback, generator, placeholders::error,
177
- placeholders::bytes_transferred)));
177
+ request_strand_.wrap ([=] (boost::system ::error_code const &ec,
178
+ std::size_t bytes_transferred) {
179
+ self->handle_sent_request (get_body, callback, generator,
180
+ ec, bytes_transferred);
181
+ }));
178
182
} else {
179
183
if (!boost::empty (endpoint_range)) {
180
184
resolver_iterator iter = boost::begin (endpoint_range);
181
185
asio::ip::tcp::endpoint endpoint (iter->endpoint ().address (), port);
186
+ auto self = this ->shared_from_this ();
182
187
delegate_->connect (
183
188
endpoint, host, source_port,
184
- request_strand_.wrap (boost::bind (
185
- &this_type::handle_connected, this_type::shared_from_this (),
186
- host, port, source_port, get_body, callback, generator,
187
- std::make_pair (++iter, resolver_iterator ()),
188
- placeholders::error)));
189
+ request_strand_.wrap ([=] (boost::system ::error_code const &ec) {
190
+ auto iter_copy = iter;
191
+ self->handle_connected (host, port, source_port, get_body, callback,
192
+ generator, std::make_pair (++iter_copy, resolver_iterator ()),
193
+ ec);
194
+ }));
189
195
} else {
190
196
set_errors (ec ? ec : boost::asio::error::host_not_found);
191
197
boost::iterator_range<const char *> range;
@@ -211,22 +217,27 @@ struct http_async_connection
211
217
std::copy (chunk.begin (), chunk.end (),
212
218
std::ostreambuf_iterator<typename char_<Tag>::type>(
213
219
&command_streambuf));
220
+ auto self = this ->shared_from_this ();
214
221
delegate_->write (
215
222
command_streambuf,
216
- request_strand_.wrap (boost::bind (
217
- &this_type::handle_sent_request,
218
- this_type::shared_from_this (), get_body, callback, generator,
219
- placeholders::error, placeholders::bytes_transferred)));
223
+ request_strand_.wrap ([=] (boost::system ::error_code const &ec,
224
+ std::size_t bytes_transferred) {
225
+ self->handle_sent_request (get_body, callback, generator,
226
+ ec, bytes_transferred);
227
+ }));
220
228
return ;
221
229
}
222
230
}
231
+
232
+ auto self = this ->shared_from_this ();
223
233
delegate_->read_some (
224
234
boost::asio::mutable_buffers_1 (this ->part .data (),
225
235
this ->part .size ()),
226
- request_strand_.wrap (boost::bind (
227
- &this_type::handle_received_data, this_type::shared_from_this (),
228
- version, get_body, callback, placeholders::error,
229
- placeholders::bytes_transferred)));
236
+ request_strand_.wrap ([=] (boost::system ::error_code const &ec,
237
+ std::size_t bytes_transferred) {
238
+ self->handle_received_data (version, get_body, callback,
239
+ ec, bytes_transferred);
240
+ }));
230
241
} else {
231
242
set_errors (is_timedout_ ? asio::error::timed_out : ec);
232
243
}
@@ -248,15 +259,17 @@ struct http_async_connection
248
259
(!ec || ec == boost::asio::error::eof || is_ssl_short_read_error)) {
249
260
logic::tribool parsed_ok;
250
261
size_t remainder ;
262
+ auto self = this ->shared_from_this ();
251
263
switch (state) {
252
264
case version:
253
265
if (ec == boost::asio::error::eof) return ;
254
266
parsed_ok = this ->parse_version (
255
267
delegate_,
256
- request_strand_.wrap (boost::bind (
257
- &this_type::handle_received_data,
258
- this_type::shared_from_this (), version, get_body, callback,
259
- placeholders::error, placeholders::bytes_transferred)),
268
+ request_strand_.wrap ([=] (boost::system ::error_code const &ec,
269
+ std::size_t bytes_transferred) {
270
+ self->handle_received_data (version, get_body, callback,
271
+ ec, bytes_transferred);
272
+ }),
260
273
bytes_transferred);
261
274
if (!parsed_ok || indeterminate (parsed_ok)) {
262
275
return ;
@@ -265,22 +278,23 @@ struct http_async_connection
265
278
if (ec == boost::asio::error::eof) return ;
266
279
parsed_ok = this ->parse_status (
267
280
delegate_,
268
- request_strand_.wrap (boost::bind (
269
- &this_type::handle_received_data,
270
- this_type::shared_from_this (), status, get_body, callback,
271
- placeholders::error, placeholders::bytes_transferred)),
281
+ request_strand_.wrap ([=] (boost::system ::error_code const &ec,
282
+ std::size_t bytes_transferred) {
283
+ self->handle_received_data (status, get_body, callback,
284
+ ec, bytes_transferred);
285
+ }),
272
286
bytes_transferred);
273
287
if (!parsed_ok || indeterminate (parsed_ok)) {
274
288
return ;
275
289
}
276
290
case status_message:
277
291
if (ec == boost::asio::error::eof) return ;
278
292
parsed_ok = this ->parse_status_message (
279
- delegate_, request_strand_.wrap (boost::bind (
280
- &this_type::handle_received_data,
281
- this_type::shared_from_this (), status_message ,
282
- get_body, callback, placeholders::error,
283
- placeholders::bytes_transferred) ),
293
+ delegate_, request_strand_.wrap ([=] ( boost::system ::error_code const &,
294
+ std:: size_t bytes_transferred) {
295
+ self-> handle_received_data (status_message, get_body, callback ,
296
+ ec, bytes_transferred);
297
+ } ),
284
298
bytes_transferred);
285
299
if (!parsed_ok || indeterminate (parsed_ok)) {
286
300
return ;
@@ -293,10 +307,11 @@ struct http_async_connection
293
307
// to get more data for the body is scheduled.
294
308
fusion::tie (parsed_ok, remainder ) = this ->parse_headers (
295
309
delegate_,
296
- request_strand_.wrap (boost::bind (
297
- &this_type::handle_received_data,
298
- this_type::shared_from_this (), headers, get_body, callback,
299
- placeholders::error, placeholders::bytes_transferred)),
310
+ request_strand_.wrap ([=] (boost::system ::error_code const &ec,
311
+ std::size_t bytes_transferred) {
312
+ self->handle_received_data (headers, get_body, callback,
313
+ ec, bytes_transferred);
314
+ }),
300
315
bytes_transferred);
301
316
302
317
if (!parsed_ok || indeterminate (parsed_ok)) {
@@ -335,22 +350,26 @@ struct http_async_connection
335
350
// wait before scheduling another read.
336
351
callback (make_iterator_range (begin, end), ec);
337
352
353
+ auto self = this ->shared_from_this ();
338
354
delegate_->read_some (
339
355
boost::asio::mutable_buffers_1 (this ->part .data (),
340
356
this ->part .size ()),
341
- request_strand_.wrap (boost::bind (
342
- &this_type::handle_received_data,
343
- this_type::shared_from_this (), body, get_body, callback,
344
- placeholders::error, placeholders::bytes_transferred)));
357
+ request_strand_.wrap ([=] (boost::system ::error_code const &ec,
358
+ std::size_t bytes_transferred) {
359
+ self->handle_received_data (body, get_body, callback,
360
+ ec, bytes_transferred);
361
+ }));
345
362
} else {
346
363
// Here we handle the body data ourself and append to an
347
364
// ever-growing string buffer.
365
+ auto self = this ->shared_from_this ();
348
366
this ->parse_body (
349
367
delegate_,
350
- request_strand_.wrap (boost::bind (
351
- &this_type::handle_received_data,
352
- this_type::shared_from_this (), body, get_body, callback,
353
- placeholders::error, placeholders::bytes_transferred)),
368
+ request_strand_.wrap ([=] (boost::system ::error_code const &ec,
369
+ std::size_t bytes_transferred) {
370
+ self->handle_received_data (body, get_body, callback,
371
+ ec, bytes_transferred);
372
+ }),
354
373
remainder );
355
374
}
356
375
return ;
@@ -399,23 +418,26 @@ struct http_async_connection
399
418
typename protocol_base::buffer_type::const_iterator end = begin;
400
419
std::advance (end, bytes_transferred);
401
420
callback (make_iterator_range (begin, end), ec);
421
+ auto self = this ->shared_from_this ();
402
422
delegate_->read_some (
403
423
boost::asio::mutable_buffers_1 (this ->part .data (),
404
424
this ->part .size ()),
405
- request_strand_.wrap (boost::bind (
406
- &this_type::handle_received_data,
407
- this_type::shared_from_this (), body, get_body, callback,
408
- placeholders::error, placeholders::bytes_transferred)));
425
+ request_strand_.wrap ([=] (boost::system ::error_code const &ec,
426
+ std::size_t bytes_transferred) {
427
+ self->handle_received_data (body, get_body, callback,
428
+ ec, bytes_transferred);
429
+ }));
409
430
} else {
410
431
// Here we don't have a body callback. Let's make sure that we
411
432
// deal with the remainder from the headers part in case we do
412
433
// have data that's still in the buffer.
413
434
this ->parse_body (
414
435
delegate_,
415
- request_strand_.wrap (boost::bind (
416
- &this_type::handle_received_data,
417
- this_type::shared_from_this (), body, get_body, callback,
418
- placeholders::error, placeholders::bytes_transferred)),
436
+ request_strand_.wrap ([=] (boost::system ::error_code const &ec,
437
+ std::size_t bytes_transferred) {
438
+ self->handle_received_data (body, get_body, callback,
439
+ ec, bytes_transferred);
440
+ }),
419
441
bytes_transferred);
420
442
}
421
443
}
0 commit comments