Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESP8266WebServer: Add variadic template version of collectHeaders() #7296

Merged
merged 2 commits into from
Mar 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
26 changes: 17 additions & 9 deletions libraries/ESP8266WebServer/src/ESP8266WebServer-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ void ESP8266WebServerTemplate<ServerType>::close() {
_server.close();
_currentStatus = HC_NONE;
if(!_headerKeysCount)
collectHeaders(0, 0);
collectHeaders();
}

template <typename ServerType>
Expand Down Expand Up @@ -544,7 +544,7 @@ void ESP8266WebServerTemplate<ServerType>::_streamFileCore(const size_t fileSize
}

template <typename ServerType>
const String& ESP8266WebServerTemplate<ServerType>::pathArg(unsigned int i) const {
const String& ESP8266WebServerTemplate<ServerType>::pathArg(unsigned int i) const {
if (_currentHandler != nullptr)
return _currentHandler->pathArg(i);
return emptyString;
Expand Down Expand Up @@ -595,7 +595,6 @@ bool ESP8266WebServerTemplate<ServerType>::hasArg(const String& name) const {
return false;
}


template <typename ServerType>
const String& ESP8266WebServerTemplate<ServerType>::header(const String& name) const {
for (int i = 0; i < _headerKeysCount; ++i) {
Expand All @@ -605,21 +604,30 @@ const String& ESP8266WebServerTemplate<ServerType>::header(const String& name) c
return emptyString;
}


template<typename ServerType>
void ESP8266WebServerTemplate<ServerType>::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 <typename ServerType>
devyte marked this conversation as resolved.
Show resolved Hide resolved
template <typename... Args>
void ESP8266WebServerTemplate<ServerType>::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 <typename ServerType>
const String& ESP8266WebServerTemplate<ServerType>::header(int i) const {
if (i < _headerKeysCount)
Expand Down
2 changes: 2 additions & 0 deletions libraries/ESP8266WebServer/src/ESP8266WebServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<typename... Args>
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
Expand Down