Skip to content

Commit

Permalink
Revise HttpConnection so it can be used with either client or server
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mikee47 committed Feb 25, 2019
1 parent b6b6317 commit 7087479
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 72 deletions.
72 changes: 1 addition & 71 deletions Sming/SmingCore/Network/Http/HttpClientConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -150,7 +81,6 @@ class HttpClientConnection : public HttpConnection

HttpRequest* incomingRequest = nullptr;
HttpRequest* outgoingRequest = nullptr;
HttpResponse response;
};

/** @} */
Expand Down
67 changes: 67 additions & 0 deletions Sming/SmingCore/Network/Http/HttpConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
};

/** @} */
Expand Down
6 changes: 5 additions & 1 deletion Sming/SmingCore/Network/Http/HttpServerConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ class HttpServerConnection : public HttpConnection
upgradeCallback = callback;
}

HttpRequest* getRequest() override
{
return &request;
}

protected:
// HTTP parser methods
/**
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 7087479

Please sign in to comment.