| 2 |
raymond |
1 |
// SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
2 |
// Copyright 2016-2026 Hristo Gochkov, Mathieu Carbou, Emil Muratov, Will Miles
|
|
|
3 |
|
|
|
4 |
//
|
|
|
5 |
// Show how to sue Middleware
|
|
|
6 |
//
|
|
|
7 |
|
|
|
8 |
#include <Arduino.h>
|
|
|
9 |
#if defined(ESP32) || defined(LIBRETINY)
|
|
|
10 |
#include <AsyncTCP.h>
|
|
|
11 |
#include <WiFi.h>
|
|
|
12 |
#elif defined(ESP8266)
|
|
|
13 |
#include <ESP8266WiFi.h>
|
|
|
14 |
#include <ESPAsyncTCP.h>
|
|
|
15 |
#elif defined(TARGET_RP2040) || defined(TARGET_RP2350) || defined(PICO_RP2040) || defined(PICO_RP2350)
|
|
|
16 |
#include <RPAsyncTCP.h>
|
|
|
17 |
#include <WiFi.h>
|
|
|
18 |
#endif
|
|
|
19 |
|
|
|
20 |
#include <ESPAsyncWebServer.h>
|
|
|
21 |
|
|
|
22 |
static AsyncWebServer server(80);
|
|
|
23 |
|
|
|
24 |
// New middleware classes can be created!
|
|
|
25 |
class MyMiddleware : public AsyncMiddleware {
|
|
|
26 |
public:
|
|
|
27 |
void run(AsyncWebServerRequest *request, ArMiddlewareNext next) override {
|
|
|
28 |
Serial.printf("Before handler: %s %s\n", request->methodToString(), request->url().c_str());
|
|
|
29 |
next(); // continue middleware chain
|
|
|
30 |
Serial.printf("After handler: response code=%d\n", request->getResponse()->code());
|
|
|
31 |
}
|
|
|
32 |
};
|
|
|
33 |
|
|
|
34 |
void setup() {
|
|
|
35 |
Serial.begin(115200);
|
|
|
36 |
|
|
|
37 |
#if ASYNCWEBSERVER_WIFI_SUPPORTED
|
|
|
38 |
WiFi.mode(WIFI_AP);
|
|
|
39 |
WiFi.softAP("esp-captive");
|
|
|
40 |
#endif
|
|
|
41 |
|
|
|
42 |
// add a global middleware to the server
|
|
|
43 |
server.addMiddleware(new MyMiddleware());
|
|
|
44 |
|
|
|
45 |
// Test with:
|
|
|
46 |
//
|
|
|
47 |
// - curl -v http://192.168.4.1/ => 200 OK
|
|
|
48 |
// - curl -v http://192.168.4.1/?user=anon => 403 Forbidden
|
|
|
49 |
// - curl -v http://192.168.4.1/?user=foo => 200 OK
|
|
|
50 |
// - curl -v http://192.168.4.1/?user=error => 400 ERROR
|
|
|
51 |
//
|
|
|
52 |
AsyncCallbackWebHandler &handler = server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
|
|
|
53 |
Serial.printf("In Handler: %s %s\n", request->methodToString(), request->url().c_str());
|
|
|
54 |
request->send(200, "text/plain", "Hello, world!");
|
|
|
55 |
});
|
|
|
56 |
|
|
|
57 |
// add a middleware to this handler only to send 403 if the user is anon
|
|
|
58 |
handler.addMiddleware([](AsyncWebServerRequest *request, ArMiddlewareNext next) {
|
|
|
59 |
Serial.println("Checking user=anon");
|
|
|
60 |
if (request->hasParam("user") && request->getParam("user")->value() == "anon") {
|
|
|
61 |
request->send(403, "text/plain", "Forbidden");
|
|
|
62 |
} else {
|
|
|
63 |
next();
|
|
|
64 |
}
|
|
|
65 |
});
|
|
|
66 |
|
|
|
67 |
// add a middleware to this handler that will replace the previously created response by another one
|
|
|
68 |
handler.addMiddleware([](AsyncWebServerRequest *request, ArMiddlewareNext next) {
|
|
|
69 |
next();
|
|
|
70 |
Serial.println("Checking user=error");
|
|
|
71 |
if (request->hasParam("user") && request->getParam("user")->value() == "error") {
|
|
|
72 |
request->send(400, "text/plain", "ERROR");
|
|
|
73 |
}
|
|
|
74 |
});
|
|
|
75 |
|
|
|
76 |
server.begin();
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
// not needed
|
|
|
80 |
void loop() {
|
|
|
81 |
delay(100);
|
|
|
82 |
}
|