Skip to content

Commit 8012c8d

Browse files
committed
Add noDefaultRequestHeaders API to turn off sending default headers
1 parent 0bcba91 commit 8012c8d

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

HttpClient.cpp

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const char* HttpClient::kContentLengthPrefix = HTTP_HEADER_CONTENT_LENGTH ": ";
1111

1212
HttpClient::HttpClient(Client& aClient, const char* aServerName, uint16_t aServerPort)
1313
: iClient(&aClient), iServerName(aServerName), iServerAddress(), iServerPort(aServerPort),
14-
iConnectionClose(true)
14+
iConnectionClose(true), iSendDefaultRequestHeaders(true)
1515
{
1616
resetState();
1717
}
@@ -23,7 +23,7 @@ HttpClient::HttpClient(Client& aClient, const String& aServerName, uint16_t aSer
2323

2424
HttpClient::HttpClient(Client& aClient, const IPAddress& aServerAddress, uint16_t aServerPort)
2525
: iClient(&aClient), iServerName(NULL), iServerAddress(aServerAddress), iServerPort(aServerPort),
26-
iConnectionClose(true)
26+
iConnectionClose(true), iSendDefaultRequestHeaders(true)
2727
{
2828
resetState();
2929
}
@@ -49,6 +49,11 @@ void HttpClient::connectionKeepAlive()
4949
iConnectionClose = false;
5050
}
5151

52+
void HttpClient::noDefaultRequestHeaders()
53+
{
54+
iSendDefaultRequestHeaders = false;
55+
}
56+
5257
void HttpClient::beginRequest()
5358
{
5459
iState = eRequestStarted;
@@ -120,20 +125,23 @@ int HttpClient::sendInitialHeaders(const char* aURLPath, const char* aHttpMethod
120125

121126
iClient->print(aURLPath);
122127
iClient->println(" HTTP/1.1");
123-
// The host header, if required
124-
if (iServerName)
128+
if (iSendDefaultRequestHeaders)
125129
{
126-
iClient->print("Host: ");
127-
iClient->print(iServerName);
128-
if (iServerPort != kHttpPort)
130+
// The host header, if required
131+
if (iServerName)
129132
{
130-
iClient->print(":");
131-
iClient->print(iServerPort);
133+
iClient->print("Host: ");
134+
iClient->print(iServerName);
135+
if (iServerPort != kHttpPort)
136+
{
137+
iClient->print(":");
138+
iClient->print(iServerPort);
139+
}
140+
iClient->println();
132141
}
133-
iClient->println();
142+
// And user-agent string
143+
sendHeader(HTTP_HEADER_USER_AGENT, kUserAgent);
134144
}
135-
// And user-agent string
136-
sendHeader(HTTP_HEADER_USER_AGENT, kUserAgent);
137145

138146
if (iConnectionClose)
139147
{

HttpClient.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ class HttpClient : public Client
5252
*/
5353
void connectionKeepAlive();
5454

55+
/** Disables sending the default request headers (Host and User Agent)
56+
*/
57+
void noDefaultRequestHeaders();
58+
5559
/** Start a more complex request.
5660
Use this when you need to send additional headers in the request,
5761
but you will also need to call endRequest() when you are finished.
@@ -293,6 +297,7 @@ class HttpClient : public Client
293297
const char* iContentLengthPtr;
294298
uint32_t iHttpResponseTimeout;
295299
bool iConnectionClose;
300+
bool iSendDefaultRequestHeaders;
296301
String iHeaderLine;
297302
};
298303

keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ endOfBodyReached KEYWORD2
2929
completed KEYWORD2
3030
contentLength KEYWORD2
3131
connectionKeepAlive KEYWORD2
32+
noDefaultRequestHeaders KEYWORD2
3233
headerAvailable KEYWORD2
3334
readHeaderName KEYWORD2
3435
readHeaderValue KEYWORD2

0 commit comments

Comments
 (0)