From 0897f9e2e31dc61e54a9b25dd9f2cf0395aa8992 Mon Sep 17 00:00:00 2001 From: ficeto Date: Fri, 15 May 2015 02:22:00 +0300 Subject: [PATCH] fix reading bytes from incoming POST upload proper error and premature connection loss should be implemented to handle weird cases where we might not get the whole post content --- cores/esp8266/Arduino.h | 2 +- cores/esp8266/spiffs/spiffs_config.h | 1 - .../ESP8266WebServer/src/ESP8266WebServer.h | 1 + libraries/ESP8266WebServer/src/Parsing.cpp | 46 ++++++++++++------- 4 files changed, 31 insertions(+), 19 deletions(-) diff --git a/cores/esp8266/Arduino.h b/cores/esp8266/Arduino.h index bd5f79625..1170b6f4a 100644 --- a/cores/esp8266/Arduino.h +++ b/cores/esp8266/Arduino.h @@ -38,7 +38,7 @@ extern "C" { #include "pgmspace.h" #include "esp8266_peri.h" #include "twi.h" -#include "spiffs/spiffs.h" + //#include "spiffs/spiffs.h" void yield(void); diff --git a/cores/esp8266/spiffs/spiffs_config.h b/cores/esp8266/spiffs/spiffs_config.h index 3c55cf97f..095bef900 100755 --- a/cores/esp8266/spiffs/spiffs_config.h +++ b/cores/esp8266/spiffs/spiffs_config.h @@ -20,7 +20,6 @@ #include "stddef.h" #include "osapi.h" #include "ets_sys.h" -#include // ----------- >8 ------------ #define IRAM_ATTR __attribute__((section(".iram.text"))) #define STORE_TYPEDEF_ATTR __attribute__((aligned(4),packed)) diff --git a/libraries/ESP8266WebServer/src/ESP8266WebServer.h b/libraries/ESP8266WebServer/src/ESP8266WebServer.h index 8f70bd621..375d09600 100644 --- a/libraries/ESP8266WebServer/src/ESP8266WebServer.h +++ b/libraries/ESP8266WebServer/src/ESP8266WebServer.h @@ -83,6 +83,7 @@ protected: static const char* _responseCodeToString(int code); void _parseForm(WiFiClient& client, String boundary, uint32_t len); void _uploadWriteByte(uint8_t b); + uint8_t _uploadReadByte(WiFiClient& client); struct RequestHandler; struct RequestArgument { diff --git a/libraries/ESP8266WebServer/src/Parsing.cpp b/libraries/ESP8266WebServer/src/Parsing.cpp index d2f0ee762..40a58d24a 100644 --- a/libraries/ESP8266WebServer/src/Parsing.cpp +++ b/libraries/ESP8266WebServer/src/Parsing.cpp @@ -214,12 +214,22 @@ void ESP8266WebServer::_uploadWriteByte(uint8_t b){ _currentUpload.buf[_currentUpload.currentSize++] = b; } +uint8_t ESP8266WebServer::_uploadReadByte(WiFiClient& client){ + int res = client.read(); + if(res == -1){ + while(!client.available()) + yield(); + res = client.read(); + } + return (uint8_t)res; +} + void ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t len){ #ifdef DEBUG DEBUG_OUTPUT.print("Parse Form: Boundary: "); DEBUG_OUTPUT.print(boundary); - DEBUG_OUTPUT.print("Length: "); + DEBUG_OUTPUT.print(" Length: "); DEBUG_OUTPUT.println(len); #endif String line; @@ -249,17 +259,17 @@ void ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t argFilename = argName.substring(nameStart+2, argName.length() - 1); argName = argName.substring(0, argName.indexOf('"')); argIsFile = true; - #ifdef DEBUG +#ifdef DEBUG DEBUG_OUTPUT.print("PostArg FileName: "); DEBUG_OUTPUT.println(argFilename); - #endif +#endif //use GET to set the filename if uploading using blob if (argFilename == "blob" && hasArg("filename")) argFilename = arg("filename"); } - #ifdef DEBUG +#ifdef DEBUG DEBUG_OUTPUT.print("PostArg Name: "); DEBUG_OUTPUT.println(argName); - #endif +#endif argType = "text/plain"; line = client.readStringUntil('\r'); client.readStringUntil('\n'); @@ -269,10 +279,10 @@ void ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t client.readStringUntil('\r'); client.readStringUntil('\n'); } - #ifdef DEBUG +#ifdef DEBUG DEBUG_OUTPUT.print("PostArg Type: "); DEBUG_OUTPUT.println(argType); - #endif +#endif if (!argIsFile){ while(1){ line = client.readStringUntil('\r'); @@ -281,20 +291,20 @@ void ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t if (argValue.length() > 0) argValue += "\n"; argValue += line; } - #ifdef DEBUG +#ifdef DEBUG DEBUG_OUTPUT.print("PostArg Value: "); DEBUG_OUTPUT.println(argValue); DEBUG_OUTPUT.println(); - #endif +#endif RequestArgument& arg = postArgs[postArgsLen++]; arg.key = argName; arg.value = argValue; if (line == ("--"+boundary+"--")){ - #ifdef DEBUG +#ifdef DEBUG DEBUG_OUTPUT.println("Done Parsing POST"); - #endif +#endif break; } } else { @@ -312,23 +322,23 @@ void ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t #endif if (_fileUploadHandler) _fileUploadHandler(); _currentUpload.status = UPLOAD_FILE_WRITE; - uint8_t argByte = client.read(); + uint8_t argByte = _uploadReadByte(client); readfile: while(argByte != 0x0D){ _uploadWriteByte(argByte); - argByte = client.read(); + argByte = _uploadReadByte(client); } - argByte = client.read(); + argByte = _uploadReadByte(client); if (argByte == 0x0A){ - argByte = client.read(); + argByte = _uploadReadByte(client); if ((char)argByte != '-'){ //continue reading the file _uploadWriteByte(0x0D); _uploadWriteByte(0x0A); goto readfile; } else { - argByte = client.read(); + argByte = _uploadReadByte(client); if ((char)argByte != '-'){ //continue reading the file _uploadWriteByte(0x0D); @@ -366,11 +376,13 @@ readfile: } else { _uploadWriteByte(0x0D); _uploadWriteByte(0x0A); + _uploadWriteByte((uint8_t)('-')); + _uploadWriteByte((uint8_t)('-')); uint32_t i = 0; while(i < boundary.length()){ _uploadWriteByte(endBuf[i++]); } - argByte = client.read(); + argByte = _uploadReadByte(client); goto readfile; } } else {