From 70874798e98c7103b039918699cdfd79b194d20e Mon Sep 17 00:00:00 2001 From: mikee47 Date: Sun, 24 Feb 2019 10:27:02 +0000 Subject: [PATCH] Revise `HttpConnection` so it can be used with either client or server Move `response` member variable into `HttpConnection` as both client and server have this. Move `getResponse()` method from `HttpClientConnection` into `HttpConnection` Add virtual `getRequest()` method to HttpConnection Move applicable methods from `HttpClientConnection` into `HttpConnection` Note: `send(HttpRquest*)` made virtual in previous commit --- .../Network/Http/HttpClientConnection.h | 72 +------------------ Sming/SmingCore/Network/Http/HttpConnection.h | 67 +++++++++++++++++ .../Network/Http/HttpServerConnection.h | 6 +- 3 files changed, 73 insertions(+), 72 deletions(-) diff --git a/Sming/SmingCore/Network/Http/HttpClientConnection.h b/Sming/SmingCore/Network/Http/HttpClientConnection.h index bbaf63fc5d..07ec924651 100644 --- a/Sming/SmingCore/Network/Http/HttpClientConnection.h +++ b/Sming/SmingCore/Network/Http/HttpClientConnection.h @@ -50,80 +50,11 @@ class HttpClientConnection : public HttpConnection bool send(HttpRequest* request) override; - /** - * @brief Returns pointer to the current request - * @return HttpRequest* - */ - HttpRequest* getRequest() + HttpRequest* getRequest() override { return incomingRequest; } - /** - * @brief Returns pointer to the current response - * @return HttpResponse* - */ - HttpResponse* getResponse() - { - return &response; - } - - using TcpClient::close; - -#ifdef ENABLE_SSL - using TcpClient::getSsl; -#endif - - // Backported for compatibility reasons - - /** - * @deprecated Use `getResponse()->code` instead - */ - int getResponseCode() const SMING_DEPRECATED - { - return response.code; - } - - /** - * @deprecated Use `getResponse()->headers[]` instead - */ - String getResponseHeader(const String& headerName, const String& defaultValue = nullptr) const SMING_DEPRECATED - { - return response.headers[headerName] ?: defaultValue; - } - - /** - * @deprecated Use `getResponse()->headers` instead - */ - HttpHeaders& getResponseHeaders() SMING_DEPRECATED - { - return response.headers; - } - - /** - * @deprecated Use `getResponse()->headers.getLastModifiedDate()` instead - */ - DateTime getLastModifiedDate() const SMING_DEPRECATED - { - return response.headers.getLastModifiedDate(); - } - - /** - * @deprecated Use `getResponse()->headers.getServerDate()` instead - */ - DateTime getServerDate() const SMING_DEPRECATED - { - return response.headers.getServerDate(); - } - - /** - * @deprecated Use `getResponse()->getBody()` instead - */ - String getResponseString() SMING_DEPRECATED - { - return response.getBody(); - } - void reset() override; protected: @@ -150,7 +81,6 @@ class HttpClientConnection : public HttpConnection HttpRequest* incomingRequest = nullptr; HttpRequest* outgoingRequest = nullptr; - HttpResponse response; }; /** @} */ diff --git a/Sming/SmingCore/Network/Http/HttpConnection.h b/Sming/SmingCore/Network/Http/HttpConnection.h index b296b81063..5b1d7a7a06 100644 --- a/Sming/SmingCore/Network/Http/HttpConnection.h +++ b/Sming/SmingCore/Network/Http/HttpConnection.h @@ -65,6 +65,71 @@ class HttpConnection : public TcpClient bool isActive(); + /** + * @brief Returns pointer to the current request + * @return HttpRequest* + */ + virtual HttpRequest* getRequest() = 0; + + /** + * @brief Returns pointer to the current response + * @return HttpResponse* + */ + HttpResponse* getResponse() + { + return &response; + } + + // Backported for compatibility reasons + + /** + * @deprecated Use `getResponse()->code` instead + */ + int getResponseCode() const SMING_DEPRECATED + { + return response.code; + } + + /** + * @deprecated Use `getResponse()->headers[]` instead + */ + String getResponseHeader(const String& headerName, const String& defaultValue = nullptr) const SMING_DEPRECATED + { + return response.headers[headerName] ?: defaultValue; + } + + /** + * @deprecated Use `getResponse()->headers` instead + */ + HttpHeaders& getResponseHeaders() SMING_DEPRECATED + { + return response.headers; + } + + /** + * @deprecated Use `getResponse()->headers.getLastModifiedDate()` instead + */ + DateTime getLastModifiedDate() const SMING_DEPRECATED + { + return response.headers.getLastModifiedDate(); + } + + /** + * @deprecated Use `getResponse()->headers.getServerDate()` instead + */ + DateTime getServerDate() const SMING_DEPRECATED + { + return response.headers.getServerDate(); + } + + /** + * @deprecated Use `getResponse()->getBody()` instead + */ + String getResponseString() SMING_DEPRECATED + { + return response.getBody(); + } + protected: /** @brief Called after all headers have been received and processed */ void resetHeaders(); @@ -167,6 +232,8 @@ class HttpConnection : public TcpClient HttpHeaderBuilder header; ///< Header construction HttpHeaders incomingHeaders; ///< Full set of incoming headers HttpConnectionState state = eHCS_Ready; + + HttpResponse response; }; /** @} */ diff --git a/Sming/SmingCore/Network/Http/HttpServerConnection.h b/Sming/SmingCore/Network/Http/HttpServerConnection.h index b419cae8a5..f1d49929b3 100644 --- a/Sming/SmingCore/Network/Http/HttpServerConnection.h +++ b/Sming/SmingCore/Network/Http/HttpServerConnection.h @@ -77,6 +77,11 @@ class HttpServerConnection : public HttpConnection upgradeCallback = callback; } + HttpRequest* getRequest() override + { + return &request; + } + protected: // HTTP parser methods /** @@ -142,7 +147,6 @@ class HttpServerConnection : public HttpConnection HttpResource* resource = nullptr; ///< Resource for currently executing path HttpRequest request; - HttpResponse response; HttpResourceDelegate headersCompleteDelegate = nullptr; HttpResourceDelegate requestCompletedDelegate = nullptr;