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.
This commit is contained in:
Vadim Zeitlin
2021-01-09 16:56:04 +01:00
parent ec2ea5c7fa
commit c70ac66200
2 changed files with 32 additions and 30 deletions

View File

@@ -138,10 +138,9 @@ public:
HINTERNET GetHandle() const { return m_handle; } HINTERNET GetHandle() const { return m_handle; }
private: private:
bool m_initialized;
HINTERNET m_handle; HINTERNET m_handle;
void Init(); bool Open();
wxDECLARE_NO_COPY_CLASS(wxWebSessionWinHTTP); wxDECLARE_NO_COPY_CLASS(wxWebSessionWinHTTP);
}; };

View File

@@ -475,7 +475,6 @@ void wxWebAuthChallengeWinHTTP::SetCredentials(const wxString& user,
// //
wxWebSessionWinHTTP::wxWebSessionWinHTTP(): wxWebSessionWinHTTP::wxWebSessionWinHTTP():
m_initialized(false),
m_handle(NULL) m_handle(NULL)
{ {
} }
@@ -486,7 +485,7 @@ wxWebSessionWinHTTP::~wxWebSessionWinHTTP()
::WinHttpCloseHandle(m_handle); ::WinHttpCloseHandle(m_handle);
} }
void wxWebSessionWinHTTP::Init() bool wxWebSessionWinHTTP::Open()
{ {
DWORD accessType; DWORD accessType;
if ( wxCheckOsVersion(6, 3) ) if ( wxCheckOsVersion(6, 3) )
@@ -497,8 +496,12 @@ void wxWebSessionWinHTTP::Init()
m_handle = ::WinHttpOpen(GetHeaders().find("User-Agent")->second.wc_str(), accessType, m_handle = ::WinHttpOpen(GetHeaders().find("User-Agent")->second.wc_str(), accessType,
WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS,
WINHTTP_FLAG_ASYNC); WINHTTP_FLAG_ASYNC);
if ( m_handle != NULL ) if ( !m_handle )
{ {
wxLogLastError("WinHttpOpen");
return false;
}
// Try to enable HTTP/2 (available since Win 10 1607) // Try to enable HTTP/2 (available since Win 10 1607)
DWORD protFlags = WINHTTP_PROTOCOL_FLAG_HTTP2; DWORD protFlags = WINHTTP_PROTOCOL_FLAG_HTTP2;
::WinHttpSetOption(m_handle, WINHTTP_OPTION_ENABLE_HTTP_PROTOCOL, ::WinHttpSetOption(m_handle, WINHTTP_OPTION_ENABLE_HTTP_PROTOCOL,
@@ -519,11 +522,8 @@ void wxWebSessionWinHTTP::Init()
::WinHttpSetOption(m_handle, WINHTTP_OPTION_SECURE_PROTOCOLS, ::WinHttpSetOption(m_handle, WINHTTP_OPTION_SECURE_PROTOCOLS,
&securityFlags, sizeof(securityFlags)); &securityFlags, sizeof(securityFlags));
} }
}
else
wxLogLastError("WinHttpOpen");
m_initialized = true; return true;
} }
wxWebRequestImplPtr wxWebRequestImplPtr
@@ -532,8 +532,11 @@ wxWebSessionWinHTTP::CreateRequest(wxWebSession& session,
const wxString& url, const wxString& url,
int id) int id)
{ {
if ( !m_initialized ) if ( !m_handle )
Init(); {
if ( !Open() )
return wxWebRequestImplPtr();
}
return wxWebRequestImplPtr( return wxWebRequestImplPtr(
new wxWebRequestWinHTTP(session, *this, handler, url, id)); new wxWebRequestWinHTTP(session, *this, handler, url, id));