| 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 |
// Shows how to rewrite URLs
|
|
|
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 |
void setup() {
|
|
|
25 |
Serial.begin(115200);
|
|
|
26 |
|
|
|
27 |
#if ASYNCWEBSERVER_WIFI_SUPPORTED
|
|
|
28 |
WiFi.mode(WIFI_AP);
|
|
|
29 |
WiFi.softAP("esp-captive");
|
|
|
30 |
#endif
|
|
|
31 |
|
|
|
32 |
// curl -v http://192.168.4.1/index.txt
|
|
|
33 |
server.on("/index.txt", HTTP_GET, [](AsyncWebServerRequest *request) {
|
|
|
34 |
request->send(200, "text/plain", "Hello, world!");
|
|
|
35 |
});
|
|
|
36 |
|
|
|
37 |
// curl -v http://192.168.4.1/index.txt
|
|
|
38 |
server.on("/index.html", HTTP_GET, [](AsyncWebServerRequest *request) {
|
|
|
39 |
request->send(200, "text/html", "<h1>Hello, world!</h1>");
|
|
|
40 |
});
|
|
|
41 |
|
|
|
42 |
// curl -v http://192.168.4.1/
|
|
|
43 |
server.rewrite("/", "/index.html");
|
|
|
44 |
server.rewrite("/index.txt", "/index.html"); // will hide the .txt file
|
|
|
45 |
|
|
|
46 |
server.begin();
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
// not needed
|
|
|
50 |
void loop() {
|
|
|
51 |
delay(100);
|
|
|
52 |
}
|