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

allocate HTTPUpload struct on demand (#2557)

This commit is contained in:
Christian Schuster 2017-10-17 22:07:17 +02:00 committed by Ivan Grokhotkov
parent 20b7e480b5
commit 2fbc619569
3 changed files with 33 additions and 26 deletions

View File

@ -279,6 +279,7 @@ void ESP8266WebServer::handleClient() {
if (!_currentClient.connected()) { if (!_currentClient.connected()) {
_currentClient = WiFiClient(); _currentClient = WiFiClient();
_currentStatus = HC_NONE; _currentStatus = HC_NONE;
_currentUpload.reset();
return; return;
} }
@ -288,6 +289,7 @@ void ESP8266WebServer::handleClient() {
if (millis() - _statusChange > HTTP_MAX_DATA_WAIT) { if (millis() - _statusChange > HTTP_MAX_DATA_WAIT) {
_currentClient = WiFiClient(); _currentClient = WiFiClient();
_currentStatus = HC_NONE; _currentStatus = HC_NONE;
_currentUpload.reset();
} }
yield(); yield();
return; return;
@ -296,6 +298,7 @@ void ESP8266WebServer::handleClient() {
if (!_parseRequest(_currentClient)) { if (!_parseRequest(_currentClient)) {
_currentClient = WiFiClient(); _currentClient = WiFiClient();
_currentStatus = HC_NONE; _currentStatus = HC_NONE;
_currentUpload.reset();
return; return;
} }
_currentClient.setTimeout(HTTP_MAX_SEND_WAIT); _currentClient.setTimeout(HTTP_MAX_SEND_WAIT);
@ -305,6 +308,7 @@ void ESP8266WebServer::handleClient() {
if (!_currentClient.connected()) { if (!_currentClient.connected()) {
_currentClient = WiFiClient(); _currentClient = WiFiClient();
_currentStatus = HC_NONE; _currentStatus = HC_NONE;
_currentUpload.reset();
return; return;
} else { } else {
_currentStatus = HC_WAIT_CLOSE; _currentStatus = HC_WAIT_CLOSE;
@ -317,6 +321,7 @@ void ESP8266WebServer::handleClient() {
if (millis() - _statusChange > HTTP_MAX_CLOSE_WAIT) { if (millis() - _statusChange > HTTP_MAX_CLOSE_WAIT) {
_currentClient = WiFiClient(); _currentClient = WiFiClient();
_currentStatus = HC_NONE; _currentStatus = HC_NONE;
_currentUpload.reset();
} else { } else {
yield(); yield();
return; return;

View File

@ -25,6 +25,7 @@
#define ESP8266WEBSERVER_H #define ESP8266WEBSERVER_H
#include <functional> #include <functional>
#include <memory>
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
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 };
@ -93,7 +94,7 @@ public:
String uri() { return _currentUri; } String uri() { return _currentUri; }
HTTPMethod method() { return _currentMethod; } HTTPMethod method() { return _currentMethod; }
WiFiClient client() { return _currentClient; } WiFiClient client() { return _currentClient; }
HTTPUpload& upload() { return _currentUpload; } HTTPUpload& upload() { return *_currentUpload; }
String arg(String name); // get request argument value by name String arg(String name); // get request argument value by name
String arg(int i); // get request argument value by number String arg(int i); // get request argument value by number
@ -177,7 +178,7 @@ protected:
int _currentArgCount; int _currentArgCount;
RequestArgument* _currentArgs; RequestArgument* _currentArgs;
HTTPUpload _currentUpload; std::unique_ptr<HTTPUpload> _currentUpload;
int _headerKeysCount; int _headerKeysCount;
RequestArgument* _currentHeaders; RequestArgument* _currentHeaders;

View File

@ -345,13 +345,13 @@ void ESP8266WebServer::_parseArguments(String data) {
} }
void ESP8266WebServer::_uploadWriteByte(uint8_t b){ void ESP8266WebServer::_uploadWriteByte(uint8_t b){
if (_currentUpload.currentSize == HTTP_UPLOAD_BUFLEN){ if (_currentUpload->currentSize == HTTP_UPLOAD_BUFLEN){
if(_currentHandler && _currentHandler->canUpload(_currentUri)) if(_currentHandler && _currentHandler->canUpload(_currentUri))
_currentHandler->upload(*this, _currentUri, _currentUpload); _currentHandler->upload(*this, _currentUri, *_currentUpload);
_currentUpload.totalSize += _currentUpload.currentSize; _currentUpload->totalSize += _currentUpload->currentSize;
_currentUpload.currentSize = 0; _currentUpload->currentSize = 0;
} }
_currentUpload.buf[_currentUpload.currentSize++] = b; _currentUpload->buf[_currentUpload->currentSize++] = b;
} }
uint8_t ESP8266WebServer::_uploadReadByte(WiFiClient& client){ uint8_t ESP8266WebServer::_uploadReadByte(WiFiClient& client){
@ -453,21 +453,22 @@ bool ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t
break; break;
} }
} else { } else {
_currentUpload.status = UPLOAD_FILE_START; _currentUpload.reset(new HTTPUpload());
_currentUpload.name = argName; _currentUpload->status = UPLOAD_FILE_START;
_currentUpload.filename = argFilename; _currentUpload->name = argName;
_currentUpload.type = argType; _currentUpload->filename = argFilename;
_currentUpload.totalSize = 0; _currentUpload->type = argType;
_currentUpload.currentSize = 0; _currentUpload->totalSize = 0;
_currentUpload->currentSize = 0;
#ifdef DEBUG_ESP_HTTP_SERVER #ifdef DEBUG_ESP_HTTP_SERVER
DEBUG_OUTPUT.print("Start File: "); DEBUG_OUTPUT.print("Start File: ");
DEBUG_OUTPUT.print(_currentUpload.filename); DEBUG_OUTPUT.print(_currentUpload->filename);
DEBUG_OUTPUT.print(" Type: "); DEBUG_OUTPUT.print(" Type: ");
DEBUG_OUTPUT.println(_currentUpload.type); DEBUG_OUTPUT.println(_currentUpload->type);
#endif #endif
if(_currentHandler && _currentHandler->canUpload(_currentUri)) if(_currentHandler && _currentHandler->canUpload(_currentUri))
_currentHandler->upload(*this, _currentUri, _currentUpload); _currentHandler->upload(*this, _currentUri, *_currentUpload);
_currentUpload.status = UPLOAD_FILE_WRITE; _currentUpload->status = UPLOAD_FILE_WRITE;
uint8_t argByte = _uploadReadByte(client); uint8_t argByte = _uploadReadByte(client);
readfile: readfile:
while(argByte != 0x0D){ while(argByte != 0x0D){
@ -503,18 +504,18 @@ readfile:
if (strstr((const char*)endBuf, boundary.c_str()) != NULL){ if (strstr((const char*)endBuf, boundary.c_str()) != NULL){
if(_currentHandler && _currentHandler->canUpload(_currentUri)) if(_currentHandler && _currentHandler->canUpload(_currentUri))
_currentHandler->upload(*this, _currentUri, _currentUpload); _currentHandler->upload(*this, _currentUri, *_currentUpload);
_currentUpload.totalSize += _currentUpload.currentSize; _currentUpload->totalSize += _currentUpload->currentSize;
_currentUpload.status = UPLOAD_FILE_END; _currentUpload->status = UPLOAD_FILE_END;
if(_currentHandler && _currentHandler->canUpload(_currentUri)) if(_currentHandler && _currentHandler->canUpload(_currentUri))
_currentHandler->upload(*this, _currentUri, _currentUpload); _currentHandler->upload(*this, _currentUri, *_currentUpload);
#ifdef DEBUG_ESP_HTTP_SERVER #ifdef DEBUG_ESP_HTTP_SERVER
DEBUG_OUTPUT.print("End File: "); DEBUG_OUTPUT.print("End File: ");
DEBUG_OUTPUT.print(_currentUpload.filename); DEBUG_OUTPUT.print(_currentUpload->filename);
DEBUG_OUTPUT.print(" Type: "); DEBUG_OUTPUT.print(" Type: ");
DEBUG_OUTPUT.print(_currentUpload.type); DEBUG_OUTPUT.print(_currentUpload->type);
DEBUG_OUTPUT.print(" Size: "); DEBUG_OUTPUT.print(" Size: ");
DEBUG_OUTPUT.println(_currentUpload.totalSize); DEBUG_OUTPUT.println(_currentUpload->totalSize);
#endif #endif
line = client.readStringUntil(0x0D); line = client.readStringUntil(0x0D);
client.readStringUntil(0x0A); client.readStringUntil(0x0A);
@ -604,8 +605,8 @@ String ESP8266WebServer::urlDecode(const String& text)
} }
bool ESP8266WebServer::_parseFormUploadAborted(){ bool ESP8266WebServer::_parseFormUploadAborted(){
_currentUpload.status = UPLOAD_FILE_ABORTED; _currentUpload->status = UPLOAD_FILE_ABORTED;
if(_currentHandler && _currentHandler->canUpload(_currentUri)) if(_currentHandler && _currentHandler->canUpload(_currentUri))
_currentHandler->upload(*this, _currentUri, _currentUpload); _currentHandler->upload(*this, _currentUri, *_currentUpload);
return false; return false;
} }