From cfa0744cc3b2d69de7bff4c097f5e1b68a97a26a Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Tue, 5 Sep 2023 16:38:41 +0200 Subject: [PATCH] update to v3.6 cpp files --- lib/framework/HttpEndpoint.h | 2 +- lib/framework/NTPSettingsService.cpp | 2 +- lib/framework/NetworkSettingsService.cpp | 4 +++ lib/framework/UploadFileService.cpp | 41 ++++++++++++++++++------ lib/framework/WiFiScanner.cpp | 3 +- 5 files changed, 39 insertions(+), 13 deletions(-) diff --git a/lib/framework/HttpEndpoint.h b/lib/framework/HttpEndpoint.h index 21ac7e6..baa2392 100644 --- a/lib/framework/HttpEndpoint.h +++ b/lib/framework/HttpEndpoint.h @@ -115,7 +115,7 @@ class HttpPostEndpoint { response->setLength(); if (outcome == StateUpdateResult::CHANGED_RESTART) { - response->setCode(202); // added by proddy + response->setCode(205); // added by proddy } request->send(response); } diff --git a/lib/framework/NTPSettingsService.cpp b/lib/framework/NTPSettingsService.cpp index be2566d..b4b9d13 100644 --- a/lib/framework/NTPSettingsService.cpp +++ b/lib/framework/NTPSettingsService.cpp @@ -45,7 +45,7 @@ void NTPSettingsService::WiFiEvent(WiFiEvent_t event) { void NTPSettingsService::configureNTP() { // emsesp::EMSESP::system_.ntp_connected(false); if (connected_ && _state.enabled) { - sntp_set_sync_interval(3600000); // onehour + sntp_set_sync_interval(3600000); // one hour sntp_set_time_sync_notification_cb(ntp_received); configTzTime(_state.tzFormat.c_str(), _state.server.c_str()); } else { diff --git a/lib/framework/NetworkSettingsService.cpp b/lib/framework/NetworkSettingsService.cpp index bf64056..bf459d4 100644 --- a/lib/framework/NetworkSettingsService.cpp +++ b/lib/framework/NetworkSettingsService.cpp @@ -89,4 +89,8 @@ void NetworkSettingsService::WiFiEvent(WiFiEvent_t event) { _stopping = false; } } + if (event == ARDUINO_EVENT_WIFI_STA_DISCONNECTED && WiFi.scanComplete() != -1) { + WiFi.scanDelete(); + WiFi.scanNetworks(true); + } } diff --git a/lib/framework/UploadFileService.cpp b/lib/framework/UploadFileService.cpp index 27e9b44..46acac2 100644 --- a/lib/framework/UploadFileService.cpp +++ b/lib/framework/UploadFileService.cpp @@ -1,4 +1,5 @@ #include +#include using namespace std::placeholders; // for `_1` etc @@ -47,40 +48,46 @@ void UploadFileService::handleUpload(AsyncWebServerRequest * request, const Stri return; } else { md5[0] = '\0'; - return; // not support file type + handleError(request, 406); // Not Acceptable - unsupported file type + return; } if (is_firmware) { // Check firmware header, 0xE9 magic offset 0 indicates esp bin, chip offset 12: esp32:0, S2:2, C3:5, S3:9 #if CONFIG_IDF_TARGET_ESP32 // ESP32/PICO-D4 if (len > 12 && (data[0] != 0xE9 || data[12] != 0)) { - handleError(request, 503); // service unavailable + handleError(request, 406); // 406-Not Acceptable + // handleError(request, 503); // 503-service unavailable return; } #elif CONFIG_IDF_TARGET_ESP32S2 if (len > 12 && (data[0] != 0xE9 || data[12] != 2)) { - handleError(request, 503); // service unavailable + handleError(request, 406); // Not Acceptable + // handleError(request, 503); // service unavailable return; } #elif CONFIG_IDF_TARGET_ESP32C3 if (len > 12 && (data[0] != 0xE9 || data[12] != 5)) { - handleError(request, 503); // service unavailable + handleError(request, 406); // Not Acceptable + // handleError(request, 503); // service unavailable return; } #elif CONFIG_IDF_TARGET_ESP32S3 if (len > 12 && (data[0] != 0xE9 || data[12] != 9)) { - handleError(request, 503); // service unavailable + handleError(request, 406); // Not Acceptable + // handleError(request, 503); // service unavailable return; } #endif // it's firmware - initialize the ArduinoOTA updater - if (Update.begin()) { + if (Update.begin(fsize - sizeof(esp_image_header_t))) { if (strlen(md5) == 32) { Update.setMD5(md5); md5[0] = '\0'; } request->onDisconnect(UploadFileService::handleEarlyDisconnect); // success, let's make sure we end the update if the client hangs up } else { + md5[0] = '\0'; #if defined(EMSESP_USE_SERIAL) Update.printError(Serial); #endif @@ -95,7 +102,9 @@ void UploadFileService::handleUpload(AsyncWebServerRequest * request, const Stri if (!is_firmware) { if (len) { - request->_tempFile.write(data, len); // stream the incoming chunk to the opened file + if (len != request->_tempFile.write(data, len)) { // stream the incoming chunk to the opened file + handleError(request, 507); // 507-Insufficient Storage + } } } else { // if we haven't delt with an error, continue with the firmware update @@ -136,13 +145,17 @@ void UploadFileService::uploadComplete(AsyncWebServerRequest * request) { request->send(response); return; } + if (strlen(md5) == 32) { - AsyncWebServerResponse * response = request->beginResponse(201, "text/plain", md5); // created + auto * response = new AsyncJsonResponse(false, 256); + JsonObject root = response->getRoot(); + root["md5"] = md5; + response->setLength(); request->send(response); return; } - handleError(request, 403); // send the forbidden response + handleError(request, 500); } void UploadFileService::handleError(AsyncWebServerRequest * request, int code) { @@ -152,9 +165,17 @@ void UploadFileService::handleError(AsyncWebServerRequest * request, int code) { } // send the error code to the client and record the error code in the temp object - request->_tempObject = new int(code); + // request->_tempObject = new int(code); AsyncWebServerResponse * response = request->beginResponse(code); request->send(response); + md5[0] = '\0'; + + // check for invalid extension and immediately kill the connection, which will through an error + // that is caught by the web code. Unfortunately the http error code is not sent to the client on fast network connections + if (code == 406) { + handleEarlyDisconnect(); + request->client()->close(true); + } } void UploadFileService::handleEarlyDisconnect() { diff --git a/lib/framework/WiFiScanner.cpp b/lib/framework/WiFiScanner.cpp index b7ac8f7..cb441e6 100644 --- a/lib/framework/WiFiScanner.cpp +++ b/lib/framework/WiFiScanner.cpp @@ -12,11 +12,12 @@ WiFiScanner::WiFiScanner(AsyncWebServer * server, SecurityManager * securityMana }; void WiFiScanner::scanNetworks(AsyncWebServerRequest * request) { + request->send(202); // special code to indicate scan in progress + if (WiFi.scanComplete() != -1) { WiFi.scanDelete(); WiFi.scanNetworks(true); } - request->send(202); } void WiFiScanner::listNetworks(AsyncWebServerRequest * request) {