From eefdc74e9ae64f9cc62f284440d4bf2c4b62139a Mon Sep 17 00:00:00 2001 From: Slider0007 <115730895+Slider0007@users.noreply.github.com> Date: Tue, 10 Jan 2023 19:37:30 +0100 Subject: [PATCH] Rest handler: Use none chunked transfer (#1797) * Resthandler info,sysinfo,starttime: no chunk trans * flowstart,statusflow,cputemp,rssi,uptime,prevalue * Renamed error messages --- .../jomjol_controlcamera/server_camera.cpp | 10 +-- .../jomjol_tfliteclass/server_tflite.cpp | 74 +++++++----------- code/main/server_main.cpp | 77 ++++++++----------- 3 files changed, 64 insertions(+), 97 deletions(-) diff --git a/code/components/jomjol_controlcamera/server_camera.cpp b/code/components/jomjol_controlcamera/server_camera.cpp index 2cac56b2..1d082947 100644 --- a/code/components/jomjol_controlcamera/server_camera.cpp +++ b/code/components/jomjol_controlcamera/server_camera.cpp @@ -48,7 +48,7 @@ esp_err_t handler_lightOn(httpd_req_t *req) } else { - httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Camera Light On API not yet initialized. Please retry later..."); + httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Camera not initialized: REST API /lighton not available!"); return ESP_ERR_NOT_FOUND; } @@ -75,7 +75,7 @@ esp_err_t handler_lightOff(httpd_req_t *req) } else { - httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Camera Light Off API not yet initialized. Please retry later..."); + httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Camera not initialized: REST API /lightoff not available!"); return ESP_ERR_NOT_FOUND; } @@ -117,7 +117,7 @@ esp_err_t handler_capture(httpd_req_t *req) } else { - httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Camera Capture API not yet initialized. Please retry later..."); + httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Camera not initialized: REST API /capture not available!"); return ESP_ERR_NOT_FOUND; } } @@ -177,7 +177,7 @@ esp_err_t handler_capture_with_ligth(httpd_req_t *req) } else { - httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Camera Capture + flashlight API not yet initialized. Please retry later..."); + httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Camera not initialized: REST API /capture_with_flashlight not available!"); return ESP_ERR_NOT_FOUND; } } @@ -248,7 +248,7 @@ esp_err_t handler_capture_save_to_file(httpd_req_t *req) } else { - httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Camera Capture + save API not yet initialized. Please retry later..."); + httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Camera not initialized: REST API /save not available!"); return ESP_ERR_NOT_FOUND; } } diff --git a/code/components/jomjol_tfliteclass/server_tflite.cpp b/code/components/jomjol_tfliteclass/server_tflite.cpp index e97cca22..1c798457 100644 --- a/code/components/jomjol_tfliteclass/server_tflite.cpp +++ b/code/components/jomjol_tfliteclass/server_tflite.cpp @@ -38,6 +38,8 @@ bool isPlannedReboot = false; static const char *TAG = "TFLITE SERVER"; +//#define DEBUG_DETAIL_ON + void CheckIsPlannedReboot() { @@ -156,6 +158,8 @@ esp_err_t handler_get_heap(httpd_req_t *req) } #endif + httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*"); + if (zw.length() > 0) { httpd_resp_send(req, zw.c_str(), zw.length()); @@ -180,16 +184,14 @@ esp_err_t handler_init(httpd_req_t *req) ESP_LOGD(TAG, "handler_doinit uri: %s", req->uri); #endif + httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*"); const char* resp_str = "Init started
"; - httpd_resp_send(req, resp_str, strlen(resp_str)); + httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN); doInit(); resp_str = "Init done
"; - httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*"); - httpd_resp_send(req, resp_str, strlen(resp_str)); - /* Respond with an empty chunk to signal HTTP response completion */ - httpd_resp_send_chunk(req, NULL, 0); + httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN); #ifdef DEBUG_DETAIL_ON LogFile.WriteHeapInfo("handler_init - Done"); @@ -207,23 +209,20 @@ esp_err_t handler_flow_start(httpd_req_t *req) { ESP_LOGD(TAG, "handler_flow_start uri: %s", req->uri); + httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*"); + if (auto_isrunning) { xTaskAbortDelay(xHandletask_autodoFlow); // Delay will be aborted if task is in blocked (waiting) state. If task is already running, no action LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Flow start triggered by REST API /flow_start"); const char* resp_str = "The flow is going to be started immediately or is already running"; - httpd_resp_send(req, resp_str, strlen(resp_str)); + httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN); } else { LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Flow start triggered by REST API, but flow is not active!"); const char* resp_str = "WARNING: Flow start triggered by REST API, but flow is not active"; - httpd_resp_send(req, resp_str, strlen(resp_str)); + httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN); } - httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*"); - - /* Respond with an empty chunk to signal HTTP response completion */ - httpd_resp_send_chunk(req, NULL, 0); - #ifdef DEBUG_DETAIL_ON LogFile.WriteHeapInfo("handler_flow_start - Done"); #endif @@ -285,7 +284,7 @@ esp_err_t handler_json(httpd_req_t *req) } else { - httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "JSON API not yet initialized. Please retry later..."); + httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Flow not (yet) started: REST API /json not yet available!"); return ESP_ERR_NOT_FOUND; } @@ -369,8 +368,8 @@ esp_err_t handler_wasserzaehler(httpd_req_t *req) zw = tfliteflow.getReadoutAll(_intype); ESP_LOGD(TAG, "ZW: %s", zw.c_str()); if (zw.length() > 0) - httpd_resp_sendstr_chunk(req, zw.c_str()); - httpd_resp_sendstr_chunk(req, NULL); + httpd_resp_send(req, zw.c_str(), zw.length()); + return ESP_OK; } @@ -439,7 +438,7 @@ esp_err_t handler_wasserzaehler(httpd_req_t *req) } else { - httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Value API not yet initialized. Please retry later..."); + httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Flow not (yet) started: REST API /value not available!"); return ESP_ERR_NOT_FOUND; } @@ -514,7 +513,7 @@ esp_err_t handler_editflow(httpd_req_t *req) CopyFile(in, out); zw = "Copy Done"; - httpd_resp_sendstr_chunk(req, zw.c_str()); + httpd_resp_send(req, zw.c_str(), zw.length()); } @@ -583,7 +582,7 @@ esp_err_t handler_editflow(httpd_req_t *req) delete cim; zw = "CutImage Done"; - httpd_resp_sendstr_chunk(req, zw.c_str()); + httpd_resp_send(req, zw.c_str(), zw.length()); } @@ -626,8 +625,7 @@ esp_err_t handler_editflow(httpd_req_t *req) Camera.SetLEDIntensity(intens); ESP_LOGD(TAG, "test_take - vor MakeImage"); std::string zw = tfliteflow.doSingleStep("[MakeImage]", _host); - httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*"); - httpd_resp_sendstr_chunk(req, zw.c_str()); + httpd_resp_send(req, zw.c_str(), zw.length()); } @@ -641,12 +639,9 @@ esp_err_t handler_editflow(httpd_req_t *req) // string zwzw = "Do " + _task + " start\n"; ESP_LOGD(TAG, zwzw.c_str()); std::string zw = tfliteflow.doSingleStep("[Alignment]", _host); - httpd_resp_sendstr_chunk(req, zw.c_str()); + httpd_resp_send(req, zw.c_str(), zw.length()); } - /* Respond with an empty chunk to signal HTTP response completion */ - httpd_resp_sendstr_chunk(req, NULL); - #ifdef DEBUG_DETAIL_ON LogFile.WriteHeapInfo("handler_editflow - Done"); #endif @@ -673,13 +668,11 @@ esp_err_t handler_statusflow(httpd_req_t *req) resp_str = zw->c_str(); httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*"); - httpd_resp_send(req, resp_str, strlen(resp_str)); - /* Respond with an empty chunk to signal HTTP response completion */ - httpd_resp_send_chunk(req, NULL, 0); + httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN); } else { - httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Flowstatus API not yet initialized. Please retry later..."); + httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Flow not (yet) started: REST API /flowstatus not available!"); return ESP_ERR_NOT_FOUND; } @@ -705,9 +698,7 @@ esp_err_t handler_cputemp(httpd_req_t *req) resp_str = cputemp; httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*"); - httpd_resp_send(req, resp_str, strlen(resp_str)); - /* Respond with an empty chunk to signal HTTP response completion */ - httpd_resp_send_chunk(req, NULL, 0); + httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN); #ifdef DEBUG_DETAIL_ON LogFile.WriteHeapInfo("handler_cputemp - End"); @@ -733,13 +724,11 @@ esp_err_t handler_rssi(httpd_req_t *req) resp_str = rssi; httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*"); - httpd_resp_send(req, resp_str, strlen(resp_str)); - /* Respond with an empty chunk to signal HTTP response completion */ - httpd_resp_send_chunk(req, NULL, 0); + httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN); } else { - httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "RSSI API not yet initialized. Please retry later..."); + httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "WIFI not (yet) connected: REST API /rssi not available!"); return ESP_ERR_NOT_FOUND; } @@ -761,9 +750,7 @@ esp_err_t handler_uptime(httpd_req_t *req) std::string formatedUptime = getFormatedUptime(false); httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*"); - httpd_resp_send(req, formatedUptime.c_str(), strlen(formatedUptime.c_str())); - /* Respond with an empty chunk to signal HTTP response completion */ - httpd_resp_send_chunk(req, NULL, 0); + httpd_resp_send(req, formatedUptime.c_str(), formatedUptime.length()); #ifdef DEBUG_DETAIL_ON LogFile.WriteHeapInfo("handler_uptime - End"); @@ -792,9 +779,9 @@ esp_err_t handler_prevalue(httpd_req_t *req) if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK) { - #ifdef DEBUG_DETAIL_ON + #ifdef DEBUG_DETAIL_ON ESP_LOGD(TAG, "Query: %s", _query); - #endif + #endif if (httpd_query_key_value(_query, "value", _size, 10) == ESP_OK) { @@ -818,11 +805,8 @@ esp_err_t handler_prevalue(httpd_req_t *req) resp_str = zw.c_str(); httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*"); - - - httpd_resp_send(req, resp_str, strlen(resp_str)); - /* Respond with an empty chunk to signal HTTP response completion */ - httpd_resp_send_chunk(req, NULL, 0); + + httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN); #ifdef DEBUG_DETAIL_ON LogFile.WriteHeapInfo("handler_prevalue - End"); diff --git a/code/main/server_main.cpp b/code/main/server_main.cpp index 17556050..0779630b 100644 --- a/code/main/server_main.cpp +++ b/code/main/server_main.cpp @@ -46,155 +46,139 @@ esp_err_t info_get_handler(httpd_req_t *req) ESP_LOGD(TAG, "type is found: %s", _valuechar); _task = std::string(_valuechar); } - }; + } httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*"); if (_task.compare("GitBranch") == 0) { - httpd_resp_sendstr_chunk(req, libfive_git_branch()); - httpd_resp_sendstr_chunk(req, NULL); + httpd_resp_sendstr(req, libfive_git_branch()); return ESP_OK; } else if (_task.compare("GitTag") == 0) { - httpd_resp_sendstr_chunk(req, libfive_git_version()); - httpd_resp_sendstr_chunk(req, NULL); + httpd_resp_sendstr(req, libfive_git_version()); return ESP_OK; } else if (_task.compare("GitRevision") == 0) { - httpd_resp_sendstr_chunk(req, libfive_git_revision()); - httpd_resp_sendstr_chunk(req, NULL); + httpd_resp_sendstr(req, libfive_git_revision()); return ESP_OK; } else if (_task.compare("BuildTime") == 0) { - httpd_resp_sendstr_chunk(req, build_time()); - httpd_resp_sendstr_chunk(req, NULL); + httpd_resp_sendstr(req, build_time()); return ESP_OK; } else if (_task.compare("FirmwareVersion") == 0) { - httpd_resp_sendstr_chunk(req, getFwVersion().c_str()); - httpd_resp_sendstr_chunk(req, NULL); + httpd_resp_sendstr(req, getFwVersion().c_str()); return ESP_OK; } else if (_task.compare("HTMLVersion") == 0) { - httpd_resp_sendstr_chunk(req, getHTMLversion().c_str()); - httpd_resp_sendstr_chunk(req, NULL); + httpd_resp_sendstr(req, getHTMLversion().c_str()); return ESP_OK; } else if (_task.compare("Hostname") == 0) { std::string zw; zw = std::string(hostname); - httpd_resp_sendstr_chunk(req, zw.c_str()); - httpd_resp_sendstr_chunk(req, NULL); + httpd_resp_sendstr(req, zw.c_str()); return ESP_OK; } else if (_task.compare("IP") == 0) { std::string *zw; zw = getIPAddress(); - httpd_resp_sendstr_chunk(req, zw->c_str()); - httpd_resp_sendstr_chunk(req, NULL); + httpd_resp_sendstr(req, zw->c_str()); return ESP_OK; } else if (_task.compare("SSID") == 0) { std::string *zw; zw = getSSID(); - httpd_resp_sendstr_chunk(req, zw->c_str()); - httpd_resp_sendstr_chunk(req, NULL); + httpd_resp_sendstr(req, zw->c_str()); return ESP_OK; } else if (_task.compare("FlowStatus") == 0) { std::string zw; zw = std::string("FlowStatus"); - httpd_resp_sendstr_chunk(req, zw.c_str()); - httpd_resp_sendstr_chunk(req, NULL); + httpd_resp_sendstr(req, zw.c_str()); return ESP_OK; } else if (_task.compare("Round") == 0) { char formated[10] = ""; snprintf(formated, sizeof(formated), "%d", getCountFlowRounds()); - httpd_resp_sendstr_chunk(req, formated); - httpd_resp_sendstr_chunk(req, NULL); + httpd_resp_sendstr(req, formated); return ESP_OK; } else if (_task.compare("SDCardPartitionSize") == 0) { std::string zw; zw = getSDCardPartitionSize(); - httpd_resp_sendstr_chunk(req, zw.c_str()); - httpd_resp_sendstr_chunk(req, NULL); + httpd_resp_sendstr(req, zw.c_str()); return ESP_OK; } else if (_task.compare("SDCardFreePartitionSpace") == 0) { std::string zw; zw = getSDCardFreePartitionSpace(); - httpd_resp_sendstr_chunk(req, zw.c_str()); - httpd_resp_sendstr_chunk(req, NULL); + httpd_resp_sendstr(req, zw.c_str()); return ESP_OK; } else if (_task.compare("SDCardPartitionAllocationSize") == 0) { std::string zw; zw = getSDCardPartitionAllocationSize(); - httpd_resp_sendstr_chunk(req, zw.c_str()); - httpd_resp_sendstr_chunk(req, NULL); + httpd_resp_sendstr(req, zw.c_str()); return ESP_OK; } else if (_task.compare("SDCardManufacturer") == 0) { std::string zw; zw = getSDCardManufacturer(); - httpd_resp_sendstr_chunk(req, zw.c_str()); - httpd_resp_sendstr_chunk(req, NULL); + httpd_resp_sendstr(req, zw.c_str()); return ESP_OK; } else if (_task.compare("SDCardName") == 0) { std::string zw; zw = getSDCardName(); - httpd_resp_sendstr_chunk(req, zw.c_str()); - httpd_resp_sendstr_chunk(req, NULL); + httpd_resp_sendstr(req, zw.c_str()); return ESP_OK; } else if (_task.compare("SDCardCapacity") == 0) { std::string zw; zw = getSDCardCapacity(); - httpd_resp_sendstr_chunk(req, zw.c_str()); - httpd_resp_sendstr_chunk(req, NULL); + httpd_resp_sendstr(req, zw.c_str()); return ESP_OK; } else if (_task.compare("SDCardSectorSize") == 0) { std::string zw; zw = getSDCardSectorSize(); - httpd_resp_sendstr_chunk(req, zw.c_str()); - httpd_resp_sendstr_chunk(req, NULL); + httpd_resp_sendstr(req, zw.c_str()); return ESP_OK; } return ESP_OK; } + esp_err_t starttime_get_handler(httpd_req_t *req) { - httpd_resp_send(req, starttime.c_str(), strlen(starttime.c_str())); - /* Respond with an empty chunk to signal HTTP response completion */ - httpd_resp_send_chunk(req, NULL, 0); + httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*"); + + httpd_resp_send(req, starttime.c_str(), starttime.length()); return ESP_OK; } + esp_err_t hello_main_handler(httpd_req_t *req) { #ifdef DEBUG_DETAIL_ON @@ -286,6 +270,7 @@ esp_err_t hello_main_handler(httpd_req_t *req) return ESP_OK; } + esp_err_t img_tmp_handler(httpd_req_t *req) { char filepath[50]; @@ -310,6 +295,7 @@ esp_err_t img_tmp_handler(httpd_req_t *req) return ESP_OK; } + esp_err_t img_tmp_virtual_handler(httpd_req_t *req) { #ifdef DEBUG_DETAIL_ON @@ -390,13 +376,12 @@ esp_err_t sysinfo_handler(httpd_req_t *req) resp_str = zw.c_str(); httpd_resp_set_type(req, "application/json"); - httpd_resp_send(req, resp_str, strlen(resp_str)); - /* Respond with an empty chunk to signal HTTP response completion */ - httpd_resp_send_chunk(req, NULL, 0); + httpd_resp_sendstr(req, resp_str); return ESP_OK; } + void register_server_main_uri(httpd_handle_t server, const char *base_path) { httpd_uri_t info_get_handle = { @@ -423,8 +408,6 @@ void register_server_main_uri(httpd_handle_t server, const char *base_path) }; httpd_register_uri_handler(server, &starttime_tmp_handle); - - httpd_uri_t img_tmp_handle = { .uri = "/img_tmp/*", // Match all URIs of type /path/to/file .method = HTTP_GET, @@ -433,7 +416,6 @@ void register_server_main_uri(httpd_handle_t server, const char *base_path) }; httpd_register_uri_handler(server, &img_tmp_handle); - httpd_uri_t main_rest_handle = { .uri = "/*", // Match all URIs of type /path/to/file .method = HTTP_GET, @@ -445,7 +427,6 @@ void register_server_main_uri(httpd_handle_t server, const char *base_path) } - httpd_handle_t start_webserver(void) { httpd_handle_t server = NULL; @@ -486,6 +467,7 @@ httpd_handle_t start_webserver(void) return NULL; } + void stop_webserver(httpd_handle_t server) { httpd_stop(server); @@ -503,6 +485,7 @@ void disconnect_handler(void* arg, esp_event_base_t event_base, } } + void connect_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {