Skip to content

Commit e0d354a

Browse files
committed
Merge branch 'thomasschaub-feature/http_client_asio-freeze' into development
2 parents 53293e4 + 124609d commit e0d354a

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

Release/src/http/client/http_client_asio.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,10 @@ class asio_context : public request_context, public std::enable_shared_from_this
792792
{
793793
write_request();
794794
}
795+
else if (ec.value() == boost::system::errc::operation_canceled)
796+
{
797+
request_context::report_error(ec.value(), "Request canceled by user.");
798+
}
795799
else if (endpoints == tcp::resolver::iterator())
796800
{
797801
report_error("Failed to connect to any resolved endpoint", ec, httpclient_errorcode_context::connect);

Release/tests/functional/http/client/connections_and_errors.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
#include "cpprest/http_listener.h"
2828
#endif
2929

30+
#include <chrono>
31+
#include <thread>
32+
3033
using namespace web;
3134
using namespace utility;
3235
using namespace concurrency;
@@ -400,6 +403,36 @@ TEST_FIXTURE(uri_address, cancel_while_downloading_data)
400403
}
401404
#endif
402405

406+
// Try to connect to a server on a closed port and cancel the operation.
407+
TEST_FIXTURE(uri_address, cancel_bad_port)
408+
{
409+
// http_client_asio had a bug where, when canceled, it would cancel only the
410+
// current connection but then go and try the next address from the list of
411+
// resolved addresses, i.e., it wouldn't actually cancel as long as there
412+
// are more addresses to try. Consequently, it would not report the task as
413+
// being canceled. This was easiest to observe when trying to connect to a
414+
// server that does not respond on a certain port, otherwise the timing
415+
// might be tricky.
416+
417+
// We need to connect to a URI for which there are multiple addresses
418+
// associated (i.e., multiple A records).
419+
web::http::uri uri(U("https://microsoft.com:442/"));
420+
421+
// Send request.
422+
http_client c(uri);
423+
web::http::http_request r;
424+
auto cts = pplx::cancellation_token_source();
425+
auto ct = cts.get_token();
426+
auto t = c.request(r, ct);
427+
428+
// Make sure that the client already finished resolving before canceling,
429+
// otherwise the bug might not be triggered.
430+
std::this_thread::sleep_for(std::chrono::seconds(1));
431+
cts.cancel();
432+
433+
VERIFY_THROWS_HTTP_ERROR_CODE(t.get(), std::errc::operation_canceled);
434+
}
435+
403436
} // SUITE(connections_and_errors)
404437

405438
}}}}

0 commit comments

Comments
 (0)