Skip to content

Commit 440a3aa

Browse files
Lucearlephilhower
Luc
authored andcommitted
Expose post args during upload (#4935)
Currently post args are only available at the end of upload but they are already listed - this PR just expose them with minimal changes It also set a define for post args array size originaly set to 32
1 parent 9ec03ed commit 440a3aa

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed

libraries/ESP8266WebServer/src/ESP8266WebServer.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ ESP8266WebServer::ESP8266WebServer(IPAddress addr, int port)
5353
, _lastHandler(nullptr)
5454
, _currentArgCount(0)
5555
, _currentArgs(nullptr)
56+
, _postArgsLen(0)
57+
, _postArgs(nullptr)
5658
, _headerKeysCount(0)
5759
, _currentHeaders(nullptr)
5860
, _contentLength(0)
@@ -71,6 +73,8 @@ ESP8266WebServer::ESP8266WebServer(int port)
7173
, _lastHandler(nullptr)
7274
, _currentArgCount(0)
7375
, _currentArgs(nullptr)
76+
, _postArgsLen(0)
77+
, _postArgs(nullptr)
7478
, _headerKeysCount(0)
7579
, _currentHeaders(nullptr)
7680
, _contentLength(0)
@@ -486,6 +490,10 @@ void ESP8266WebServer::_streamFileCore(const size_t fileSize, const String & fil
486490

487491

488492
const String& ESP8266WebServer::arg(String name) const {
493+
for (int j = 0; j < _postArgsLen; ++j) {
494+
if ( _postArgs[j].key == name )
495+
return _postArgs[j].value;
496+
}
489497
for (int i = 0; i < _currentArgCount; ++i) {
490498
if ( _currentArgs[i].key == name )
491499
return _currentArgs[i].value;
@@ -510,6 +518,10 @@ int ESP8266WebServer::args() const {
510518
}
511519

512520
bool ESP8266WebServer::hasArg(const String& name) const {
521+
for (int j = 0; j < _postArgsLen; ++j) {
522+
if (_postArgs[j].key == name)
523+
return true;
524+
}
513525
for (int i = 0; i < _currentArgCount; ++i) {
514526
if (_currentArgs[i].key == name)
515527
return true;

libraries/ESP8266WebServer/src/ESP8266WebServer.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,12 @@ class ESP8266WebServer
180180
int _currentArgCount;
181181
RequestArgument* _currentArgs;
182182
std::unique_ptr<HTTPUpload> _currentUpload;
183-
183+
int _postArgsLen;
184+
RequestArgument* _postArgs;
185+
184186
int _headerKeysCount;
185187
RequestArgument* _currentHeaders;
188+
186189
size_t _contentLength;
187190
String _responseHeaders;
188191

libraries/ESP8266WebServer/src/Parsing.cpp

+19-11
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
#define DEBUG_OUTPUT Serial
3333
#endif
3434

35+
#ifndef WEBSERVER_MAX_POST_ARGS
36+
#define WEBSERVER_MAX_POST_ARGS 32
37+
#endif
38+
3539
static const char Content_Type[] PROGMEM = "Content-Type";
3640
static const char filename[] PROGMEM = "filename";
3741

@@ -383,8 +387,9 @@ bool ESP8266WebServer::_parseForm(WiFiClient& client, const String& boundary, ui
383387
client.readStringUntil('\n');
384388
//start reading the form
385389
if (line == ("--"+boundary)){
386-
RequestArgument* postArgs = new RequestArgument[32];
387-
int postArgsLen = 0;
390+
if(_postArgs) delete[] _postArgs;
391+
_postArgs = new RequestArgument[WEBSERVER_MAX_POST_ARGS];
392+
_postArgsLen = 0;
388393
while(1){
389394
String argName;
390395
String argValue;
@@ -445,7 +450,7 @@ bool ESP8266WebServer::_parseForm(WiFiClient& client, const String& boundary, ui
445450
DEBUG_OUTPUT.println();
446451
#endif
447452

448-
RequestArgument& arg = postArgs[postArgsLen++];
453+
RequestArgument& arg = _postArgs[_postArgsLen++];
449454
arg.key = argName;
450455
arg.value = argValue;
451456

@@ -552,22 +557,25 @@ bool ESP8266WebServer::_parseForm(WiFiClient& client, const String& boundary, ui
552557
}
553558

554559
int iarg;
555-
int totalArgs = ((32 - postArgsLen) < _currentArgCount)?(32 - postArgsLen):_currentArgCount;
560+
int totalArgs = ((WEBSERVER_MAX_POST_ARGS - _postArgsLen) < _currentArgCount)?(WEBSERVER_MAX_POST_ARGS - _postArgsLen):_currentArgCount;
556561
for (iarg = 0; iarg < totalArgs; iarg++){
557-
RequestArgument& arg = postArgs[postArgsLen++];
562+
RequestArgument& arg = _postArgs[_postArgsLen++];
558563
arg.key = _currentArgs[iarg].key;
559564
arg.value = _currentArgs[iarg].value;
560565
}
561566
if (_currentArgs) delete[] _currentArgs;
562-
_currentArgs = new RequestArgument[postArgsLen];
563-
for (iarg = 0; iarg < postArgsLen; iarg++){
567+
_currentArgs = new RequestArgument[_postArgsLen];
568+
for (iarg = 0; iarg < _postArgsLen; iarg++){
564569
RequestArgument& arg = _currentArgs[iarg];
565-
arg.key = postArgs[iarg].key;
566-
arg.value = postArgs[iarg].value;
570+
arg.key = _postArgs[iarg].key;
571+
arg.value = _postArgs[iarg].value;
567572
}
568573
_currentArgCount = iarg;
569-
if (postArgs)
570-
delete[] postArgs;
574+
if (_postArgs) {
575+
delete[] _postArgs;
576+
_postArgs = nullptr;
577+
_postArgsLen = 0;
578+
}
571579
return true;
572580
}
573581
#ifdef DEBUG_ESP_HTTP_SERVER

0 commit comments

Comments
 (0)