Implement Cancel for WinHTTP backend

This commit is contained in:
Tobias Taschner
2018-11-01 11:54:30 +01:00
parent 6bafed4ceb
commit 56af6cbdee
3 changed files with 25 additions and 12 deletions

View File

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

View File

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

View File

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