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
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+
3438static 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
0 commit comments