Skip to content

Commit

Permalink
ESP8266WebServer: Add variadic template version of collectHeaders()
Browse files Browse the repository at this point in the history
* More user-friendly, less RODATA usage.

eg. `webServer.collectHeaders(F("Content-Type"), F("Origin"));`

In this example, less about 20 bytes than the traditional way.
  • Loading branch information
jjsuwa-sys3175 committed Dec 1, 2020
1 parent 7a36874 commit ef79356
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
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 @@ -372,7 +372,7 @@ void ESP8266WebServerTemplate<ServerType>::close() {
_server.close();
_currentStatus = HC_NONE;
if(!_headerKeysCount)
collectHeaders(0, 0);
collectHeaders();
}

template <typename ServerType>
Expand Down Expand Up @@ -557,7 +557,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 @@ -608,7 +608,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 @@ -618,21 +617,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>
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

0 comments on commit ef79356

Please sign in to comment.