Skip to content

Feature/http client asio freeze #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Release/src/http/client/http_client_asio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,10 @@ class asio_context : public request_context, public std::enable_shared_from_this
{
write_request();
}
else if (ec.value() == boost::system::errc::operation_canceled)
{
request_context::report_error(ec.value(), "Request canceled by user.");
}
else if (endpoints == tcp::resolver::iterator())
{
report_error("Failed to connect to any resolved endpoint", ec, httpclient_errorcode_context::connect);
Expand Down
33 changes: 33 additions & 0 deletions Release/tests/functional/http/client/connections_and_errors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
#include "cpprest/http_listener.h"
#endif

#include <chrono>
#include <thread>

using namespace web;
using namespace utility;
using namespace concurrency;
Expand Down Expand Up @@ -400,6 +403,36 @@ TEST_FIXTURE(uri_address, cancel_while_downloading_data)
}
#endif

// Try to connect to a server on a closed port and cancel the operation.
TEST_FIXTURE(uri_address, cancel_bad_port)
{
// http_client_asio had a bug where, when canceled, it would cancel only the
// current connection but then go and try the next address from the list of
// resolved addresses, i.e., it wouldn't actually cancel as long as there
// are more addresses to try. Consequently, it would not report the task as
// being canceled. This was easiest to observe when trying to connect to a
// server that does not respond on a certain port, otherwise the timing
// might be tricky.

// We need to connect to a URI for which there are multiple addresses
// associated (i.e., multiple A records).
web::http::uri uri(U("https://microsoft.com:442/"));

// Send request.
http_client c(uri);
web::http::http_request r;
auto cts = pplx::cancellation_token_source();
auto ct = cts.get_token();
auto t = c.request(r, ct);

// Make sure that the client already finished resolving before canceling,
// otherwise the bug might not be triggered.
std::this_thread::sleep_for(std::chrono::seconds(1));
cts.cancel();

VERIFY_THROWS_HTTP_ERROR_CODE(t.get(), std::errc::operation_canceled);
}

} // SUITE(connections_and_errors)

}}}}