diff --git a/src/HttpClient.cpp b/src/HttpClient.cpp index 7517eea..22a0c4f 100644 --- a/src/HttpClient.cpp +++ b/src/HttpClient.cpp @@ -12,7 +12,7 @@ const char* HttpClient::kTransferEncodingChunked = HTTP_HEADER_TRANSFER_ENCODING HttpClient::HttpClient(Client& aClient, const char* aServerName, uint16_t aServerPort) : iClient(&aClient), iServerName(aServerName), iServerAddress(), iServerPort(aServerPort), - iConnectionClose(true), iSendDefaultRequestHeaders(true) + iConnectionClose(true), iSendDefaultRequestHeaders(true), iUseServerAddressForHostHeader(false) { resetState(); } @@ -24,7 +24,7 @@ HttpClient::HttpClient(Client& aClient, const String& aServerName, uint16_t aSer HttpClient::HttpClient(Client& aClient, const IPAddress& aServerAddress, uint16_t aServerPort) : iClient(&aClient), iServerName(NULL), iServerAddress(aServerAddress), iServerPort(aServerPort), - iConnectionClose(true), iSendDefaultRequestHeaders(true) + iConnectionClose(true), iSendDefaultRequestHeaders(true), iUseServerAddressForHostHeader(false) { resetState(); } @@ -58,6 +58,11 @@ void HttpClient::noDefaultRequestHeaders() iSendDefaultRequestHeaders = false; } +void HttpClient::useServerAddressForHostHeader() +{ + iUseServerAddressForHostHeader = true; +} + void HttpClient::beginRequest() { iState = eRequestStarted; @@ -157,17 +162,7 @@ int HttpClient::sendInitialHeaders(const char* aURLPath, const char* aHttpMethod if (iSendDefaultRequestHeaders) { // The host header, if required - if (iServerName) - { - iClient->print("Host: "); - iClient->print(iServerName); - if (iServerPort != kHttpPort) - { - iClient->print(":"); - iClient->print(iServerPort); - } - iClient->println(); - } + sendHostHeader(); // And user-agent string sendHeader(HTTP_HEADER_USER_AGENT, kUserAgent); } @@ -184,6 +179,32 @@ int HttpClient::sendInitialHeaders(const char* aURLPath, const char* aHttpMethod return HTTP_SUCCESS; } +void HttpClient::sendHostHeader() +{ + if(iServerName || iUseServerAddressForHostHeader) + { + iClient->print(HTTP_HEADER_HOST); + iClient->print(": "); + + if (iServerName) + { + iClient->print(iServerName); + } + else if(iUseServerAddressForHostHeader) + { + iClient->print(iServerAddress); + } + + if (iServerPort != kHttpPort) + { + iClient->print(":"); + iClient->print(iServerPort); + } + iClient->println(); + } +} + + void HttpClient::sendHeader(const char* aHeader) { iClient->println(aHeader); diff --git a/src/HttpClient.h b/src/HttpClient.h index 38fd799..b713766 100644 --- a/src/HttpClient.h +++ b/src/HttpClient.h @@ -34,6 +34,7 @@ static const int HTTP_ERROR_INVALID_RESPONSE =-4; #define HTTP_HEADER_CONTENT_LENGTH "Content-Length" #define HTTP_HEADER_CONTENT_TYPE "Content-Type" #define HTTP_HEADER_CONNECTION "Connection" +#define HTTP_HEADER_HOST "Host" #define HTTP_HEADER_TRANSFER_ENCODING "Transfer-Encoding" #define HTTP_HEADER_USER_AGENT "User-Agent" #define HTTP_HEADER_VALUE_CHUNKED "chunked" @@ -294,6 +295,10 @@ class HttpClient : public Client */ void noDefaultRequestHeaders(); + /** Enables using ServerAddress provided in ctor to be used for the Host header + */ + void useServerAddressForHostHeader(); + // Inherited from Print // Note: 1st call to these indicates the user is sending the body, so if need // Note: be we should finish the header first @@ -330,6 +335,10 @@ class HttpClient : public Client int sendInitialHeaders(const char* aURLPath, const char* aHttpMethod); + /** Sends the host header. + */ + void sendHostHeader(); + /* Let the server know that we've reached the end of the headers */ void finishHeaders(); @@ -386,6 +395,7 @@ class HttpClient : public Client uint32_t iHttpResponseTimeout; bool iConnectionClose; bool iSendDefaultRequestHeaders; + bool iUseServerAddressForHostHeader; String iHeaderLine; };