Start wxWebRequestWinHTTP implementation

Can already run the GET request in the sample app
This commit is contained in:
Tobias Taschner
2018-10-20 19:21:35 +02:00
parent e07c1bf40c
commit a4279752f8
9 changed files with 676 additions and 26 deletions

View File

@@ -18,6 +18,11 @@
#include "wx/webrequest.h"
#ifndef WX_PRECOMP
#include "wx/app.h"
#include "wx/translation.h"
#endif
#if defined(__WINDOWS__)
#include "wx/msw/webrequest_winhttp.h"
#endif
@@ -34,13 +39,86 @@ wxDEFINE_EVENT(wxEVT_WEBREQUEST_READY, wxWebRequestEvent);
wxDEFINE_EVENT(wxEVT_WEBREQUEST_FAILED, wxWebRequestEvent);
wxDEFINE_EVENT(wxEVT_WEBREQUEST_AUTH_REQUIRED, wxWebRequestEvent);
//
// wxWebRequest
//
bool wxWebRequest::CheckServerStatus()
{
const wxWebResponse* resp = GetResponse();
if ( resp && resp->GetStatus() >= 400 && !m_ignoreServerErrorStatus )
{
SetState(State_Failed, wxString::Format(_("Error: %s (%d)"),
resp->GetStatusText(), resp->GetStatus()));
return false;
}
else
return true;
}
void wxWebRequest::SetState(State state, const wxString & failMsg)
{
switch (state)
{
case State_Active:
// Add a reference while the request is active
if ( m_state != State_Active )
{
IncRef();
m_state = state;
}
break;
case State_Ready:
// Trigger the ready event in main thread
CallAfter(&wxWebRequest::ProcessReadyEvent);
break;
case State_Failed:
m_failMessage = failMsg;
// Trigger the failed event in main thread
CallAfter(&wxWebRequest::ProcessFailedEvent);
break;
}
}
void wxWebRequest::ProcessReadyEvent()
{
wxWebRequestEvent evt(wxEVT_WEBREQUEST_READY, GetId(), GetResponse());
ProcessEvent(evt);
// Remove reference after the request is no longer active
if ( m_state == State_Active )
DecRef();
m_state = State_Ready;
}
void wxWebRequest::ProcessFailedEvent()
{
wxWebRequestEvent evt(wxEVT_WEBREQUEST_FAILED, GetId(), NULL,
m_failMessage);
ProcessEvent(evt);
// Remove reference after the request is no longer active
if ( m_state == State_Active )
DecRef();
m_state = State_Failed;
}
//
// wxWebSession
//
wxScopedPtr<wxWebSession> wxWebSession::ms_defaultSession;
wxStringWebSessionFactoryMap wxWebSession::ms_factoryMap;
wxWebSession::wxWebSession()
{
// Initialize the user-Agent header with a reasonable default
SetHeader("User-Agent", wxString::Format("%s/1 wxWidgets/%d.%d.%d",
wxTheApp->GetAppName(),
wxMAJOR_VERSION, wxMINOR_VERSION, wxRELEASE_NUMBER));
}
// static
wxWebSession& wxWebSession::GetDefault()
{
if (ms_defaultSession == NULL)
if ( ms_defaultSession == NULL )
ms_defaultSession.reset(wxWebSession::New());
return *ms_defaultSession;
@@ -49,11 +127,11 @@ wxWebSession& wxWebSession::GetDefault()
// static
wxWebSession* wxWebSession::New(const wxString& backend)
{
if (ms_factoryMap.empty())
if ( ms_factoryMap.empty() )
InitFactoryMap();
wxStringWebSessionFactoryMap::iterator factory = ms_factoryMap.find(backend);
if (factory != ms_factoryMap.end())
if ( factory != ms_factoryMap.end() )
return factory->second->Create();
else
return NULL;
@@ -77,13 +155,11 @@ void wxWebSession::InitFactoryMap()
// static
bool wxWebSession::IsBackendAvailable(const wxString& backend)
{
if (ms_factoryMap.empty())
if ( ms_factoryMap.empty() )
InitFactoryMap();
wxStringWebSessionFactoryMap::iterator factory = ms_factoryMap.find(backend);
return factory != ms_factoryMap.end();
}
#endif // wxUSE_WEBREQUEST