Skip to content

Commit d23bfb2

Browse files
committed
Merge pull request cpp-netlib#641 from gb-krk/body_callback_fix
Body callback function is not called when some errors occur (timeout etc.) cpp-netlib#640
2 parents f2095b6 + a1e16f3 commit d23bfb2

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

boost/network/protocol/http/client/connection/async_normal.hpp

+13-11
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ struct http_async_connection
118118
}
119119

120120
private:
121-
void set_errors(std::error_code const& ec) {
121+
void set_errors(std::error_code const& ec, body_callback_function_type callback) {
122122
std::system_error error(ec);
123123
this->version_promise.set_exception(std::make_exception_ptr(error));
124124
this->status_promise.set_exception(std::make_exception_ptr(error));
@@ -127,6 +127,8 @@ struct http_async_connection
127127
this->source_promise.set_exception(std::make_exception_ptr(error));
128128
this->destination_promise.set_exception(std::make_exception_ptr(error));
129129
this->body_promise.set_exception(std::make_exception_ptr(error));
130+
if ( callback )
131+
callback( boost::iterator_range<const char*>(), ec );
130132
this->timer_.cancel();
131133
}
132134

@@ -155,9 +157,7 @@ struct http_async_connection
155157
generator, std::make_pair(++iter_copy, resolver_iterator()), ec);
156158
}));
157159
} else {
158-
set_errors(ec ? ec : ::asio::error::host_not_found);
159-
boost::iterator_range<const char*> range;
160-
if (callback) callback(range, ec);
160+
set_errors((ec ? ec : ::asio::error::host_not_found), callback);
161161
}
162162
}
163163

@@ -168,7 +168,7 @@ struct http_async_connection
168168
resolver_iterator_pair endpoint_range,
169169
std::error_code const& ec) {
170170
if (is_timedout_) {
171-
set_errors(::asio::error::timed_out);
171+
set_errors(::asio::error::timed_out, callback);
172172
} else if (!ec) {
173173
BOOST_ASSERT(delegate_.get() != 0);
174174
auto self = this->shared_from_this();
@@ -193,9 +193,7 @@ struct http_async_connection
193193
ec);
194194
}));
195195
} else {
196-
set_errors(ec ? ec : ::asio::error::host_not_found);
197-
boost::iterator_range<const char*> range;
198-
if (callback) callback(range, ec);
196+
set_errors((ec ? ec : ::asio::error::host_not_found), callback);
199197
}
200198
}
201199
}
@@ -239,7 +237,7 @@ struct http_async_connection
239237
ec, bytes_transferred);
240238
}));
241239
} else {
242-
set_errors(is_timedout_ ? ::asio::error::timed_out : ec);
240+
set_errors((is_timedout_ ? ::asio::error::timed_out : ec), callback);
243241
}
244242
}
245243

@@ -322,6 +320,8 @@ struct http_async_connection
322320
// We short-circuit here because the user does not want to get the
323321
// body (in the case of a HEAD request).
324322
this->body_promise.set_value("");
323+
if ( callback )
324+
callback( boost::iterator_range<const char*>(), ::asio::error::eof );
325325
this->destination_promise.set_value("");
326326
this->source_promise.set_value("");
327327
// this->part.assign('\0');
@@ -446,8 +446,8 @@ struct http_async_connection
446446
BOOST_ASSERT(false && "Bug, report this to the developers!");
447447
}
448448
} else {
449-
std::system_error error(is_timedout_ ? ::asio::error::timed_out
450-
: ec);
449+
std::error_code report_code = is_timedout_ ? ::asio::error::timed_out : ec;
450+
std::system_error error(report_code);
451451
this->source_promise.set_exception(std::make_exception_ptr(error));
452452
this->destination_promise.set_exception(std::make_exception_ptr(error));
453453
switch (state) {
@@ -467,6 +467,8 @@ struct http_async_connection
467467
// so no exception should be set
468468
this->body_promise.set_exception(std::make_exception_ptr(error));
469469
}
470+
else
471+
callback( boost::iterator_range<const char*>(), report_code );
470472
break;
471473
default:
472474
BOOST_ASSERT(false && "Bug, report this to the developers!");

0 commit comments

Comments
 (0)