1
1
/* *
2
- * ESP32WebThingAdapter .h
2
+ * ESPWebThingAdapter .h
3
3
*
4
4
* Exposes the Web Thing API based on provided ThingDevices.
5
- * Suitable for ESP32, ESPAsyncWebServer, ESPAsyncTCP
5
+ * Suitable for ESP32 and ESP8266 using ESPAsyncWebServer and ESPAsyncTCP
6
6
*
7
7
* This Source Code Form is subject to the terms of the Mozilla Public
8
8
* License, v. 2.0. If a copy of the MPL was not distributed with this
9
9
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
10
10
*/
11
11
12
- #ifndef MOZILLA_IOT_ESP32WEBTHINGADAPTER_H
13
- #define MOZILLA_IOT_ESP32WEBTHINGADAPTER_H
12
+ #ifndef MOZILLA_IOT_ESPWEBTHINGADAPTER_H
13
+ #define MOZILLA_IOT_ESPWEBTHINGADAPTER_H
14
14
15
- #ifdef ESP32
15
+ #if defined( ESP32) || defined(ESP8266)
16
16
17
- #include < WiFiClient.h>
18
17
#include < ArduinoJson.h>
19
18
#include < ESPAsyncWebServer.h>
19
+ #ifdef ESP8266
20
+ #include < ESP8266mDNS.h>
21
+ #else
20
22
#include < ESPmDNS.h>
23
+ #endif
21
24
#include " Thing.h"
22
25
23
- #define ESP32_MAX_PUT_BODY_SIZE 256
26
+ #define ESP_MAX_PUT_BODY_SIZE 512
24
27
25
28
26
29
class WebThingAdapter {
27
30
public:
28
- WebThingAdapter (String _name): name(_name), server(80 ) {
31
+ WebThingAdapter (String _name, IPAddress _ip ): name(_name), server(80 ), ip(_ip.toString() ) {
29
32
}
30
33
31
34
void begin () {
@@ -85,19 +88,44 @@ class WebThingAdapter {
85
88
private:
86
89
AsyncWebServer server;
87
90
String name;
91
+ String ip;
88
92
ThingDevice* firstDevice = nullptr ;
89
93
ThingDevice* lastDevice = nullptr ;
90
- char body_data[ESP32_MAX_PUT_BODY_SIZE ];
94
+ char body_data[ESP_MAX_PUT_BODY_SIZE ];
91
95
bool b_has_body_data = false ;
92
96
97
+ bool verifyHost (AsyncWebServerRequest *request) {
98
+ AsyncWebHeader* header = request->getHeader (" Host" );
99
+ if (header == nullptr ) {
100
+ request->send (403 );
101
+ return false ;
102
+ }
103
+ String value = header->value ();
104
+ int colonIndex = value.indexOf (' :' );
105
+ if (colonIndex >= 0 ) {
106
+ value.remove (colonIndex);
107
+ }
108
+ if (value == name + " .local" || value == ip) {
109
+ return true ;
110
+ }
111
+ request->send (403 );
112
+ return false ;
113
+ }
114
+
93
115
void handleUnknown (AsyncWebServerRequest *request) {
116
+ if (!verifyHost (request)) {
117
+ return ;
118
+ }
94
119
request->send (404 );
95
120
}
96
121
97
122
void handleThings (AsyncWebServerRequest *request) {
123
+ if (!verifyHost (request)) {
124
+ return ;
125
+ }
98
126
AsyncResponseStream *response = request->beginResponseStream (" application/json" );
99
127
100
- StaticJsonBuffer<2048 > buf;
128
+ StaticJsonBuffer<4096 > buf;
101
129
JsonArray& things = buf.createArray ();
102
130
ThingDevice* device = this ->firstDevice ;
103
131
while (device != nullptr ) {
@@ -148,6 +176,9 @@ class WebThingAdapter {
148
176
}
149
177
150
178
void handleThing (AsyncWebServerRequest *request, ThingDevice*& device) {
179
+ if (!verifyHost (request)) {
180
+ return ;
181
+ }
151
182
AsyncResponseStream *response = request->beginResponseStream (" application/json" );
152
183
153
184
StaticJsonBuffer<1024 > buf;
@@ -159,6 +190,9 @@ class WebThingAdapter {
159
190
}
160
191
161
192
void handleThingPropertyGet (AsyncWebServerRequest *request, ThingProperty* property) {
193
+ if (!verifyHost (request)) {
194
+ return ;
195
+ }
162
196
AsyncResponseStream *response = request->beginResponseStream (" application/json" );
163
197
164
198
StaticJsonBuffer<256 > buf;
@@ -181,7 +215,7 @@ class WebThingAdapter {
181
215
182
216
183
217
void handleBody (AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total) {
184
- if ( total >= ESP32_MAX_PUT_BODY_SIZE || index+len >= ESP32_MAX_PUT_BODY_SIZE ) {
218
+ if ( total >= ESP_MAX_PUT_BODY_SIZE || index+len >= ESP_MAX_PUT_BODY_SIZE ) {
185
219
return ; // cannot store this size..
186
220
}
187
221
// copy to internal buffer
@@ -190,6 +224,9 @@ class WebThingAdapter {
190
224
}
191
225
192
226
void handleThingPropertyPut (AsyncWebServerRequest *request, ThingProperty* property) {
227
+ if (!verifyHost (request)) {
228
+ return ;
229
+ }
193
230
if (!b_has_body_data) {
194
231
request->send (422 ); // unprocessable entity (b/c no body)
195
232
return ;
@@ -231,6 +268,6 @@ class WebThingAdapter {
231
268
232
269
};
233
270
234
- #endif // ESP32
271
+ #endif // ESP
235
272
236
273
#endif
0 commit comments