Implement Cancel for WinHTTP backend

This commit is contained in:
Tobias Taschner 2018-11-01 11:54:30 +01:00
parent 6bafed4ceb
commit 56af6cbdee
No known key found for this signature in database
GPG Key ID: AE6ECD71294F87FD
3 changed files with 25 additions and 12 deletions

View File

@ -106,6 +106,8 @@ protected:
bool CheckServerStatus();
static bool IsActiveState(State state);
private:
wxWebSession& m_session;
int m_id;
@ -141,10 +143,10 @@ public:
wxString AsString(wxMBConv* conv = NULL) const;
void ReportDataCompleted();
bool Init();
void Finalize();
virtual wxString GetFileName() const;
protected:

View File

@ -71,6 +71,11 @@ bool wxWebRequest::CheckServerStatus()
return true;
}
bool wxWebRequest::IsActiveState(State state)
{
return (state == State_Active || state == State_Unauthorized);
}
void wxWebRequest::SetData(const wxString& text, const wxString& contentType, const wxMBConv& conv)
{
m_dataText = text.mb_str(conv);
@ -100,15 +105,20 @@ void wxWebRequest::SetData(wxSharedPtr<wxInputStream> dataStream, const wxString
void wxWebRequest::SetState(State state, const wxString & failMsg)
{
// Add a reference while the request is active
if (state == State_Active && m_state != State_Active && m_state != State_Unauthorized)
if ( IsActiveState(state) && !IsActiveState(m_state) )
IncRef();
m_state = state;
// Trigger the event in the main thread
CallAfter(&wxWebRequest::ProcessStateEvent, state, failMsg);
}
void wxWebRequest::ProcessStateEvent(State state, const wxString& failMsg)
{
if (!IsActiveState(state) && GetResponse())
GetResponse()->Finalize();
wxString responseFileName;
wxWebRequestEvent evt(wxEVT_WEBREQUEST_STATE, GetId(), state,
@ -127,10 +137,8 @@ void wxWebRequest::ProcessStateEvent(State state, const wxString& failMsg)
wxRemoveFile(responseFileName);
// Remove reference after the request is no longer active
if (state == State_Completed || state == State_Failed ||
state == State_Cancelled)
if ( !IsActiveState(state) )
DecRef();
m_state = state;
}
//
@ -262,7 +270,7 @@ wxString wxWebResponse::GetFileName() const
return m_file.GetName();
}
void wxWebResponse::ReportDataCompleted()
void wxWebResponse::Finalize()
{
if ( m_request.GetStorage() == wxWebRequest::Storage_File )
m_file.Close();

View File

@ -176,14 +176,12 @@ void wxWebRequestWinHTTP::HandleCallback(DWORD dwInternetStatus,
case WINHTTP_CALLBACK_STATUS_READ_COMPLETE:
if ( dwStatusInformationLength > 0 )
{
if ( !m_response->ReportAvailableData(dwStatusInformationLength) )
if ( !m_response->ReportAvailableData(dwStatusInformationLength) &&
GetState() != State_Cancelled )
SetFailedWithLastError();
}
else
{
m_response->ReportDataCompleted();
SetState(State_Completed);
}
break;
case WINHTTP_CALLBACK_STATUS_WRITE_COMPLETE:
WriteData();
@ -340,7 +338,12 @@ void wxWebRequestWinHTTP::SendRequest()
void wxWebRequestWinHTTP::Cancel()
{
wxFAIL_MSG("not implemented");
SetState(State_Cancelled);
if ( m_request != NULL )
{
::WinHttpCloseHandle(m_request);
m_request = NULL;
}
}
wxWebResponse* wxWebRequestWinHTTP::GetResponse()