From 74918ce805de260371ad1be0604ee7369f5f809b Mon Sep 17 00:00:00 2001 From: Mattes D Date: Tue, 16 Aug 2016 11:55:49 +0200 Subject: [PATCH] cUrlClient: Refactored callbacks to use UniquePtr. --- src/HTTP/UrlClient.cpp | 40 ++++++++++++++++++------------------ src/HTTP/UrlClient.h | 9 ++++---- tests/HTTP/UrlClientTest.cpp | 16 +++++++-------- 3 files changed, 33 insertions(+), 32 deletions(-) diff --git a/src/HTTP/UrlClient.cpp b/src/HTTP/UrlClient.cpp index 9346882f1..29d5e28d7 100644 --- a/src/HTTP/UrlClient.cpp +++ b/src/HTTP/UrlClient.cpp @@ -32,7 +32,7 @@ public: static std::pair Request( const AString & a_Method, const AString & a_URL, - cUrlClient::cCallbacks & a_Callbacks, + cUrlClient::cCallbacksPtr && a_Callbacks, AStringMap && a_Headers, const AString & a_Body, AStringMap && a_Options @@ -41,7 +41,7 @@ public: // Create a new instance of cUrlClientRequest, wrapped in a SharedPtr, so that it has a controlled lifetime. // Cannot use std::make_shared, because the constructor is not public SharedPtr ptr (new cUrlClientRequest( - a_Method, a_URL, a_Callbacks, std::move(a_Headers), a_Body, std::move(a_Options) + a_Method, a_URL, std::move(a_Callbacks), std::move(a_Headers), a_Body, std::move(a_Options) )); return ptr->DoRequest(ptr); } @@ -51,7 +51,7 @@ public: void CallErrorCallback(const AString & a_ErrorMessage) { // Call the error callback: - m_Callbacks.OnError(a_ErrorMessage); + m_Callbacks->OnError(a_ErrorMessage); // Terminate the request's TCP link: auto link = m_Link; @@ -63,7 +63,7 @@ public: } - cUrlClient::cCallbacks & GetCallbacks() { return m_Callbacks; } + cUrlClient::cCallbacks & GetCallbacks() { return *m_Callbacks; } void RedirectTo(const AString & a_RedirectUrl); @@ -115,7 +115,7 @@ protected: UInt16 m_UrlPort; /** Callbacks that report progress and results of the request. */ - cUrlClient::cCallbacks & m_Callbacks; + cUrlClient::cCallbacksPtr m_Callbacks; /** Extra headers to be sent with the request (besides the normal ones). */ AStringMap m_Headers; @@ -145,14 +145,14 @@ protected: cUrlClientRequest( const AString & a_Method, const AString & a_Url, - cUrlClient::cCallbacks & a_Callbacks, + cUrlClient::cCallbacksPtr && a_Callbacks, AStringMap && a_Headers, const AString & a_Body, AStringMap && a_Options ): m_Method(a_Method), m_Url(a_Url), - m_Callbacks(a_Callbacks), + m_Callbacks(std::move(a_Callbacks)), m_Headers(std::move(a_Headers)), m_Body(a_Body), m_Options(std::move(a_Options)) @@ -170,7 +170,7 @@ protected: // cNetwork::cConnectCallbacks override: An error has occurred: virtual void OnError(int a_ErrorCode, const AString & a_ErrorMsg) override { - m_Callbacks.OnError(Printf("Network error %d (%s)", a_ErrorCode, a_ErrorMsg.c_str())); + m_Callbacks->OnError(Printf("Network error %d (%s)", a_ErrorCode, a_ErrorMsg.c_str())); m_Self.reset(); } @@ -486,7 +486,7 @@ cSchemeHandlerPtr cSchemeHandler::Create(const AString & a_Scheme, cUrlClientReq void cUrlClientRequest::RedirectTo(const AString & a_RedirectUrl) { // Check that redirection is allowed: - m_Callbacks.OnRedirecting(a_RedirectUrl); + m_Callbacks->OnRedirecting(a_RedirectUrl); if (!ShouldAllowRedirects()) { CallErrorCallback(Printf("Redirect to \"%s\" not allowed", a_RedirectUrl.c_str())); @@ -500,7 +500,7 @@ void cUrlClientRequest::RedirectTo(const AString & a_RedirectUrl) auto res = DoRequest(m_Self); if (!res.first) { - m_Callbacks.OnError(Printf("Redirection failed: %s", res.second.c_str())); + m_Callbacks->OnError(Printf("Redirection failed: %s", res.second.c_str())); return; } } @@ -520,7 +520,7 @@ bool cUrlClientRequest::ShouldAllowRedirects() const void cUrlClientRequest::OnConnected(cTCPLink & a_Link) { - m_Callbacks.OnConnected(a_Link); + m_Callbacks->OnConnected(a_Link); m_SchemeHandler->OnConnected(a_Link); } @@ -532,7 +532,7 @@ void cUrlClientRequest::OnTlsHandshakeCompleted(void) { // Notify the scheme handler and the callbacks: m_SchemeHandler->OnTlsHandshakeCompleted(); - m_Callbacks.OnTlsHandshakeCompleted(); + m_Callbacks->OnTlsHandshakeCompleted(); } @@ -607,14 +607,14 @@ std::pair cUrlClientRequest::DoRequest(SharedPtr cUrlClient::Request( const AString & a_Method, const AString & a_URL, - cCallbacks & a_Callbacks, + cCallbacksPtr && a_Callbacks, AStringMap && a_Headers, AString && a_Body, AStringMap && a_Options ) { return cUrlClientRequest::Request( - a_Method, a_URL, a_Callbacks, std::move(a_Headers), std::move(a_Body), std::move(a_Options) + a_Method, a_URL, std::move(a_Callbacks), std::move(a_Headers), std::move(a_Body), std::move(a_Options) ); } @@ -624,14 +624,14 @@ std::pair cUrlClient::Request( std::pair cUrlClient::Get( const AString & a_URL, - cCallbacks & a_Callbacks, + cCallbacksPtr && a_Callbacks, AStringMap a_Headers, AString a_Body, AStringMap a_Options ) { return cUrlClientRequest::Request( - "GET", a_URL, a_Callbacks, std::move(a_Headers), std::move(a_Body), std::move(a_Options) + "GET", a_URL, std::move(a_Callbacks), std::move(a_Headers), std::move(a_Body), std::move(a_Options) ); } @@ -641,14 +641,14 @@ std::pair cUrlClient::Get( std::pair cUrlClient::Post( const AString & a_URL, - cCallbacks & a_Callbacks, + cCallbacksPtr && a_Callbacks, AStringMap && a_Headers, AString && a_Body, AStringMap && a_Options ) { return cUrlClientRequest::Request( - "POST", a_URL, a_Callbacks, std::move(a_Headers), std::move(a_Body), std::move(a_Options) + "POST", a_URL, std::move(a_Callbacks), std::move(a_Headers), std::move(a_Body), std::move(a_Options) ); } @@ -658,14 +658,14 @@ std::pair cUrlClient::Post( std::pair cUrlClient::Put( const AString & a_URL, - cCallbacks & a_Callbacks, + cCallbacksPtr && a_Callbacks, AStringMap && a_Headers, AString && a_Body, AStringMap && a_Options ) { return cUrlClientRequest::Request( - "PUT", a_URL, a_Callbacks, std::move(a_Headers), std::move(a_Body), std::move(a_Options) + "PUT", a_URL, std::move(a_Callbacks), std::move(a_Headers), std::move(a_Body), std::move(a_Options) ); } diff --git a/src/HTTP/UrlClient.h b/src/HTTP/UrlClient.h index 652cc76f7..ac772235e 100644 --- a/src/HTTP/UrlClient.h +++ b/src/HTTP/UrlClient.h @@ -86,6 +86,7 @@ public: for such a response; instead, the redirect is silently attempted. */ virtual void OnRedirecting(const AString & a_NewLocation) {} }; + typedef UniquePtr cCallbacksPtr; /** Used for HTTP status codes. */ @@ -112,7 +113,7 @@ public: static std::pair Request( const AString & a_Method, const AString & a_URL, - cCallbacks & a_Callbacks, + cCallbacksPtr && a_Callbacks, AStringMap && a_Headers, AString && a_Body, AStringMap && a_Options @@ -121,7 +122,7 @@ public: /** Alias for Request("GET", ...) */ static std::pair Get( const AString & a_URL, - cCallbacks & a_Callbacks, + cCallbacksPtr && a_Callbacks, AStringMap a_Headers = AStringMap(), AString a_Body = AString(), AStringMap a_Options = AStringMap() @@ -130,7 +131,7 @@ public: /** Alias for Request("POST", ...) */ static std::pair Post( const AString & a_URL, - cCallbacks & a_Callbacks, + cCallbacksPtr && a_Callbacks, AStringMap && a_Headers, AString && a_Body, AStringMap && a_Options @@ -139,7 +140,7 @@ public: /** Alias for Request("PUT", ...) */ static std::pair Put( const AString & a_URL, - cCallbacks & a_Callbacks, + cCallbacksPtr && a_Callbacks, AStringMap && a_Headers, AString && a_Body, AStringMap && a_Options diff --git a/tests/HTTP/UrlClientTest.cpp b/tests/HTTP/UrlClientTest.cpp index 97cdc6d6e..d8412fddf 100644 --- a/tests/HTTP/UrlClientTest.cpp +++ b/tests/HTTP/UrlClientTest.cpp @@ -106,10 +106,10 @@ static int TestRequest1() { LOG("Running test 1"); cEvent evtFinished; - cCallbacks callbacks(evtFinished); + auto callbacks = cpp14::make_unique(evtFinished); AStringMap options; options["MaxRedirects"] = "0"; - auto res = cUrlClient::Get("http://github.com", callbacks, AStringMap(), AString(), options); + auto res = cUrlClient::Get("http://github.com", std::move(callbacks), AStringMap(), AString(), options); if (res.first) { evtFinished.Wait(); @@ -130,8 +130,8 @@ static int TestRequest2() { LOG("Running test 2"); cEvent evtFinished; - cCallbacks callbacks(evtFinished); - auto res = cUrlClient::Get("http://github.com", callbacks); + auto callbacks = cpp14::make_unique(evtFinished); + auto res = cUrlClient::Get("http://github.com", std::move(callbacks)); if (res.first) { evtFinished.Wait(); @@ -152,10 +152,10 @@ static int TestRequest3() { LOG("Running test 3"); cEvent evtFinished; - cCallbacks callbacks(evtFinished); + auto callbacks = cpp14::make_unique(evtFinished); AStringMap options; options["MaxRedirects"] = "0"; - auto res = cUrlClient::Get("https://github.com", callbacks, AStringMap(), AString(), options); + auto res = cUrlClient::Get("https://github.com", std::move(callbacks), AStringMap(), AString(), options); if (res.first) { evtFinished.Wait(); @@ -176,8 +176,8 @@ static int TestRequest4() { LOG("Running test 4"); cEvent evtFinished; - cCallbacks callbacks(evtFinished); - auto res = cUrlClient::Get("https://github.com", callbacks); + auto callbacks = cpp14::make_unique(evtFinished); + auto res = cUrlClient::Get("https://github.com", std::move(callbacks)); if (res.first) { evtFinished.Wait();