diff --git a/libraries/ESP8266WebServer/examples/SimpleAuthentication/SimpleAuthentication.ino b/libraries/ESP8266WebServer/examples/SimpleAuthentication/SimpleAuthentication.ino index 38e68a2275..2cba08fe9c 100644 --- a/libraries/ESP8266WebServer/examples/SimpleAuthentication/SimpleAuthentication.ino +++ b/libraries/ESP8266WebServer/examples/SimpleAuthentication/SimpleAuthentication.ino @@ -123,11 +123,8 @@ void setup(void) { }); server.onNotFound(handleNotFound); - //here the list of headers to be recorded - const char * headerkeys[] = {"User-Agent", "Cookie"} ; - size_t headerkeyssize = sizeof(headerkeys) / sizeof(char*); //ask server to track these headers - server.collectHeaders(headerkeys, headerkeyssize); + server.collectHeaders("User-Agent", "Cookie"); server.begin(); Serial.println("HTTP server started"); } diff --git a/libraries/ESP8266WebServer/src/ESP8266WebServer-impl.h b/libraries/ESP8266WebServer/src/ESP8266WebServer-impl.h index 9ea77718f8..016bb6b593 100644 --- a/libraries/ESP8266WebServer/src/ESP8266WebServer-impl.h +++ b/libraries/ESP8266WebServer/src/ESP8266WebServer-impl.h @@ -372,7 +372,7 @@ void ESP8266WebServerTemplate::close() { _server.close(); _currentStatus = HC_NONE; if(!_headerKeysCount) - collectHeaders(0, 0); + collectHeaders(); } template @@ -557,7 +557,7 @@ void ESP8266WebServerTemplate::_streamFileCore(const size_t fileSize } template -const String& ESP8266WebServerTemplate::pathArg(unsigned int i) const { +const String& ESP8266WebServerTemplate::pathArg(unsigned int i) const { if (_currentHandler != nullptr) return _currentHandler->pathArg(i); return emptyString; @@ -608,7 +608,6 @@ bool ESP8266WebServerTemplate::hasArg(const String& name) const { return false; } - template const String& ESP8266WebServerTemplate::header(const String& name) const { for (int i = 0; i < _headerKeysCount; ++i) { @@ -618,21 +617,30 @@ const String& ESP8266WebServerTemplate::header(const String& name) c return emptyString; } - template void ESP8266WebServerTemplate::collectHeaders(const char* headerKeys[], const size_t headerKeysCount) { - _headerKeysCount = headerKeysCount + 2; - if (_currentHeaders){ + if (_currentHeaders) delete[] _currentHeaders; - } - _currentHeaders = new RequestArgument[_headerKeysCount]; + _currentHeaders = new RequestArgument[_headerKeysCount = headerKeysCount + 2]; _currentHeaders[0].key = FPSTR(AUTHORIZATION_HEADER); _currentHeaders[1].key = FPSTR(ETAG_HEADER); for (int i = 2; i < _headerKeysCount; i++){ - _currentHeaders[i].key = headerKeys[i-2]; + _currentHeaders[i].key = headerKeys[i - 2]; } } +template +template +void ESP8266WebServerTemplate::collectHeaders(const Args&... args) { + if (_currentHeaders) + delete[] _currentHeaders; + _currentHeaders = new RequestArgument[_headerKeysCount = sizeof...(args) + 2] { + { .key = FPSTR(AUTHORIZATION_HEADER), .value = emptyString }, + { .key = FPSTR(ETAG_HEADER), .value = emptyString }, + { .key = args, .value = emptyString } ... + }; +} + template const String& ESP8266WebServerTemplate::header(int i) const { if (i < _headerKeysCount) diff --git a/libraries/ESP8266WebServer/src/ESP8266WebServer.h b/libraries/ESP8266WebServer/src/ESP8266WebServer.h index a8453b42a2..8a9c279c45 100644 --- a/libraries/ESP8266WebServer/src/ESP8266WebServer.h +++ b/libraries/ESP8266WebServer/src/ESP8266WebServer.h @@ -138,6 +138,8 @@ class ESP8266WebServerTemplate int args() const; // get arguments count bool hasArg(const String& name) const; // check if argument exists void collectHeaders(const char* headerKeys[], const size_t headerKeysCount); // set the request headers to collect + template + void collectHeaders(const Args&... args); // set the request headers to collect (variadic template version) const String& header(const String& name) const; // get request header value by name const String& header(int i) const; // get request header value by number const String& headerName(int i) const; // get request header name by number