Skip to content

Commit 0897f9e

Browse files
ficetoficeto
ficeto
authored and
ficeto
committed
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
1 parent 57c0d3e commit 0897f9e

File tree

4 files changed

+31
-19
lines changed

4 files changed

+31
-19
lines changed

cores/esp8266/Arduino.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ extern "C" {
3838
#include "pgmspace.h"
3939
#include "esp8266_peri.h"
4040
#include "twi.h"
41-
#include "spiffs/spiffs.h"
41+
//#include "spiffs/spiffs.h"
4242

4343
void yield(void);
4444

cores/esp8266/spiffs/spiffs_config.h

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include "stddef.h"
2121
#include "osapi.h"
2222
#include "ets_sys.h"
23-
#include <user_config.h>
2423
// ----------- >8 ------------
2524
#define IRAM_ATTR __attribute__((section(".iram.text")))
2625
#define STORE_TYPEDEF_ATTR __attribute__((aligned(4),packed))

libraries/ESP8266WebServer/src/ESP8266WebServer.h

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ class ESP8266WebServer
8383
static const char* _responseCodeToString(int code);
8484
void _parseForm(WiFiClient& client, String boundary, uint32_t len);
8585
void _uploadWriteByte(uint8_t b);
86+
uint8_t _uploadReadByte(WiFiClient& client);
8687

8788
struct RequestHandler;
8889
struct RequestArgument {

libraries/ESP8266WebServer/src/Parsing.cpp

+29-17
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,22 @@ void ESP8266WebServer::_uploadWriteByte(uint8_t b){
214214
_currentUpload.buf[_currentUpload.currentSize++] = b;
215215
}
216216

217+
uint8_t ESP8266WebServer::_uploadReadByte(WiFiClient& client){
218+
int res = client.read();
219+
if(res == -1){
220+
while(!client.available())
221+
yield();
222+
res = client.read();
223+
}
224+
return (uint8_t)res;
225+
}
226+
217227
void ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t len){
218228

219229
#ifdef DEBUG
220230
DEBUG_OUTPUT.print("Parse Form: Boundary: ");
221231
DEBUG_OUTPUT.print(boundary);
222-
DEBUG_OUTPUT.print("Length: ");
232+
DEBUG_OUTPUT.print(" Length: ");
223233
DEBUG_OUTPUT.println(len);
224234
#endif
225235
String line;
@@ -249,17 +259,17 @@ void ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t
249259
argFilename = argName.substring(nameStart+2, argName.length() - 1);
250260
argName = argName.substring(0, argName.indexOf('"'));
251261
argIsFile = true;
252-
#ifdef DEBUG
262+
#ifdef DEBUG
253263
DEBUG_OUTPUT.print("PostArg FileName: ");
254264
DEBUG_OUTPUT.println(argFilename);
255-
#endif
265+
#endif
256266
//use GET to set the filename if uploading using blob
257267
if (argFilename == "blob" && hasArg("filename")) argFilename = arg("filename");
258268
}
259-
#ifdef DEBUG
269+
#ifdef DEBUG
260270
DEBUG_OUTPUT.print("PostArg Name: ");
261271
DEBUG_OUTPUT.println(argName);
262-
#endif
272+
#endif
263273
argType = "text/plain";
264274
line = client.readStringUntil('\r');
265275
client.readStringUntil('\n');
@@ -269,10 +279,10 @@ void ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t
269279
client.readStringUntil('\r');
270280
client.readStringUntil('\n');
271281
}
272-
#ifdef DEBUG
282+
#ifdef DEBUG
273283
DEBUG_OUTPUT.print("PostArg Type: ");
274284
DEBUG_OUTPUT.println(argType);
275-
#endif
285+
#endif
276286
if (!argIsFile){
277287
while(1){
278288
line = client.readStringUntil('\r');
@@ -281,20 +291,20 @@ void ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t
281291
if (argValue.length() > 0) argValue += "\n";
282292
argValue += line;
283293
}
284-
#ifdef DEBUG
294+
#ifdef DEBUG
285295
DEBUG_OUTPUT.print("PostArg Value: ");
286296
DEBUG_OUTPUT.println(argValue);
287297
DEBUG_OUTPUT.println();
288-
#endif
298+
#endif
289299

290300
RequestArgument& arg = postArgs[postArgsLen++];
291301
arg.key = argName;
292302
arg.value = argValue;
293303

294304
if (line == ("--"+boundary+"--")){
295-
#ifdef DEBUG
305+
#ifdef DEBUG
296306
DEBUG_OUTPUT.println("Done Parsing POST");
297-
#endif
307+
#endif
298308
break;
299309
}
300310
} else {
@@ -312,23 +322,23 @@ void ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t
312322
#endif
313323
if (_fileUploadHandler) _fileUploadHandler();
314324
_currentUpload.status = UPLOAD_FILE_WRITE;
315-
uint8_t argByte = client.read();
325+
uint8_t argByte = _uploadReadByte(client);
316326
readfile:
317327
while(argByte != 0x0D){
318328
_uploadWriteByte(argByte);
319-
argByte = client.read();
329+
argByte = _uploadReadByte(client);
320330
}
321331

322-
argByte = client.read();
332+
argByte = _uploadReadByte(client);
323333
if (argByte == 0x0A){
324-
argByte = client.read();
334+
argByte = _uploadReadByte(client);
325335
if ((char)argByte != '-'){
326336
//continue reading the file
327337
_uploadWriteByte(0x0D);
328338
_uploadWriteByte(0x0A);
329339
goto readfile;
330340
} else {
331-
argByte = client.read();
341+
argByte = _uploadReadByte(client);
332342
if ((char)argByte != '-'){
333343
//continue reading the file
334344
_uploadWriteByte(0x0D);
@@ -366,11 +376,13 @@ void ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t
366376
} else {
367377
_uploadWriteByte(0x0D);
368378
_uploadWriteByte(0x0A);
379+
_uploadWriteByte((uint8_t)('-'));
380+
_uploadWriteByte((uint8_t)('-'));
369381
uint32_t i = 0;
370382
while(i < boundary.length()){
371383
_uploadWriteByte(endBuf[i++]);
372384
}
373-
argByte = client.read();
385+
argByte = _uploadReadByte(client);
374386
goto readfile;
375387
}
376388
} else {

0 commit comments

Comments
 (0)