|
| 1 | +// SPDX-License-Identifier: LGPL-3.0-or-later |
| 2 | +// Copyright 2016-2026 Hristo Gochkov, Mathieu Carbou, Emil Muratov, Will Miles |
| 3 | + |
| 4 | +// |
| 5 | +// HTTP Method usage example and check compatibility with ESP-IDF HTTP Methods when http_parser.h is included. |
| 6 | +// ESP-IDF enums are imported, and HTTP_ANY is NOT defined by ESP-IDF. |
| 7 | +// So asyncws is able to define it. |
| 8 | +// We cannot use directly other asyncws enums to avoid conflicts with ESP-IDF: we have to namespace them. |
| 9 | +// |
| 10 | + |
| 11 | +#include <Arduino.h> |
| 12 | + |
| 13 | +#if !defined(ESP8266) |
| 14 | +#include "http_parser.h" |
| 15 | +#endif |
| 16 | + |
| 17 | +#if defined(ESP32) || defined(LIBRETINY) |
| 18 | +#include <AsyncTCP.h> |
| 19 | +#include <WiFi.h> |
| 20 | +#elif defined(ESP8266) |
| 21 | +#include <ESP8266WiFi.h> |
| 22 | +#include <ESPAsyncTCP.h> |
| 23 | +#elif defined(TARGET_RP2040) || defined(TARGET_RP2350) || defined(PICO_RP2040) || defined(PICO_RP2350) |
| 24 | +#include <RPAsyncTCP.h> |
| 25 | +#include <WiFi.h> |
| 26 | +#endif |
| 27 | + |
| 28 | +#include <ESPAsyncWebServer.h> |
| 29 | + |
| 30 | +static AsyncWebServer server(80); |
| 31 | + |
| 32 | +#if ASYNC_JSON_SUPPORT == 1 |
| 33 | +// https://github.com/ESP32Async/ESPAsyncWebServer/issues/404 |
| 34 | +static void handlePostTest(AsyncWebServerRequest *req, JsonVariant &json) { |
| 35 | + AsyncWebServerResponse *response; |
| 36 | + if (req->method() == WebRequestMethod::HTTP_POST) { |
| 37 | + response = req->beginResponse(200, "application/json", "{\"msg\": \"OK\"}"); |
| 38 | + } else { |
| 39 | + response = req->beginResponse(501, "application/json", "{\"msg\": \"Not Implemented\"}"); |
| 40 | + } |
| 41 | + req->send(response); |
| 42 | +} |
| 43 | +#endif |
| 44 | + |
| 45 | +// user defined functions that turns out to have similar signatures to the ones in global header |
| 46 | +int stringToMethod(const String &) { |
| 47 | + return 0; |
| 48 | +} |
| 49 | +const char *methodToString(WebRequestMethod) { |
| 50 | + return "METHOD"; |
| 51 | +} |
| 52 | +bool methodMatches(WebRequestMethodComposite c, WebRequestMethod m) { |
| 53 | + return false; |
| 54 | +}; |
| 55 | + |
| 56 | +static WebRequestMethodComposite allowed = AsyncWebRequestMethod::HTTP_HEAD | AsyncWebRequestMethod::HTTP_OPTIONS; |
| 57 | + |
| 58 | +class MyRequestHandler : public AsyncWebHandler { |
| 59 | +public: |
| 60 | + bool canHandle(__unused AsyncWebServerRequest *request) const override { |
| 61 | + // Test backward compatibility with previous way of checking if a method is allowed in a composite using a bit operator |
| 62 | + return allowed & request->method(); |
| 63 | + } |
| 64 | + |
| 65 | + void handleRequest(AsyncWebServerRequest *request) override { |
| 66 | + request->send(200, "text/plain", "Hello from custom handler"); |
| 67 | + } |
| 68 | +}; |
| 69 | + |
| 70 | +void setup() { |
| 71 | + Serial.begin(115200); |
| 72 | + |
| 73 | +#if ASYNCWEBSERVER_WIFI_SUPPORTED |
| 74 | + WiFi.mode(WIFI_AP); |
| 75 | + WiFi.softAP("esp-captive"); |
| 76 | +#endif |
| 77 | + |
| 78 | + // curl -v http://192.168.4.1/get-or-post |
| 79 | + // curl -v -X POST -d "a=b" http://192.168.4.1/get-or-post |
| 80 | + server.on("/get-or-post", AsyncWebRequestMethod::HTTP_GET | AsyncWebRequestMethod::HTTP_POST, [](AsyncWebServerRequest *request) { |
| 81 | + request->send(200, "text/plain", "Hello"); |
| 82 | + }); |
| 83 | + |
| 84 | + // curl -v http://192.168.4.1/all |
| 85 | + server.on("/all", AsyncWebRequestMethod::HTTP_ALL, [](AsyncWebServerRequest *request) { |
| 86 | + request->send(200, "text/plain", "Hello"); |
| 87 | + }); |
| 88 | + |
| 89 | + // will show a deprecation warning |
| 90 | + server.on("/any", AsyncWebRequestMethod::HTTP_ANY, [](AsyncWebServerRequest *request) { |
| 91 | + request->send(200, "text/plain", "Hello"); |
| 92 | + }); |
| 93 | + |
| 94 | +#if ASYNC_JSON_SUPPORT == 1 |
| 95 | + // curl -v http://192.168.4.1/test => Not Implemented |
| 96 | + // curl -v -X POST -H 'Content-Type: application/json' -d '{"name":"You"}' http://192.168.4.1/test => OK |
| 97 | + AsyncCallbackJsonWebHandler *testHandler = new AsyncCallbackJsonWebHandler("/test", handlePostTest); |
| 98 | + server.addHandler(testHandler); |
| 99 | +#endif |
| 100 | + |
| 101 | + // curl -v -X HEAD http://192.168.4.1/custom => answers |
| 102 | + // curl -v -X OPTIONS http://192.168.4.1/custom => answers |
| 103 | + // curl -v -X POST http://192.168.4.1/custom => no answer |
| 104 | + server.addHandler(new MyRequestHandler()); |
| 105 | + |
| 106 | + server.begin(); |
| 107 | +} |
| 108 | + |
| 109 | +// not needed |
| 110 | +void loop() { |
| 111 | + delay(100); |
| 112 | +} |
0 commit comments