From 2b7729b4ab558b3e77aaf42e883fdaae89de2e5a Mon Sep 17 00:00:00 2001 From: Ryan Hurey Date: Tue, 25 Nov 2025 21:22:24 -0800 Subject: [PATCH] Windows fixes --- .../win_http_websockets_transport.hpp | 19 ++++++- .../src/http/winhttp/win_http_websockets.cpp | 54 +++++++++++++++++-- 2 files changed, 68 insertions(+), 5 deletions(-) diff --git a/sdk/core/azure-core/inc/azure/core/http/websockets/win_http_websockets_transport.hpp b/sdk/core/azure-core/inc/azure/core/http/websockets/win_http_websockets_transport.hpp index 2ad25060b..68fc2ee58 100644 --- a/sdk/core/azure-core/inc/azure/core/http/websockets/win_http_websockets_transport.hpp +++ b/sdk/core/azure-core/inc/azure/core/http/websockets/win_http_websockets_transport.hpp @@ -16,7 +16,24 @@ #include "azure/core/internal/unique_handle.hpp" #include #include + +#if defined(AZ_PLATFORM_WINDOWS) +// Need to include the WinHttpRequest for the method signature +namespace Azure { namespace Core { namespace Http { namespace _detail { + class WinHttpRequest; +}}}} +#endif + +#if defined(AZ_PLATFORM_WINDOWS) +#if !defined(WIN32_LEAN_AND_MEAN) +#define WIN32_LEAN_AND_MEAN +#endif +#if !defined(NOMINMAX) +#define NOMINMAX +#endif +#include #include +#endif namespace Azure { namespace Core { namespace Http { namespace WebSockets { @@ -31,7 +48,7 @@ namespace Azure { namespace Core { namespace Http { namespace WebSockets { // Fixed method signature to match current base class void OnUpgradedConnection( - std::unique_ptr<_detail::WinHttpRequest> const& request) override; + std::unique_ptr const& request) const override; public: /** diff --git a/sdk/core/azure-core/src/http/winhttp/win_http_websockets.cpp b/sdk/core/azure-core/src/http/winhttp/win_http_websockets.cpp index d1791f02f..d4cd029f1 100644 --- a/sdk/core/azure-core/src/http/winhttp/win_http_websockets.cpp +++ b/sdk/core/azure-core/src/http/winhttp/win_http_websockets.cpp @@ -7,7 +7,7 @@ #include "azure/core/http/websockets/win_http_websockets_transport.hpp" #include "azure/core/internal/diagnostics/log.hpp" #include "azure/core/platform.hpp" -#include "azure/core/request_failed_exception.hpp" +#include "azure/core/exception.hpp" #include "win_http_request.hpp" #if defined(AZ_PLATFORM_POSIX) @@ -20,6 +20,7 @@ #if !defined(NOMINMAX) #define NOMINMAX #endif +#include #include #include // for WSAPoll(); #include @@ -28,14 +29,59 @@ namespace Azure { namespace Core { namespace Http { namespace WebSockets { + namespace { + std::string GetErrorMessage(DWORD error) + { + std::string errorMessage = " Error Code: " + std::to_string(error); + + char* errorMsg = nullptr; + if (FormatMessageA( + FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_ALLOCATE_BUFFER, + GetModuleHandleA("winhttp.dll"), + error, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + reinterpret_cast(&errorMsg), + 0, + nullptr) + != 0) + { + // Use a unique_ptr to manage the lifetime of errorMsg. + std::unique_ptr errorString(errorMsg, &LocalFree); + errorMsg = nullptr; + + errorMessage += ": "; + errorMessage += errorString.get(); + // If the end of the error message is a CRLF, remove it. + if (errorMessage.back() == '\n') + { + errorMessage.erase(errorMessage.size() - 1); + if (errorMessage.back() == '\r') + { + errorMessage.erase(errorMessage.size() - 1); + } + } + } + errorMessage += '.'; + return errorMessage; + } + + void GetErrorAndThrow(const std::string& exceptionMessage, DWORD error = GetLastError()) + { + throw Azure::Core::Http::TransportException(exceptionMessage + GetErrorMessage(error)); + } + } // namespace + void WinHttpWebSocketTransport::OnUpgradedConnection( - std::unique_ptr<_detail::WinHttpRequest> const& request) + std::unique_ptr const& request) const { // Convert the request handle into a WebSocket handle for us to use later. - m_socketHandle = Azure::Core::_internal::UniqueHandle( + // Note: Need to cast away const because WinHttpWebSocketCompleteUpgrade modifies the handle state + auto* transport = const_cast(this); + + transport->m_socketHandle = Azure::Core::_internal::UniqueHandle( WinHttpWebSocketCompleteUpgrade(request->GetRequestHandle(), 0)); - if (!m_socketHandle) + if (!transport->m_socketHandle) { GetErrorAndThrow("Error Upgrading HttpRequest handle to WebSocket handle."); }