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 Jan 9, 2018
1 parent 6495a3e commit 73bb2ab
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 25 deletions.
35 changes: 14 additions & 21 deletions src/Homie/Boot/BootConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,31 +44,31 @@ void BootConfig::setup() {
}

Helpers::ipToString(ACCESS_POINT_IP, _apIpStr);

Interface::get().getLogger() << F("AP started as ") << apName << F(" with IP ") << _apIpStr << endl;

_dns.setTTL(30);
_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 @@ -403,12 +403,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 All @@ -423,11 +421,6 @@ void BootConfig::__parsePost(AsyncWebServerRequest *request, uint8_t *data, size
strcat(buff, (const char*)data);
}
}
static const String ConfigJSONError(const String error) {
const String BEGINNING = String(FPSTR(PROGMEM_CONFIG_JSON_FAILURE_BEGINNING));
const String END = String(FPSTR(PROGMEM_CONFIG_JSON_FAILURE_END));
return BEGINNING + error + END;
}

void HomieInternals::BootConfig::__SendJSONError(AsyncWebServerRequest * request, String msg, int16_t code) {
Interface::get().getLogger() << msg << endl;
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
6 changes: 3 additions & 3 deletions src/Homie/Boot/BootNormal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,7 @@ bool BootNormal::__handleResets(char * topic, char * payload, const AsyncMqttCli
&& strcmp_P(_mqttTopicLevels.get()[1], PSTR("$implementation")) == 0
&& strcmp_P(_mqttTopicLevels.get()[2], PSTR("reset")) == 0
) {
if(strcmp_P(_mqttPayloadBuffer.get(), PSTR("true")) == 0){
if ( strcmp_P(_mqttPayloadBuffer.get(), PSTR("true") ) == 0 ) {
Interface::get().getMqttClient().publish(_prefixMqttTopic(PSTR("/$implementation/reset")), 1, true, "false");
Interface::get().getLogger() << F("Flagged for reset by network") << endl;
Interface::get().disable = true;
Expand All @@ -827,13 +827,13 @@ bool BootNormal::__handleResets(char * topic, char * payload, const AsyncMqttCli
return false;
}

bool BootNormal::__handleRestarts(char* topic, char* payload, const AsyncMqttClientMessageProperties& properties, size_t len, size_t index, size_t total){
bool BootNormal::__handleRestarts(char* topic, char* payload, const AsyncMqttClientMessageProperties& properties, size_t len, size_t index, size_t total) {
if (
_mqttTopicLevelsCount == 3
&& strcmp_P(_mqttTopicLevels.get()[1], PSTR("$implementation")) == 0
&& strcmp_P(_mqttTopicLevels.get()[2], PSTR("restart")) == 0
) {
if(strcmp_P(_mqttPayloadBuffer.get(), PSTR("true")) == 0){
if ( strcmp_P(_mqttPayloadBuffer.get(), PSTR("true") ) == 0 ) {
Interface::get().getMqttClient().publish(_prefixMqttTopic(PSTR("/$implementation/restart")), 1, true, "false");
Interface::get().getLogger() << F("Flagged for restart by network") << endl;
Interface::get().disable = true;
Expand Down

0 comments on commit 73bb2ab

Please sign in to comment.