added basic authorization to the POST requests, removed var zize

definitions from old API
This commit is contained in:
Bogdan Pilyugin 2023-10-24 12:39:20 +02:00
parent 490bf91161
commit 9116052ecb
4 changed files with 34 additions and 27 deletions

View File

@ -43,10 +43,6 @@
#include "mbedtls/base64.h" #include "mbedtls/base64.h"
#include "SystemApplication.h" #include "SystemApplication.h"
#define MAX_DYNVAR_NAME_LENGTH 32
#define MAX_DYNVAR_LENGTH 256
#define MAX_INCFILE_LENGTH 1024
/* Max length a file path can have on storage */ /* Max length a file path can have on storage */
#define FILE_PATH_MAX (ESP_VFS_PATH_MAX + CONFIG_SPIFFS_OBJ_NAME_LEN) #define FILE_PATH_MAX (ESP_VFS_PATH_MAX + CONFIG_SPIFFS_OBJ_NAME_LEN)
#define MAX_FILE_SIZE (200*1024) // 200 KB #define MAX_FILE_SIZE (200*1024) // 200 KB

View File

@ -31,7 +31,7 @@
#define REAST_API_DEBUG_MODE 0 #define REAST_API_DEBUG_MODE 0
#define EXPECTED_MAX_DATA_SIZE (4096) #define EXPECTED_MAX_DATA_SIZE (4096)
#define VAR_MAX_NAME_LENGTH MAX_DYNVAR_NAME_LENGTH #define VAR_MAX_NAME_LENGTH (32)
#define VAR_MAX_VALUE_LENGTH (2048) #define VAR_MAX_VALUE_LENGTH (2048)
#define PAYLOAD_ERROR 0 #define PAYLOAD_ERROR 0

View File

