From 85ea4e995a75abe061f6fc375ea0481084dddd43 Mon Sep 17 00:00:00 2001 From: schererleander Date: Tue, 20 Jan 2026 08:34:54 +0100 Subject: initial commit --- .../examples/RateLimit/RateLimit.ino | 64 ++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 libraries/ESP_Async_WebServer/examples/RateLimit/RateLimit.ino (limited to 'libraries/ESP_Async_WebServer/examples/RateLimit') diff --git a/libraries/ESP_Async_WebServer/examples/RateLimit/RateLimit.ino b/libraries/ESP_Async_WebServer/examples/RateLimit/RateLimit.ino new file mode 100644 index 0000000..89d6090 --- /dev/null +++ b/libraries/ESP_Async_WebServer/examples/RateLimit/RateLimit.ino @@ -0,0 +1,64 @@ +// SPDX-License-Identifier: LGPL-3.0-or-later +// Copyright 2016-2025 Hristo Gochkov, Mathieu Carbou, Emil Muratov + +// +// Show how to rate limit the server or some endpoints +// + +#include +#ifdef ESP32 +#include +#include +#elif defined(ESP8266) +#include +#include +#elif defined(TARGET_RP2040) || defined(TARGET_RP2350) || defined(PICO_RP2040) || defined(PICO_RP2350) +#include +#include +#endif + +#include + +static AsyncWebServer server(80); +static AsyncRateLimitMiddleware rateLimit; + +void setup() { + Serial.begin(115200); + +#ifndef CONFIG_IDF_TARGET_ESP32H2 + WiFi.mode(WIFI_AP); + WiFi.softAP("esp-captive"); +#endif + + // maximum 5 requests per 10 seconds + rateLimit.setMaxRequests(5); + rateLimit.setWindowSize(10); + + // run quickly several times: + // + // curl -v http://192.168.4.1/ + // + server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) { + request->send(200, "text/plain", "Hello, world!"); + }); + + // run quickly several times: + // + // curl -v http://192.168.4.1/rate-limited + // + server + .on( + "/rate-limited", HTTP_GET, + [](AsyncWebServerRequest *request) { + request->send(200, "text/plain", "Hello, world!"); + } + ) + .addMiddleware(&rateLimit); // only rate limit this endpoint, but could be applied globally to the server + + server.begin(); +} + +// not needed +void loop() { + delay(100); +} -- cgit v1.3.1