-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add header access using same method as arguments #886
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
Conversation
I worry about the memory and CPU overhead, specifically if I never use this new API. Ideally |
@drewfish, that's definitely a valid concern and I think I have a good solution to it. I'll update the code later today once I get everything working. |
I've updated the method of collecting request headers so that it is off by default in response to @drewfish's resource concerns. Now if I want to have access to the User-Agent and Cookie headers for example... const size_t headerKeysCount = 2;
const char* headerKeys[headerKeysCount] = {"User-Agent", "Cookie"};
server.collectHeaders(headerKeys, headerKeysCount); The methods to access the collected headers remain the same. |
@@ -41,6 +41,10 @@ ESP8266WebServer::ESP8266WebServer(int port) | |||
} | |||
|
|||
ESP8266WebServer::~ESP8266WebServer() { | |||
if (_headerKeys) { | |||
free(_headerKeys); | |||
_headerKeys = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we also maybe want to do _headerKeysCount = 0
just to be safe?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I doubt it because the object is being destroyed anyway but why not, can't be too safe. Does the rest of the PR work for you?
@@ -83,6 +83,13 @@ class ESP8266WebServer | |||
String argName(int i); // get request argument name by number | |||
int args(); // get arguments count | |||
bool hasArg(const char* name); // check if argument exists | |||
|
|||
void collectHeaders(const char* headerKeys[], const size_t headerKeysCount); // set the request headers to collect | |||
String header(const char* name); // get request header value by name |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this end up copying the returned String (or will the compiler optimize)? Is there a "best practice" (examples in other well-used libraries) on how to return a String efficiently?
(Am I focusing too much on optimizations 😄 😞 ?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For example, would const String&
be a better return type? (Will be tricky to handle the "not found" case, but we could make a single empty string to use for all "not found" return values.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you look just above (ESP8266WebServer.h Line 81), you'll see that the existing argument access method is the same. There may be a better way but I decided to follow the conventions of the existing code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, cool, good enough for me.
I like the approach. I'm uncertain about fine-tuning the performance, since I'm not that experienced with microprocessor (and specifically esp8266) programming, so I'm fine with whatever practice is common in this world. |
+1 from me |
Looks mostly good to me. Few comments from my side:
|
Hi looks very nice and I am really willing to use it ( I need a cookie 😄 ) |
I would be happy to help doing modification - I just do not want overlap also it won't be better instead of free / allocate the header array at each page, to allocate one for all ? So we could directly copy the requested keys in _currentHeaders[i].key at the initialization and just set _currentHeaders[i].value="" for each reset, instead of delete all array and allocate it again. so no need to allocate the Keys array and no need to delete/allocate the header array at each page |
@luc-github please do, this should be merged before Friday. |
Ok just did #933 |
Here's my fix for issue #795 which adds access to request headers by the same method that arguments are accessed. I needed to access the Cookie header and now I can do that like this.