@ -188,13 +188,13 @@ static esp_err_t POSTHandler(httpd_req_t *req)
int received; int received;
int remaining = req->content_len; int remaining = req->content_len;
buf[req->content_len] = 0x00; buf[req->content_len] = 0x00;
HTTP_IO_RESULT http_res;
while (remaining > 0) while (remaining > 0)
{ {
#if HTTP_SERVER_DEBUG_LEVEL > 0 #if HTTP_SERVER_DEBUG_LEVEL > 0
ESP_LOGI(TAG, "Remaining size : %d", remaining); ESP_LOGI(TAG, "Remaining size : %d", remaining);
#endif #endif
/* Receive the file part by part into a buffer */ /* Receive the file part by part into a buffer */
if ((received = httpd_req_recv(req, buf, if ((received = httpd_req_recv(req, buf,
MIN(remaining, SCRATCH_BUFSIZE))) <= 0) MIN(remaining, SCRATCH_BUFSIZE))) <= 0)
{ {
@ -218,13 +218,24 @@ static esp_err_t POSTHandler(httpd_req_t *req)
char filepath[FILE_PATH_MAX]; char filepath[FILE_PATH_MAX];
const char *filename; const char *filename;
//check auth for all files
if (CheckAuth(req) != ESP_OK)
{
return ESP_FAIL;
}
filename = get_path_from_uri(filepath, filename = get_path_from_uri(filepath,
((struct file_server_data*) req->user_ctx)->base_path, ((struct file_server_data*) req->user_ctx)->base_path,
req->uri, req->uri,
sizeof(filepath)); sizeof(filepath));
http_res = HTTP_IO_DONE;
if (!memcmp(filename, url_api, sizeof(url_api))) if (!memcmp(filename, url_api, sizeof(url_api)))
http_res = HTTPPostSysAPI(req, buf); HTTPPostSysAPI(req, buf);
else
{
httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, "URL not found");
return ESP_FAIL;
}
} }
@ -301,7 +312,7 @@ static esp_err_t GETHandler2(httpd_req_t *req)
/*Check if content of file is compressed*/ /*Check if content of file is compressed*/
char file_header[3]; char file_header[3];
espfs_fread(file, file_header, 3); espfs_fread(file, file_header, 3);
if(!memcmp(file_header, GZIP_SIGN, 3)) if (!memcmp(file_header, GZIP_SIGN, 3))
{ {
httpd_resp_set_hdr(req, "Content-Encoding", "gzip"); httpd_resp_set_hdr(req, "Content-Encoding", "gzip");
} }

View File

@ -43,7 +43,7 @@ void SetAppVars(rest_var_t *appvars, int size)
static void PrintInterfaceState(char *argres, int rw, esp_netif_t *netif) static void PrintInterfaceState(char *argres, int rw, esp_netif_t *netif)
{ {
snprintf(argres, MAX_DYNVAR_LENGTH, snprintf(argres, VAR_MAX_VALUE_LENGTH,
(netif != NULL && esp_netif_is_netif_up(netif)) ? "\"CONNECTED\"" : "\"DISCONNECTED\""); (netif != NULL && esp_netif_is_netif_up(netif)) ? "\"CONNECTED\"" : "\"DISCONNECTED\"");
} }
@ -71,11 +71,11 @@ static void funct_gsm_stat(char *argres, int rw)
static void funct_mqtt_1_stat(char *argres, int rw) static void funct_mqtt_1_stat(char *argres, int rw)
{ {
snprintf(argres, MAX_DYNVAR_LENGTH, (GetMQTT1Connected()) ? "\"CONNECTED\"" : "\"DISCONNECTED\""); snprintf(argres, VAR_MAX_VALUE_LENGTH, (GetMQTT1Connected()) ? "\"CONNECTED\"" : "\"DISCONNECTED\"");
} }
static void funct_mqtt_2_stat(char *argres, int rw) static void funct_mqtt_2_stat(char *argres, int rw)
{ {
snprintf(argres, MAX_DYNVAR_LENGTH, (GetMQTT2Connected()) ? "\"CONNECTED\"" : "\"DISCONNECTED\""); snprintf(argres, VAR_MAX_VALUE_LENGTH, (GetMQTT2Connected()) ? "\"CONNECTED\"" : "\"DISCONNECTED\"");
} }
static void funct_mqtt_1_test(char *argres, int rw) static void funct_mqtt_1_test(char *argres, int rw)
{ {
@ -102,63 +102,63 @@ static void funct_time(char *argres, int rw)
{ {
time_t now; time_t now;
time(&now); time(&now);
snprintf(argres, MAX_DYNVAR_LENGTH, "%d", (int) now); snprintf(argres, VAR_MAX_VALUE_LENGTH, "%d", (int) now);
} }
static void funct_uptime(char *argres, int rw) static void funct_uptime(char *argres, int rw)
{ {
snprintf(argres, MAX_DYNVAR_LENGTH, "%d", (int) GetUpTime()); snprintf(argres, VAR_MAX_VALUE_LENGTH, "%d", (int) GetUpTime());
} }
static void funct_wifi_level(char *argres, int rw) static void funct_wifi_level(char *argres, int rw)
{ {
wifi_ap_record_t wifi; wifi_ap_record_t wifi;
if (esp_wifi_sta_get_ap_info(&wifi) == ESP_OK) if (esp_wifi_sta_get_ap_info(&wifi) == ESP_OK)
snprintf(argres, MAX_DYNVAR_LENGTH, "\"%ddBm\"", wifi.rssi); snprintf(argres, VAR_MAX_VALUE_LENGTH, "\"%ddBm\"", wifi.rssi);
else else
snprintf(argres, MAX_DYNVAR_LENGTH, "\"-\""); snprintf(argres, VAR_MAX_VALUE_LENGTH, "\"-\"");
} }
static void funct_fram(char *argres, int rw) static void funct_fram(char *argres, int rw)
{ {
snprintf(argres, MAX_DYNVAR_LENGTH, "%d", (int) esp_get_free_heap_size()); snprintf(argres, VAR_MAX_VALUE_LENGTH, "%d", (int) esp_get_free_heap_size());
} }
static void funct_fram_min(char *argres, int rw) static void funct_fram_min(char *argres, int rw)
{ {
snprintf(argres, MAX_DYNVAR_LENGTH, "%d", (int) esp_get_minimum_free_heap_size()); snprintf(argres, VAR_MAX_VALUE_LENGTH, "%d", (int) esp_get_minimum_free_heap_size());
} }
static void funct_idf_ver(char *argres, int rw) static void funct_idf_ver(char *argres, int rw)
{ {
esp_app_desc_t cur_app_info; esp_app_desc_t cur_app_info;
if (esp_ota_get_partition_description(esp_ota_get_running_partition(), &cur_app_info) == ESP_OK) if (esp_ota_get_partition_description(esp_ota_get_running_partition(), &cur_app_info) == ESP_OK)
snprintf(argres, MAX_DYNVAR_LENGTH, "\"%s\"", cur_app_info.idf_ver); snprintf(argres, VAR_MAX_VALUE_LENGTH, "\"%s\"", cur_app_info.idf_ver);
else else
snprintf(argres, MAX_DYNVAR_LENGTH, "%s", "ESP_ERR_NOT_SUPPORTED"); snprintf(argres, VAR_MAX_VALUE_LENGTH, "%s", "ESP_ERR_NOT_SUPPORTED");
} }
static void funct_fw_ver(char *argres, int rw) static void funct_fw_ver(char *argres, int rw)
{ {
esp_app_desc_t cur_app_info; esp_app_desc_t cur_app_info;
if (esp_ota_get_partition_description(esp_ota_get_running_partition(), &cur_app_info) == ESP_OK) if (esp_ota_get_partition_description(esp_ota_get_running_partition(), &cur_app_info) == ESP_OK)
snprintf(argres, MAX_DYNVAR_LENGTH, "\"%s\"", cur_app_info.version); snprintf(argres, VAR_MAX_VALUE_LENGTH, "\"%s\"", cur_app_info.version);
else else
snprintf(argres, MAX_DYNVAR_LENGTH, "%s", "ESP_ERR_NOT_SUPPORTED"); snprintf(argres, VAR_MAX_VALUE_LENGTH, "%s", "ESP_ERR_NOT_SUPPORTED");
} }
static void funct_build_date(char *argres, int rw) static void funct_build_date(char *argres, int rw)
{ {
esp_app_desc_t cur_app_info; esp_app_desc_t cur_app_info;
if (esp_ota_get_partition_description(esp_ota_get_running_partition(), &cur_app_info) == ESP_OK) if (esp_ota_get_partition_description(esp_ota_get_running_partition(), &cur_app_info) == ESP_OK)
snprintf(argres, MAX_DYNVAR_LENGTH, "\"%s %s\"", cur_app_info.date, cur_app_info.time); snprintf(argres, VAR_MAX_VALUE_LENGTH, "\"%s %s\"", cur_app_info.date, cur_app_info.time);
else else
snprintf(argres, MAX_DYNVAR_LENGTH, "%s", "ESP_ERR_NOT_SUPPORTED"); snprintf(argres, VAR_MAX_VALUE_LENGTH, "%s", "ESP_ERR_NOT_SUPPORTED");
} }
static void PrintMACFromInterface(char *argres, int rw, esp_netif_t *netif) static void PrintMACFromInterface(char *argres, int rw, esp_netif_t *netif)
{ {
uint8_t mac_addr[6] = { 0 }; uint8_t mac_addr[6] = { 0 };
esp_netif_get_mac(netif, mac_addr); esp_netif_get_mac(netif, mac_addr);
snprintf(argres, MAX_DYNVAR_LENGTH, "\"%02x-%02x-%02x-%02x-%02x-%02x\"", snprintf(argres, VAR_MAX_VALUE_LENGTH, "\"%02x-%02x-%02x-%02x-%02x-%02x\"",
mac_addr[0], mac_addr[0],
mac_addr[1], mac_addr[1],
mac_addr[2], mac_addr[2],
@ -274,7 +274,7 @@ static void funct_ota_start(char *argres, int rw)
} }
static void funct_ota_newver(char *argres, int rw) static void funct_ota_newver(char *argres, int rw)
{ {
snprintf(argres, MAX_DYNVAR_LENGTH, "\"%s\"", GetAvailVersion()); snprintf(argres, VAR_MAX_VALUE_LENGTH, "\"%s\"", GetAvailVersion());
} }
//CRON implementation BEGIN //CRON implementation BEGIN
@ -338,7 +338,7 @@ static void funct_exec(char *argres, int rw)
if (rw) if (rw)
ExecCommand(argres); ExecCommand(argres);
else else
snprintf(argres, MAX_DYNVAR_LENGTH, "\"EXECUTED\""); snprintf(argres, VAR_MAX_VALUE_LENGTH, "\"EXECUTED\"");
} }
const int hw_rev = CONFIG_BOARD_HARDWARE_REVISION; const int hw_rev = CONFIG_BOARD_HARDWARE_REVISION;