Skip to content

Commit 7001086

Browse files
ficetoficeto
ficeto
authored and
ficeto
committed
fix binary file http upload
1 parent 8ce762d commit 7001086

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

libraries/ESP8266WebServer/src/ESP8266WebServer.cpp

+27-7
Original file line numberDiff line numberDiff line change
@@ -441,16 +441,35 @@ void ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t
441441

442442
argByte = client.read();
443443
if(argByte == 0x0A){
444-
line = client.readStringUntil(0x0D);
445-
client.readStringUntil(0x0A);
446444
#ifdef DEBUG
447445
DEBUG_OUTPUT.print("Write File: ");
448446
DEBUG_OUTPUT.println(_currentUpload.buflen);
449447
#endif
450448
if(_fileUploadHandler) _fileUploadHandler();
451449
_currentUpload.size += _currentUpload.buflen;
452450
_currentUpload.buflen = 0;
453-
if(line.startsWith("--"+boundary)){
451+
452+
argByte = client.read();
453+
if((char)argByte != '-'){
454+
//continue reading the file
455+
_currentUpload.buf[_currentUpload.buflen++] = 0x0D;
456+
_currentUpload.buf[_currentUpload.buflen++] = 0x0A;
457+
goto readfile;
458+
} else {
459+
argByte = client.read();
460+
if((char)argByte != '-'){
461+
//continue reading the file
462+
_currentUpload.buf[_currentUpload.buflen++] = 0x0D;
463+
_currentUpload.buf[_currentUpload.buflen++] = 0x0A;
464+
_currentUpload.buf[_currentUpload.buflen++] = (uint8_t)('-');
465+
goto readfile;
466+
}
467+
}
468+
469+
uint8_t endBuf[boundary.length()];
470+
client.readBytes(endBuf, boundary.length());
471+
472+
if(strstr((const char*)endBuf, (const char*)(boundary.c_str())) != NULL){
454473
_currentUpload.status = UPLOAD_FILE_END;
455474
#ifdef DEBUG
456475
DEBUG_OUTPUT.print("End File: ");
@@ -461,7 +480,9 @@ void ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t
461480
DEBUG_OUTPUT.println(_currentUpload.size);
462481
#endif
463482
if(_fileUploadHandler) _fileUploadHandler();
464-
if(line == ("--"+boundary+"--")){
483+
line = client.readStringUntil(0x0D);
484+
client.readStringUntil(0x0A);
485+
if(line == "--"){
465486
#ifdef DEBUG
466487
DEBUG_OUTPUT.println("Done Parsing POST");
467488
#endif
@@ -471,10 +492,9 @@ void ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t
471492
} else {
472493
_currentUpload.buf[_currentUpload.buflen++] = 0x0D;
473494
_currentUpload.buf[_currentUpload.buflen++] = 0x0A;
474-
const char * lineChars = line.c_str();
475495
uint32_t i = 0;
476-
while(i < os_strlen(lineChars)){
477-
_currentUpload.buf[_currentUpload.buflen++] = lineChars[i++];
496+
while(i < boundary.length()){
497+
_currentUpload.buf[_currentUpload.buflen++] = endBuf[i++];
478498
if(_currentUpload.buflen == 1460){
479499
#ifdef DEBUG
480500
DEBUG_OUTPUT.println("Write File: 1460");

0 commit comments

Comments
 (0)