diff --git a/libraries/ESP8266WebServer/src/ESP8266WebServer.h b/libraries/ESP8266WebServer/src/ESP8266WebServer.h index ae38eb8cc..d104fbec3 100644 --- a/libraries/ESP8266WebServer/src/ESP8266WebServer.h +++ b/libraries/ESP8266WebServer/src/ESP8266WebServer.h @@ -111,7 +111,7 @@ protected: bool _parseRequest(WiFiClient& client); void _parseArguments(String data); static const char* _responseCodeToString(int code); - void _parseForm(WiFiClient& client, String boundary, uint32_t len); + bool _parseForm(WiFiClient& client, String boundary, uint32_t len); void _uploadWriteByte(uint8_t b); uint8_t _uploadReadByte(WiFiClient& client); void _prepareHeader(String& response, int code, const char* content_type, size_t contentLength); diff --git a/libraries/ESP8266WebServer/src/Parsing.cpp b/libraries/ESP8266WebServer/src/Parsing.cpp index 682839d13..618270247 100644 --- a/libraries/ESP8266WebServer/src/Parsing.cpp +++ b/libraries/ESP8266WebServer/src/Parsing.cpp @@ -1,8 +1,8 @@ -/* +/* Parsing.cpp - HTTP request parsing. Copyright (c) 2015 Ivan Grokhotkov. All rights reserved. - + This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either @@ -24,8 +24,8 @@ #include "WiFiClient.h" #include "ESP8266WebServer.h" -// #define DEBUG -#define DEBUG_OUTPUT Serial1 +#define DEBUG +#define DEBUG_OUTPUT Serial bool ESP8266WebServer::_parseRequest(WiFiClient& client) { // Read the first line of HTTP request @@ -43,7 +43,7 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) { #endif return false; } - + String methodStr = req.substring(0, addr_start); String url = req.substring(addr_start + 1, addr_end); String searchStr = ""; @@ -53,7 +53,7 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) { url = url.substring(0, hasSearch); } _currentUri = url; - + HTTPMethod method = HTTP_GET; if (methodStr == "POST") { method = HTTP_POST; @@ -65,7 +65,7 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) { method = HTTP_PATCH; } _currentMethod = method; - + #ifdef DEBUG DEBUG_OUTPUT.print("method: "); DEBUG_OUTPUT.print(methodStr); @@ -103,9 +103,10 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) { } } else if (headerName == "Content-Length"){ contentLength = headerValue.toInt(); + Serial.printf("Content-Length: %d\r\n", contentLength); } } - + if (!isForm){ if (searchStr != "") searchStr += '&'; //some clients send headers first and data after (like we do) @@ -131,7 +132,9 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) { } _parseArguments(searchStr); if (isForm){ - _parseForm(client, boundaryStr, contentLength); + if (!_parseForm(client, boundaryStr, contentLength)) { + return false; + } } } else { _parseArguments(searchStr); @@ -242,8 +245,8 @@ uint8_t ESP8266WebServer::_uploadReadByte(WiFiClient& client){ return (uint8_t)res; } -void ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t len){ - +bool ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t len){ + #ifdef DEBUG DEBUG_OUTPUT.print("Parse Form: Boundary: "); DEBUG_OUTPUT.print(boundary); @@ -251,7 +254,12 @@ void ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t DEBUG_OUTPUT.println(len); #endif String line; - line = client.readStringUntil('\r'); + int retry = 0; + do { + line = client.readStringUntil('\r'); + ++retry; + } while (line.length() == 0 && retry < 3); + client.readStringUntil('\n'); //start reading the form if (line == ("--"+boundary)){ @@ -263,7 +271,7 @@ void ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t String argType; String argFilename; bool argIsFile = false; - + line = client.readStringUntil('\r'); client.readStringUntil('\n'); if (line.startsWith("Content-Disposition")){ @@ -314,11 +322,11 @@ void ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t DEBUG_OUTPUT.println(argValue); DEBUG_OUTPUT.println(); #endif - + RequestArgument& arg = postArgs[postArgsLen++]; arg.key = argName; arg.value = argValue; - + if (line == ("--"+boundary+"--")){ #ifdef DEBUG DEBUG_OUTPUT.println("Done Parsing POST"); @@ -346,7 +354,7 @@ readfile: _uploadWriteByte(argByte); argByte = _uploadReadByte(client); } - + argByte = _uploadReadByte(client); if (argByte == 0x0A){ argByte = _uploadReadByte(client); @@ -365,10 +373,10 @@ readfile: goto readfile; } } - + uint8_t endBuf[boundary.length()]; client.readBytes(endBuf, boundary.length()); - + if (strstr((const char*)endBuf, boundary.c_str()) != NULL){ if (_fileUploadHandler) _fileUploadHandler(); _currentUpload.totalSize += _currentUpload.currentSize; @@ -412,7 +420,7 @@ readfile: } } } - + int iarg; int totalArgs = ((32 - postArgsLen) < _currentArgCount)?(32 - postArgsLen):_currentArgCount; for (iarg = 0; iarg < totalArgs; iarg++){ @@ -429,7 +437,11 @@ readfile: } _currentArgCount = iarg; if (postArgs) delete[] postArgs; + return true; } +#ifdef DEBUG + DEBUG_OUTPUT.print("Error: line: "); + DEBUG_OUTPUT.println(line); +#endif + return false; } - -