Merge commit '77f3cfbba80d4cef603fc644a7f3848d5249b039' into pullstream

This commit is contained in:
Rebekah 2024-02-14 06:16:14 -05:00
commit b9b2b8aaed
Signed by: oneechanhax
GPG Key ID: 183EB7902964DAE5
2 changed files with 16 additions and 8 deletions

View File

@ -48,7 +48,8 @@ namespace
public: public:
explicit cBlockingHTTPCallbacks(std::shared_ptr<cEvent> a_Event, AString & a_ResponseBody) : explicit cBlockingHTTPCallbacks(std::shared_ptr<cEvent> 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)
{ {
} }
@ -60,6 +61,7 @@ namespace
void OnError(const AString & a_ErrorMsg) override void OnError(const AString & a_ErrorMsg) override
{ {
LOGERROR("%s %d: HTTP Error: %s", __FILE__, __LINE__, a_ErrorMsg.c_str()); LOGERROR("%s %d: HTTP Error: %s", __FILE__, __LINE__, a_ErrorMsg.c_str());
m_IsError = true;
m_Event->Set(); m_Event->Set();
} }
@ -72,6 +74,9 @@ namespace
/** The accumulator for the partial body data, so that OnBodyFinished() can send the entire thing at once. */ /** The accumulator for the partial body data, so that OnBodyFinished() can send the entire thing at once. */
AString & m_ResponseBody; AString & m_ResponseBody;
/** Indicates whether an error was encountered while processing the request. */
bool m_IsError;
}; };
} }
@ -762,14 +767,18 @@ std::pair<bool, AString> cUrlClient::BlockingRequest(
{ {
auto EvtFinished = std::make_shared<cEvent>(); auto EvtFinished = std::make_shared<cEvent>();
AString Response; AString Response;
auto Callbacks = std::make_unique<cBlockingHTTPCallbacks>(EvtFinished, Response); auto Callbacks = std::make_shared<cBlockingHTTPCallbacks>(EvtFinished, Response);
auto [Success, ErrorMessage] = cUrlClient::Request(a_Method, a_URL, std::move(Callbacks), std::move(a_Headers), a_Body, a_Options); auto [Success, ErrorMessage] = cUrlClient::Request(a_Method, a_URL, Callbacks, std::move(a_Headers), a_Body, a_Options);
if (Success) if (Success)
{ {
if (!EvtFinished->Wait(10000)) if (!EvtFinished->Wait(10000))
{ {
return std::make_pair(false, "Timeout"); return std::make_pair(false, "Timeout");
} }
if (Callbacks->m_IsError)
{
return std::make_pair(false, AString());
}
} }
else else
{ {

View File

@ -103,7 +103,7 @@ public:
for such a response; instead, the redirect is silently attempted. */ for such a response; instead, the redirect is silently attempted. */
virtual void OnRedirecting(const AString & a_NewLocation) {} virtual void OnRedirecting(const AString & a_NewLocation) {}
}; };
using cCallbacksPtr = std::unique_ptr<cCallbacks>; using cCallbacksPtr = std::shared_ptr<cCallbacks>;
/** Used for HTTP status codes. */ /** Used for HTTP status codes. */
@ -163,10 +163,9 @@ public:
const AStringMap & a_Options = {} const AStringMap & a_Options = {}
); );
/** The method will run a thread blocking HTTP request. Any error handling /** Sends a generic request and block until a response is received or an error occurs.
is done inside the functions. Check the LOG or stdout for any occurring The first returned value specifies whether the response was received successfully.
errors. Other parameters are the same as for the regular request method. If successful, the second value provides the actual response data. */
The return value is if the request was successful and the response. */
static std::pair<bool, AString> BlockingRequest( static std::pair<bool, AString> BlockingRequest(
const AString & a_Method, const AString & a_Method,
const AString & a_URL, const AString & a_URL,