Skip to content

Commit 247fd33

Browse files
committed
Merge pull request #846 from mangelajo/esp8266-fix-833
Handle form upload abortion in ESP8266WebServer (fix #833)
2 parents 2c37445 + 3770882 commit 247fd33

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

hardware/esp8266com/esp8266/libraries/ESP8266WebServer/src/ESP8266WebServer.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
#include <functional>
2828

2929
enum HTTPMethod { HTTP_ANY, HTTP_GET, HTTP_POST, HTTP_PUT, HTTP_PATCH, HTTP_DELETE, HTTP_OPTIONS };
30-
enum HTTPUploadStatus { UPLOAD_FILE_START, UPLOAD_FILE_WRITE, UPLOAD_FILE_END };
30+
enum HTTPUploadStatus { UPLOAD_FILE_START, UPLOAD_FILE_WRITE, UPLOAD_FILE_END,
31+
UPLOAD_FILE_ABORTED };
3132

3233
#define HTTP_DOWNLOAD_UNIT_SIZE 1460
3334
#define HTTP_UPLOAD_BUFLEN 2048
@@ -116,6 +117,7 @@ template<typename T> size_t streamFile(T &file, const String& contentType){
116117
void _parseArguments(String data);
117118
static const char* _responseCodeToString(int code);
118119
bool _parseForm(WiFiClient& client, String boundary, uint32_t len);
120+
bool _parseFormUploadAborted();
119121
void _uploadWriteByte(uint8_t b);
120122
uint8_t _uploadReadByte(WiFiClient& client);
121123
void _prepareHeader(String& response, int code, const char* content_type, size_t contentLength);

hardware/esp8266com/esp8266/libraries/ESP8266WebServer/src/Parsing.cpp

+11-1
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ void ESP8266WebServer::_uploadWriteByte(uint8_t b){
274274
uint8_t ESP8266WebServer::_uploadReadByte(WiFiClient& client){
275275
int res = client.read();
276276
if(res == -1){
277-
while(!client.available())
277+
while(!client.available() && client.connected())
278278
yield();
279279
res = client.read();
280280
}
@@ -387,20 +387,24 @@ bool ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t
387387
uint8_t argByte = _uploadReadByte(client);
388388
readfile:
389389
while(argByte != 0x0D){
390+
if (!client.connected()) return _parseFormUploadAborted();
390391
_uploadWriteByte(argByte);
391392
argByte = _uploadReadByte(client);
392393
}
393394

394395
argByte = _uploadReadByte(client);
396+
if (!client.connected()) return _parseFormUploadAborted();
395397
if (argByte == 0x0A){
396398
argByte = _uploadReadByte(client);
399+
if (!client.connected()) return _parseFormUploadAborted();
397400
if ((char)argByte != '-'){
398401
//continue reading the file
399402
_uploadWriteByte(0x0D);
400403
_uploadWriteByte(0x0A);
401404
goto readfile;
402405
} else {
403406
argByte = _uploadReadByte(client);
407+
if (!client.connected()) return _parseFormUploadAborted();
404408
if ((char)argByte != '-'){
405409
//continue reading the file
406410
_uploadWriteByte(0x0D);
@@ -481,3 +485,9 @@ bool ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t
481485
#endif
482486
return false;
483487
}
488+
489+
bool ESP8266WebServer::_parseFormUploadAborted(){
490+
_currentUpload.status = UPLOAD_FILE_ABORTED;
491+
if (_fileUploadHandler) _fileUploadHandler();
492+
return false;
493+
}

0 commit comments

Comments
 (0)