Skip to content

Commit 7eb29f8

Browse files
committed
Host header support
1 parent fe8c86a commit 7eb29f8

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

hardware/esp8266com/esp8266/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
}

hardware/esp8266com/esp8266/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;

hardware/esp8266com/esp8266/libraries/ESP8266WebServer/src/Parsing.cpp

Lines changed: 35 additions & 0 deletions
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,6 +111,8 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
103111
}
104112
} else if (headerName == "Content-Length"){
105113
contentLength = headerValue.toInt();
114+
} else if (headerName == "Host"){
115+
_hostHeader = headerValue;
106116
}
107117
}
108118

@@ -134,6 +144,31 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
134144
_parseForm(client, boundaryStr, contentLength);
135145
}
136146
} else {
147+
String headerName;
148+
String headerValue;
149+
//parse headers
150+
while(1){
151+
req = client.readStringUntil('\r');
152+
client.readStringUntil('\n');
153+
if (req == "") break;//no moar headers
154+
int headerDiv = req.indexOf(':');
155+
if (headerDiv == -1){
156+
break;
157+
}
158+
headerName = req.substring(0, headerDiv);
159+
headerValue = req.substring(headerDiv + 2);
160+
161+
#ifdef DEBUG
162+
DEBUG_OUTPUT.print("headerName: ");
163+
DEBUG_OUTPUT.println(headerName);
164+
DEBUG_OUTPUT.print("headerValue: ");
165+
DEBUG_OUTPUT.println(headerValue);
166+
#endif
167+
168+
if (headerName == "Host"){
169+
_hostHeader = headerValue;
170+
}
171+
}
137172
_parseArguments(searchStr);
138173
}
139174
client.flush();

0 commit comments

Comments
 (0)