diff --git a/libraries/ESP8266WebServer/src/ESP8266WebServer.cpp b/libraries/ESP8266WebServer/src/ESP8266WebServer.cpp index 6a624cf927..7f0657629f 100644 --- a/libraries/ESP8266WebServer/src/ESP8266WebServer.cpp +++ b/libraries/ESP8266WebServer/src/ESP8266WebServer.cpp @@ -36,11 +36,9 @@ #define DEBUG_OUTPUT Serial #endif -//const char * AUTHORIZATION_HEADER = "Authorization"; static const char AUTHORIZATION_HEADER[] PROGMEM = "Authorization"; static const char qop_auth[] PROGMEM = "qop=auth"; static const char WWW_Authenticate[] PROGMEM = "WWW-Authenticate"; -static const char colon[] PROGMEM = ":"; static const char Content_Length[] PROGMEM = "Content-Length"; @@ -81,10 +79,9 @@ ESP8266WebServer::ESP8266WebServer(int port) } ESP8266WebServer::~ESP8266WebServer() { - close(); + _server.close(); if (_currentHeaders) delete[]_currentHeaders; - _headerKeysCount = 0; RequestHandler* handler = _firstHandler; while (handler) { RequestHandler* next = handler->next(); @@ -105,7 +102,7 @@ void ESP8266WebServer::begin(uint16_t port) { String ESP8266WebServer::_extractParam(String& authReq,const String& param,const char delimit){ int _begin = authReq.indexOf(param); - if (_begin==-1) + if (_begin == -1) return ""; return authReq.substring(_begin+param.length(),authReq.indexOf(delimit,_begin+param.length())); } @@ -195,9 +192,9 @@ bool ESP8266WebServer::authenticate(const char * username, const char * password #endif md5.begin(); if(authReq.indexOf(FPSTR(qop_auth)) != -1) { - md5.add(_H1 + FPSTR(colon) + _nonce + FPSTR(colon) + _nc + FPSTR(colon) + _cnonce + ":auth:" + _H2); - }else{ - md5.add(_H1 + FPSTR(colon) + _nonce + FPSTR(colon) + _H2); + md5.add(_H1 + ':' + _nonce + ':' + _nc + ':' + _cnonce + F(":auth:") + _H2); + } else { + md5.add(_H1 + ':' + _nonce + ':' + _H2); } md5.calculate(); String _responsecheck = md5.toString(); @@ -237,7 +234,7 @@ void ESP8266WebServer::requestAuthentication(HTTPAuthMethod mode, const char* re sendHeader(String(FPSTR(WWW_Authenticate)), String(F("Digest realm=\"")) +_srealm + String(F("\", qop=\"auth\", nonce=\"")) + _snonce + String(F("\", opaque=\"")) + _sopaque + String(F("\""))); } using namespace mime; - send(401, mimeTable[html].mimeType, authFailMsg); + send(401, String(FPSTR(mimeTable[html].mimeType)), authFailMsg); } void ESP8266WebServer::on(const String &uri, ESP8266WebServer::THandlerFunction handler) { @@ -603,7 +600,7 @@ void ESP8266WebServer::_handleRequest() { } if (!handled) { using namespace mime; - send(404, mimeTable[html].mimeType, String(F("Not found: ")) + _currentUri); + send(404, String(FPSTR(mimeTable[html].mimeType)), String(F("Not found: ")) + _currentUri); handled = true; } if (handled) { diff --git a/libraries/ESP8266WebServer/src/Parsing.cpp b/libraries/ESP8266WebServer/src/Parsing.cpp index ac65d3779d..25bf6acec0 100644 --- a/libraries/ESP8266WebServer/src/Parsing.cpp +++ b/libraries/ESP8266WebServer/src/Parsing.cpp @@ -23,6 +23,7 @@ #include "WiFiServer.h" #include "WiFiClient.h" #include "ESP8266WebServer.h" +#include "detail/mimetable.h" //#define DEBUG_ESP_HTTP_SERVER #ifdef DEBUG_ESP_PORT @@ -31,6 +32,9 @@ #define DEBUG_OUTPUT Serial #endif +static const char Content_Type[] PROGMEM = "Content-Type"; +static const char filename[] PROGMEM = "filename"; + static char* readBytesWithTimeout(WiFiClient& client, size_t maxLength, size_t& dataLength, int timeout_ms) { char *buf = nullptr; @@ -98,15 +102,15 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) { _chunked = false; HTTPMethod method = HTTP_GET; - if (methodStr == "POST") { + if (methodStr == F("POST")) { method = HTTP_POST; - } else if (methodStr == "DELETE") { + } else if (methodStr == F("DELETE")) { method = HTTP_DELETE; - } else if (methodStr == "OPTIONS") { + } else if (methodStr == F("OPTIONS")) { method = HTTP_OPTIONS; - } else if (methodStr == "PUT") { + } else if (methodStr == F("PUT")) { method = HTTP_PUT; - } else if (methodStr == "PATCH") { + } else if (methodStr == F("PATCH")) { method = HTTP_PATCH; } _currentMethod = method; @@ -158,20 +162,21 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) { DEBUG_OUTPUT.println(headerValue); #endif - if (headerName.equalsIgnoreCase("Content-Type")){ - if (headerValue.startsWith("text/plain")){ + if (headerName.equalsIgnoreCase(FPSTR(Content_Type))){ + using namespace mime; + if (headerValue.startsWith(FPSTR(mimeTable[txt].mimeType))){ isForm = false; - } else if (headerValue.startsWith("application/x-www-form-urlencoded")){ + } else if (headerValue.startsWith(F("application/x-www-form-urlencoded"))){ isForm = false; isEncoded = true; - } else if (headerValue.startsWith("multipart/")){ - boundaryStr = headerValue.substring(headerValue.indexOf('=')+1); + } else if (headerValue.startsWith(F("multipart/"))){ + boundaryStr = headerValue.substring(headerValue.indexOf('=') + 1); boundaryStr.replace("\"",""); isForm = true; } - } else if (headerName.equalsIgnoreCase("Content-Length")){ + } else if (headerName.equalsIgnoreCase(F("Content-Length"))){ contentLength = headerValue.toInt(); - } else if (headerName.equalsIgnoreCase("Host")){ + } else if (headerName.equalsIgnoreCase(F("Host"))){ _hostHeader = headerValue; } } @@ -193,7 +198,7 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) { if(!isEncoded){ //plain post json or other data RequestArgument& arg = _currentArgs[_currentArgCount++]; - arg.key = "plain"; + arg.key = F("plain"); arg.value = String(plainBuf); } @@ -389,7 +394,7 @@ bool ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t line = client.readStringUntil('\r'); client.readStringUntil('\n'); - if (line.length() > 19 && line.substring(0, 19).equalsIgnoreCase("Content-Disposition")){ + if (line.length() > 19 && line.substring(0, 19).equalsIgnoreCase(F("Content-Disposition"))){ int nameStart = line.indexOf('='); if (nameStart != -1){ argName = line.substring(nameStart+2); @@ -405,16 +410,18 @@ bool ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t DEBUG_OUTPUT.println(argFilename); #endif //use GET to set the filename if uploading using blob - if (argFilename == "blob" && hasArg("filename")) argFilename = arg("filename"); + if (argFilename == F("blob") && hasArg(FPSTR(filename))) + argFilename = arg(FPSTR(filename)); } #ifdef DEBUG_ESP_HTTP_SERVER DEBUG_OUTPUT.print("PostArg Name: "); DEBUG_OUTPUT.println(argName); #endif - argType = "text/plain"; + using namespace mime; + argType = FPSTR(mimeTable[txt].mimeType); line = client.readStringUntil('\r'); client.readStringUntil('\n'); - if (line.length() > 12 && line.substring(0, 12).equalsIgnoreCase("Content-Type")){ + if (line.length() > 12 && line.substring(0, 12).equalsIgnoreCase(FPSTR(Content_Type))){ argType = line.substring(line.indexOf(':')+2); //skip next line client.readStringUntil('\r'); @@ -559,7 +566,8 @@ bool ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t arg.value = postArgs[iarg].value; } _currentArgCount = iarg; - if (postArgs) delete[] postArgs; + if (postArgs) + delete[] postArgs; return true; } #ifdef DEBUG_ESP_HTTP_SERVER diff --git a/libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h b/libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h index 63f0339995..feada8c13b 100644 --- a/libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h +++ b/libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h @@ -91,7 +91,8 @@ class StaticRequestHandler : public RequestHandler { if (!_isFile) { // Base URI doesn't point to a file. // If a directory is requested, look for index file. - if (requestUri.endsWith("/")) requestUri += "index.htm"; + if (requestUri.endsWith("/")) + requestUri += "index.htm"; // Append whatever follows this URI in request to get the file path. path += requestUri.substring(_baseUriLength);