From c70ac66200829fb8f3fe577f6596fc2f21dde683 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 9 Jan 2021 16:56:04 +0100 Subject: [PATCH] Simplify and make more robust wxWebSessionWinHTTP initialization Rename Init() to Open() as we need this method to return bool to indicate its success in order to avoid using non-initialized handle later. Init() is also reserved, by convention, for the common part of all class ctors in wx code. Remove m_initialized entirely, it doesn't seem to be obviously better to cache the failure to create a session than to retry doing it every time (in fact, it would seem to be worse) and not having it is simpler. This commit is best viewed ignoring white space. --- include/wx/msw/private/webrequest_winhttp.h | 3 +- src/msw/webrequest_winhttp.cpp | 59 +++++++++++---------- 2 files changed, 32 insertions(+), 30 deletions(-) diff --git a/include/wx/msw/private/webrequest_winhttp.h b/include/wx/msw/private/webrequest_winhttp.h index 91620bc444..dca28f6ce5 100644 --- a/include/wx/msw/private/webrequest_winhttp.h +++ b/include/wx/msw/private/webrequest_winhttp.h @@ -138,10 +138,9 @@ public: HINTERNET GetHandle() const { return m_handle; } private: - bool m_initialized; HINTERNET m_handle; - void Init(); + bool Open(); wxDECLARE_NO_COPY_CLASS(wxWebSessionWinHTTP); }; diff --git a/src/msw/webrequest_winhttp.cpp b/src/msw/webrequest_winhttp.cpp index 49532ea78a..a56ce2c585 100644 --- a/src/msw/webrequest_winhttp.cpp +++ b/src/msw/webrequest_winhttp.cpp @@ -475,7 +475,6 @@ void wxWebAuthChallengeWinHTTP::SetCredentials(const wxString& user, // wxWebSessionWinHTTP::wxWebSessionWinHTTP(): - m_initialized(false), m_handle(NULL) { } @@ -486,7 +485,7 @@ wxWebSessionWinHTTP::~wxWebSessionWinHTTP() ::WinHttpCloseHandle(m_handle); } -void wxWebSessionWinHTTP::Init() +bool wxWebSessionWinHTTP::Open() { DWORD accessType; if ( wxCheckOsVersion(6, 3) ) @@ -497,33 +496,34 @@ void wxWebSessionWinHTTP::Init() m_handle = ::WinHttpOpen(GetHeaders().find("User-Agent")->second.wc_str(), accessType, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, WINHTTP_FLAG_ASYNC); - if ( m_handle != NULL ) + if ( !m_handle ) { - // Try to enable HTTP/2 (available since Win 10 1607) - DWORD protFlags = WINHTTP_PROTOCOL_FLAG_HTTP2; - ::WinHttpSetOption(m_handle, WINHTTP_OPTION_ENABLE_HTTP_PROTOCOL, - &protFlags, sizeof(protFlags)); - - // Try to enable GZIP and DEFLATE (available since Win 8.1) - DWORD decompressFlags = WINHTTP_DECOMPRESSION_FLAG_ALL; - ::WinHttpSetOption(m_handle, WINHTTP_OPTION_DECOMPRESSION, - &decompressFlags, sizeof(decompressFlags)); - - // Try to enable modern TLS for older Windows versions - if ( !wxCheckOsVersion(6, 3) ) - { - DWORD securityFlags = WINHTTP_FLAG_SECURE_PROTOCOL_SSL3 | - WINHTTP_FLAG_SECURE_PROTOCOL_TLS1 | - WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_1 | - WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2; - ::WinHttpSetOption(m_handle, WINHTTP_OPTION_SECURE_PROTOCOLS, - &securityFlags, sizeof(securityFlags)); - } - } - else wxLogLastError("WinHttpOpen"); + return false; + } - m_initialized = true; + // Try to enable HTTP/2 (available since Win 10 1607) + DWORD protFlags = WINHTTP_PROTOCOL_FLAG_HTTP2; + ::WinHttpSetOption(m_handle, WINHTTP_OPTION_ENABLE_HTTP_PROTOCOL, + &protFlags, sizeof(protFlags)); + + // Try to enable GZIP and DEFLATE (available since Win 8.1) + DWORD decompressFlags = WINHTTP_DECOMPRESSION_FLAG_ALL; + ::WinHttpSetOption(m_handle, WINHTTP_OPTION_DECOMPRESSION, + &decompressFlags, sizeof(decompressFlags)); + + // Try to enable modern TLS for older Windows versions + if ( !wxCheckOsVersion(6, 3) ) + { + DWORD securityFlags = WINHTTP_FLAG_SECURE_PROTOCOL_SSL3 | + WINHTTP_FLAG_SECURE_PROTOCOL_TLS1 | + WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_1 | + WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2; + ::WinHttpSetOption(m_handle, WINHTTP_OPTION_SECURE_PROTOCOLS, + &securityFlags, sizeof(securityFlags)); + } + + return true; } wxWebRequestImplPtr @@ -532,8 +532,11 @@ wxWebSessionWinHTTP::CreateRequest(wxWebSession& session, const wxString& url, int id) { - if ( !m_initialized ) - Init(); + if ( !m_handle ) + { + if ( !Open() ) + return wxWebRequestImplPtr(); + } return wxWebRequestImplPtr( new wxWebRequestWinHTTP(session, *this, handler, url, id));