Skip to content
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

Make the HTTP transport behavior consistent between WinHTTP and libcurl by disabling automatically following redirects on Windows. #6018

Merged
merged 7 commits into from
Sep 24, 2024
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
2 changes: 2 additions & 0 deletions sdk/core/azure-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

### Bugs Fixed

- Make the HTTP transport behavior consistent between WinHTTP and libcurl by disabling automatically following redirects on Windows.

### Other Changes

## 1.14.0-beta.2 (2024-09-12)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ typedef struct x509_store_ctx_st X509_STORE_CTX;
namespace Azure { namespace Core {
namespace _detail {
/**
* @brief Unique handle for WinHTTP HINTERNET handles.
* @brief Unique handle for CURL handles.
*
* @note HINTERNET is declared as a "void *". This means that this definition subsumes all other
* `void *` types when used with Azure::Core::_internal::UniqueHandle.
* @note CURL is declared as a "void".
*
*/
template <> struct UniqueHandleHelper<CURL>
Expand Down
10 changes: 10 additions & 0 deletions sdk/core/azure-core/src/http/winhttp/win_http_transport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,16 @@ _detail::WinHttpRequest::WinHttpRequest(
}
}

DWORD disableRedirects = WINHTTP_DISABLE_REDIRECTS;
if (!WinHttpSetOption(
m_requestHandle.get(),
WINHTTP_OPTION_DISABLE_FEATURE,
&disableRedirects,
sizeof(disableRedirects)))
{
GetErrorAndThrow("Error while disabling redirects.");
}

// Set the callback function to be called whenever the state of the request handle changes.
m_httpAction = std::make_unique<_detail::WinHttpAction>(this);

Expand Down
15 changes: 15 additions & 0 deletions sdk/core/azure-core/test/ut/transport_adapter_base_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,21 @@ namespace Azure { namespace Core { namespace Test {
t1.join();
}

TEST_P(TransportAdapter, redirectsNotFollowed)
{
// We don't expect the transport adapter to follow redirects automatically to this url.
std::string redirectToUrl = AzureSdkHttpbinServer::ResponseHeaders("foo=bar");

Azure::Core::Http::Request request(
Azure::Core::Http::HttpMethod::Get,
Azure::Core::Url(AzureSdkHttpbinServer::RedirectTo(redirectToUrl)));

auto response = m_pipeline->Send(request, Context{});
EXPECT_EQ(response->GetStatusCode(), Azure::Core::Http::HttpStatusCode::Found);
EXPECT_TRUE(response->GetHeaders().find("location") != response->GetHeaders().end());
EXPECT_EQ(response->GetHeaders().at("location"), redirectToUrl);
}

TEST_P(TransportAdapter, cancelRequest)
{
Azure::Core::Url hostPath(AzureSdkHttpbinServer::Delay() + "/2"); // 2 seconds delay on server
Expand Down
8 changes: 8 additions & 0 deletions sdk/core/azure-core/test/ut/transport_adapter_base_test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ namespace Azure { namespace Core { namespace Test {
{
return Schema() + "://" + Host() + "/status/" + std::to_string(statusCode);
}
inline static std::string ResponseHeaders(std::string responseHeaders)
{
return Schema() + "://" + Host() + "/response-headers?" + responseHeaders;
}
inline static std::string RedirectTo(std::string redirectToUrl)
{
return Schema() + "://" + Host() + "/redirect-to?url=" + redirectToUrl;
}
inline static std::string Host() { return std::string(_detail::AzureSdkHttpbinServer); }
inline static std::string Schema() { return std::string(_detail::AzureSdkHttpbinServerSchema); }
};
Expand Down