Skip to content

Commit ad246e5

Browse files
authored
Handle WINHTTP_OPTION_SECURE_PROTOCOLS failure (#917)
1 parent 313b107 commit ad246e5

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

Source/HTTP/WinHttp/winhttp_provider.cpp

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -413,10 +413,49 @@ Result<HINTERNET> WinHttpProvider::GetHSession(uint32_t securityProtocolFlags, c
413413
sizeof(securityProtocolFlags));
414414
if (!result)
415415
{
416-
HRESULT hr = HRESULT_FROM_WIN32(GetLastError());
417-
HC_TRACE_ERROR_HR(HTTPCLIENT, hr, "WinHttpProvider WinHttpSetOption WINHTTP_OPTION_SECURE_PROTOCOLS");
416+
DWORD lastErr = GetLastError();
417+
// Occasionally WinHttpSetOption(WINHTTP_OPTION_SECURE_PROTOCOLS) can fail on some
418+
// platforms / configurations (e.g. older OS versions or when specific protocol
419+
// flags are already implicitly enabled). The caller requested that we treat this
420+
// as non-fatal: emit a warning and proceed with the session using WinHTTP defaults.
421+
// If GetLastError() returned 0 (no extended error), fabricate a generic failure
422+
// HRESULT just for logging purposes.
423+
HRESULT hr = lastErr != 0 ? HRESULT_FROM_WIN32(lastErr) : E_FAIL;
424+
HC_TRACE_WARNING_HR(HTTPCLIENT, hr, "WinHttpProvider WinHttpSetOption WINHTTP_OPTION_SECURE_PROTOCOLS failed; retrying with WinHttpOpen WINHTTP_FLAG_ASYNC session");
425+
426+
// Retry strategy: Some platforms may not allow modifying secure protocols after
427+
// opening the session with WINHTTP_FLAG_SECURE_DEFAULTS. Re-open a plain ASYNC
428+
// session (no secure defaults) and try setting the option again.
418429
WinHttpCloseHandle(hSession);
419-
return hr;
430+
hSession = WinHttpOpen(
431+
nullptr,
432+
accessType,
433+
wProxyName.length() > 0 ? wProxyName.c_str() : WINHTTP_NO_PROXY_NAME,
434+
WINHTTP_NO_PROXY_BYPASS,
435+
WINHTTP_FLAG_ASYNC);
436+
if (hSession == nullptr)
437+
{
438+
HRESULT openHr = HRESULT_FROM_WIN32(GetLastError());
439+
HC_TRACE_WARNING_HR(HTTPCLIENT, openHr, "WinHttpProvider fallback WinHttpOpen with WINHTTP_FLAG_ASYNC failed; continuing without explicitly setting secure protocols");
440+
}
441+
else
442+
{
443+
auto retryResult = WinHttpSetOption(
444+
hSession,
445+
WINHTTP_OPTION_SECURE_PROTOCOLS,
446+
&securityProtocolFlags,
447+
sizeof(securityProtocolFlags));
448+
if (!retryResult)
449+
{
450+
DWORD retryErr = GetLastError();
451+
HRESULT retryHr = retryErr != 0 ? HRESULT_FROM_WIN32(retryErr) : E_FAIL;
452+
HC_TRACE_WARNING_HR(HTTPCLIENT, retryHr, "WinHttpProvider retry WinHttpSetOption WINHTTP_OPTION_SECURE_PROTOCOLS still failed; proceeding with WinHTTP defaults");
453+
}
454+
else
455+
{
456+
HC_TRACE_INFORMATION(HTTPCLIENT, "WinHttpProvider retry WinHttpSetOption WINHTTP_OPTION_SECURE_PROTOCOLS succeeded after reopening session");
457+
}
458+
}
420459
}
421460
}
422461

0 commit comments

Comments
 (0)