Skip to content

Commit 6bfad6e

Browse files
authored
Merge branch 'master' into pr_fix_SPI0Command
2 parents 5045f29 + 7e0d20e commit 6bfad6e

File tree

15 files changed

+358
-60
lines changed

15 files changed

+358
-60
lines changed

cores/esp8266/LwipIntfDev.h

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,11 @@ class LwipIntfDev: public LwipIntf, public RawDev
8383
return &_netif;
8484
}
8585

86-
uint8_t* macAddress(uint8_t* mac) // WiFi lib way
86+
uint8_t* macAddress(uint8_t* mac)
8787
{
8888
memcpy(mac, &_netif.hwaddr, 6);
8989
return mac;
9090
}
91-
void MACAddress(uint8_t* mac) // Ethernet lib way
92-
{
93-
macAddress(mac);
94-
}
9591
IPAddress localIP() const
9692
{
9793
return IPAddress(ip4_addr_get_u32(ip_2_ip4(&_netif.ip_addr)));
@@ -104,15 +100,11 @@ class LwipIntfDev: public LwipIntf, public RawDev
104100
{
105101
return IPAddress(ip4_addr_get_u32(ip_2_ip4(&_netif.gw)));
106102
}
107-
IPAddress dnsIP(int n = 0) const // WiFi lib way
103+
IPAddress dnsIP(int n = 0) const
108104
{
109105
return IPAddress(dns_getserver(n));
110106
}
111-
IPAddress dnsServerIP() const // Ethernet lib way
112-
{
113-
return dnsIP(0);
114-
}
115-
void setDNS(IPAddress dns1, IPAddress dns2 = INADDR_ANY) // WiFi lib way
107+
void setDNS(IPAddress dns1, IPAddress dns2 = INADDR_ANY)
116108
{
117109
if (dns1.isSet())
118110
{
@@ -123,10 +115,6 @@ class LwipIntfDev: public LwipIntf, public RawDev
123115
dns_setserver(1, dns2);
124116
}
125117
}
126-
void setDnsServerIP(const IPAddress dnsIP) // Ethernet lib way
127-
{
128-
setDNS(dnsIP);
129-
}
130118

131119
// 1. Currently when no default is set, esp8266-Arduino uses the first
132120
// DHCP client interface receiving a valid address and gateway to

libraries/ESP8266SdFat

Submodule ESP8266SdFat updated 155 files

libraries/ESP8266WebServer/README.rst

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ Client request handlers
5454

5555
.. code:: cpp
5656
57-
void on();
57+
RequestHandler<ServerType>& on();
58+
bool removeRoute();
5859
void addHandler();
60+
bool removeHandler();
5961
void onNotFound();
6062
void onFileUpload();
6163
@@ -64,9 +66,26 @@ Client request handlers
6466
.. code:: cpp
6567
6668
server.on("/", handlerFunction);
69+
server.removeRoute("/"); // Removes any route which points to "/" and has HTTP_ANY attribute
70+
server.removeRoute("/", HTTP_GET); // Removes any route which points to "/" and has HTTP_GET attribute
6771
server.onNotFound(handlerFunction); // called when handler is not assigned
6872
server.onFileUpload(handlerFunction); // handle file uploads
6973
74+
Client request filters
75+
^^^^^^^^^^^^^^^^^^^^^^
76+
77+
.. code:: cpp
78+
79+
RequestHandler<ServerType>& setFilter();
80+
81+
*Example:*
82+
83+
More details about this in `Filters.ino` example.
84+
85+
.. code:: cpp
86+
87+
server.on("/", handlerFunction).setFilter(ON_AP_FILTER)
88+
7089
Sending responses to the client
7190
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7291

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#include <ESP8266WiFi.h>
2+
#include <WiFiClient.h>
3+
#include <ESP8266WebServer.h>
4+
#include <ESP8266mDNS.h>
5+
6+
// Your STA WiFi Credentials
7+
// ( This is the AP your ESP will connect to )
8+
const char *ssid = "...";
9+
const char *password = "...";
10+
11+
// Your AP WiFi Credentials
12+
// ( This is the AP your ESP will broadcast )
13+
const char *ap_ssid = "ESP8266_Demo";
14+
const char *ap_password = "";
15+
16+
ESP8266WebServer server(80);
17+
18+
const int led = 13;
19+
20+
// ON_STA_FILTER - Only accept requests coming from STA interface
21+
bool ON_STA_FILTER(ESP8266WebServer &server) {
22+
return WiFi.localIP() == server.client().localIP();
23+
}
24+
25+
// ON_AP_FILTER - Only accept requests coming from AP interface
26+
bool ON_AP_FILTER(ESP8266WebServer &server) {
27+
return WiFi.softAPIP() == server.client().localIP();
28+
}
29+
30+
void handleNotFound() {
31+
digitalWrite(led, 1);
32+
String message = "File Not Found\n\n";
33+
message += "URI: ";
34+
message += server.uri();
35+
message += "\nMethod: ";
36+
message += (server.method() == HTTP_GET) ? "GET" : "POST";
37+
message += "\nArguments: ";
38+
message += server.args();
39+
message += "\n";
40+
for (uint8_t i = 0; i < server.args(); i++) {
41+
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
42+
}
43+
server.send(404, "text/plain", message);
44+
digitalWrite(led, 0);
45+
}
46+
47+
void setup(void) {
48+
pinMode(led, OUTPUT);
49+
digitalWrite(led, 0);
50+
Serial.begin(115200);
51+
WiFi.mode(WIFI_AP_STA);
52+
// Connect to STA
53+
WiFi.begin(ssid, password);
54+
// Start AP
55+
WiFi.softAP(ap_ssid, ap_password);
56+
Serial.println("");
57+
58+
// Wait for connection
59+
while (WiFi.status() != WL_CONNECTED) {
60+
delay(500);
61+
Serial.print(".");
62+
}
63+
Serial.println("");
64+
Serial.print("Connected to ");
65+
Serial.println(ssid);
66+
Serial.print("IP address: ");
67+
Serial.println(WiFi.localIP());
68+
69+
if (MDNS.begin("esp8266")) {
70+
Serial.println("MDNS responder started");
71+
}
72+
73+
// This route will be accessible by STA clients only
74+
server.on("/", [&]() {
75+
digitalWrite(led, 1);
76+
server.send(200, "text/plain", "Hi!, This route is accessible for STA clients only");
77+
digitalWrite(led, 0);
78+
})
79+
.setFilter(ON_STA_FILTER);
80+
81+
// This route will be accessible by AP clients only
82+
server.on("/", [&]() {
83+
digitalWrite(led, 1);
84+
server.send(200, "text/plain", "Hi!, This route is accessible for AP clients only");
85+
digitalWrite(led, 0);
86+
})
87+
.setFilter(ON_AP_FILTER);
88+
89+
server.on("/inline", []() {
90+
server.send(200, "text/plain", "this works as well");
91+
});
92+
93+
server.onNotFound(handleNotFound);
94+
95+
server.begin();
96+
Serial.println("HTTP server started");
97+
}
98+
99+
void loop(void) {
100+
server.handleClient();
101+
}

libraries/ESP8266WebServer/examples/WebServer/README.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ It features
2121
* Only files in the root folder are supported for simplicity - no directories.
2222

2323

24-
25-
2624
## Implementing a web server
2725

2826
The ESP8266WebServer library offers a simple path to implement a web server on a ESP8266 board.
@@ -90,7 +88,7 @@ that actually has only one line of functionality by sending a string as result t
9088
> });
9189
> ```
9290
93-
Here the text from a static String with html code is returned instead of a file from the filesystem.
91+
Here the text from a static string with html code is returned instead of a file from the filesystem.
9492
The content of this string can be found in the file `builtinfiles.h`. It contains a small html+javascript implementation
9593
that allows uploading new files into the empty filesystem.
9694
@@ -100,14 +98,14 @@ Just open <http://webserver/$upload.htm> and drag some files from the data folde
10098
## Registering a function to handle requests to the server without a path
10199
102100
Often servers are addressed by using the base URL like <http://webserver/> where no further path details is given.
103-
Of course we like the user to be redirected to something usable. Therefore the `handleRoot()` function is registered:
101+
Of course we like the user to be redirected to something usable. Therefore the `handleRedirect()` function is registered:
104102
105103
> ```CPP
106-
> server.on("/$upload.htm", handleRoot);
104+
> server.on("/", HTTP_GET, handleRedirect);
107105
> ```
108106
109-
The `handleRoot()` function checks the filesystem for the file named **/index.htm** and creates a redirect to this file when the file exists.
110-
Otherwise the redirection goes to the built-in **/$upload.htm** web page.
107+
The `handleRedirect()` function checks the filesystem for the file named **/index.htm** and creates a redirect
108+
response to this file when the file exists. Otherwise the redirection goes to the built-in **/$upload.htm** web page.
111109
112110
113111
@@ -122,7 +120,7 @@ The **serveStatic** plug in is part of the library and handles delivering files
122120
> ```
123121
124122
125-
### Cross-Origin Ressource Sharing (CORS)
123+
### Cross-Origin Resource Sharing (CORS)
126124
127125
The `enableCORS(true)` function adds a `Access-Control-Allow-Origin: *` http-header to all responses to the client
128126
to inform that it is allowed to call URLs and services on this server from other web sites.

