1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-21 10:26:06 +03:00

Host header support

This commit is contained in:
aalku 2015-08-14 22:30:15 +02:00
parent 5cae888278
commit 559670baaa
3 changed files with 43 additions and 0 deletions

View File

@ -253,6 +253,10 @@ bool ESP8266WebServer::hasArg(const char* name) {
return false; return false;
} }
String ESP8266WebServer::hostHeader() {
return _hostHeader;
}
void ESP8266WebServer::onFileUpload(THandlerFunction fn) { void ESP8266WebServer::onFileUpload(THandlerFunction fn) {
_fileUploadHandler = fn; _fileUploadHandler = fn;
} }

View File

@ -80,6 +80,8 @@ public:
int args(); // get arguments count int args(); // get arguments count
bool hasArg(const char* name); // check if argument exists bool hasArg(const char* name); // check if argument exists
String hostHeader(); // get request host header if available or empty String if not
// send response to the client // send response to the client
// code - HTTP response code, can be 200 or 404 // code - HTTP response code, can be 200 or 404
// content_type - HTTP content type, like "text/plain" or "image/png" // content_type - HTTP content type, like "text/plain" or "image/png"
@ -134,6 +136,8 @@ protected:
size_t _contentLength; size_t _contentLength;
String _responseHeaders; String _responseHeaders;
String _hostHeader;
RequestHandler* _firstHandler; RequestHandler* _firstHandler;
RequestHandler* _lastHandler; RequestHandler* _lastHandler;
THandlerFunction _notFoundHandler; THandlerFunction _notFoundHandler;

View File

@ -94,6 +94,14 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
} }
headerName = req.substring(0, headerDiv); headerName = req.substring(0, headerDiv);
headerValue = req.substring(headerDiv + 2); headerValue = req.substring(headerDiv + 2);
#ifdef DEBUG
DEBUG_OUTPUT.print("headerName: ");
DEBUG_OUTPUT.println(headerName);
DEBUG_OUTPUT.print("headerValue: ");
DEBUG_OUTPUT.println(headerValue);
#endif
if (headerName == "Content-Type"){ if (headerName == "Content-Type"){
if (headerValue.startsWith("text/plain")){ if (headerValue.startsWith("text/plain")){
isForm = false; isForm = false;
@ -103,6 +111,8 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
} }
} else if (headerName == "Content-Length"){ } else if (headerName == "Content-Length"){
contentLength = headerValue.toInt(); contentLength = headerValue.toInt();
} else if (headerName == "Host"){
_hostHeader = headerValue;
} }
} }
@ -134,6 +144,31 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
_parseForm(client, boundaryStr, contentLength); _parseForm(client, boundaryStr, contentLength);
} }
} else { } else {
String headerName;
String headerValue;
//parse headers
while(1){
req = client.readStringUntil('\r');
client.readStringUntil('\n');
if (req == "") break;//no moar headers
int headerDiv = req.indexOf(':');
if (headerDiv == -1){
break;
}
headerName = req.substring(0, headerDiv);
headerValue = req.substring(headerDiv + 2);
#ifdef DEBUG
DEBUG_OUTPUT.print("headerName: ");
DEBUG_OUTPUT.println(headerName);
DEBUG_OUTPUT.print("headerValue: ");
DEBUG_OUTPUT.println(headerValue);
#endif
if (headerName == "Host"){
_hostHeader = headerValue;
}
}
_parseArguments(searchStr); _parseArguments(searchStr);
} }
client.flush(); client.flush();