-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Implemented support for filters and removable routes in ESP8266WebServer #9152
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f905395
feat: added filters and removeable routes
ayushsharma82 fab4f0c
Update Filters.ino
ayushsharma82 82f71b7
fix: clang-format
ayushsharma82 25bd0c0
Merge branch 'webserver-extended' of https://github.com/ayushsharma82…
ayushsharma82 ff5e56f
chore: updated docs
ayushsharma82 b14ccec
chore: updated doc
ayushsharma82 0e684fe
fix: use new implementation
ayushsharma82 70453d2
fix: filters.ino example
ayushsharma82 0e44f52
fix: filters.ino
ayushsharma82 9661ded
fix: formatting
ayushsharma82 1727420
fix: formatting (2)
ayushsharma82 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
101 changes: 101 additions & 0 deletions
101
libraries/ESP8266WebServer/examples/Filters/Filters.ino
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
#include <ESP8266WiFi.h> | ||
#include <WiFiClient.h> | ||
#include <ESP8266WebServer.h> | ||
#include <ESP8266mDNS.h> | ||
|
||
// Your STA WiFi Credentials | ||
// ( This is the AP your ESP will connect to ) | ||
const char *ssid = "..."; | ||
const char *password = "..."; | ||
|
||
// Your AP WiFi Credentials | ||
// ( This is the AP your ESP will broadcast ) | ||
const char *ap_ssid = "ESP8266_Demo"; | ||
const char *ap_password = ""; | ||
|
||
ESP8266WebServer server(80); | ||
|
||
const int led = 13; | ||
|
||
// ON_STA_FILTER - Only accept requests coming from STA interface | ||
bool ON_STA_FILTER(ESP8266WebServer &server) { | ||
return WiFi.localIP() == server.client().localIP(); | ||
} | ||
|
||
// ON_AP_FILTER - Only accept requests coming from AP interface | ||
bool ON_AP_FILTER(ESP8266WebServer &server) { | ||
return WiFi.softAPIP() == server.client().localIP(); | ||
} | ||
|
||
void handleNotFound() { | ||
digitalWrite(led, 1); | ||
String message = "File Not Found\n\n"; | ||
message += "URI: "; | ||
message += server.uri(); | ||
message += "\nMethod: "; | ||
message += (server.method() == HTTP_GET) ? "GET" : "POST"; | ||
message += "\nArguments: "; | ||
message += server.args(); | ||
message += "\n"; | ||
for (uint8_t i = 0; i < server.args(); i++) { | ||
message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; | ||
} | ||
server.send(404, "text/plain", message); | ||
digitalWrite(led, 0); | ||
} | ||
|
||
void setup(void) { | ||
pinMode(led, OUTPUT); | ||
digitalWrite(led, 0); | ||
Serial.begin(115200); | ||
WiFi.mode(WIFI_AP_STA); | ||
// Connect to STA | ||
WiFi.begin(ssid, password); | ||
// Start AP | ||
WiFi.softAP(ap_ssid, ap_password); | ||
Serial.println(""); | ||
|
||
// Wait for connection | ||
while (WiFi.status() != WL_CONNECTED) { | ||
delay(500); | ||
Serial.print("."); | ||
} | ||
Serial.println(""); | ||
Serial.print("Connected to "); | ||
Serial.println(ssid); | ||
Serial.print("IP address: "); | ||
Serial.println(WiFi.localIP()); | ||
|
||
if (MDNS.begin("esp8266")) { | ||
Serial.println("MDNS responder started"); | ||
} | ||
|
||
// This route will be accessible by STA clients only | ||
server.on("/", [&]() { | ||
digitalWrite(led, 1); | ||
server.send(200, "text/plain", "Hi!, This route is accessible for STA clients only"); | ||
digitalWrite(led, 0); | ||
}) | ||
.setFilter(ON_STA_FILTER); | ||
|
||
// This route will be accessible by AP clients only | ||
server.on("/", [&]() { | ||
digitalWrite(led, 1); | ||
server.send(200, "text/plain", "Hi!, This route is accessible for AP clients only"); | ||
digitalWrite(led, 0); | ||
}) | ||
.setFilter(ON_AP_FILTER); | ||
|
||
server.on("/inline", []() { | ||
server.send(200, "text/plain", "this works as well"); | ||
}); | ||
|
||
server.onNotFound(handleNotFound); | ||
|
||
server.begin(); | ||
Serial.println("HTTP server started"); | ||
} | ||
|
||
void loop(void) { | ||
server.handleClient(); | ||
} |
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
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
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
https://github.com/esp8266/Arduino/actions/runs/9516245776/job/26232190207#step:4:400
./tests/restyle.sh runs clang-format-15 on everything, chaining makes extra indentation
lose
[&]
and maybe do something explicit with 'on' return value?auto& onlyAp = ...; onlyAp.setFilter(ON_AP_FILTER);
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 tried running
./tests/restyle.sh
on my PC, odd enough that did nothing. Do we have to install anything for it to run?Regarding Example:
I think it's better if we keep it same as ESP32 Filters.ino example, that way people are not confused.
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.
clang-format-15
in $PATH / env CLANG_FORMAT=path-to-clang-format ./tests/restyle.sh(e.g. https://github.com/muttleyxd/clang-tools-static-binaries)
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.
Done
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.
Right, but it definitely got me confused
[&]
meant to capture by reference, but server callback should not do that. if you do it in setup() and capture value from there, later callback in loop() references invalid memory locationSpecifically for us here and clang-format, without chaining - there are less spaces.