libraries/ESP8266WebServer/examples/WebServer/WebServer.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void handleRedirect() {
4141
TRACE("Redirect...");
4242
String url = "/index.htm";
4343

44-
if (!LittleFS.exists(url)) { url = "/$update.htm"; }
44+
if (!LittleFS.exists(url)) { url = "/$upload.htm"; }
4545

4646
server.redirect(url);
4747
} // handleRedirect()
@@ -104,7 +104,7 @@ public:
104104

105105
// @brief check incoming request. Can handle POST for uploads and DELETE.
106106
// @param requestMethod method of the http request line.
107-
// @param requestUri request ressource from the http request line.
107+
// @param requestUri request resource from the http request line.
108108
// @return true when method can be handled.
109109
bool canHandle(HTTPMethod requestMethod, const String UNUSED &_uri) override {
110110
return ((requestMethod == HTTP_POST) || (requestMethod == HTTP_DELETE));

libraries/ESP8266WebServer/examples/WebServer/builtinfiles.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ R"==(
5454
static const char notFoundContent[] PROGMEM = R"==(
5555
<html>
5656
<head>
57-
<title>Ressource not found</title>
57+
<title>Resource not found</title>
5858
</head>
5959
<body>
60-
<p>The ressource was not found.</p>
60+
<p>The resource was not found.</p>
6161
<p><a href="/">Start again</a></p>
6262
</body>
6363
)==";

