From 6530e3c08e4007a600147a6c68b10d919274e670 Mon Sep 17 00:00:00 2001 From: Tobias Taschner Date: Mon, 29 Oct 2018 23:00:08 +0100 Subject: [PATCH] Move response data handling to base class --- include/wx/msw/webrequest_winhttp.h | 11 +------ include/wx/webrequest.h | 35 +++++++++++++++------- src/common/webrequest.cpp | 36 +++++++++++++++++++++++ src/msw/webrequest_winhttp.cpp | 45 +++++++---------------------- 4 files changed, 72 insertions(+), 55 deletions(-) diff --git a/include/wx/msw/webrequest_winhttp.h b/include/wx/msw/webrequest_winhttp.h index b6e707cdc4..b23d7e2dff 100644 --- a/include/wx/msw/webrequest_winhttp.h +++ b/include/wx/msw/webrequest_winhttp.h @@ -32,22 +32,13 @@ public: wxString GetStatusText() const wxOVERRIDE; - wxInputStream* GetStream() const wxOVERRIDE; - - wxString AsString(wxMBConv* conv = NULL) const wxOVERRIDE; - bool ReadData(); bool ReportAvailableData(DWORD dataLen); - void ReportDataComplete(); - private: - wxWebRequestWinHTTP& m_request; + HINTERNET m_requestHandle; wxInt64 m_contentLength; - long m_readSize; - wxMemoryBuffer m_readBuffer; - wxScopedPtr m_stream; wxDECLARE_NO_COPY_CLASS(wxWebResponseWinHTTP); }; diff --git a/include/wx/webrequest.h b/include/wx/webrequest.h index 20d8b2b59c..50d5974564 100644 --- a/include/wx/webrequest.h +++ b/include/wx/webrequest.h @@ -40,12 +40,12 @@ public: State_Cancelled }; - enum Storage - { - Storage_Memory, - Storage_File, - Storage_None - }; + enum Storage + { + Storage_Memory, + Storage_File, + Storage_None + }; virtual ~wxWebRequest() { } @@ -60,8 +60,10 @@ public: void SetIgnoreServerErrorStatus(bool ignore) { m_ignoreServerErrorStatus = ignore; } - virtual void SetStorage(Storage storage); - + virtual void SetStorage(Storage storage) { m_storage = storage; } + + Storage GetStorage() const { return m_storage; } + virtual void Start() = 0; virtual void Cancel() = 0; @@ -128,16 +130,27 @@ public: virtual wxString GetStatusText() const = 0; - virtual wxInputStream* GetStream() const = 0; + virtual wxInputStream* GetStream() const; virtual wxString GetSuggestedFileName() const; - virtual wxString AsString(wxMBConv* conv = NULL) const = 0; + wxString AsString(wxMBConv* conv = NULL) const; protected: - wxWebResponse() { } + wxWebRequest& m_request; + size_t m_readSize; + + wxWebResponse(wxWebRequest& request): + m_request(request), m_readSize(8 * 1024) { } + + void* GetDataBuffer(size_t sizeNeeded); + + void ReportDataReceived(size_t sizeReceived); private: + wxMemoryBuffer m_readBuffer; + mutable wxScopedPtr m_stream; + wxDECLARE_NO_COPY_CLASS(wxWebResponse); }; diff --git a/src/common/webrequest.cpp b/src/common/webrequest.cpp index 20f6afa719..997054aadc 100644 --- a/src/common/webrequest.cpp +++ b/src/common/webrequest.cpp @@ -123,6 +123,17 @@ wxString wxWebResponse::GetMimeType() const return GetHeader("Mime-Type"); } +wxInputStream * wxWebResponse::GetStream() const +{ + if ( !m_stream.get() ) + { + // Create stream + m_stream.reset(new wxMemoryInputStream(m_readBuffer.GetData(), m_readBuffer.GetDataLen())); + } + + return m_stream.get(); +} + wxString wxWebResponse::GetSuggestedFileName() const { wxString suggestedFilename; @@ -141,6 +152,31 @@ wxString wxWebResponse::GetSuggestedFileName() const return suggestedFilename; } +wxString wxWebResponse::AsString(wxMBConv * conv) const +{ + // TODO: try to determine encoding type from content-type header + if (!conv) + conv = &wxConvUTF8; + + if (m_request.GetStorage() == wxWebRequest::Storage_Memory) + { + size_t outLen = 0; + return conv->cMB2WC((const char*)m_readBuffer.GetData(), m_readBuffer.GetDataLen(), &outLen); + } + else + return wxString(); +} + +void* wxWebResponse::GetDataBuffer(size_t sizeNeeded) +{ + return m_readBuffer.GetAppendBuf(sizeNeeded); +} + +void wxWebResponse::ReportDataReceived(size_t sizeReceived) +{ + m_readBuffer.UngetAppendBuf(sizeReceived); +} + // // wxWebSession diff --git a/src/msw/webrequest_winhttp.cpp b/src/msw/webrequest_winhttp.cpp index 76248d45f2..394c56e2a8 100644 --- a/src/msw/webrequest_winhttp.cpp +++ b/src/msw/webrequest_winhttp.cpp @@ -181,10 +181,7 @@ void wxWebRequestWinHTTP::HandleCallback(DWORD dwInternetStatus, SetFailedWithLastError(); } else - { - m_response->ReportDataComplete(); SetState(State_Completed); - } break; case WINHTTP_CALLBACK_STATUS_WRITE_COMPLETE: WriteData(); @@ -351,10 +348,10 @@ wxWebResponse* wxWebRequestWinHTTP::GetResponse() // wxWebResponseWinHTTP::wxWebResponseWinHTTP(wxWebRequestWinHTTP& request): - m_request(request), - m_readSize(8 * 1024) + wxWebResponse(request), + m_requestHandle(request.GetHandle()) { - wxString contentLengthStr = wxWinHTTPQueryHeaderString(m_request.GetHandle(), + wxString contentLengthStr = wxWinHTTPQueryHeaderString(m_requestHandle, WINHTTP_QUERY_CONTENT_LENGTH); if ( contentLengthStr.empty() || !contentLengthStr.ToLongLong(&m_contentLength) ) @@ -363,12 +360,12 @@ wxWebResponseWinHTTP::wxWebResponseWinHTTP(wxWebRequestWinHTTP& request): wxString wxWebResponseWinHTTP::GetURL() const { - return wxWinHTTPQueryOptionString(m_request.GetHandle(), WINHTTP_OPTION_URL); + return wxWinHTTPQueryOptionString(m_requestHandle, WINHTTP_OPTION_URL); } wxString wxWebResponseWinHTTP::GetHeader(const wxString& name) const { - return wxWinHTTPQueryHeaderString(m_request.GetHandle(), + return wxWinHTTPQueryHeaderString(m_requestHandle, WINHTTP_QUERY_CUSTOM, name.wc_str()); } @@ -376,7 +373,7 @@ int wxWebResponseWinHTTP::GetStatus() const { DWORD status = 0; DWORD statusSize = sizeof(status); - if ( !::WinHttpQueryHeaders(m_request.GetHandle(), + if ( !::WinHttpQueryHeaders(m_requestHandle, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, WINHTTP_HEADER_NAME_BY_INDEX, &status, &statusSize, 0) ) { @@ -389,28 +386,13 @@ int wxWebResponseWinHTTP::GetStatus() const wxString wxWebResponseWinHTTP::GetStatusText() const { - return wxWinHTTPQueryHeaderString(m_request.GetHandle(), WINHTTP_QUERY_STATUS_TEXT); -} - -wxInputStream* wxWebResponseWinHTTP::GetStream() const -{ - return m_stream.get(); -} - -wxString wxWebResponseWinHTTP::AsString(wxMBConv* conv) const -{ - // TODO: try to determine encoding type from content-type header - if ( !conv ) - conv = &wxConvUTF8; - - size_t outLen = 0; - return conv->cMB2WC((const char*) m_readBuffer.GetData(), m_readBuffer.GetDataLen(), &outLen); + return wxWinHTTPQueryHeaderString(m_requestHandle, WINHTTP_QUERY_STATUS_TEXT); } bool wxWebResponseWinHTTP::ReadData() { - if ( ::WinHttpReadData(m_request.GetHandle(), - m_readBuffer.GetAppendBuf(m_readSize), m_readSize, NULL) ) + if ( ::WinHttpReadData(m_requestHandle, + GetDataBuffer(m_readSize), m_readSize, NULL) ) return true; else return false; @@ -418,16 +400,11 @@ bool wxWebResponseWinHTTP::ReadData() bool wxWebResponseWinHTTP::ReportAvailableData(DWORD dataLen) { - m_readBuffer.UngetAppendBuf(dataLen); - m_request.m_bytesReceived += dataLen; + ReportDataReceived(dataLen); + static_cast(m_request).m_bytesReceived += dataLen; return ReadData(); } -void wxWebResponseWinHTTP::ReportDataComplete() -{ - m_stream.reset(new wxMemoryInputStream(m_readBuffer.GetData(), m_readBuffer.GetDataLen())); -} - // // wxWebAuthChallengeWinHTTP //