From 64a380116009426ac416a75c4006986ef7680e1f Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 12 Dec 2020 18:46:28 +0100 Subject: [PATCH] Tweaks to wxWebRequest::SetData() overload taking stream Check that the stream is valid, if specified at all, and return false if it isn't -- or if no size was specified and determining stream size failed. Check for SetData() success in the test to provide better diagnostics in case the file it uses is not found (as is the case when running the test from another directory, for example). Also pass wxSharedPtr<> by const reference instead of by value to avoid unnecessary copies. --- include/wx/webrequest.h | 2 +- interface/wx/webrequest.h | 9 +++++++-- src/common/webrequest.cpp | 10 +++++++++- tests/net/webrequest.cpp | 6 ++++-- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/include/wx/webrequest.h b/include/wx/webrequest.h index 644be4eb89..1cf1604e00 100644 --- a/include/wx/webrequest.h +++ b/include/wx/webrequest.h @@ -58,7 +58,7 @@ public: void SetData(const wxString& text, const wxString& contentType, const wxMBConv& conv = wxConvUTF8); - void SetData(wxSharedPtr dataStream, const wxString& contentType, wxFileOffset dataSize = wxInvalidOffset); + bool SetData(const wxSharedPtr& dataStream, const wxString& contentType, wxFileOffset dataSize = wxInvalidOffset); void SetIgnoreServerErrorStatus(bool ignore) { m_ignoreServerErrorStatus = ignore; } diff --git a/interface/wx/webrequest.h b/interface/wx/webrequest.h index 2e2f443854..841c95bab7 100644 --- a/interface/wx/webrequest.h +++ b/interface/wx/webrequest.h @@ -245,7 +245,9 @@ public: this request. @param dataStream - The data in this stream will be posted as the request body + The data in this stream will be posted as the request body. The + stream may be empty, which will result in sending 0 bytes of data, + but if not empty, should be valid. @param contentType The value of HTTP "Content-Type" header, e.g. "application/octet-stream". @@ -253,8 +255,11 @@ public: Amount of data which is sent to the server. If set to @c wxInvalidOffset all stream data is sent. + @return @false if @a dataStream is not-empty but invalid or if @a + dataSize is not specified and the attempt to determine stream size + failed; @true in all the other cases. */ - void SetData(wxSharedPtr dataStream, + bool SetData(const wxSharedPtr& dataStream, const wxString& contentType, wxFileOffset dataSize = wxInvalidOffset); /** diff --git a/src/common/webrequest.cpp b/src/common/webrequest.cpp index 48f63d4bc7..e4bb3d6171 100644 --- a/src/common/webrequest.cpp +++ b/src/common/webrequest.cpp @@ -83,8 +83,11 @@ void wxWebRequest::SetData(const wxString& text, const wxString& contentType, co SetData(wxSharedPtr(new wxMemoryInputStream(m_dataText, m_dataText.length())), contentType); } -void wxWebRequest::SetData(wxSharedPtr dataStream, const wxString& contentType, wxFileOffset dataSize) +bool wxWebRequest::SetData(const wxSharedPtr& dataStream, const wxString& contentType, wxFileOffset dataSize) { + if ( !dataStream->IsOk() ) + return false; + m_dataStream = dataStream; if ( m_dataStream.get() ) { @@ -92,6 +95,9 @@ void wxWebRequest::SetData(wxSharedPtr dataStream, const wxString { // Determine data size m_dataSize = m_dataStream->SeekI(0, wxFromEnd); + if ( m_dataSize == wxInvalidOffset ) + return false; + m_dataStream->SeekI(0); } else @@ -101,6 +107,8 @@ void wxWebRequest::SetData(wxSharedPtr dataStream, const wxString m_dataSize = 0; SetHeader("Content-Type", contentType); + + return true; } wxFileOffset wxWebRequest::GetBytesReceived() const diff --git a/tests/net/webrequest.cpp b/tests/net/webrequest.cpp index b2c0de4223..3482492d11 100644 --- a/tests/net/webrequest.cpp +++ b/tests/net/webrequest.cpp @@ -172,8 +172,10 @@ TEST_CASE_METHOD(RequestFixture, "WebRequest", "[net][.]") SECTION("PUT file data") { Create("/put"); - request->SetData(wxSharedPtr(new wxFileInputStream("horse.png")), - "image/png"); + wxSharedPtr is(new wxFileInputStream("horse.png")); + REQUIRE( is->IsOk() ); + + request->SetData(is, "image/png"); request->SetMethod("PUT"); Run(); }