From 77f3cfbba80d4cef603fc644a7f3848d5249b039 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Wed, 10 May 2023 00:03:15 +0200 Subject: UrlClient: Fixed blocking request's error return value. --- src/HTTP/UrlClient.cpp | 15 ++++++++++++--- src/HTTP/UrlClient.h | 9 ++++----- 2 files changed, 16 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/HTTP/UrlClient.cpp b/src/HTTP/UrlClient.cpp index eb52acfee..11e4d2cc1 100644 --- a/src/HTTP/UrlClient.cpp +++ b/src/HTTP/UrlClient.cpp @@ -32,7 +32,8 @@ namespace public: explicit cBlockingHTTPCallbacks(std::shared_ptr a_Event, AString & a_ResponseBody) : - m_Event(std::move(a_Event)), m_ResponseBody(a_ResponseBody) + m_Event(std::move(a_Event)), m_ResponseBody(a_ResponseBody), + m_IsError(false) { } @@ -44,6 +45,7 @@ namespace void OnError(const AString & a_ErrorMsg) override { LOGERROR("%s %d: HTTP Error: %s", __FILE__, __LINE__, a_ErrorMsg.c_str()); + m_IsError = true; m_Event->Set(); } @@ -56,6 +58,9 @@ namespace /** The accumulator for the partial body data, so that OnBodyFinished() can send the entire thing at once. */ AString & m_ResponseBody; + + /** Indicates whether an error was encountered while processing the request. */ + bool m_IsError; }; } @@ -746,14 +751,18 @@ std::pair cUrlClient::BlockingRequest( { auto EvtFinished = std::make_shared(); AString Response; - auto Callbacks = std::make_unique(EvtFinished, Response); - auto [Success, ErrorMessage] = cUrlClient::Request(a_Method, a_URL, std::move(Callbacks), std::move(a_Headers), a_Body, a_Options); + auto Callbacks = std::make_shared(EvtFinished, Response); + auto [Success, ErrorMessage] = cUrlClient::Request(a_Method, a_URL, Callbacks, std::move(a_Headers), a_Body, a_Options); if (Success) { if (!EvtFinished->Wait(10000)) { return std::make_pair(false, "Timeout"); } + if (Callbacks->m_IsError) + { + return std::make_pair(false, AString()); + } } else { diff --git a/src/HTTP/UrlClient.h b/src/HTTP/UrlClient.h index a73f22521..6de18672a 100644 --- a/src/HTTP/UrlClient.h +++ b/src/HTTP/UrlClient.h @@ -87,7 +87,7 @@ public: for such a response; instead, the redirect is silently attempted. */ virtual void OnRedirecting(const AString & a_NewLocation) {} }; - using cCallbacksPtr = std::unique_ptr; + using cCallbacksPtr = std::shared_ptr; /** Used for HTTP status codes. */ @@ -147,10 +147,9 @@ public: const AStringMap & a_Options = {} ); - /** The method will run a thread blocking HTTP request. Any error handling - is done inside the functions. Check the LOG or stdout for any occurring - errors. Other parameters are the same as for the regular request method. - The return value is if the request was successful and the response. */ + /** Sends a generic request and block until a response is received or an error occurs. + The first returned value specifies whether the response was received successfully. + If successful, the second value provides the actual response data. */ static std::pair BlockingRequest( const AString & a_Method, const AString & a_URL, -- cgit v1.2.3