mirror of
https://github.com/esp8266/Arduino.git
synced 2025-04-21 10:26:06 +03:00
ESP8266WebServer: Add variadic template version of collectHeaders() (#7296)
* 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.
This commit is contained in:
parent
8ffe41b7df
commit
47b8947e72
@ -123,11 +123,8 @@ void setup(void) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
server.onNotFound(handleNotFound);
|
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
|
//ask server to track these headers
|
||||||
server.collectHeaders(headerkeys, headerkeyssize);
|
server.collectHeaders("User-Agent", "Cookie");
|
||||||
server.begin();
|
server.begin();
|
||||||
Serial.println("HTTP server started");
|
Serial.println("HTTP server started");
|
||||||
}
|
}
|
||||||
|
@ -373,7 +373,7 @@ void ESP8266WebServerTemplate<ServerType>::close() {
|
|||||||
_server.close();
|
_server.close();
|
||||||
_currentStatus = HC_NONE;
|
_currentStatus = HC_NONE;
|
||||||
if(!_headerKeysCount)
|
if(!_headerKeysCount)
|
||||||
collectHeaders(0, 0);
|
collectHeaders();
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename ServerType>
|
template <typename ServerType>
|
||||||
@ -595,7 +595,6 @@ bool ESP8266WebServerTemplate<ServerType>::hasArg(const String& name) const {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template <typename ServerType>
|
template <typename ServerType>
|
||||||
const String& ESP8266WebServerTemplate<ServerType>::header(const String& name) const {
|
const String& ESP8266WebServerTemplate<ServerType>::header(const String& name) const {
|
||||||
for (int i = 0; i < _headerKeysCount; ++i) {
|
for (int i = 0; i < _headerKeysCount; ++i) {
|
||||||
@ -605,21 +604,30 @@ const String& ESP8266WebServerTemplate<ServerType>::header(const String& name) c
|
|||||||
return emptyString;
|
return emptyString;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<typename ServerType>
|
template<typename ServerType>
|
||||||
void ESP8266WebServerTemplate<ServerType>::collectHeaders(const char* headerKeys[], const size_t headerKeysCount) {
|
void ESP8266WebServerTemplate<ServerType>::collectHeaders(const char* headerKeys[], const size_t headerKeysCount) {
|
||||||
_headerKeysCount = headerKeysCount + 2;
|
if (_currentHeaders)
|
||||||
if (_currentHeaders){
|
|
||||||
delete[] _currentHeaders;
|
delete[] _currentHeaders;
|
||||||
}
|
_currentHeaders = new RequestArgument[_headerKeysCount = headerKeysCount + 2];
|
||||||
_currentHeaders = new RequestArgument[_headerKeysCount];
|
|
||||||
_currentHeaders[0].key = FPSTR(AUTHORIZATION_HEADER);
|
_currentHeaders[0].key = FPSTR(AUTHORIZATION_HEADER);
|
||||||
_currentHeaders[1].key = FPSTR(ETAG_HEADER);
|
_currentHeaders[1].key = FPSTR(ETAG_HEADER);
|
||||||
for (int i = 2; i < _headerKeysCount; i++){
|
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>
|
template <typename ServerType>
|
||||||
const String& ESP8266WebServerTemplate<ServerType>::header(int i) const {
|
const String& ESP8266WebServerTemplate<ServerType>::header(int i) const {
|
||||||
if (i < _headerKeysCount)
|
if (i < _headerKeysCount)
|
||||||
|
@ -138,6 +138,8 @@ public:
|
|||||||
int args() const; // get arguments count
|
int args() const; // get arguments count
|
||||||
bool hasArg(const String& name) const; // check if argument exists
|
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
|
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(const String& name) const; // get request header value by name
|
||||||
const String& header(int i) const; // get request header value by number
|
const String& header(int i) const; // get request header value by number
|
||||||
const String& headerName(int i) const; // get request header name by number
|
const String& headerName(int i) const; // get request header name by number
|
||||||
|
Loading…
x
Reference in New Issue
Block a user