feat(webserver): Middleware with default middleware for cors, authc, curl-like logging by mathieucarbou · Pull Request #10750 · espressif/arduino-esp32 (original) (raw)
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
[ Show hidden characters]({{ revealButtonHref }})
For history, please see: #10186
This PR improves WebServer with the following additions:
- Add Middleware support (aka Expressif) with some built-in middlewares like cors, authc and logging (curl-like)
- Add ability to collect all incoming request headers
- Added response code and info on server
| Messages | |
|---|---|
| 📖 | You might consider squashing your 4 commits (simplifying branch history). |
👋 Hello mathieucarbou, we appreciate your contribution to this project!
Click to see more instructions ...
This automated output is generated by the PR linter DangerJS, which checks if your Pull Request meets the project's requirements and helps you fix potential issues.
DangerJS is triggered with each push event to a Pull Request and modify the contents of this comment.
Please consider the following:
- Danger mainly focuses on the PR structure and formatting and can't understand the meaning behind your code or changes.
- Danger is not a substitute for human code reviews; it's still important to request a code review from your colleagues.
- Addressing info messages (📖) is strongly recommended; they're less critical but valuable.
- To manually retry these Danger checks, please navigate to the Actions tab and re-run last Danger workflow.
Review and merge process you can expect ...
We do welcome contributions in the form of bug reports, feature requests and pull requests.
1. An internal issue has been created for the PR, we assign it to the relevant engineer.
2. They review the PR and either approve it or ask you for changes or clarifications.
3. Once the GitHub PR is approved we do the final review, collect approvals from core owners and make sure all the automated tests are passing.
- At this point we may do some adjustments to the proposed change, or extend it by adding tests or documentation.
4. If the change is approved and passes the tests it is merged into the default branch.
Generated by 🚫 dangerJS against f977d60
Note:
I kept using chained pointers, I don't know why STL lists are not used across the code, if it is because Arduino Core has to support AVR-like boards where C++ STL is not available ?
Because if it is the case, then why not creating a sort of ArduinoList (like it was done with String) that could be used everywhere, instead of repeating the CRUD logic to manage a list everywhere ? Maybe these structures exist already ?
Test Results
62 files 62 suites 5m 41s ⏱️
21 tests 21 ✅ 0 💤 0 ❌
154 runs 154 ✅ 0 💤 0 ❌
Results for commit f977d60.
♻️ This comment has been updated with latest results.
It's been on my backlog to create something that is thread-safe, but so is a nice Task class... maybe we will find time in near future to do it. In order to have real impact and because the core is partially in C, it would be nice to have a C version that would be implemented in the CPP as well. Then we can go around and replace all lists with that
Memory usage test (comparing PR against master branch)
The table below shows the summary of memory usage change (decrease - increase) in bytes and percentage for each target.
| Memory | FLASH [bytes] | FLASH [%] | RAM [bytes] | RAM [%] | ||||
|---|---|---|---|---|---|---|---|---|
| Target | DEC | INC | DEC | INC | DEC | INC | DEC | INC |
| ESP32P4 | 0 | ⚠️ +2002 | 0.00 | ⚠️ +0.28 | 0 | ⚠️ +16 | 0.00 | ⚠️ +0.06 |
| ESP32S3 | 0 | ‼️ +3K | 0.00 | ⚠️ +0.34 | 0 | 0 | 0.00 | 0.00 |
| ESP32S2 | 0 | ‼️ +3K | 0.00 | ⚠️ +0.35 | 0 | 0 | 0.00 | 0.00 |
| ESP32C3 | 0 | ⚠️ +2000 | 0.00 | ⚠️ +0.20 | 0 | ⚠️ +16 | 0.00 | ⚠️ +0.04 |
| ESP32C6 | 0 | ⚠️ +2000 | 0.00 | ⚠️ +0.20 | 0 | ⚠️ +16 | 0.00 | ⚠️ +0.02 |
| ESP32 | 0 | ‼️ +3K | 0.00 | ⚠️ +0.32 | 0 | 0 | 0.00 | 0.00 |
Click to expand the detailed deltas report [usage change in BYTES]
| Target | ESP32P4 | ESP32S3 | ESP32S2 | ESP32C3 | ESP32C6 | ESP32 | ||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Example | FLASH | RAM | FLASH | RAM | FLASH | RAM | FLASH | RAM | FLASH | RAM | FLASH | RAM |
| WebServer/examples/AdvancedWebServer | ⚠️ +1894 | 0 | ‼️ +3K | 0 | ‼️ +3K | 0 | ⚠️ +1898 | 0 | ⚠️ +1894 | 0 | ‼️ +3K | 0 |
| WebServer/examples/FSBrowser | ⚠️ +1890 | 0 | ‼️ +3K | 0 | ‼️ +3K | 0 | ⚠️ +1890 | ⚠️ +16 | ⚠️ +1890 | 0 | ‼️ +3K | 0 |
| WebServer/examples/Filters | ⚠️ +1894 | 0 | ‼️ +3K | 0 | ‼️ +3K | 0 | ⚠️ +1898 | 0 | ⚠️ +1894 | 0 | ‼️ +3K | 0 |
| WebServer/examples/HelloServer | ⚠️ +1894 | 0 | ‼️ +3K | 0 | ‼️ +3K | 0 | ⚠️ +1898 | 0 | ⚠️ +1896 | 0 | ‼️ +3K | 0 |
| WebServer/examples/HttpAdvancedAuth | ⚠️ +1882 | ⚠️ +16 | ‼️ +3K | 0 | ‼️ +3K | 0 | ⚠️ +1878 | 0 | ⚠️ +1882 | ⚠️ +16 | ‼️ +3K | 0 |
| WebServer/examples/HttpAuthCallback | ⚠️ +1882 | ⚠️ +16 | ‼️ +3K | 0 | ‼️ +3K | 0 | ⚠️ +1880 | 0 | ⚠️ +1882 | ⚠️ +16 | ‼️ +3K | 0 |
| WebServer/examples/HttpAuthCallbackInline | ⚠️ +1882 | ⚠️ +16 | ‼️ +3K | 0 | ‼️ +3K | 0 | ⚠️ +1880 | 0 | ⚠️ +1882 | ⚠️ +16 | ‼️ +3K | 0 |
| WebServer/examples/HttpBasicAuth | ⚠️ +1882 | ⚠️ +16 | ‼️ +3K | 0 | ‼️ +3K | 0 | ⚠️ +1878 | 0 | ⚠️ +1882 | ⚠️ +16 | ‼️ +3K | 0 |
| WebServer/examples/HttpBasicAuthSHA1 | ⚠️ +1882 | ⚠️ +16 | ‼️ +3K | 0 | ‼️ +3K | 0 | ⚠️ +1878 | 0 | ⚠️ +1882 | ⚠️ +16 | ‼️ +3K | 0 |
| WebServer/examples/HttpBasicAuthSHA1orBearerToken | ⚠️ +1882 | 0 | ‼️ +3K | 0 | ‼️ +3K | 0 | ⚠️ +1878 | ⚠️ +16 | ⚠️ +1886 | 0 | ‼️ +3K | 0 |
| WebServer/examples/MultiHomedServers | ⚠️ +1882 | 0 | ‼️ +3K | 0 | ‼️ +3K | 0 | ⚠️ +1886 | 0 | ⚠️ +1884 | 0 | ‼️ +3K | 0 |
| WebServer/examples/PathArgServer | ⚠️ +1882 | 0 | ‼️ +3K | 0 | ‼️ +3K | 0 | ⚠️ +1886 | 0 | ⚠️ +1882 | 0 | ‼️ +3K | 0 |
| WebServer/examples/SDWebServer | ⚠️ +1894 | ⚠️ +16 | ‼️ +3K | 0 | ‼️ +3K | 0 | ⚠️ +1894 | ⚠️ +16 | ⚠️ +1894 | ⚠️ +16 | ‼️ +3K | 0 |
| WebServer/examples/SimpleAuthentification | ⚠️ +1860 | 0 | ‼️ +3K | 0 | ‼️ +3K | 0 | ⚠️ +1864 | 0 | ⚠️ +1860 | 0 | ‼️ +3K | 0 |
| WebServer/examples/UploadHugeFile | ⚠️ +1898 | 0 | ‼️ +3K | 0 | ‼️ +3K | 0 | ⚠️ +1898 | 0 | ⚠️ +1898 | 0 | ‼️ +3K | 0 |
| WebServer/examples/WebServer | ⚠️ +2002 | ⚠️ +16 | ‼️ +3K | 0 | ‼️ +3K | 0 | ⚠️ +2000 | 0 | ⚠️ +2000 | ⚠️ +16 | ‼️ +3K | 0 |
| WebServer/examples/WebUpdate | ⚠️ +1882 | 0 | ‼️ +3K | 0 | ‼️ +3K | 0 | ⚠️ +1882 | 0 | ⚠️ +1882 | 0 | ‼️ +3K | 0 |
| WebServer/examples/Middleware | - | - | - | - | - | - | - | - | - | - | - | - |
| } |
|---|
| assert(last); |
| if (_collectAllHeaders) { |
| last->next = new RequestArgument(); |
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe replace default construction with non-default member initialization?last->next = new RequestArgument{.key = headerName, .value = headerValue};
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can use consteval in arduino code ? removing it to your sample and switching to c++11 or 17 leads to a completely different output. So is it worth doing it ?
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can use consteval in arduino code ?
consteval introduce in c++20 -- if this standard is available, consteval will be available.
but I gave that code as a demonstration that everything will be fine (even in constexpr context)
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to say in general that instead of several operations, you can do all of them at once with one.
last->next = new RequestArgument(); last->next->key = headerName; last->next->value = headerValue;
->last->next = new RequestArgument{ headerName, headerValue };
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, completely agree!