1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-19 23:22:16 +03:00

Handle form upload abortion in ESP8266WebServer

If the client connection gets closed during a form upload,
the fileUploadHandler is notified with the new
UPLOAD_FILE_ABORTED status, and the loop is ended
gracefully.
This commit is contained in:
Miguel Angel Ajo 2015-09-28 20:08:47 +02:00
parent ebdaedff4b
commit b4cc0c263e
2 changed files with 14 additions and 2 deletions

View File

@ -27,7 +27,8 @@
#include <functional> #include <functional>
enum HTTPMethod { HTTP_ANY, HTTP_GET, HTTP_POST, HTTP_PUT, HTTP_PATCH, HTTP_DELETE, HTTP_OPTIONS }; enum HTTPMethod { HTTP_ANY, HTTP_GET, HTTP_POST, HTTP_PUT, HTTP_PATCH, HTTP_DELETE, HTTP_OPTIONS };
enum HTTPUploadStatus { UPLOAD_FILE_START, UPLOAD_FILE_WRITE, UPLOAD_FILE_END }; enum HTTPUploadStatus { UPLOAD_FILE_START, UPLOAD_FILE_WRITE, UPLOAD_FILE_END,
UPLOAD_FILE_ABORTED };
#define HTTP_DOWNLOAD_UNIT_SIZE 1460 #define HTTP_DOWNLOAD_UNIT_SIZE 1460
#define HTTP_UPLOAD_BUFLEN 2048 #define HTTP_UPLOAD_BUFLEN 2048
@ -116,6 +117,7 @@ protected:
void _parseArguments(String data); void _parseArguments(String data);
static const char* _responseCodeToString(int code); static const char* _responseCodeToString(int code);
bool _parseForm(WiFiClient& client, String boundary, uint32_t len); bool _parseForm(WiFiClient& client, String boundary, uint32_t len);
bool _parseFormUploadAborted();
void _uploadWriteByte(uint8_t b); void _uploadWriteByte(uint8_t b);
uint8_t _uploadReadByte(WiFiClient& client); uint8_t _uploadReadByte(WiFiClient& client);
void _prepareHeader(String& response, int code, const char* content_type, size_t contentLength); void _prepareHeader(String& response, int code, const char* content_type, size_t contentLength);

View File

@ -274,7 +274,7 @@ void ESP8266WebServer::_uploadWriteByte(uint8_t b){
uint8_t ESP8266WebServer::_uploadReadByte(WiFiClient& client){ uint8_t ESP8266WebServer::_uploadReadByte(WiFiClient& client){
int res = client.read(); int res = client.read();
if(res == -1){ if(res == -1){
while(!client.available()) while(!client.available() && client.connected())
yield(); yield();
res = client.read(); res = client.read();
} }
@ -387,13 +387,16 @@ bool ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t
uint8_t argByte = _uploadReadByte(client); uint8_t argByte = _uploadReadByte(client);
readfile: readfile:
while(argByte != 0x0D){ while(argByte != 0x0D){
if (!client.connected()) return _parseFormUploadAborted();
_uploadWriteByte(argByte); _uploadWriteByte(argByte);
argByte = _uploadReadByte(client); argByte = _uploadReadByte(client);
} }
argByte = _uploadReadByte(client); argByte = _uploadReadByte(client);
if (!client.connected()) return _parseFormUploadAborted();
if (argByte == 0x0A){ if (argByte == 0x0A){
argByte = _uploadReadByte(client); argByte = _uploadReadByte(client);
if (!client.connected()) return _parseFormUploadAborted();
if ((char)argByte != '-'){ if ((char)argByte != '-'){
//continue reading the file //continue reading the file
_uploadWriteByte(0x0D); _uploadWriteByte(0x0D);
@ -401,6 +404,7 @@ readfile:
goto readfile; goto readfile;
} else { } else {
argByte = _uploadReadByte(client); argByte = _uploadReadByte(client);
if (!client.connected()) return _parseFormUploadAborted();
if ((char)argByte != '-'){ if ((char)argByte != '-'){
//continue reading the file //continue reading the file
_uploadWriteByte(0x0D); _uploadWriteByte(0x0D);
@ -481,3 +485,9 @@ readfile:
#endif #endif
return false; return false;
} }
bool ESP8266WebServer::_parseFormUploadAborted(){
_currentUpload.status = UPLOAD_FILE_ABORTED;
if (_fileUploadHandler) _fileUploadHandler();
return false;
}