23
23
#include " WiFiServer.h"
24
24
#include " WiFiClient.h"
25
25
#include " ESP8266WebServer.h"
26
+ #include " detail/mimetable.h"
26
27
27
28
// #define DEBUG_ESP_HTTP_SERVER
28
29
#ifdef DEBUG_ESP_PORT
31
32
#define DEBUG_OUTPUT Serial
32
33
#endif
33
34
35
+ static const char Content_Type[] PROGMEM = " Content-Type" ;
36
+ static const char filename[] PROGMEM = " filename" ;
37
+
34
38
static char * readBytesWithTimeout (WiFiClient& client, size_t maxLength, size_t & dataLength, int timeout_ms)
35
39
{
36
40
char *buf = nullptr ;
@@ -98,15 +102,15 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
98
102
_chunked = false ;
99
103
100
104
HTTPMethod method = HTTP_GET;
101
- if (methodStr == " POST" ) {
105
+ if (methodStr == F ( " POST" ) ) {
102
106
method = HTTP_POST;
103
- } else if (methodStr == " DELETE" ) {
107
+ } else if (methodStr == F ( " DELETE" ) ) {
104
108
method = HTTP_DELETE;
105
- } else if (methodStr == " OPTIONS" ) {
109
+ } else if (methodStr == F ( " OPTIONS" ) ) {
106
110
method = HTTP_OPTIONS;
107
- } else if (methodStr == " PUT" ) {
111
+ } else if (methodStr == F ( " PUT" ) ) {
108
112
method = HTTP_PUT;
109
- } else if (methodStr == " PATCH" ) {
113
+ } else if (methodStr == F ( " PATCH" ) ) {
110
114
method = HTTP_PATCH;
111
115
}
112
116
_currentMethod = method;
@@ -158,20 +162,21 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
158
162
DEBUG_OUTPUT.println (headerValue);
159
163
#endif
160
164
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 ))){
163
168
isForm = false ;
164
- } else if (headerValue.startsWith (" application/x-www-form-urlencoded" )){
169
+ } else if (headerValue.startsWith (F ( " application/x-www-form-urlencoded" ) )){
165
170
isForm = false ;
166
171
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 );
169
174
boundaryStr.replace (" \" " ," " );
170
175
isForm = true ;
171
176
}
172
- } else if (headerName.equalsIgnoreCase (" Content-Length" )){
177
+ } else if (headerName.equalsIgnoreCase (F ( " Content-Length" ) )){
173
178
contentLength = headerValue.toInt ();
174
- } else if (headerName.equalsIgnoreCase (" Host" )){
179
+ } else if (headerName.equalsIgnoreCase (F ( " Host" ) )){
175
180
_hostHeader = headerValue;
176
181
}
177
182
}
@@ -193,7 +198,7 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
193
198
if (!isEncoded){
194
199
// plain post json or other data
195
200
RequestArgument& arg = _currentArgs[_currentArgCount++];
196
- arg.key = " plain" ;
201
+ arg.key = F ( " plain" ) ;
197
202
arg.value = String (plainBuf);
198
203
}
199
204
@@ -389,7 +394,7 @@ bool ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t
389
394
390
395
line = client.readStringUntil (' \r ' );
391
396
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" ) )){
393
398
int nameStart = line.indexOf (' =' );
394
399
if (nameStart != -1 ){
395
400
argName = line.substring (nameStart+2 );
@@ -405,16 +410,18 @@ bool ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t
405
410
DEBUG_OUTPUT.println (argFilename);
406
411
#endif
407
412
// 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));
409
415
}
410
416
#ifdef DEBUG_ESP_HTTP_SERVER
411
417
DEBUG_OUTPUT.print (" PostArg Name: " );
412
418
DEBUG_OUTPUT.println (argName);
413
419
#endif
414
- argType = " text/plain" ;
420
+ using namespace mime ;
421
+ argType = FPSTR (mimeTable[txt].mimeType );
415
422
line = client.readStringUntil (' \r ' );
416
423
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) )){
418
425
argType = line.substring (line.indexOf (' :' )+2 );
419
426
// skip next line
420
427
client.readStringUntil (' \r ' );
@@ -559,7 +566,8 @@ bool ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t
559
566
arg.value = postArgs[iarg].value ;
560
567
}
561
568
_currentArgCount = iarg;
562
- if (postArgs) delete[] postArgs;
569
+ if (postArgs)
570
+ delete[] postArgs;
563
571
return true ;
564
572
}
565
573
#ifdef DEBUG_ESP_HTTP_SERVER
0 commit comments