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:
@@ -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);
|
||||
};
|
||||
|
@@ -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,8 +496,12 @@ 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 )
|
||||
{
|
||||
wxLogLastError("WinHttpOpen");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Try to enable HTTP/2 (available since Win 10 1607)
|
||||
DWORD protFlags = WINHTTP_PROTOCOL_FLAG_HTTP2;
|
||||
::WinHttpSetOption(m_handle, WINHTTP_OPTION_ENABLE_HTTP_PROTOCOL,
|
||||
@@ -519,11 +522,8 @@ void wxWebSessionWinHTTP::Init()
|
||||
::WinHttpSetOption(m_handle, WINHTTP_OPTION_SECURE_PROTOCOLS,
|
||||
&securityFlags, sizeof(securityFlags));
|
||||
}
|
||||
}
|
||||
else
|
||||
wxLogLastError("WinHttpOpen");
|
||||
|
||||
m_initialized = true;
|
||||
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));
|
||||
|
Reference in New Issue
Block a user