22#include " HTTP/httpcall.h"
33#include " winhttp_provider.h"
44#include " winhttp_connection.h"
5+ #include " uri.h"
56
67#if HC_PLATFORM == HC_PLATFORM_GDK
78#include < XGameRuntimeFeature.h>
@@ -97,7 +98,7 @@ HRESULT WinHttpProvider::PerformAsync(
9798 RETURN_IF_FAILED (getSecurityInfoResult.hr );
9899
99100 // Get HSession for the call
100- auto getHSessionResult = GetHSession (getSecurityInfoResult.Payload ().enabledHttpSecurityProtocolFlags );
101+ auto getHSessionResult = GetHSession (getSecurityInfoResult.Payload ().enabledHttpSecurityProtocolFlags , callHandle-> url . data () );
101102 RETURN_IF_FAILED (getHSessionResult.hr );
102103
103104 std::unique_lock<std::mutex> lock{ m_lock };
@@ -152,7 +153,7 @@ HRESULT WinHttpProvider::ConnectAsync(
152153 RETURN_IF_FAILED (getSecurityInfoResult.hr );
153154
154155 // Get HSession for the call
155- auto getHSessionResult = GetHSession (getSecurityInfoResult.Payload ().enabledHttpSecurityProtocolFlags );
156+ auto getHSessionResult = GetHSession (getSecurityInfoResult.Payload ().enabledHttpSecurityProtocolFlags , uri. data () );
156157 RETURN_IF_FAILED (getHSessionResult.hr );
157158
158159 std::unique_lock<std::mutex> lock{ m_lock };
@@ -326,8 +327,25 @@ Result<XPlatSecurityInformation> WinHttpProvider::GetSecurityInformation(const c
326327#endif
327328}
328329
329- Result<HINTERNET> WinHttpProvider::GetHSession (uint32_t securityProtocolFlags)
330+ Result<HINTERNET> WinHttpProvider::GetHSession (uint32_t securityProtocolFlags, const char * url )
330331{
332+ // Parse URL to determine scheme
333+ xbox::httpclient::Uri uri (url);
334+ if (!uri.IsValid ())
335+ {
336+ return E_INVALIDARG;
337+ }
338+
339+ bool isHttps = uri.IsSecure ();
340+
341+ #if HC_PLATFORM == HC_PLATFORM_GDK
342+ // Log warning for insecure HTTP requests on GDK for console certification reasons
343+ if (!isHttps)
344+ {
345+ HC_TRACE_WARNING (HTTPCLIENT, " WARNING: Insecure HTTP request \" %s\" " , url);
346+ }
347+ #endif
348+
331349 std::lock_guard<std::mutex> lock (m_lock);
332350 auto iter = m_hSessions.find (securityProtocolFlags);
333351 if (iter != m_hSessions.end ())
@@ -342,31 +360,40 @@ Result<HINTERNET> WinHttpProvider::GetHSession(uint32_t securityProtocolFlags)
342360 m_proxyType = get_ie_proxy_info (proxy_protocol::https, proxyUri);
343361 GetProxyName (m_proxyType, proxyUri, accessType, wProxyName);
344362
363+ // Determine WinHTTP flags based on URL scheme
364+ // Use WINHTTP_FLAG_SECURE_DEFAULTS for HTTPS and WINHTTP_FLAG_ASYNC for HTTP
365+ DWORD openFlags;
366+ if (isHttps)
367+ {
368+ // For HTTPS, use secure defaults which implies WINHTTP_FLAG_ASYNC
369+ openFlags = WINHTTP_FLAG_SECURE_DEFAULTS;
370+ }
371+ else
372+ {
373+ // For HTTP, use async only (allow insecure connections)
374+ openFlags = WINHTTP_FLAG_ASYNC;
375+ }
376+
345377 HINTERNET hSession = WinHttpOpen (
346378 nullptr ,
347379 accessType,
348380 wProxyName.length () > 0 ? wProxyName.c_str () : WINHTTP_NO_PROXY_NAME,
349381 WINHTTP_NO_PROXY_BYPASS,
350- #if HC_PLATFORM == HC_PLATFORM_GDK
351- WINHTTP_FLAG_SECURE_DEFAULTS
352- #else
353- WINHTTP_FLAG_ASYNC
354- #endif
382+ openFlags
355383 );
356384
357- #if HC_PLATFORM == HC_PLATFORM_GDK
358385 DWORD error = GetLastError ();
359- if (error == ERROR_INVALID_PARAMETER)
386+ if (error == ERROR_INVALID_PARAMETER && isHttps )
360387 {
361- // This might happen on older Win10 PC versions that don't support WINHTTP_FLAG_SECURE_DEFAULTS
388+ // WINHTTP_FLAG_SECURE_DEFAULTS exists only on newer Windows versions;
389+ // on earlier OS releases we will receive ERROR_INVALID_PARAMETER and should continue without it.
362390 hSession = WinHttpOpen (
363391 nullptr ,
364- WINHTTP_ACCESS_TYPE_AUTOMATIC_PROXY ,
365- WINHTTP_NO_PROXY_NAME,
392+ accessType ,
393+ wProxyName. length () > 0 ? wProxyName. c_str () : WINHTTP_NO_PROXY_NAME,
366394 WINHTTP_NO_PROXY_BYPASS,
367395 WINHTTP_FLAG_ASYNC);
368396 }
369- #endif
370397
371398 if (hSession == nullptr )
372399 {
@@ -375,28 +402,34 @@ Result<HINTERNET> WinHttpProvider::GetHSession(uint32_t securityProtocolFlags)
375402 return hr;
376403 }
377404
378- auto result = WinHttpSetOption (
379- hSession,
380- WINHTTP_OPTION_SECURE_PROTOCOLS,
381- &securityProtocolFlags,
382- sizeof (securityProtocolFlags));
383- if (!result)
405+ // Only set secure protocols for HTTPS requests
406+ // For HTTP requests, ignore the security protocol settings as they don't apply
407+ if (isHttps)
384408 {
385- HRESULT hr = HRESULT_FROM_WIN32 (GetLastError ());
386- HC_TRACE_ERROR_HR (HTTPCLIENT, hr, " WinHttpProvider WinHttpSetOption" );
387- return hr;
409+ auto result = WinHttpSetOption (
410+ hSession,
411+ WINHTTP_OPTION_SECURE_PROTOCOLS,
412+ &securityProtocolFlags,
413+ sizeof (securityProtocolFlags));
414+ if (!result)
415+ {
416+ HRESULT hr = HRESULT_FROM_WIN32 (GetLastError ());
417+ HC_TRACE_ERROR_HR (HTTPCLIENT, hr, " WinHttpProvider WinHttpSetOption WINHTTP_OPTION_SECURE_PROTOCOLS" );
418+ WinHttpCloseHandle (hSession);
419+ return hr;
420+ }
388421 }
389422
390423 BOOL enableFallback = TRUE ;
391- result = WinHttpSetOption (
424+ auto result = WinHttpSetOption (
392425 hSession,
393426 WINHTTP_OPTION_IPV6_FAST_FALLBACK,
394427 &enableFallback,
395428 sizeof (enableFallback));
396429 if (!result)
397430 {
398431 HRESULT hr = HRESULT_FROM_WIN32 (GetLastError ());
399- HC_TRACE_WARNING_HR (HTTPCLIENT, hr, " WinHttpProvider WinHttpSetOption" );
432+ HC_TRACE_WARNING_HR (HTTPCLIENT, hr, " WinHttpProvider WinHttpSetOption WINHTTP_OPTION_IPV6_FAST_FALLBACK " );
400433 }
401434
402435 if (!m_globalProxy.empty ())
0 commit comments