Skip to content

Commit 2fbc619

Browse files
Christian Schusterigrr
Christian Schuster
authored andcommitted
allocate HTTPUpload struct on demand (#2557)
1 parent 20b7e48 commit 2fbc619

File tree

3 files changed

+33
-26
lines changed

3 files changed

+33
-26
lines changed

libraries/ESP8266WebServer/src/ESP8266WebServer.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ void ESP8266WebServer::handleClient() {
279279
if (!_currentClient.connected()) {
280280
_currentClient = WiFiClient();
281281
_currentStatus = HC_NONE;
282+
_currentUpload.reset();
282283
return;
283284
}
284285

@@ -288,6 +289,7 @@ void ESP8266WebServer::handleClient() {
288289
if (millis() - _statusChange > HTTP_MAX_DATA_WAIT) {
289290
_currentClient = WiFiClient();
290291
_currentStatus = HC_NONE;
292+
_currentUpload.reset();
291293
}
292294
yield();
293295
return;
@@ -296,6 +298,7 @@ void ESP8266WebServer::handleClient() {
296298
if (!_parseRequest(_currentClient)) {
297299
_currentClient = WiFiClient();
298300
_currentStatus = HC_NONE;
301+
_currentUpload.reset();
299302
return;
300303
}
301304
_currentClient.setTimeout(HTTP_MAX_SEND_WAIT);
@@ -305,6 +308,7 @@ void ESP8266WebServer::handleClient() {
305308
if (!_currentClient.connected()) {
306309
_currentClient = WiFiClient();
307310
_currentStatus = HC_NONE;
311+
_currentUpload.reset();
308312
return;
309313
} else {
310314
_currentStatus = HC_WAIT_CLOSE;
@@ -317,6 +321,7 @@ void ESP8266WebServer::handleClient() {
317321
if (millis() - _statusChange > HTTP_MAX_CLOSE_WAIT) {
318322
_currentClient = WiFiClient();
319323
_currentStatus = HC_NONE;
324+
_currentUpload.reset();
320325
} else {
321326
yield();
322327
return;

libraries/ESP8266WebServer/src/ESP8266WebServer.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#define ESP8266WEBSERVER_H
2626

2727
#include <functional>
28+
#include <memory>
2829
#include <ESP8266WiFi.h>
2930

3031
enum HTTPMethod { HTTP_ANY, HTTP_GET, HTTP_POST, HTTP_PUT, HTTP_PATCH, HTTP_DELETE, HTTP_OPTIONS };
@@ -93,7 +94,7 @@ class ESP8266WebServer
9394
String uri() { return _currentUri; }
9495
HTTPMethod method() { return _currentMethod; }
9596
WiFiClient client() { return _currentClient; }
96-
HTTPUpload& upload() { return _currentUpload; }
97+
HTTPUpload& upload() { return *_currentUpload; }
9798

9899
String arg(String name); // get request argument value by name
99100
String arg(int i); // get request argument value by number
@@ -177,7 +178,7 @@ template<typename T> size_t streamFile(T &file, const String& contentType){
177178

178179
int _currentArgCount;
179180
RequestArgument* _currentArgs;
180-
HTTPUpload _currentUpload;
181+
std::unique_ptr<HTTPUpload> _currentUpload;
181182

182183
int _headerKeysCount;
183184
RequestArgument* _currentHeaders;

libraries/ESP8266WebServer/src/Parsing.cpp

+25-24
Original file line numberDiff line numberDiff line change
@@ -345,13 +345,13 @@ void ESP8266WebServer::_parseArguments(String data) {
345345
}
346346

347347
void ESP8266WebServer::_uploadWriteByte(uint8_t b){
348-
if (_currentUpload.currentSize == HTTP_UPLOAD_BUFLEN){
348+
if (_currentUpload->currentSize == HTTP_UPLOAD_BUFLEN){
349349
if(_currentHandler && _currentHandler->canUpload(_currentUri))
350-
_currentHandler->upload(*this, _currentUri, _currentUpload);
351-
_currentUpload.totalSize += _currentUpload.currentSize;
352-
_currentUpload.currentSize = 0;
350+
_currentHandler->upload(*this, _currentUri, *_currentUpload);
351+
_currentUpload->totalSize += _currentUpload->currentSize;
352+
_currentUpload->currentSize = 0;
353353
}
354-
_currentUpload.buf[_currentUpload.currentSize++] = b;
354+
_currentUpload->buf[_currentUpload->currentSize++] = b;
355355
}
356356

357357
uint8_t ESP8266WebServer::_uploadReadByte(WiFiClient& client){
@@ -453,21 +453,22 @@ bool ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t
453453
break;
454454
}
455455
} else {
456-
_currentUpload.status = UPLOAD_FILE_START;
457-
_currentUpload.name = argName;
458-
_currentUpload.filename = argFilename;
459-
_currentUpload.type = argType;
460-
_currentUpload.totalSize = 0;
461-
_currentUpload.currentSize = 0;
456+
_currentUpload.reset(new HTTPUpload());
457+
_currentUpload->status = UPLOAD_FILE_START;
458+
_currentUpload->name = argName;
459+
_currentUpload->filename = argFilename;
460+
_currentUpload->type = argType;
461+
_currentUpload->totalSize = 0;
462+
_currentUpload->currentSize = 0;
462463
#ifdef DEBUG_ESP_HTTP_SERVER
463464
DEBUG_OUTPUT.print("Start File: ");
464-
DEBUG_OUTPUT.print(_currentUpload.filename);
465+
DEBUG_OUTPUT.print(_currentUpload->filename);
465466
DEBUG_OUTPUT.print(" Type: ");
466-
DEBUG_OUTPUT.println(_currentUpload.type);
467+
DEBUG_OUTPUT.println(_currentUpload->type);
467468
#endif
468469
if(_currentHandler && _currentHandler->canUpload(_currentUri))
469-
_currentHandler->upload(*this, _currentUri, _currentUpload);
470-
_currentUpload.status = UPLOAD_FILE_WRITE;
470+
_currentHandler->upload(*this, _currentUri, *_currentUpload);
471+
_currentUpload->status = UPLOAD_FILE_WRITE;
471472
uint8_t argByte = _uploadReadByte(client);
472473
readfile:
473474
while(argByte != 0x0D){
@@ -503,18 +504,18 @@ bool ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t
503504

504505
if (strstr((const char*)endBuf, boundary.c_str()) != NULL){
505506
if(_currentHandler && _currentHandler->canUpload(_currentUri))
506-
_currentHandler->upload(*this, _currentUri, _currentUpload);
507-
_currentUpload.totalSize += _currentUpload.currentSize;
508-
_currentUpload.status = UPLOAD_FILE_END;
507+
_currentHandler->upload(*this, _currentUri, *_currentUpload);
508+
_currentUpload->totalSize += _currentUpload->currentSize;
509+
_currentUpload->status = UPLOAD_FILE_END;
509510
if(_currentHandler && _currentHandler->canUpload(_currentUri))
510-
_currentHandler->upload(*this, _currentUri, _currentUpload);
511+
_currentHandler->upload(*this, _currentUri, *_currentUpload);
511512
#ifdef DEBUG_ESP_HTTP_SERVER
512513
DEBUG_OUTPUT.print("End File: ");
513-
DEBUG_OUTPUT.print(_currentUpload.filename);
514+
DEBUG_OUTPUT.print(_currentUpload->filename);
514515
DEBUG_OUTPUT.print(" Type: ");
515-
DEBUG_OUTPUT.print(_currentUpload.type);
516+
DEBUG_OUTPUT.print(_currentUpload->type);
516517
DEBUG_OUTPUT.print(" Size: ");
517-
DEBUG_OUTPUT.println(_currentUpload.totalSize);
518+
DEBUG_OUTPUT.println(_currentUpload->totalSize);
518519
#endif
519520
line = client.readStringUntil(0x0D);
520521
client.readStringUntil(0x0A);
@@ -604,8 +605,8 @@ String ESP8266WebServer::urlDecode(const String& text)
604605
}
605606

606607
bool ESP8266WebServer::_parseFormUploadAborted(){
607-
_currentUpload.status = UPLOAD_FILE_ABORTED;
608+
_currentUpload->status = UPLOAD_FILE_ABORTED;
608609
if(_currentHandler && _currentHandler->canUpload(_currentUri))
609-
_currentHandler->upload(*this, _currentUri, _currentUpload);
610+
_currentHandler->upload(*this, _currentUri, *_currentUpload);
610611
return false;
611612
}

0 commit comments

Comments
 (0)