libraries/ESP8266WebServer/src/ESP8266WebServer-impl.h

Lines changed: 81 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,25 +230,73 @@ void ESP8266WebServerTemplate<ServerType>::requestAuthentication(HTTPAuthMethod
230230
}
231231

232232
template <typename ServerType>
233-
void ESP8266WebServerTemplate<ServerType>::on(const Uri &uri, ESP8266WebServerTemplate<ServerType>::THandlerFunction handler) {
234-
on(uri, HTTP_ANY, handler);
233+
RequestHandler<ServerType>& ESP8266WebServerTemplate<ServerType>::on(const Uri &uri, ESP8266WebServerTemplate<ServerType>::THandlerFunction handler) {
234+
return on(uri, HTTP_ANY, handler);
235235
}
236236

237237
template <typename ServerType>
238-
void ESP8266WebServerTemplate<ServerType>::on(const Uri &uri, HTTPMethod method, ESP8266WebServerTemplate<ServerType>::THandlerFunction fn) {
239-
on(uri, method, fn, _fileUploadHandler);
238+
RequestHandler<ServerType>& ESP8266WebServerTemplate<ServerType>::on(const Uri &uri, HTTPMethod method, ESP8266WebServerTemplate<ServerType>::THandlerFunction fn) {
239+
return on(uri, method, fn, _fileUploadHandler);
240240
}
241241

242242
template <typename ServerType>
243-
void ESP8266WebServerTemplate<ServerType>::on(const Uri &uri, HTTPMethod method, ESP8266WebServerTemplate<ServerType>::THandlerFunction fn, ESP8266WebServerTemplate<ServerType>::THandlerFunction ufn) {
244-
_addRequestHandler(new FunctionRequestHandler<ServerType>(fn, ufn, uri, method));
243+
RequestHandler<ServerType>& ESP8266WebServerTemplate<ServerType>::on(const Uri &uri, HTTPMethod method, ESP8266WebServerTemplate<ServerType>::THandlerFunction fn, ESP8266WebServerTemplate<ServerType>::THandlerFunction ufn) {
244+
RequestHandler<ServerType> *handler = new FunctionRequestHandler<ServerType>(fn, ufn, uri, method);
245+
_addRequestHandler(handler);
246+
return *handler;
247+
}
248+
249+
template <typename ServerType>
250+
bool ESP8266WebServerTemplate<ServerType>::removeRoute(const char *uri) {
251+
return removeRoute(String(uri), HTTP_ANY);
252+
}
253+
254+
template <typename ServerType>
255+
bool ESP8266WebServerTemplate<ServerType>::removeRoute(const char *uri, HTTPMethod method) {
256+
return removeRoute(String(uri), method);
257+
}
258+
259+
template <typename ServerType>
260+
bool ESP8266WebServerTemplate<ServerType>::removeRoute(const String &uri) {
261+
return removeRoute(uri, HTTP_ANY);
262+
}
263+
264+
template <typename ServerType>
265+
bool ESP8266WebServerTemplate<ServerType>::removeRoute(const String &uri, HTTPMethod method) {
266+
bool anyHandlerRemoved = false;
267+
RequestHandlerType *handler = _firstHandler;
268+
RequestHandlerType *previousHandler = nullptr;
269+
270+
while (handler) {
271+
if (handler->canHandle(method, uri)) {
272+
if (_removeRequestHandler(handler)) {
273+
anyHandlerRemoved = true;
274+
// Move to the next handler
275+
if (previousHandler) {
276+
handler = previousHandler->next();
277+
} else {
278+
handler = _firstHandler;
279+
}
280+
continue;
281+
}
282+
}
283+
previousHandler = handler;
284+
handler = handler->next();
285+
}
286+
287+
return anyHandlerRemoved;
245288
}
246289

247290
template <typename ServerType>
248291
void ESP8266WebServerTemplate<ServerType>::addHandler(RequestHandlerType* handler) {
249292
_addRequestHandler(handler);
250293
}
251294

295+
template <typename ServerType>
296+
bool ESP8266WebServerTemplate<ServerType>::removeHandler(RequestHandlerType *handler) {
297+
return _removeRequestHandler(handler);
298+
}
299+
252300
template <typename ServerType>
253301
void ESP8266WebServerTemplate<ServerType>::_addRequestHandler(RequestHandlerType* handler) {
254302
if (!_lastHandler) {
@@ -261,6 +309,33 @@ void ESP8266WebServerTemplate<ServerType>::_addRequestHandler(RequestHandlerType
261309
}
262310
}
263311

312+
template <typename ServerType>
313+
bool ESP8266WebServerTemplate<ServerType>::_removeRequestHandler(RequestHandlerType *handler) {
314+
RequestHandlerType *current = _firstHandler;
315+
RequestHandlerType *previous = nullptr;
316+
317+
while (current != nullptr) {
318+
if (current == handler) {
319+
if (previous == nullptr) {
320+
_firstHandler = current->next();
321+
} else {
322+
previous->next(current->next());
323+
}
324+
325+
if (current == _lastHandler) {
326+
_lastHandler = previous;
327+
}
328+
329+
// Delete 'matching' handler
330+
delete current;
331+
return true;
332+
}
333+
previous = current;
334+
current = current->next();
335+
}
336+
return false;
337+
}
338+
264339
template <typename ServerType>
265340
void ESP8266WebServerTemplate<ServerType>::serveStatic(const char* uri, FS& fs, const char* path, const char* cache_header) {
266341
bool is_file = false;

0 commit comments

Comments
 (0)