Skip to content

Commit cac9adf

Browse files
authored
Merge pull request microsoft#566 from deeringc/auth_proxy_redirect
Fix an issue where requests sent via authenticated proxies could not be redirected
2 parents 50e81a5 + d305566 commit cac9adf

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

Release/src/http/client/http_client_winhttp.cpp

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,25 @@ class winhttp_client : public _http_client_communicator
10411041
}
10421042
}
10431043

1044+
static utility::string_t get_request_url(HINTERNET hRequestHandle)
1045+
{
1046+
DWORD urlSize{ 0 };
1047+
if(FALSE == WinHttpQueryOption(hRequestHandle, WINHTTP_OPTION_URL, nullptr, &urlSize) || urlSize == 0)
1048+
{
1049+
return U("");
1050+
}
1051+
1052+
auto urlwchar = new WCHAR[urlSize / sizeof(WCHAR)];
1053+
1054+
WinHttpQueryOption(hRequestHandle, WINHTTP_OPTION_URL, (void*)urlwchar, &urlSize);
1055+
1056+
utility::string_t url(urlwchar);
1057+
1058+
delete[] urlwchar;
1059+
1060+
return url;
1061+
}
1062+
10441063
// Returns true if we handle successfully and resending the request
10451064
// or false if we fail to handle.
10461065
static bool handle_authentication_failure(
@@ -1097,10 +1116,22 @@ class winhttp_client : public _http_client_communicator
10971116
cred = p_request_context->m_http_client->client_config().credentials();
10981117
p_request_context->m_server_authentication_tried = true;
10991118
}
1100-
else if (dwAuthTarget == WINHTTP_AUTH_TARGET_PROXY && !p_request_context->m_proxy_authentication_tried)
1119+
else if (dwAuthTarget == WINHTTP_AUTH_TARGET_PROXY)
11011120
{
1102-
cred = p_request_context->m_http_client->client_config().proxy().credentials();
1103-
p_request_context->m_proxy_authentication_tried = true;
1121+
bool is_redirect = false;
1122+
try
1123+
{
1124+
web::uri current_uri(get_request_url(hRequestHandle));
1125+
is_redirect = p_request_context->m_request.absolute_uri().to_string() != current_uri.to_string();
1126+
}
1127+
catch (const std::exception&) {}
1128+
1129+
// If we have been redirected, then WinHttp needs the proxy credentials again to make the next request leg (which may be on a different server)
1130+
if (is_redirect || !p_request_context->m_proxy_authentication_tried)
1131+
{
1132+
cred = p_request_context->m_http_client->client_config().proxy().credentials();
1133+
p_request_context->m_proxy_authentication_tried = true;
1134+
}
11041135
}
11051136

11061137
// No credentials found so can't resend.

0 commit comments

Comments
 (0)