Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using ServerAddress for Host header #83

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 34 additions & 13 deletions src/HttpClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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();
}
Expand Down Expand Up @@ -58,6 +58,11 @@ void HttpClient::noDefaultRequestHeaders()
iSendDefaultRequestHeaders = false;
}

void HttpClient::useServerAddressForHostHeader()
{
iUseServerAddressForHostHeader = true;
}

void HttpClient::beginRequest()
{
iState = eRequestStarted;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions src/HttpClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -386,6 +395,7 @@ class HttpClient : public Client
uint32_t iHttpResponseTimeout;
bool iConnectionClose;
bool iSendDefaultRequestHeaders;
bool iUseServerAddressForHostHeader;
String iHeaderLine;
};

Expand Down