forked from me-no-dev/ESPAsyncWebServer
-
Notifications
You must be signed in to change notification settings - Fork 98
Review HTTP method enum definition to avoid collisions with plaforms ones #413
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 all commits
Commits
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
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
112 changes: 112 additions & 0 deletions
112
examples/HTTPMethodsWithESPIDF/HTTPMethodsWithESPIDF.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,112 @@ | ||
| // SPDX-License-Identifier: LGPL-3.0-or-later | ||
| // Copyright 2016-2026 Hristo Gochkov, Mathieu Carbou, Emil Muratov, Will Miles | ||
|
|
||
| // | ||
| // HTTP Method usage example and check compatibility with ESP-IDF HTTP Methods when http_parser.h is included. | ||
| // ESP-IDF enums are imported, and HTTP_ANY is NOT defined by ESP-IDF. | ||
| // So asyncws is able to define it. | ||
| // We cannot use directly other asyncws enums to avoid conflicts with ESP-IDF: we have to namespace them. | ||
| // | ||
|
|
||
| #include <Arduino.h> | ||
|
|
||
| #if !defined(ESP8266) | ||
| #include "http_parser.h" | ||
| #endif | ||
|
|
||
| #if defined(ESP32) || defined(LIBRETINY) | ||
| #include <AsyncTCP.h> | ||
| #include <WiFi.h> | ||
| #elif defined(ESP8266) | ||
| #include <ESP8266WiFi.h> | ||
| #include <ESPAsyncTCP.h> | ||
| #elif defined(TARGET_RP2040) || defined(TARGET_RP2350) || defined(PICO_RP2040) || defined(PICO_RP2350) | ||
| #include <RPAsyncTCP.h> | ||
| #include <WiFi.h> | ||
| #endif | ||
|
|
||
| #include <ESPAsyncWebServer.h> | ||
|
|
||
| static AsyncWebServer server(80); | ||
|
|
||
| #if ASYNC_JSON_SUPPORT == 1 | ||
| // https://github.com/ESP32Async/ESPAsyncWebServer/issues/404 | ||
| static void handlePostTest(AsyncWebServerRequest *req, JsonVariant &json) { | ||
| AsyncWebServerResponse *response; | ||
| if (req->method() == WebRequestMethod::HTTP_POST) { | ||
| response = req->beginResponse(200, "application/json", "{\"msg\": \"OK\"}"); | ||
| } else { | ||
| response = req->beginResponse(501, "application/json", "{\"msg\": \"Not Implemented\"}"); | ||
| } | ||
| req->send(response); | ||
| } | ||
| #endif | ||
|
|
||
| // user defined functions that turns out to have similar signatures to the ones in global header | ||
| int stringToMethod(const String &) { | ||
| return 0; | ||
| } | ||
| const char *methodToString(WebRequestMethod) { | ||
| return "METHOD"; | ||
| } | ||
| bool methodMatches(WebRequestMethodComposite c, WebRequestMethod m) { | ||
| return false; | ||
| }; | ||
|
|
||
| static WebRequestMethodComposite allowed = AsyncWebRequestMethod::HTTP_HEAD | AsyncWebRequestMethod::HTTP_OPTIONS; | ||
|
|
||
| class MyRequestHandler : public AsyncWebHandler { | ||
| public: | ||
| bool canHandle(__unused AsyncWebServerRequest *request) const override { | ||
| // Test backward compatibility with previous way of checking if a method is allowed in a composite using a bit operator | ||
| return allowed & request->method(); | ||
| } | ||
|
|
||
| void handleRequest(AsyncWebServerRequest *request) override { | ||
| request->send(200, "text/plain", "Hello from custom handler"); | ||
| } | ||
| }; | ||
|
|
||
| void setup() { | ||
| Serial.begin(115200); | ||
|
|
||
| #if ASYNCWEBSERVER_WIFI_SUPPORTED | ||
| WiFi.mode(WIFI_AP); | ||
| WiFi.softAP("esp-captive"); | ||
| #endif | ||
|
|
||
| // curl -v http://192.168.4.1/get-or-post | ||
| // curl -v -X POST -d "a=b" http://192.168.4.1/get-or-post | ||
| server.on("/get-or-post", AsyncWebRequestMethod::HTTP_GET | AsyncWebRequestMethod::HTTP_POST, [](AsyncWebServerRequest *request) { | ||
| request->send(200, "text/plain", "Hello"); | ||
| }); | ||
|
|
||
| // curl -v http://192.168.4.1/all | ||
| server.on("/all", AsyncWebRequestMethod::HTTP_ALL, [](AsyncWebServerRequest *request) { | ||
| request->send(200, "text/plain", "Hello"); | ||
| }); | ||
|
|
||
| // will show a deprecation warning | ||
| server.on("/any", AsyncWebRequestMethod::HTTP_ANY, [](AsyncWebServerRequest *request) { | ||
| request->send(200, "text/plain", "Hello"); | ||
| }); | ||
|
|
||
| #if ASYNC_JSON_SUPPORT == 1 | ||
| // curl -v http://192.168.4.1/test => Not Implemented | ||
| // curl -v -X POST -H 'Content-Type: application/json' -d '{"name":"You"}' http://192.168.4.1/test => OK | ||
| AsyncCallbackJsonWebHandler *testHandler = new AsyncCallbackJsonWebHandler("/test", handlePostTest); | ||
| server.addHandler(testHandler); | ||
| #endif | ||
|
|
||
| // curl -v -X HEAD http://192.168.4.1/custom => answers | ||
| // curl -v -X OPTIONS http://192.168.4.1/custom => answers | ||
| // curl -v -X POST http://192.168.4.1/custom => no answer | ||
| server.addHandler(new MyRequestHandler()); | ||
|
|
||
| server.begin(); | ||
| } | ||
|
|
||
| // not needed | ||
| void loop() { | ||
| delay(100); | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.