Skip to content

Commit da5886f

Browse files
authored
Fix CORS issues. (#105)
* Handle OPTIONS requests in ESP port. * Add Access-Control-Allow-Headers to all ports.
1 parent df0ce46 commit da5886f

File tree

6 files changed

+20
-27
lines changed

6 files changed

+20
-27
lines changed

ESPWebThingAdapter.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,16 @@ class WebThingAdapter {
5858
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Origin", "*");
5959
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Methods",
6060
"GET, POST, PUT, DELETE, OPTIONS");
61+
DefaultHeaders::Instance().addHeader(
62+
"Access-Control-Allow-Headers",
63+
"Origin, X-Requested-With, Content-Type, Accept");
6164

6265
this->server.onNotFound(std::bind(&WebThingAdapter::handleUnknown, this,
6366
std::placeholders::_1));
6467

68+
this->server.on("/*", HTTP_OPTIONS,
69+
std::bind(&WebThingAdapter::handleOptions, this,
70+
std::placeholders::_1));
6571
this->server.on("/", HTTP_GET,
6672
std::bind(&WebThingAdapter::handleThings, this,
6773
std::placeholders::_1));
@@ -333,6 +339,13 @@ class WebThingAdapter {
333339
request->send(404);
334340
}
335341

342+
void handleOptions(AsyncWebServerRequest *request) {
343+
if (!verifyHost(request)) {
344+
return;
345+
}
346+
request->send(204);
347+
}
348+
336349
void handleThings(AsyncWebServerRequest *request) {
337350
if (!verifyHost(request)) {
338351
return;

EthernetWebThingAdapter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,8 @@ class WebThingAdapter {
411411
client.println("Access-Control-Allow-Origin: *");
412412
client.println(
413413
"Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
414+
client.println("Access-Control-Allow-Headers: "
415+
"Origin, X-Requested-With, Content-Type, Accept");
414416
client.println("Content-Type: application/json");
415417
client.println("Connection: close");
416418
client.println();

WiFi101WebThingAdapter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,8 @@ class WebThingAdapter {
397397
client.println("Access-Control-Allow-Origin: *");
398398
client.println(
399399
"Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
400+
client.println("Access-Control-Allow-Headers: "
401+
"Origin, X-Requested-With, Content-Type, Accept");
400402
client.println("Content-Type: application/json");
401403
client.println("Connection: close");
402404
client.println();

examples/PlatformIO/TextDisplay/platformio.ini

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,6 @@ lib_deps =
2323
Adafruit SSD1306
2424
monitor_speed = 115200
2525

26-
[env:d1]
27-
platform = espressif8266
28-
board = d1
29-
framework = arduino
30-
lib_deps =
31-
${global.lib_deps}
32-
ESP Async WebServer
33-
lib_ignore = WiFi101
34-
lib_ldf_mode = deep+
35-
monitor_speed = ${global.monitor_speed}
36-
37-
[env:nodemcuv2]
38-
platform = espressif8266
39-
board = nodemcuv2
40-
framework = arduino
41-
lib_deps =
42-
${global.lib_deps}
43-
ESP Async WebServer
44-
lib_ignore =
45-
ArduinoMDNS
46-
WiFi101
47-
lib_ldf_mode = deep+
48-
monitor_speed = ${global.monitor_speed}
49-
5026
[env:esp32dev]
5127
platform = espressif32
5228
board = esp32dev

library.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "webthing-arduino",
33
"description": "A library for creating Web Things using the Web of Things API. Runs on ESP8266, ESP32, Ethernet, and WiFi101-compatible boards. Compatible with the Mozilla WebThings Gateway.",
44
"keywords": "Communication",
5-
"version": "0.11.1",
5+
"version": "0.11.2",
66
"authors": {
77
"name": "Mozilla IoT <iot@mozilla.com>"
88
},
@@ -14,7 +14,7 @@
1414
"license": "Mozilla Public License Version 2.0",
1515
"platforms": "espressif8266,espressif32,atmelavr",
1616
"dependencies": {
17-
"ArduinoJson": "6.13.0"
17+
"ArduinoJson": "6.15.0"
1818
},
1919
"export": {
2020
"include":

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=webthing-arduino
2-
version=0.11.1
2+
version=0.11.2
33
author=Mozilla IoT <iot@mozilla.com>
44
maintainer=James Hobin <hobinjk@gmail.com>
55
sentence=A library for creating Web Things using the Web of Things API

0 commit comments

Comments
 (0)