Skip to content

Commit e09d8eb

Browse files
committed
Fix CORS issues.
* Handle OPTIONS requests in ESP port. * Add Access-Control-Allow-Headers to all ports.
1 parent df0ce46 commit e09d8eb

File tree

5 files changed

+25
-3
lines changed

5 files changed

+25
-3
lines changed

ESPWebThingAdapter.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,17 @@ 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"
64+
);
6165

6266
this->server.onNotFound(std::bind(&WebThingAdapter::handleUnknown, this,
6367
std::placeholders::_1));
6468

69+
this->server.on("/*", HTTP_OPTIONS,
70+
std::bind(&WebThingAdapter::handleOptions, this,
71+
std::placeholders::_1));
6572
this->server.on("/", HTTP_GET,
6673
std::bind(&WebThingAdapter::handleThings, this,
6774
std::placeholders::_1));
@@ -333,6 +340,13 @@ class WebThingAdapter {
333340
request->send(404);
334341
}
335342

343+
void handleOptions(AsyncWebServerRequest *request) {
344+
if (!verifyHost(request)) {
345+
return;
346+
}
347+
request->send(204);
348+
}
349+
336350
void handleThings(AsyncWebServerRequest *request) {
337351
if (!verifyHost(request)) {
338352
return;

EthernetWebThingAdapter.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,10 @@ 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(
415+
"Access-Control-Allow-Headers: "
416+
"Origin, X-Requested-With, Content-Type, Accept"
417+
);
414418
client.println("Content-Type: application/json");
415419
client.println("Connection: close");
416420
client.println();

WiFi101WebThingAdapter.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,10 @@ 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(
401+
"Access-Control-Allow-Headers: "
402+
"Origin, X-Requested-With, Content-Type, Accept"
403+
);
400404
client.println("Content-Type: application/json");
401405
client.println("Connection: close");
402406
client.println();

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)