Skip to content

Commit 9e72884

Browse files
committed
Adding support to use ServerAddress for Host header
1 parent 7f36561 commit 9e72884

File tree

2 files changed

+41
-13
lines changed

2 files changed

+41
-13
lines changed

src/HttpClient.cpp

+31-13
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const char* HttpClient::kTransferEncodingChunked = HTTP_HEADER_TRANSFER_ENCODING
1212

1313
HttpClient::HttpClient(Client& aClient, const char* aServerName, uint16_t aServerPort)
1414
: iClient(&aClient), iServerName(aServerName), iServerAddress(), iServerPort(aServerPort),
15-
iConnectionClose(true), iSendDefaultRequestHeaders(true)
15+
iConnectionClose(true), iSendDefaultRequestHeaders(true), iUseServerAddressForHostHeader(false)
1616
{
1717
resetState();
1818
}
@@ -24,7 +24,7 @@ HttpClient::HttpClient(Client& aClient, const String& aServerName, uint16_t aSer
2424

2525
HttpClient::HttpClient(Client& aClient, const IPAddress& aServerAddress, uint16_t aServerPort)
2626
: iClient(&aClient), iServerName(NULL), iServerAddress(aServerAddress), iServerPort(aServerPort),
27-
iConnectionClose(true), iSendDefaultRequestHeaders(true)
27+
iConnectionClose(true), iSendDefaultRequestHeaders(true), iUseServerAddressForHostHeader(false)
2828
{
2929
resetState();
3030
}
@@ -58,6 +58,11 @@ void HttpClient::noDefaultRequestHeaders()
5858
iSendDefaultRequestHeaders = false;
5959
}
6060

61+
void HttpClient::useServerAddressForHostHeader()
62+
{
63+
iUseServerAddressForHostHeader = true;
64+
}
65+
6166
void HttpClient::beginRequest()
6267
{
6368
iState = eRequestStarted;
@@ -157,17 +162,7 @@ int HttpClient::sendInitialHeaders(const char* aURLPath, const char* aHttpMethod
157162
if (iSendDefaultRequestHeaders)
158163
{
159164
// The host header, if required
160-
if (iServerName)
161-
{
162-
iClient->print("Host: ");
163-
iClient->print(iServerName);
164-
if (iServerPort != kHttpPort)
165-
{
166-
iClient->print(":");
167-
iClient->print(iServerPort);
168-
}
169-
iClient->println();
170-
}
165+
sendHostHeader();
171166
// And user-agent string
172167
sendHeader(HTTP_HEADER_USER_AGENT, kUserAgent);
173168
}
@@ -184,6 +179,29 @@ int HttpClient::sendInitialHeaders(const char* aURLPath, const char* aHttpMethod
184179
return HTTP_SUCCESS;
185180
}
186181

182+
void HttpClient::sendHostHeader()
183+
{
184+
if(iServerName || iUseServerAddressForHostHeader) {
185+
iClient->print(HTTP_HEADER_HOST);
186+
iClient->print(": ");
187+
188+
if (iServerName)
189+
{
190+
iClient->print(iServerName);
191+
} else if(iUseServerAddressForHostHeader) {
192+
iClient->print(iServerAddress);
193+
}
194+
195+
if (iServerPort != kHttpPort)
196+
{
197+
iClient->print(":");
198+
iClient->print(iServerPort);
199+
}
200+
iClient->println();
201+
}
202+
}
203+
204+
187205
void HttpClient::sendHeader(const char* aHeader)
188206
{
189207
iClient->println(aHeader);

src/HttpClient.h

+10
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ static const int HTTP_ERROR_INVALID_RESPONSE =-4;
3434
#define HTTP_HEADER_CONTENT_LENGTH "Content-Length"
3535
#define HTTP_HEADER_CONTENT_TYPE "Content-Type"
3636
#define HTTP_HEADER_CONNECTION "Connection"
37+
#define HTTP_HEADER_HOST "Host"
3738
#define HTTP_HEADER_TRANSFER_ENCODING "Transfer-Encoding"
3839
#define HTTP_HEADER_USER_AGENT "User-Agent"
3940
#define HTTP_HEADER_VALUE_CHUNKED "chunked"
@@ -294,6 +295,10 @@ class HttpClient : public Client
294295
*/
295296
void noDefaultRequestHeaders();
296297

298+
/** Enables using ServerAddress provided in ctor to be used for the Host header
299+
*/
300+
void useServerAddressForHostHeader();
301+
297302
// Inherited from Print
298303
// Note: 1st call to these indicates the user is sending the body, so if need
299304
// Note: be we should finish the header first
@@ -330,6 +335,10 @@ class HttpClient : public Client
330335
int sendInitialHeaders(const char* aURLPath,
331336
const char* aHttpMethod);
332337

338+
/** Sends the host header.
339+
*/
340+
void sendHostHeader();
341+
333342
/* Let the server know that we've reached the end of the headers
334343
*/
335344
void finishHeaders();
@@ -386,6 +395,7 @@ class HttpClient : public Client
386395
uint32_t iHttpResponseTimeout;
387396
bool iConnectionClose;
388397
bool iSendDefaultRequestHeaders;
398+
bool iUseServerAddressForHostHeader;
389399
String iHeaderLine;
390400
};
391401

0 commit comments

Comments
 (0)