fixed POST multipacket data handler

This commit is contained in:
Bogdan Pilyugin 2023-11-29 17:51:59 +02:00
parent e4783cba1d
commit 78e66bdf5f
3 changed files with 35 additions and 30 deletions

View File

@ -30,9 +30,9 @@
#define REAST_API_DEBUG_MODE 0
#define EXPECTED_MAX_DATA_SIZE (4096)
#define EXPECTED_MAX_DATA_SIZE (4096 + 2048)
#define VAR_MAX_NAME_LENGTH (32)
#define VAR_MAX_VALUE_LENGTH (2048)
#define VAR_MAX_VALUE_LENGTH (EXPECTED_MAX_DATA_SIZE - 512)
#define PAYLOAD_ERROR 0
#define PAYLOAD_DEFAULT 1

View File

@ -28,7 +28,7 @@ extern espfs_fs_t *fs;
const char GZIP_SIGN[] = { 0x1f, 0x8b, 0x08 };
static esp_err_t GETHandler2(httpd_req_t *req);
static esp_err_t GETHandler(httpd_req_t *req);
static esp_err_t CheckAuth(httpd_req_t *req);
struct file_server_data *server_data = NULL;
@ -186,6 +186,7 @@ static esp_err_t POSTHandler(httpd_req_t *req)
char *buf = ((struct file_server_data*) req->user_ctx)->scratch;
int received;
int offset = 0;
int remaining = req->content_len;
buf[req->content_len] = 0x00;
while (remaining > 0)
@ -195,7 +196,7 @@ static esp_err_t POSTHandler(httpd_req_t *req)
#endif
/* Receive the file part by part into a buffer */
if ((received = httpd_req_recv(req, buf,
if ((received = httpd_req_recv(req, buf + offset,
MIN(remaining, SCRATCH_BUFSIZE))) <= 0)
{
if (received == HTTPD_SOCK_ERR_TIMEOUT)
@ -214,15 +215,23 @@ static esp_err_t POSTHandler(httpd_req_t *req)
/* Write buffer content to file on storage */
if (received)
offset += received;
{
#if HTTP_SERVER_DEBUG_LEVEL > 0
ESP_LOGI(TAG, "Received : %d", received);
#endif
}
/* Keep track of remaining size of
* the file left to be uploaded */
remaining -= received;
}
char filepath[FILE_PATH_MAX];
const char *filename;
//check auth for all files
if (CheckAuth(req) != ESP_OK)
{
return ESP_FAIL;
}
filename = get_path_from_uri(filepath,
((struct file_server_data*) req->user_ctx)->base_path,
@ -237,20 +246,13 @@ static esp_err_t POSTHandler(httpd_req_t *req)
return ESP_FAIL;
}
}
/* Keep track of remaining size of
* the file left to be uploaded */
remaining -= received;
}
return ESP_OK;
}
static esp_err_t GETHandler2(httpd_req_t *req)
static esp_err_t GETHandler(httpd_req_t *req)
{
#if HTTP_SERVER_DEBUG_LEVEL > 0
ESP_LOGI(TAG, "GET request handle URL: %s",req->uri);
ESP_LOGI(TAG, "GET request handle URL: %s", req->uri);
#endif
//Route to file server GET handler
@ -373,7 +375,7 @@ static httpd_handle_t start_webserver(void)
/* URI handler for GET request */
httpd_uri_t get = { .uri = "/*",
.method = HTTP_GET,
.handler = GETHandler2,
.handler = GETHandler,
.user_ctx = server_data // Pass server data as context
};
httpd_register_uri_handler(server, &get);

View File

@ -210,6 +210,7 @@ static sys_error_code DataHeaderParser(data_message_t *MSG)
return SYS_ERROR_PARSE_DATA;
}
jRead(MSG->inputDataBuffer, "{'signature'", &result);
if (result.elements == 1)
{
@ -222,6 +223,8 @@ static sys_error_code DataHeaderParser(data_message_t *MSG)
else
return SYS_ERROR_PARSE_SIGNATURE;
//Extract 'messidx' or throw exception
jRead(MSG->inputDataBuffer, "{'data'{'msgid'", &result);
if (result.elements == 1)
@ -313,7 +316,7 @@ esp_err_t ServiceDataHandler(data_message_t *MSG)
{
//ToDo Here handler of received data
#if REAST_API_DEBUG_MODE
ESP_LOGI(TAG, "Got response message with msgid=%d", MSG->parsedData.msgID);
ESP_LOGI(TAG, "Got response message with msgid=%d", (int)MSG->parsedData.msgID);
#endif
MSG->outputDataBuffer[0] = 0x00;
MSG->outputDataLength = 0;