Skip to content

Commit 5a91c66

Browse files
aalkuigrr
authored andcommitted
Host header support
1 parent acc34e2 commit 5a91c66

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

libraries/ESP8266WebServer/src/ESP8266WebServer.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,10 @@ bool ESP8266WebServer::hasArg(const char* name) {
253253
return false;
254254
}
255255

256+
String ESP8266WebServer::hostHeader() {
257+
return _hostHeader;
258+
}
259+
256260
void ESP8266WebServer::onFileUpload(THandlerFunction fn) {
257261
_fileUploadHandler = fn;
258262
}

libraries/ESP8266WebServer/src/ESP8266WebServer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ class ESP8266WebServer
8080
int args(); // get arguments count
8181
bool hasArg(const char* name); // check if argument exists
8282

83+
String hostHeader(); // get request host header if available or empty String if not
84+
8385
// send response to the client
8486
// code - HTTP response code, can be 200 or 404
8587
// content_type - HTTP content type, like "text/plain" or "image/png"
@@ -134,6 +136,8 @@ template<typename T> size_t streamFile(T &file, const String& contentType){
134136
size_t _contentLength;
135137
String _responseHeaders;
136138

139+
String _hostHeader;
140+
137141
RequestHandler* _firstHandler;
138142
RequestHandler* _lastHandler;
139143
THandlerFunction _notFoundHandler;

libraries/ESP8266WebServer/src/Parsing.cpp

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
9494
}
9595
headerName = req.substring(0, headerDiv);
9696
headerValue = req.substring(headerDiv + 2);
97+
98+
#ifdef DEBUG
99+
DEBUG_OUTPUT.print("headerName: ");
100+
DEBUG_OUTPUT.println(headerName);
101+
DEBUG_OUTPUT.print("headerValue: ");
102+
DEBUG_OUTPUT.println(headerValue);
103+
#endif
104+
97105
if (headerName == "Content-Type"){
98106
if (headerValue.startsWith("text/plain")){
99107
isForm = false;
@@ -103,7 +111,8 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
103111
}
104112
} else if (headerName == "Content-Length"){
105113
contentLength = headerValue.toInt();
106-
Serial.printf("Content-Length: %d\r\n", contentLength);
114+
} else if (headerName == "Host"){
115+
_hostHeader = headerValue;
107116
}
108117
}
109118

@@ -137,6 +146,31 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
137146
}
138147
}
139148
} else {
149+
String headerName;
150+
String headerValue;
151+
//parse headers
152+
while(1){
153+
req = client.readStringUntil('\r');
154+
client.readStringUntil('\n');
155+
if (req == "") break;//no moar headers
156+
int headerDiv = req.indexOf(':');
157+
if (headerDiv == -1){
158+
break;
159+
}
160+
headerName = req.substring(0, headerDiv);
161+
headerValue = req.substring(headerDiv + 2);
162+
163+
#ifdef DEBUG
164+
DEBUG_OUTPUT.print("headerName: ");
165+
DEBUG_OUTPUT.println(headerName);
166+
DEBUG_OUTPUT.print("headerValue: ");
167+
DEBUG_OUTPUT.println(headerValue);
168+
#endif
169+
170+
if (headerName == "Host"){
171+
_hostHeader = headerValue;
172+
}
173+
}
140174
_parseArguments(searchStr);
141175
}
142176
client.flush();

0 commit comments

Comments
 (0)