From e2ea5e0dd0d8fb688d6375e3d421a1e294ce81d6 Mon Sep 17 00:00:00 2001 From: Pawel Kurowski Date: Mon, 29 Jun 2020 17:31:24 +0200 Subject: [PATCH 1/2] Allow casting return value from HttpResponse::GetData --- .../FSecure/WinHttp/Handles/HttpResponse.h | 35 ++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/Src/Common/FSecure/WinHttp/Handles/HttpResponse.h b/Src/Common/FSecure/WinHttp/Handles/HttpResponse.h index 46920bb..55371d9 100644 --- a/Src/Common/FSecure/WinHttp/Handles/HttpResponse.h +++ b/Src/Common/FSecure/WinHttp/Handles/HttpResponse.h @@ -11,7 +11,7 @@ namespace FSecure::WinHttp { public: /// Create response handle - /// @param requestHadle - a request coresponding to this response + /// @param requestHadle - a request corresponding to this response HttpResponse(HttpHandle requestHandle) : m_RequestHandle{ std::move(requestHandle) } { @@ -72,15 +72,42 @@ namespace FSecure::WinHttp /// Get HTTP response body /// @returns HTTP response body /// @throws std::runtime_error if response body cannot be retreived - ByteVector GetData() const + template + std::enable_if_t, T> GetData() const& + { + return ByteView{ GetDataInternal() }; + } + + /// Get HTTP response body + /// @returns HTTP response body + /// @throws std::runtime_error if response body cannot be retreived + template + std::enable_if_t, T> GetData() const&& + { + return ByteView{ GetDataInternal() }; + } + + /// Get HTTP response body + /// @param c callable object used to obtain data. + /// @returns HTTP response body + /// @throws std::runtime_error if response body cannot be retreived + template + auto GetData(Callable c) const + { + return c(GetDataInternal()); + } + + private: + /// Get HTTP response body. + /// @returns HTTP response body + /// @throws std::runtime_error if response body cannot be retreived + ByteVector const& GetDataInternal() const { if (!m_Data.size()) ReceiveData(); return m_Data; } - - private: /// Read status code from HTTP response /// @throws std::runtime_error if status code cannot be retreived void ReadStatusCode() const From b9394037158a193af3b79ff9c63d818bcbd46b56 Mon Sep 17 00:00:00 2001 From: Pawel Kurowski Date: Mon, 29 Jun 2020 19:35:08 +0200 Subject: [PATCH 2/2] Change GetDataInternal() returned type to ByteView --- Src/Common/FSecure/WinHttp/Handles/HttpResponse.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Src/Common/FSecure/WinHttp/Handles/HttpResponse.h b/Src/Common/FSecure/WinHttp/Handles/HttpResponse.h index 55371d9..ca0133e 100644 --- a/Src/Common/FSecure/WinHttp/Handles/HttpResponse.h +++ b/Src/Common/FSecure/WinHttp/Handles/HttpResponse.h @@ -75,7 +75,7 @@ namespace FSecure::WinHttp template std::enable_if_t, T> GetData() const& { - return ByteView{ GetDataInternal() }; + return GetDataInternal(); } /// Get HTTP response body @@ -84,7 +84,7 @@ namespace FSecure::WinHttp template std::enable_if_t, T> GetData() const&& { - return ByteView{ GetDataInternal() }; + return GetDataInternal(); } /// Get HTTP response body @@ -101,7 +101,7 @@ namespace FSecure::WinHttp /// Get HTTP response body. /// @returns HTTP response body /// @throws std::runtime_error if response body cannot be retreived - ByteVector const& GetDataInternal() const + ByteView GetDataInternal() const { if (!m_Data.size()) ReceiveData();