Skip to content

Commit 5328a8b

Browse files
authored
additional mimetable fixes, additional string moves to progmem (#4371)
1 parent bb90e12 commit 5328a8b

File tree

3 files changed

+35
-29
lines changed

3 files changed

+35
-29
lines changed

libraries/ESP8266WebServer/src/ESP8266WebServer.cpp

+7-10
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,9 @@
3636
#define DEBUG_OUTPUT Serial
3737
#endif
3838

39-
//const char * AUTHORIZATION_HEADER = "Authorization";
4039
static const char AUTHORIZATION_HEADER[] PROGMEM = "Authorization";
4140
static const char qop_auth[] PROGMEM = "qop=auth";
4241
static const char WWW_Authenticate[] PROGMEM = "WWW-Authenticate";
43-
static const char colon[] PROGMEM = ":";
4442
static const char Content_Length[] PROGMEM = "Content-Length";
4543

4644

@@ -81,10 +79,9 @@ ESP8266WebServer::ESP8266WebServer(int port)
8179
}
8280

8381
ESP8266WebServer::~ESP8266WebServer() {
84-
close();
82+
_server.close();
8583
if (_currentHeaders)
8684
delete[]_currentHeaders;
87-
_headerKeysCount = 0;
8885
RequestHandler* handler = _firstHandler;
8986
while (handler) {
9087
RequestHandler* next = handler->next();
@@ -105,7 +102,7 @@ void ESP8266WebServer::begin(uint16_t port) {
105102

106103
String ESP8266WebServer::_extractParam(String& authReq,const String& param,const char delimit){
107104
int _begin = authReq.indexOf(param);
108-
if (_begin==-1)
105+
if (_begin == -1)
109106
return "";
110107
return authReq.substring(_begin+param.length(),authReq.indexOf(delimit,_begin+param.length()));
111108
}
@@ -195,9 +192,9 @@ bool ESP8266WebServer::authenticate(const char * username, const char * password
195192
#endif
196193
md5.begin();
197194
if(authReq.indexOf(FPSTR(qop_auth)) != -1) {
198-
md5.add(_H1 + FPSTR(colon) + _nonce + FPSTR(colon) + _nc + FPSTR(colon) + _cnonce + ":auth:" + _H2);
199-
}else{
200-
md5.add(_H1 + FPSTR(colon) + _nonce + FPSTR(colon) + _H2);
195+
md5.add(_H1 + ':' + _nonce + ':' + _nc + ':' + _cnonce + F(":auth:") + _H2);
196+
} else {
197+
md5.add(_H1 + ':' + _nonce + ':' + _H2);
201198
}
202199
md5.calculate();
203200
String _responsecheck = md5.toString();
@@ -237,7 +234,7 @@ void ESP8266WebServer::requestAuthentication(HTTPAuthMethod mode, const char* re
237234
sendHeader(String(FPSTR(WWW_Authenticate)), String(F("Digest realm=\"")) +_srealm + String(F("\", qop=\"auth\", nonce=\"")) + _snonce + String(F("\", opaque=\"")) + _sopaque + String(F("\"")));
238235
}
239236
using namespace mime;
240-
send(401, mimeTable[html].mimeType, authFailMsg);
237+
send(401, String(FPSTR(mimeTable[html].mimeType)), authFailMsg);
241238
}
242239

243240
void ESP8266WebServer::on(const String &uri, ESP8266WebServer::THandlerFunction handler) {
@@ -603,7 +600,7 @@ void ESP8266WebServer::_handleRequest() {
603600
}
604601
if (!handled) {
605602
using namespace mime;
606-
send(404, mimeTable[html].mimeType, String(F("Not found: ")) + _currentUri);
603+
send(404, String(FPSTR(mimeTable[html].mimeType)), String(F("Not found: ")) + _currentUri);
607604
handled = true;
608605
}
609606
if (handled) {

libraries/ESP8266WebServer/src/Parsing.cpp

+26-18
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "WiFiServer.h"
2424
#include "WiFiClient.h"
2525
#include "ESP8266WebServer.h"
26+
#include "detail/mimetable.h"
2627

2728
//#define DEBUG_ESP_HTTP_SERVER
2829
#ifdef DEBUG_ESP_PORT
@@ -31,6 +32,9 @@
3132
#define DEBUG_OUTPUT Serial
3233
#endif
3334

35+
static const char Content_Type[] PROGMEM = "Content-Type";
36+
static const char filename[] PROGMEM = "filename";
37+
3438
static char* readBytesWithTimeout(WiFiClient& client, size_t maxLength, size_t& dataLength, int timeout_ms)
3539
{
3640
char *buf = nullptr;
@@ -98,15 +102,15 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
98102
_chunked = false;
99103

100104
HTTPMethod method = HTTP_GET;
101-
if (methodStr == "POST") {
105+
if (methodStr == F("POST")) {
102106
method = HTTP_POST;
103-
} else if (methodStr == "DELETE") {
107+
} else if (methodStr == F("DELETE")) {
104108
method = HTTP_DELETE;
105-
} else if (methodStr == "OPTIONS") {
109+
} else if (methodStr == F("OPTIONS")) {
106110
method = HTTP_OPTIONS;
107-
} else if (methodStr == "PUT") {
111+
} else if (methodStr == F("PUT")) {
108112
method = HTTP_PUT;
109-
} else if (methodStr == "PATCH") {
113+
} else if (methodStr == F("PATCH")) {
110114
method = HTTP_PATCH;
111115
}
112116
_currentMethod = method;
@@ -158,20 +162,21 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
158162
DEBUG_OUTPUT.println(headerValue);
159163
#endif
160164

161-
if (headerName.equalsIgnoreCase("Content-Type")){
162-
if (headerValue.startsWith("text/plain")){
165+
if (headerName.equalsIgnoreCase(FPSTR(Content_Type))){
166+
using namespace mime;
167+
if (headerValue.startsWith(FPSTR(mimeTable[txt].mimeType))){
163168
isForm = false;
164-
} else if (headerValue.startsWith("application/x-www-form-urlencoded")){
169+
} else if (headerValue.startsWith(F("application/x-www-form-urlencoded"))){
165170
isForm = false;
166171
isEncoded = true;
167-
} else if (headerValue.startsWith("multipart/")){
168-
boundaryStr = headerValue.substring(headerValue.indexOf('=')+1);
172+
} else if (headerValue.startsWith(F("multipart/"))){
173+
boundaryStr = headerValue.substring(headerValue.indexOf('=') + 1);
169174
boundaryStr.replace("\"","");
170175
isForm = true;
171176
}
172-
} else if (headerName.equalsIgnoreCase("Content-Length")){
177+
} else if (headerName.equalsIgnoreCase(F("Content-Length"))){
173178
contentLength = headerValue.toInt();
174-
} else if (headerName.equalsIgnoreCase("Host")){
179+
} else if (headerName.equalsIgnoreCase(F("Host"))){
175180
_hostHeader = headerValue;
176181
}
177182
}
@@ -193,7 +198,7 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
193198
if(!isEncoded){
194199
//plain post json or other data
195200
RequestArgument& arg = _currentArgs[_currentArgCount++];
196-
arg.key = "plain";
201+
arg.key = F("plain");
197202
arg.value = String(plainBuf);
198203
}
199204

@@ -389,7 +394,7 @@ bool ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t
389394

390395
line = client.readStringUntil('\r');
391396
client.readStringUntil('\n');
392-
if (line.length() > 19 && line.substring(0, 19).equalsIgnoreCase("Content-Disposition")){
397+
if (line.length() > 19 && line.substring(0, 19).equalsIgnoreCase(F("Content-Disposition"))){
393398
int nameStart = line.indexOf('=');
394399
if (nameStart != -1){
395400
argName = line.substring(nameStart+2);
@@ -405,16 +410,18 @@ bool ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t
405410
DEBUG_OUTPUT.println(argFilename);
406411
#endif
407412
//use GET to set the filename if uploading using blob
408-
if (argFilename == "blob" && hasArg("filename")) argFilename = arg("filename");
413+
if (argFilename == F("blob") && hasArg(FPSTR(filename)))
414+
argFilename = arg(FPSTR(filename));
409415
}
410416
#ifdef DEBUG_ESP_HTTP_SERVER
411417
DEBUG_OUTPUT.print("PostArg Name: ");
412418
DEBUG_OUTPUT.println(argName);
413419
#endif
414-
argType = "text/plain";
420+
using namespace mime;
421+
argType = FPSTR(mimeTable[txt].mimeType);
415422
line = client.readStringUntil('\r');
416423
client.readStringUntil('\n');
417-
if (line.length() > 12 && line.substring(0, 12).equalsIgnoreCase("Content-Type")){
424+
if (line.length() > 12 && line.substring(0, 12).equalsIgnoreCase(FPSTR(Content_Type))){
418425
argType = line.substring(line.indexOf(':')+2);
419426
//skip next line
420427
client.readStringUntil('\r');
@@ -559,7 +566,8 @@ bool ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t
559566
arg.value = postArgs[iarg].value;
560567
}
561568
_currentArgCount = iarg;
562-
if (postArgs) delete[] postArgs;
569+
if (postArgs)
570+
delete[] postArgs;
563571
return true;
564572
}
565573
#ifdef DEBUG_ESP_HTTP_SERVER

libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ class StaticRequestHandler : public RequestHandler {
9191
if (!_isFile) {
9292
// Base URI doesn't point to a file.
9393
// If a directory is requested, look for index file.
94-
if (requestUri.endsWith("/")) requestUri += "index.htm";
94+
if (requestUri.endsWith("/"))
95+
requestUri += "index.htm";
9596

9697
// Append whatever follows this URI in request to get the file path.
9798
path += requestUri.substring(_baseUriLength);

0 commit comments

Comments
 (0)