From 56af6cbdeec4c88127d34ccf4313adf03e04896a Mon Sep 17 00:00:00 2001 From: Tobias Taschner Date: Thu, 1 Nov 2018 11:54:30 +0100 Subject: [PATCH] Implement Cancel for WinHTTP backend --- include/wx/webrequest.h | 6 ++++-- src/common/webrequest.cpp | 18 +++++++++++++----- src/msw/webrequest_winhttp.cpp | 13 ++++++++----- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/include/wx/webrequest.h b/include/wx/webrequest.h index 60d874266d..edd130c338 100644 --- a/include/wx/webrequest.h +++ b/include/wx/webrequest.h @@ -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: diff --git a/src/common/webrequest.cpp b/src/common/webrequest.cpp index be00b4a5be..bca423a6a7 100644 --- a/src/common/webrequest.cpp +++ b/src/common/webrequest.cpp @@ -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 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(); diff --git a/src/msw/webrequest_winhttp.cpp b/src/msw/webrequest_winhttp.cpp index 55397aac54..1d1d6bb617 100644 --- a/src/msw/webrequest_winhttp.cpp +++ b/src/msw/webrequest_winhttp.cpp @@ -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()