Skip to content

Commit

Permalink
Fix homieiot#446 CORS Issue
Browse files Browse the repository at this point in the history
  • Loading branch information
timpur committed Mar 18, 2018
1 parent 89991d4 commit b884730
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
28 changes: 13 additions & 15 deletions src/Homie/Boot/BootConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,25 @@ void BootConfig::setup() {
_dns.setErrorReplyCode(DNSReplyCode::NoError);
_dns.start(53, F("*"), ACCESS_POINT_IP);

__setCORS();
_http.on("/heart", HTTP_GET, [this](AsyncWebServerRequest *request) {
Interface::get().getLogger() << F("Received heart request") << endl;
request->send(204);
});
_http.on("/device-info", HTTP_GET, [this](AsyncWebServerRequest *request) { _onDeviceInfoRequest(request); });
_http.on("/networks", HTTP_GET, [this](AsyncWebServerRequest *request) { _onNetworksRequest(request); });
_http.on("/config", HTTP_PUT, [this](AsyncWebServerRequest *request) { _onConfigRequest(request); }).onBody(BootConfig::__parsePost);
_http.on("/config", HTTP_OPTIONS, [this](AsyncWebServerRequest *request) { // CORS
Interface::get().getLogger() << F("Received CORS request for /config") << endl;
__sendCORS(request);
});
_http.on("/wifi/connect", HTTP_PUT, [this](AsyncWebServerRequest *request) { _onWifiConnectRequest(request); }).onBody(BootConfig::__parsePost);
_http.on("/wifi/connect", HTTP_OPTIONS, [this](AsyncWebServerRequest *request) { // CORS
Interface::get().getLogger() << F("Received CORS request for /wifi/connect") << endl;
__sendCORS(request);
});
_http.on("/wifi/status", HTTP_GET, [this](AsyncWebServerRequest *request) { _onWifiStatusRequest(request); });
_http.on("/proxy/control", HTTP_PUT, [this](AsyncWebServerRequest *request) { _onProxyControlRequest(request); }).onBody(BootConfig::__parsePost);
_http.onNotFound([this](AsyncWebServerRequest *request) { _onCaptivePortal(request); });
_http.onNotFound([this](AsyncWebServerRequest *request) {
if ( request->method() == HTTP_OPTIONS ) {
Interface::get().getLogger() << F("Received CORS request for ")<< request->url() << endl;
request->send(200);
} else {
_onCaptivePortal(request);
}
});
_http.begin();
}

Expand Down Expand Up @@ -409,12 +409,10 @@ void BootConfig::_onConfigRequest(AsyncWebServerRequest *request) {
_flaggedForRebootAt = millis();
}

void BootConfig::__sendCORS(AsyncWebServerRequest *request) {
AsyncWebServerResponse *response = request->beginResponse(204);
response->addHeader(F("Access-Control-Allow-Origin"), F("*"));
response->addHeader(F("Access-Control-Allow-Methods"), F("PUT"));
response->addHeader(F("Access-Control-Allow-Headers"), F("Content-Type, Origin, Referer, User-Agent"));
request->send(response);
void BootConfig::__setCORS() {
DefaultHeaders::Instance().addHeader(F("Access-Control-Allow-Origin"), F("*"));
DefaultHeaders::Instance().addHeader(F("Access-Control-Allow-Methods"), F("GET, PUT"));
DefaultHeaders::Instance().addHeader(F("Access-Control-Allow-Headers"), F("Content-Type, Origin, Referer, User-Agent"));
}

void BootConfig::__parsePost(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total) {
Expand Down
2 changes: 1 addition & 1 deletion src/Homie/Boot/BootConfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class BootConfig : public Boot {
void _onWifiStatusRequest(AsyncWebServerRequest *request);

// Helpers
static void __sendCORS(AsyncWebServerRequest *request);
static void __setCORS();
static const int MAX_POST_SIZE = 1500;
static void __parsePost(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total);
static void __SendJSONError(AsyncWebServerRequest *request, String msg, int16_t code = 400);
Expand Down

0 comments on commit b884730

Please sign in to comment.