Skip to content

Commit

Permalink
Fix #446 #477 (#501)
Browse files Browse the repository at this point in the history
* Fix #446 CORS Issue

* Fix for #477

* Fix Lint
  • Loading branch information
timpur authored Mar 18, 2018
1 parent b5e8926 commit 95d9566
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
49 changes: 25 additions & 24 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,24 +409,25 @@ 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) {
if (!index && total > MAX_POST_SIZE) {
Interface::get().getLogger() << "Request is to large to be processed." << endl;
} else if (!index && total <= MAX_POST_SIZE) {
char* buff = new char[total + 1];
strcpy(buff, (const char*)data);
request->_tempObject = buff;
} else if (total <= MAX_POST_SIZE) {
char* buff = reinterpret_cast<char*>(request->_tempObject);
strcat(buff, (const char*)data);
if (total > MAX_POST_SIZE) {
Interface::get().getLogger() << F("Request is to large to be processed.") << endl;
} else {
if (index == 0) {
request->_tempObject = new char[total + 1];
}
void* buff = request->_tempObject + index;
memcpy(buff, data, len);
if (index + len == total) {
void* buff = request->_tempObject + total;
*reinterpret_cast<char*>(buff) = 0;
}
}
}
static const String ConfigJSONError(const String error) {
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 95d9566

Please sign in to comment.