From 2033448f65264320e914fc059c9088ebd5c4639a Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Sun, 13 Feb 2022 01:05:02 +0100 Subject: [PATCH] Fixed webserver locks --- main/webserver.cpp | 89 ++++++++++++++++++------------- main/webserver_displaycontrol.cpp | 45 ---------------- main/webserver_dumpnvs.cpp | 9 ---- main/webserver_lock.cpp | 2 - main/webserver_lock.h | 2 - main/webserver_newsettings.cpp | 28 ---------- main/webserver_ota.cpp | 27 ---------- main/webserver_settings.cpp | 18 ------- 8 files changed, 53 insertions(+), 167 deletions(-) diff --git a/main/webserver.cpp b/main/webserver.cpp index 3b00fc4..59db95c 100644 --- a/main/webserver.cpp +++ b/main/webserver.cpp @@ -40,16 +40,42 @@ std::vector> menuBuf{}; esp_err_t webserver_reboot_handler(httpd_req_t *req); bool menuDisplayChanged(); esp_err_t webserver_status_handler(httpd_req_t *req); -} // namespace bobbywebserver + +esp_err_t webserver_middleware_handler(httpd_req_t *req) { + const auto handler = reinterpret_cast(req->user_ctx); + + if (configs.feature.webserver_disable_lock.value) + { + return handler(req); + } + + if (!webserver_lock.constructed()) + { + webserver_lock.construct(); + webserver_lock->take(portMAX_DELAY); + } + + espcpputils::LockHelper helper{webserver_lock->handle, std::chrono::ceil(5s).count()}; + if (!helper.locked()) + { + constexpr const std::string_view msg = "could not lock webserver_lock"; + ESP_LOGE(TAG, "%.*s", msg.size(), msg.data()); + CALL_AND_EXIT(esphttpdutils::webserver_resp_send, req, esphttpdutils::ResponseStatus::BadRequest, "text/plain", msg); + } + + return handler(req); +} +} // namespace httpd_handle_t httpdHandle; void initWebserver() { -#ifndef FEATURE_IS_MIR_EGAL_OB_DER_WEBSERVER_KORREKT_ARBEITET - webserver_lock.construct(); - webserver_lock->take(portMAX_DELAY); -#endif + if(!configs.feature.webserver_disable_lock.value) + { + webserver_lock.construct(); + webserver_lock->take(portMAX_DELAY); + } { httpd_config_t httpConfig HTTPD_DEFAULT_CONFIG(); @@ -64,22 +90,22 @@ void initWebserver() } for (const httpd_uri_t &uri : { - httpd_uri_t { .uri = "/", .method = HTTP_GET, .handler = webserver_root_handler, .user_ctx = NULL }, - httpd_uri_t { .uri = "/triggerRawButton", .method = HTTP_GET, .handler = webserver_triggerRawButton_handler, .user_ctx = NULL }, - httpd_uri_t { .uri = "/triggerButton", .method = HTTP_GET, .handler = webserver_triggerButton_handler, .user_ctx = NULL }, - httpd_uri_t { .uri = "/triggerItem", .method = HTTP_GET, .handler = webserver_triggerItem_handler, .user_ctx = NULL }, - httpd_uri_t { .uri = "/setValue", .method = HTTP_GET, .handler = webserver_setValue_handler, .user_ctx = NULL }, - httpd_uri_t { .uri = "/reboot", .method = HTTP_GET, .handler = webserver_reboot_handler, .user_ctx = NULL }, - httpd_uri_t { .uri = "/ota", .method = HTTP_GET, .handler = webserver_ota_handler, .user_ctx = NULL }, - httpd_uri_t { .uri = "/otaPercent", .method = HTTP_GET, .handler = webserver_ota_percentage_handler, .user_ctx = NULL }, - httpd_uri_t { .uri = "/triggerOta", .method = HTTP_GET, .handler = webserver_trigger_ota_handler, .user_ctx = NULL }, - httpd_uri_t { .uri = "/settings", .method = HTTP_GET, .handler = webserver_settings_handler, .user_ctx = NULL }, - httpd_uri_t { .uri = "/saveSettings", .method = HTTP_GET, .handler = webserver_saveSettings_handler, .user_ctx = NULL }, - httpd_uri_t { .uri = "/newSettings", .method = HTTP_GET, .handler = webserver_newSettings_handler, .user_ctx = NULL }, - httpd_uri_t { .uri = "/saveNewSettings", .method = HTTP_GET, .handler = webserver_saveNewSettings_handler, .user_ctx = NULL }, - httpd_uri_t { .uri = "/resetNewSettings", .method = HTTP_GET, .handler = webserver_resetNewSettings_handler, .user_ctx = NULL }, - httpd_uri_t { .uri = "/dumpnvs", .method = HTTP_GET, .handler = webserver_dump_nvs_handler, .user_ctx = NULL }, - httpd_uri_t { .uri = "/check", .method = HTTP_GET, .handler = webserver_status_handler, .user_ctx = NULL }, + httpd_uri_t { .uri = "/", .method = HTTP_GET, .handler = webserver_middleware_handler, .user_ctx = (void*)&webserver_root_handler, }, + httpd_uri_t { .uri = "/triggerRawButton", .method = HTTP_GET, .handler = webserver_middleware_handler, .user_ctx = (void*)&webserver_triggerRawButton_handler, }, + httpd_uri_t { .uri = "/triggerButton", .method = HTTP_GET, .handler = webserver_middleware_handler, .user_ctx = (void*)&webserver_triggerButton_handler, }, + httpd_uri_t { .uri = "/triggerItem", .method = HTTP_GET, .handler = webserver_middleware_handler, .user_ctx = (void*)&webserver_triggerItem_handler, }, + httpd_uri_t { .uri = "/setValue", .method = HTTP_GET, .handler = webserver_middleware_handler, .user_ctx = (void*)&webserver_setValue_handler, }, + httpd_uri_t { .uri = "/reboot", .method = HTTP_GET, .handler = webserver_middleware_handler, .user_ctx = (void*)&webserver_reboot_handler, }, + httpd_uri_t { .uri = "/ota", .method = HTTP_GET, .handler = webserver_middleware_handler, .user_ctx = (void*)&webserver_ota_handler, }, + httpd_uri_t { .uri = "/otaPercent", .method = HTTP_GET, .handler = webserver_middleware_handler, .user_ctx = (void*)&webserver_ota_percentage_handler, }, + httpd_uri_t { .uri = "/triggerOta", .method = HTTP_GET, .handler = webserver_middleware_handler, .user_ctx = (void*)&webserver_trigger_ota_handler, }, + httpd_uri_t { .uri = "/settings", .method = HTTP_GET, .handler = webserver_middleware_handler, .user_ctx = (void*)&webserver_settings_handler, }, + httpd_uri_t { .uri = "/saveSettings", .method = HTTP_GET, .handler = webserver_middleware_handler, .user_ctx = (void*)&webserver_saveSettings_handler, }, + httpd_uri_t { .uri = "/newSettings", .method = HTTP_GET, .handler = webserver_middleware_handler, .user_ctx = (void*)&webserver_newSettings_handler, }, + httpd_uri_t { .uri = "/saveNewSettings", .method = HTTP_GET, .handler = webserver_middleware_handler, .user_ctx = (void*)&webserver_saveNewSettings_handler, }, + httpd_uri_t { .uri = "/resetNewSettings", .method = HTTP_GET, .handler = webserver_middleware_handler, .user_ctx = (void*)&webserver_resetNewSettings_handler, }, + httpd_uri_t { .uri = "/dumpnvs", .method = HTTP_GET, .handler = webserver_middleware_handler, .user_ctx = (void*)&webserver_dump_nvs_handler, }, + httpd_uri_t { .uri = "/check", .method = HTTP_GET, .handler = webserver_middleware_handler, .user_ctx = (void*)&webserver_status_handler, }, }) { const auto result = httpd_register_uri_handler(httpdHandle, &uri); @@ -91,11 +117,12 @@ void initWebserver() void handleWebserver() { -#ifndef FEATURE_IS_MIR_EGAL_OB_DER_WEBSERVER_KORREKT_ARBEITET - webserver_lock->give(); - vTaskDelay(1); - webserver_lock->take(portMAX_DELAY); -#endif + if (!configs.feature.webserver_disable_lock.value) + { + webserver_lock->give(); + vTaskDelay(1); + webserver_lock->take(portMAX_DELAY); + } } namespace { @@ -156,16 +183,6 @@ bool menuDisplayChanged() esp_err_t webserver_status_handler(httpd_req_t *req) { -#ifndef FEATURE_IS_MIR_EGAL_OB_DER_WEBSERVER_KORREKT_ARBEITET - espcpputils::LockHelper helper{webserver_lock->handle, std::chrono::ceil(5s).count()}; - if (!helper.locked()) - { - constexpr const std::string_view msg = "could not lock webserver_lock"; - ESP_LOGE(TAG, "%.*s", msg.size(), msg.data()); - CALL_AND_EXIT(esphttpdutils::webserver_resp_send, req, esphttpdutils::ResponseStatus::BadRequest, "text/plain", msg); - } -#endif - CALL_AND_EXIT_ON_ERROR(httpd_resp_set_hdr, req, "Access-Control-Allow-Origin", "http://web.bobbycar.cloud"); std::string wants_json_query; diff --git a/main/webserver_displaycontrol.cpp b/main/webserver_displaycontrol.cpp index 34d4bff..bd92d52 100644 --- a/main/webserver_displaycontrol.cpp +++ b/main/webserver_displaycontrol.cpp @@ -33,15 +33,6 @@ constexpr const char * const TAG = "BOBBYWEB"; esp_err_t webserver_root_handler(httpd_req_t *req) { -#ifndef FEATURE_IS_MIR_EGAL_OB_DER_WEBSERVER_KORREKT_ARBEITET - espcpputils::LockHelper helper{webserver_lock->handle, std::chrono::ceil(5s).count()}; - if (!helper.locked()) - { - constexpr const std::string_view msg = "could not lock webserver_lock"; - ESP_LOGE(TAG, "%.*s", msg.size(), msg.data()); - CALL_AND_EXIT(esphttpdutils::webserver_resp_send, req, esphttpdutils::ResponseStatus::BadRequest, "text/plain", msg); - } -#endif std::string body; @@ -282,15 +273,6 @@ esp_err_t webserver_root_handler(httpd_req_t *req) esp_err_t webserver_triggerRawButton_handler(httpd_req_t *req) { -#ifndef FEATURE_IS_MIR_EGAL_OB_DER_WEBSERVER_KORREKT_ARBEITET - espcpputils::LockHelper helper{webserver_lock->handle, std::chrono::ceil(5s).count()}; - if (!helper.locked()) - { - constexpr const std::string_view msg = "could not lock webserver_lock"; - ESP_LOGE(TAG, "%.*s", msg.size(), msg.data()); - CALL_AND_EXIT(esphttpdutils::webserver_resp_send, req, esphttpdutils::ResponseStatus::BadRequest, "text/plain", msg); - } -#endif std::string query; if (auto result = esphttpdutils::webserver_get_query(req)) @@ -356,15 +338,6 @@ esp_err_t webserver_triggerRawButton_handler(httpd_req_t *req) esp_err_t webserver_triggerButton_handler(httpd_req_t *req) { -#ifndef FEATURE_IS_MIR_EGAL_OB_DER_WEBSERVER_KORREKT_ARBEITET - espcpputils::LockHelper helper{webserver_lock->handle, std::chrono::ceil(5s).count()}; - if (!helper.locked()) - { - constexpr const std::string_view msg = "could not lock webserver_lock"; - ESP_LOGE(TAG, "%.*s", msg.size(), msg.data()); - CALL_AND_EXIT(esphttpdutils::webserver_resp_send, req, esphttpdutils::ResponseStatus::BadRequest, "text/plain", msg); - } -#endif std::string query; if (auto result = esphttpdutils::webserver_get_query(req)) @@ -430,15 +403,6 @@ esp_err_t webserver_triggerButton_handler(httpd_req_t *req) esp_err_t webserver_triggerItem_handler(httpd_req_t *req) { -#ifndef FEATURE_IS_MIR_EGAL_OB_DER_WEBSERVER_KORREKT_ARBEITET - espcpputils::LockHelper helper{webserver_lock->handle, std::chrono::ceil(5s).count()}; - if (!helper.locked()) - { - constexpr const std::string_view msg = "could not lock webserver_lock"; - ESP_LOGE(TAG, "%.*s", msg.size(), msg.data()); - CALL_AND_EXIT(esphttpdutils::webserver_resp_send, req, esphttpdutils::ResponseStatus::BadRequest, "text/plain", msg); - } -#endif CALL_AND_EXIT_ON_ERROR(httpd_resp_set_hdr, req, "Access-Control-Allow-Origin", "http://web.bobbycar.cloud"); @@ -520,15 +484,6 @@ esp_err_t webserver_triggerItem_handler(httpd_req_t *req) esp_err_t webserver_setValue_handler(httpd_req_t *req) { -#ifndef FEATURE_IS_MIR_EGAL_OB_DER_WEBSERVER_KORREKT_ARBEITET - espcpputils::LockHelper helper{webserver_lock->handle, std::chrono::ceil(5s).count()}; - if (!helper.locked()) - { - constexpr const std::string_view msg = "could not lock webserver_lock"; - ESP_LOGE(TAG, "%.*s", msg.size(), msg.data()); - CALL_AND_EXIT(esphttpdutils::webserver_resp_send, req, esphttpdutils::ResponseStatus::BadRequest, "text/plain", msg); - } -#endif CALL_AND_EXIT_ON_ERROR(httpd_resp_set_hdr, req, "Access-Control-Allow-Origin", "http://web.bobbycar.cloud"); diff --git a/main/webserver_dumpnvs.cpp b/main/webserver_dumpnvs.cpp index 6a8ef47..0eaa6e8 100644 --- a/main/webserver_dumpnvs.cpp +++ b/main/webserver_dumpnvs.cpp @@ -128,15 +128,6 @@ showInputForSetting(std::string_view key, T value, JsonObject &body) esp_err_t webserver_dump_nvs_handler(httpd_req_t *req) { -#ifndef FEATURE_IS_MIR_EGAL_OB_DER_WEBSERVER_KORREKT_ARBEITET - espcpputils::LockHelper helper{webserver_lock->handle, std::chrono::ceil(5s).count()}; - if (!helper.locked()) - { - constexpr const std::string_view msg = "could not lock webserver_lock"; - ESP_LOGE(TAG, "%.*s", msg.size(), msg.data()); - CALL_AND_EXIT(esphttpdutils::webserver_resp_send, req, esphttpdutils::ResponseStatus::BadRequest, "text/plain", msg); - } -#endif DynamicJsonDocument doc(6144); diff --git a/main/webserver_lock.cpp b/main/webserver_lock.cpp index c2215f9..ae0dbe1 100644 --- a/main/webserver_lock.cpp +++ b/main/webserver_lock.cpp @@ -1,5 +1,3 @@ #include "webserver_lock.h" -#ifndef FEATURE_IS_MIR_EGAL_OB_DER_WEBSERVER_KORREKT_ARBEITET cpputils::DelayedConstruction webserver_lock; -#endif diff --git a/main/webserver_lock.h b/main/webserver_lock.h index e6f2429..ddb94b5 100644 --- a/main/webserver_lock.h +++ b/main/webserver_lock.h @@ -4,6 +4,4 @@ #include #include -#ifndef FEATURE_IS_MIR_EGAL_OB_DER_WEBSERVER_KORREKT_ARBEITET extern cpputils::DelayedConstruction webserver_lock; -#endif diff --git a/main/webserver_newsettings.cpp b/main/webserver_newsettings.cpp index 35036d7..f88bc23 100644 --- a/main/webserver_newsettings.cpp +++ b/main/webserver_newsettings.cpp @@ -259,16 +259,6 @@ showInputForSetting(std::string_view key, T value, std::string &body) esp_err_t webserver_newSettings_handler(httpd_req_t *req) { -#ifndef FEATURE_IS_MIR_EGAL_OB_DER_WEBSERVER_KORREKT_ARBEITET - ESP_LOGI(TAG, "trying to lock..."); - espcpputils::LockHelper helper{webserver_lock->handle, std::chrono::ceil(5s).count()}; - if (!helper.locked()) - { - constexpr const std::string_view msg = "could not lock webserver_lock"; - ESP_LOGE(TAG, "%.*s", msg.size(), msg.data()); - CALL_AND_EXIT(esphttpdutils::webserver_resp_send, req, esphttpdutils::ResponseStatus::BadRequest, "text/plain", msg); - } -#endif std::string body; @@ -502,15 +492,6 @@ saveSetting(ConfigWrapper &config, std::string_view newValue) esp_err_t webserver_saveNewSettings_handler(httpd_req_t *req) { -#ifndef FEATURE_IS_MIR_EGAL_OB_DER_WEBSERVER_KORREKT_ARBEITET - espcpputils::LockHelper helper{webserver_lock->handle, std::chrono::ceil(5s).count()}; - if (!helper.locked()) - { - constexpr const std::string_view msg = "could not lock webserver_lock"; - ESP_LOGE(TAG, "%.*s", msg.size(), msg.data()); - CALL_AND_EXIT(esphttpdutils::webserver_resp_send, req, esphttpdutils::ResponseStatus::BadRequest, "text/plain", msg); - } -#endif std::string query; if (auto result = esphttpdutils::webserver_get_query(req)) @@ -571,15 +552,6 @@ esp_err_t webserver_saveNewSettings_handler(httpd_req_t *req) esp_err_t webserver_resetNewSettings_handler(httpd_req_t *req) { -#ifndef FEATURE_IS_MIR_EGAL_OB_DER_WEBSERVER_KORREKT_ARBEITET - espcpputils::LockHelper helper{webserver_lock->handle, std::chrono::ceil(5s).count()}; - if (!helper.locked()) - { - constexpr const std::string_view msg = "could not lock webserver_lock"; - ESP_LOGE(TAG, "%.*s", msg.size(), msg.data()); - CALL_AND_EXIT(esphttpdutils::webserver_resp_send, req, esphttpdutils::ResponseStatus::BadRequest, "text/plain", msg); - } -#endif std::string query; if (auto result = esphttpdutils::webserver_get_query(req)) diff --git a/main/webserver_ota.cpp b/main/webserver_ota.cpp index 407036c..fa3038a 100644 --- a/main/webserver_ota.cpp +++ b/main/webserver_ota.cpp @@ -29,15 +29,6 @@ constexpr const char * const TAG = "BOBBYWEB"; esp_err_t webserver_ota_percentage_handler(httpd_req_t *req) { -#ifndef FEATURE_IS_MIR_EGAL_OB_DER_WEBSERVER_KORREKT_ARBEITET - espcpputils::LockHelper helper{webserver_lock->handle, std::chrono::ceil(5s).count()}; - if (!helper.locked()) - { - constexpr const std::string_view msg = "could not lock webserver_lock"; - ESP_LOGE(TAG, "%.*s", msg.size(), msg.data()); - CALL_AND_EXIT(esphttpdutils::webserver_resp_send, req, esphttpdutils::ResponseStatus::BadRequest, "text/plain", msg); - } -#endif CALL_AND_EXIT_ON_ERROR(httpd_resp_set_hdr, req, "Access-Control-Allow-Origin", "http://web.bobbycar.cloud"); @@ -90,15 +81,6 @@ esp_err_t webserver_ota_percentage_handler(httpd_req_t *req) esp_err_t webserver_ota_handler(httpd_req_t *req) { -#ifndef FEATURE_IS_MIR_EGAL_OB_DER_WEBSERVER_KORREKT_ARBEITET - espcpputils::LockHelper helper{webserver_lock->handle, std::chrono::ceil(5s).count()}; - if (!helper.locked()) - { - constexpr const std::string_view msg = "could not lock webserver_lock"; - ESP_LOGE(TAG, "%.*s", msg.size(), msg.data()); - CALL_AND_EXIT(esphttpdutils::webserver_resp_send, req, esphttpdutils::ResponseStatus::BadRequest, "text/plain", msg); - } -#endif CALL_AND_EXIT_ON_ERROR(httpd_resp_set_hdr, req, "Access-Control-Allow-Origin", "http://web.bobbycar.cloud"); @@ -360,15 +342,6 @@ esp_err_t webserver_ota_handler(httpd_req_t *req) esp_err_t webserver_trigger_ota_handler(httpd_req_t *req) { -#ifndef FEATURE_IS_MIR_EGAL_OB_DER_WEBSERVER_KORREKT_ARBEITET - espcpputils::LockHelper helper{webserver_lock->handle, std::chrono::ceil(5s).count()}; - if (!helper.locked()) - { - constexpr const std::string_view msg = "could not lock webserver_lock"; - ESP_LOGE(TAG, "%.*s", msg.size(), msg.data()); - CALL_AND_EXIT(esphttpdutils::webserver_resp_send, req, esphttpdutils::ResponseStatus::BadRequest, "text/plain", msg); - } -#endif CALL_AND_EXIT_ON_ERROR(httpd_resp_set_hdr, req, "Access-Control-Allow-Origin", "http://web.bobbycar.cloud"); diff --git a/main/webserver_settings.cpp b/main/webserver_settings.cpp index a93817f..a805a47 100644 --- a/main/webserver_settings.cpp +++ b/main/webserver_settings.cpp @@ -86,15 +86,6 @@ showInputForSetting(std::string_view key, T value, std::string &body) esp_err_t webserver_settings_handler(httpd_req_t *req) { -#ifndef FEATURE_IS_MIR_EGAL_OB_DER_WEBSERVER_KORREKT_ARBEITET - espcpputils::LockHelper helper{webserver_lock->handle, std::chrono::ceil(5s).count()}; - if (!helper.locked()) - { - constexpr const std::string_view msg = "could not lock webserver_lock"; - ESP_LOGE(TAG, "%.*s", msg.size(), msg.data()); - CALL_AND_EXIT(esphttpdutils::webserver_resp_send, req, esphttpdutils::ResponseStatus::BadRequest, "text/plain", msg); - } -#endif std::string body; @@ -252,15 +243,6 @@ saveSetting(T &value, std::string_view newValue, std::string &body) esp_err_t webserver_saveSettings_handler(httpd_req_t *req) { -#ifndef FEATURE_IS_MIR_EGAL_OB_DER_WEBSERVER_KORREKT_ARBEITET - espcpputils::LockHelper helper{webserver_lock->handle, std::chrono::ceil(5s).count()}; - if (!helper.locked()) - { - constexpr const std::string_view msg = "could not lock webserver_lock"; - ESP_LOGE(TAG, "%.*s", msg.size(), msg.data()); - CALL_AND_EXIT(esphttpdutils::webserver_resp_send, req, esphttpdutils::ResponseStatus::BadRequest, "text/plain", msg); - } -#endif std::string query; if (auto result = esphttpdutils::webserver_get_query(req))