clean from old post get handlers
This commit is contained in:
parent
8dd3f1bdac
commit
b4cb524ea1
|
|
@ -16,8 +16,8 @@ idf_component_register(
|
|||
"src/HTTPServer.c"
|
||||
"src/FileServer.c"
|
||||
"src/HTTPAPISystem.c"
|
||||
"src/HTTPPrintSystem.c"
|
||||
"src/HTTPPostSystem.c"
|
||||
# "src/HTTPPrintSystem.c"
|
||||
# "src/HTTPPostSystem.c"
|
||||
"src/CommandProcSys.c"
|
||||
"src/Helpers.c"
|
||||
"src/NetTransport.c"
|
||||
|
|
|
|||
|
|
@ -30,10 +30,7 @@
|
|||
|
||||
#define REAST_API_DEBUG_MODE 0
|
||||
|
||||
#define EXPECTED_MAX_HEADER_SIZE 512
|
||||
#define EXPECTED_MAX_PAYLOAD_SIZE 4096
|
||||
|
||||
#define EXPECTED_MAX_DATA_SIZE (EXPECTED_MAX_HEADER_SIZE + EXPECTED_MAX_PAYLOAD_SIZE)
|
||||
#define EXPECTED_MAX_DATA_SIZE (4096)
|
||||
#define VAR_MAX_NAME_LENGTH MAX_DYNVAR_NAME_LENGTH
|
||||
#define VAR_MAX_VALUE_LENGTH (2048)
|
||||
|
||||
|
|
|
|||
|
|
@ -1,657 +0,0 @@
|
|||
/*! Copyright 2022 Bogdan Pilyugin
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* \file HTTPPostSystem.c
|
||||
* \version 1.0
|
||||
* \date 2022-08-14
|
||||
* \author Bogdan Pilyugin
|
||||
* \brief
|
||||
* \details
|
||||
* \copyright Apache License, Version 2.0
|
||||
*/
|
||||
|
||||
#include "HTTPServer.h"
|
||||
#include "LoRaWAN.h"
|
||||
#include "Helpers.h"
|
||||
#include "MQTT.h"
|
||||
#include "UserCallbacks.h"
|
||||
|
||||
static const char *TAG = "HTTPServerPost";
|
||||
|
||||
#define FILE_PATH_MAX (ESP_VFS_PATH_MAX + CONFIG_SPIFFS_OBJ_NAME_LEN)
|
||||
|
||||
char rtstat[2048];
|
||||
|
||||
const char url_adapters[] = "adapters.html";
|
||||
const char url_services[] = "services.html";
|
||||
const char url_system[] = "system.html";
|
||||
const char url_reboot[] = "reboot.html";
|
||||
const char url_api[] = "api";
|
||||
|
||||
static HTTP_IO_RESULT AfterPostHandler(httpd_req_t *req, const char *filename, char *PostData);
|
||||
static HTTP_IO_RESULT HTTPPostAdaptersSettings(httpd_req_t *req, char *PostData);
|
||||
static HTTP_IO_RESULT HTTPPostServicesSettings(httpd_req_t *req, char *PostData);
|
||||
static HTTP_IO_RESULT HTTPPostSystemSettings(httpd_req_t *req, char *PostData);
|
||||
static HTTP_IO_RESULT HTTPPostReboot(httpd_req_t *req, char *PostData);
|
||||
|
||||
HTTP_IO_RESULT (*AfterPostHandlerCust)(httpd_req_t *req, const char *filename, char *PostData);
|
||||
|
||||
void regAfterPostHandlerCustom(HTTP_IO_RESULT (*post_handler)(httpd_req_t *req, const char *filename, char *PostData))
|
||||
{
|
||||
AfterPostHandlerCust = post_handler;
|
||||
}
|
||||
|
||||
HTTP_IO_RESULT HTTPPostApp(httpd_req_t *req, const char *filename, char *PostData)
|
||||
{
|
||||
const char *pt = filename + 1;
|
||||
|
||||
#if HTTP_SERVER_DEBUG_LEVEL > 0
|
||||
//ESP_LOGI(TAG, "URI for POST processing:%s", req->uri);
|
||||
ESP_LOGI(TAG, "Filename:%s", pt);
|
||||
ESP_LOGI(TAG, "DATA for POST processing:%s", PostData);
|
||||
#endif
|
||||
|
||||
HTTP_IO_RESULT res = AfterPostHandler(req, pt, PostData);
|
||||
|
||||
switch (res)
|
||||
{
|
||||
case HTTP_IO_DONE:
|
||||
break;
|
||||
case HTTP_IO_WAITING:
|
||||
break;
|
||||
case HTTP_IO_NEED_DATA:
|
||||
break;
|
||||
case HTTP_IO_REDIRECT:
|
||||
strcpy((char*) filename, PostData);
|
||||
break;
|
||||
case HTTP_IO_DONE_NOREFRESH:
|
||||
break;
|
||||
case HTTP_IO_DONE_API:
|
||||
break;
|
||||
break;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
static HTTP_IO_RESULT AfterPostHandler(httpd_req_t *req, const char *filename, char *PostData)
|
||||
{
|
||||
if (!memcmp(filename, url_api, sizeof(url_api)))
|
||||
return HTTPPostSysAPI(req, PostData);
|
||||
if (!memcmp(filename, url_adapters, sizeof(url_adapters)))
|
||||
return HTTPPostAdaptersSettings(req, PostData);
|
||||
if (!memcmp(filename, url_services, sizeof(url_services)))
|
||||
return HTTPPostServicesSettings(req, PostData);
|
||||
if (!memcmp(filename, url_system, sizeof(url_system)))
|
||||
return HTTPPostSystemSettings(req, PostData);
|
||||
|
||||
if (!memcmp(filename, url_reboot, sizeof(url_reboot)))
|
||||
return HTTPPostReboot(req, PostData);
|
||||
|
||||
// If not found target URL here, try to call custom code
|
||||
if (AfterPostHandlerCust != NULL)
|
||||
return AfterPostHandlerCust(req, filename, PostData);
|
||||
|
||||
return HTTP_IO_DONE;
|
||||
}
|
||||
|
||||
static HTTP_IO_RESULT HTTPPostAdaptersSettings(httpd_req_t *req, char *PostData)
|
||||
{
|
||||
char tmp[33];
|
||||
#if CONFIG_WEBGUIAPP_ETHERNET_ENABLE
|
||||
|
||||
bool TempIsETHEnabled = false;
|
||||
bool TempIsETHDHCPEnabled = false;
|
||||
if (httpd_query_key_value(PostData, "ethen", tmp, sizeof(tmp)) == ESP_OK)
|
||||
{
|
||||
if (!strcmp((const char*) tmp, (const char*) "1"))
|
||||
TempIsETHEnabled = true;
|
||||
}
|
||||
if (httpd_query_key_value(PostData, "edhcp", tmp, sizeof(tmp)) == ESP_OK)
|
||||
{
|
||||
if (!strcmp((const char*) tmp, (const char*) "1"))
|
||||
TempIsETHDHCPEnabled = true;
|
||||
}
|
||||
|
||||
if (httpd_query_key_value(PostData, "eipa", tmp, 15) == ESP_OK)
|
||||
esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.IPAddr);
|
||||
if (httpd_query_key_value(PostData, "emas", tmp, 15) == ESP_OK)
|
||||
esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.Mask);
|
||||
if (httpd_query_key_value(PostData, "egte", tmp, 15) == ESP_OK)
|
||||
esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.Gateway);
|
||||
if (httpd_query_key_value(PostData, "edns1", tmp, 15) == ESP_OK)
|
||||
esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.DNSAddr1);
|
||||
if (httpd_query_key_value(PostData, "edns2", tmp, 15) == ESP_OK)
|
||||
esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.DNSAddr2);
|
||||
if (httpd_query_key_value(PostData, "edns3", tmp, 15) == ESP_OK)
|
||||
esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.DNSAddr3);
|
||||
|
||||
#endif
|
||||
|
||||
#if CONFIG_WEBGUIAPP_WIFI_ENABLE
|
||||
|
||||
bool TempIsWiFiEnabled = false;
|
||||
bool TempIsWIFIDHCPEnabled = false;
|
||||
if (httpd_query_key_value(PostData, "wifien", tmp, sizeof(tmp)) == ESP_OK)
|
||||
{
|
||||
if (!strcmp((const char*) tmp, (const char*) "1"))
|
||||
TempIsWiFiEnabled = true;
|
||||
}
|
||||
if (httpd_query_key_value(PostData, "wfmode", tmp, sizeof(tmp)) == ESP_OK)
|
||||
{
|
||||
if (!strcmp((const char*) tmp, (const char*) "sta"))
|
||||
GetSysConf()->wifiSettings.WiFiMode = WIFI_MODE_STA;
|
||||
else if (!strcmp((const char*) tmp, (const char*) "ap"))
|
||||
GetSysConf()->wifiSettings.WiFiMode = WIFI_MODE_AP;
|
||||
else if (!strcmp((const char*) tmp, (const char*) "apsta"))
|
||||
GetSysConf()->wifiSettings.WiFiMode = WIFI_MODE_APSTA;
|
||||
}
|
||||
if (httpd_query_key_value(PostData, "wifipwr", tmp, sizeof(tmp)) == ESP_OK)
|
||||
{
|
||||
uint8_t pwr = atoi((const char*) tmp);
|
||||
if (pwr >= 1 && pwr <= 20)
|
||||
GetSysConf()->wifiSettings.MaxPower = pwr * 4;
|
||||
}
|
||||
|
||||
/*AP section*/
|
||||
httpd_query_key_value(PostData, "wfiap", GetSysConf()->wifiSettings.ApSSID,
|
||||
sizeof(GetSysConf()->wifiSettings.ApSSID));
|
||||
if (httpd_query_key_value(PostData, "wfpap", tmp, sizeof(tmp)) == ESP_OK)
|
||||
{
|
||||
if (strcmp(tmp, (const char*) "********"))
|
||||
strcpy(GetSysConf()->wifiSettings.ApSecurityKey, tmp);
|
||||
}
|
||||
if (httpd_query_key_value(PostData, "ipaap", tmp, sizeof(tmp)) == ESP_OK)
|
||||
esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->wifiSettings.ApIPAddr);
|
||||
|
||||
httpd_query_key_value(PostData, "wfi", GetSysConf()->wifiSettings.InfSSID,
|
||||
sizeof(GetSysConf()->wifiSettings.InfSSID));
|
||||
if (httpd_query_key_value(PostData, "wfp", tmp, sizeof(tmp)) == ESP_OK)
|
||||
{
|
||||
if (strcmp(tmp, (const char*) "********"))
|
||||
strcpy(GetSysConf()->wifiSettings.InfSecurityKey, tmp);
|
||||
}
|
||||
/*STATION section*/
|
||||
if (httpd_query_key_value(PostData, "dhcp", tmp, sizeof(tmp)) == ESP_OK)
|
||||
{
|
||||
if (!strcmp((const char*) tmp, (const char*) "1"))
|
||||
TempIsWIFIDHCPEnabled = true;
|
||||
}
|
||||
if (httpd_query_key_value(PostData, "ipa", tmp, 15) == ESP_OK)
|
||||
esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->wifiSettings.InfIPAddr);
|
||||
if (httpd_query_key_value(PostData, "mas", tmp, 15) == ESP_OK)
|
||||
esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->wifiSettings.InfMask);
|
||||
if (httpd_query_key_value(PostData, "gte", tmp, 15) == ESP_OK)
|
||||
esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->wifiSettings.InfGateway);
|
||||
if (httpd_query_key_value(PostData, "dns", tmp, 15) == ESP_OK)
|
||||
esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->wifiSettings.DNSAddr1);
|
||||
if (httpd_query_key_value(PostData, "dns2", tmp, 15) == ESP_OK)
|
||||
esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->wifiSettings.DNSAddr2);
|
||||
if (httpd_query_key_value(PostData, "dns3", tmp, 15) == ESP_OK)
|
||||
esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->wifiSettings.DNSAddr3);
|
||||
|
||||
#endif
|
||||
#if CONFIG_WEBGUIAPP_GPRS_ENABLE
|
||||
bool TempIsGSMEnabled = false;
|
||||
if (httpd_query_key_value(PostData, "gsmen", tmp, sizeof(tmp)) == ESP_OK)
|
||||
{
|
||||
if (!strcmp((const char*) tmp, (const char*) "1"))
|
||||
TempIsGSMEnabled = true;
|
||||
}
|
||||
|
||||
if (httpd_query_key_value(PostData, "sav", tmp, 4) == ESP_OK)
|
||||
{
|
||||
if (!strcmp(tmp, (const char*) "prs"))
|
||||
{
|
||||
GetSysConf()->gsmSettings.Flags1.bIsGSMEnabled = TempIsGSMEnabled;
|
||||
WriteNVSSysConfig(GetSysConf());
|
||||
memcpy(PostData, "/reboot.html", sizeof "/reboot.html");
|
||||
return HTTP_IO_REDIRECT;
|
||||
}
|
||||
}
|
||||
if (httpd_query_key_value(PostData, "restart", tmp, 4) == ESP_OK)
|
||||
{
|
||||
if (!strcmp(tmp, (const char*) "prs"))
|
||||
{
|
||||
//PPPModemSoftRestart();
|
||||
}
|
||||
}
|
||||
if (httpd_query_key_value(PostData, "hdrst", tmp, 4) == ESP_OK)
|
||||
{
|
||||
if (!strcmp(tmp, (const char*) "prs"))
|
||||
{
|
||||
//PPPModemColdStart();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if CONFIG_WEBGUIAPP_LORAWAN_ENABLE
|
||||
bool TempIsLoRaEnabled = false;
|
||||
if (httpd_query_key_value(PostData, "lren", tmp, sizeof(tmp)) == ESP_OK)
|
||||
{
|
||||
if (!strcmp((const char*) tmp, (const char*) "1"))
|
||||
TempIsLoRaEnabled = true;
|
||||
}
|
||||
if (httpd_query_key_value(PostData, "lrdvid", tmp, sizeof(tmp)) == ESP_OK)
|
||||
{
|
||||
if (strlen(tmp) == 16)
|
||||
StrToBytesLen((unsigned char*) tmp, (unsigned char*) GetSysConf()->lorawanSettings.DevEui, 16);
|
||||
}
|
||||
if (httpd_query_key_value(PostData, "lrapid", tmp, sizeof(tmp)) == ESP_OK)
|
||||
{
|
||||
if (strlen(tmp) == 16)
|
||||
StrToBytesLen((unsigned char*) tmp, (unsigned char*) GetSysConf()->lorawanSettings.AppEui, 16);
|
||||
}
|
||||
if (httpd_query_key_value(PostData, "lrapkey", tmp, sizeof(tmp)) == ESP_OK)
|
||||
{
|
||||
if (strlen(tmp) == 32)
|
||||
StrToBytesLen((unsigned char*) tmp, (unsigned char*) GetSysConf()->lorawanSettings.AppKey, 32);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (httpd_query_key_value(PostData, "wifisc", tmp, 4) == ESP_OK)
|
||||
{
|
||||
if (!strcmp(tmp, (const char*) "prs"))
|
||||
{
|
||||
WiFiScan();
|
||||
}
|
||||
}
|
||||
|
||||
if (httpd_query_key_value(PostData, "wifistart", tmp, 4) == ESP_OK)
|
||||
{
|
||||
if (!strcmp(tmp, (const char*) "prs"))
|
||||
{
|
||||
WiFiStartAPTemp(120);
|
||||
}
|
||||
}
|
||||
if (httpd_query_key_value(PostData, "wifistop", tmp, 4) == ESP_OK)
|
||||
{
|
||||
if (!strcmp(tmp, (const char*) "prs"))
|
||||
{
|
||||
WiFiStopAP();
|
||||
}
|
||||
}
|
||||
|
||||
if (httpd_query_key_value(PostData, "wifisave", tmp, 4) == ESP_OK)
|
||||
{
|
||||
if (!strcmp(tmp, (const char*) "prs"))
|
||||
{
|
||||
WriteNVSSysConfig(GetSysConf());
|
||||
memcpy(PostData, "/reboot.html", sizeof "/reboot.html");
|
||||
return HTTP_IO_REDIRECT;
|
||||
}
|
||||
}
|
||||
|
||||
if (httpd_query_key_value(PostData, "save", tmp, 5) == ESP_OK ||
|
||||
httpd_query_key_value(PostData, "apply", tmp, 5) == ESP_OK)
|
||||
{
|
||||
if (!strcmp(tmp, (const char*) "eth"))
|
||||
{
|
||||
#if CONFIG_WEBGUIAPP_ETHERNET_ENABLE
|
||||
GetSysConf()->ethSettings.Flags1.bIsETHEnabled = TempIsETHEnabled;
|
||||
GetSysConf()->ethSettings.Flags1.bIsDHCPEnabled = TempIsETHDHCPEnabled;
|
||||
#endif
|
||||
}
|
||||
else if (!strcmp(tmp, (const char*) "wifi"))
|
||||
{
|
||||
#if CONFIG_WEBGUIAPP_WIFI_ENABLE
|
||||
GetSysConf()->wifiSettings.Flags1.bIsWiFiEnabled = TempIsWiFiEnabled;
|
||||
GetSysConf()->wifiSettings.Flags1.bIsDHCPEnabled = TempIsWIFIDHCPEnabled;
|
||||
#endif
|
||||
}
|
||||
else if (!strcmp(tmp, (const char*) "gprs"))
|
||||
{
|
||||
#if CONFIG_WEBGUIAPP_GPRS_ENABLE
|
||||
GetSysConf()->gsmSettings.Flags1.bIsGSMEnabled = TempIsGSMEnabled;
|
||||
#endif
|
||||
}
|
||||
else if (!strcmp(tmp, (const char*) "lora"))
|
||||
{
|
||||
#if CONFIG_WEBGUIAPP_LORAWAN_ENABLE
|
||||
GetSysConf()->lorawanSettings.Flags1.bIsLoRaWANEnabled = TempIsLoRaEnabled;
|
||||
#endif
|
||||
}
|
||||
|
||||
if (httpd_query_key_value(PostData, "apply", tmp, 5) == ESP_OK)
|
||||
{
|
||||
WriteNVSSysConfig(GetSysConf());
|
||||
return HTTP_IO_DONE;
|
||||
}
|
||||
else if (httpd_query_key_value(PostData, "save", tmp, 5) == ESP_OK)
|
||||
{
|
||||
WriteNVSSysConfig(GetSysConf());
|
||||
memcpy(PostData, "/reboot.html", sizeof "/reboot.html");
|
||||
return HTTP_IO_REDIRECT;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return HTTP_IO_DONE;
|
||||
}
|
||||
|
||||
static HTTP_IO_RESULT HTTPPostServicesSettings(httpd_req_t *req, char *PostData)
|
||||
{
|
||||
char tmp[64];
|
||||
#if CONFIG_WEBGUIAPP_MQTT_ENABLE
|
||||
bool TempIsMQTT1Enabled = false;
|
||||
|
||||
#if CONFIG_WEBGUIAPP_MQTT_CLIENTS_NUM == 2
|
||||
bool TempIsMQTT2Enabled = false;
|
||||
#endif
|
||||
httpd_query_key_value(PostData, "mqurl1", GetSysConf()->mqttStation[0].ServerAddr,
|
||||
sizeof(GetSysConf()->mqttStation[0].ServerAddr));
|
||||
httpd_query_key_value(PostData, "mqid1", GetSysConf()->mqttStation[0].ClientID,
|
||||
sizeof(GetSysConf()->mqttStation[0].ClientID));
|
||||
httpd_query_key_value(PostData, "mqsys1", GetSysConf()->mqttStation[0].SystemName,
|
||||
sizeof(GetSysConf()->mqttStation[0].SystemName));
|
||||
httpd_query_key_value(PostData, "mqgrp1", GetSysConf()->mqttStation[0].GroupName,
|
||||
sizeof(GetSysConf()->mqttStation[0].GroupName));
|
||||
httpd_query_key_value(PostData, "mqname1", GetSysConf()->mqttStation[0].UserName,
|
||||
sizeof(GetSysConf()->mqttStation[0].UserName));
|
||||
|
||||
if (httpd_query_key_value(PostData, "mqen1", tmp, sizeof(tmp)) == ESP_OK)
|
||||
{
|
||||
if (!strcmp((const char*) tmp, (const char*) "1"))
|
||||
TempIsMQTT1Enabled = true;
|
||||
}
|
||||
if (httpd_query_key_value(PostData, "mqport1", tmp, sizeof(tmp)) == ESP_OK)
|
||||
{
|
||||
uint16_t tp = atoi((const char*) tmp);
|
||||
if (tp < 65535 && tp >= 1000)
|
||||
GetSysConf()->mqttStation[0].ServerPort = tp;
|
||||
}
|
||||
|
||||
if (httpd_query_key_value(PostData, "mqpass1", tmp, sizeof(tmp)) == ESP_OK &&
|
||||
strcmp(tmp, (const char*) "******"))
|
||||
{
|
||||
strcpy(GetSysConf()->mqttStation[0].UserPass, tmp);
|
||||
}
|
||||
|
||||
#if CONFIG_WEBGUIAPP_MQTT_CLIENTS_NUM == 2
|
||||
httpd_query_key_value(PostData, "mqurl2", GetSysConf()->mqttStation[1].ServerAddr,
|
||||
sizeof(GetSysConf()->mqttStation[1].ServerAddr));
|
||||
httpd_query_key_value(PostData, "mqid2", GetSysConf()->mqttStation[1].ClientID,
|
||||
sizeof(GetSysConf()->mqttStation[1].ClientID));
|
||||
httpd_query_key_value(PostData, "mqsys2", GetSysConf()->mqttStation[1].SystemName,
|
||||
sizeof(GetSysConf()->mqttStation[1].SystemName));
|
||||
httpd_query_key_value(PostData, "mqgrp2", GetSysConf()->mqttStation[1].GroupName,
|
||||
sizeof(GetSysConf()->mqttStation[1].GroupName));
|
||||
httpd_query_key_value(PostData, "mqname2", GetSysConf()->mqttStation[1].UserName,
|
||||
sizeof(GetSysConf()->mqttStation[1].UserName));
|
||||
|
||||
if (httpd_query_key_value(PostData, "mqen2", tmp, sizeof(tmp)) == ESP_OK)
|
||||
{
|
||||
if (!strcmp((const char*) tmp, (const char*) "1"))
|
||||
TempIsMQTT2Enabled = true;
|
||||
}
|
||||
|
||||
if (httpd_query_key_value(PostData, "mqport2", tmp, sizeof(tmp)) == ESP_OK)
|
||||
{
|
||||
uint16_t tp = atoi((const char*) tmp);
|
||||
if (tp < 65535 && tp >= 1000)
|
||||
GetSysConf()->mqttStation[1].ServerPort = tp;
|
||||
}
|
||||
|
||||
if (httpd_query_key_value(PostData, "mqpass2", tmp, sizeof(tmp)) == ESP_OK &&
|
||||
strcmp(tmp, (const char*) "******"))
|
||||
{
|
||||
strcpy(GetSysConf()->mqttStation[1].UserPass, tmp);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/*SNTP*/
|
||||
bool TempIsSNTPEnabled = false;
|
||||
if (httpd_query_key_value(PostData, "sntpen", tmp, sizeof(tmp)) == ESP_OK)
|
||||
{
|
||||
if (!strcmp((const char*) tmp, (const char*) "1"))
|
||||
TempIsSNTPEnabled = true;
|
||||
}
|
||||
httpd_query_key_value(PostData, "tsr", GetSysConf()->sntpClient.SntpServerAdr,
|
||||
sizeof(GetSysConf()->sntpClient.SntpServerAdr));
|
||||
httpd_query_key_value(PostData, "tsr2", GetSysConf()->sntpClient.SntpServer2Adr,
|
||||
sizeof(GetSysConf()->sntpClient.SntpServer2Adr));
|
||||
httpd_query_key_value(PostData, "tsr3", GetSysConf()->sntpClient.SntpServer3Adr,
|
||||
sizeof(GetSysConf()->sntpClient.SntpServer3Adr));
|
||||
|
||||
/*MQTT Test button handlers*/
|
||||
if (httpd_query_key_value(PostData, "mqtttest1", tmp, 6) == ESP_OK)
|
||||
{
|
||||
if (!strcmp(tmp, (const char*) "prs"))
|
||||
{
|
||||
if (GetSysConf()->mqttStation[0].Flags1.bIsGlobalEnabled)
|
||||
PublicTestMQTT(0);
|
||||
return HTTP_IO_DONE;
|
||||
}
|
||||
}
|
||||
|
||||
if (httpd_query_key_value(PostData, "mqtttest2", tmp, 6) == ESP_OK)
|
||||
{
|
||||
if (!strcmp(tmp, (const char*) "prs"))
|
||||
{
|
||||
if (GetSysConf()->mqttStation[1].Flags1.bIsGlobalEnabled)
|
||||
PublicTestMQTT(1);
|
||||
return HTTP_IO_DONE;
|
||||
}
|
||||
}
|
||||
|
||||
if (httpd_query_key_value(PostData, "save", tmp, 6) == ESP_OK ||
|
||||
httpd_query_key_value(PostData, "apply", tmp, 6) == ESP_OK)
|
||||
{
|
||||
if (!strcmp(tmp, (const char*) "mqtt1"))
|
||||
{
|
||||
GetSysConf()->mqttStation[0].Flags1.bIsGlobalEnabled = TempIsMQTT1Enabled;
|
||||
}
|
||||
|
||||
else if (!strcmp(tmp, (const char*) "mqtt2"))
|
||||
{
|
||||
#if CONFIG_WEBGUIAPP_MQTT_CLIENTS_NUM == 2
|
||||
GetSysConf()->mqttStation[1].Flags1.bIsGlobalEnabled = TempIsMQTT2Enabled;
|
||||
#endif
|
||||
}
|
||||
|
||||
else if (!strcmp(tmp, (const char*) "sntp"))
|
||||
{
|
||||
GetSysConf()->sntpClient.Flags1.bIsGlobalEnabled = TempIsSNTPEnabled;
|
||||
}
|
||||
|
||||
if (httpd_query_key_value(PostData, "apply", tmp, 6) == ESP_OK)
|
||||
{
|
||||
WriteNVSSysConfig(GetSysConf());
|
||||
return HTTP_IO_DONE;
|
||||
}
|
||||
else if (httpd_query_key_value(PostData, "save", tmp, 6) == ESP_OK)
|
||||
{
|
||||
WriteNVSSysConfig(GetSysConf());
|
||||
memcpy(PostData, "/reboot.html", sizeof "/reboot.html");
|
||||
return HTTP_IO_REDIRECT;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
return HTTP_IO_DONE;
|
||||
}
|
||||
|
||||
static HTTP_IO_RESULT HTTPPostSystemSettings(httpd_req_t *req, char *PostData)
|
||||
{
|
||||
char tmp[128];
|
||||
bool TempIsOTAEnabled = false;
|
||||
bool TempIsRstEnabled = false;
|
||||
if (httpd_query_key_value(PostData, "nam", tmp, sizeof(tmp)) == ESP_OK)
|
||||
{
|
||||
UnencodeURL(tmp);
|
||||
strcpy(GetSysConf()->NetBIOSName, tmp);
|
||||
}
|
||||
|
||||
httpd_query_key_value(PostData, "lgn", GetSysConf()->SysName, sizeof(GetSysConf()->SysName));
|
||||
|
||||
if (httpd_query_key_value(PostData, "psn", tmp, sizeof(tmp)) == ESP_OK)
|
||||
{
|
||||
if (strcmp(tmp, (const char*) "******"))
|
||||
strcpy(GetSysConf()->SysPass, tmp);
|
||||
}
|
||||
|
||||
if (httpd_query_key_value(PostData, "ota", tmp, sizeof(tmp)) == ESP_OK)
|
||||
{
|
||||
if (!strcmp((const char*) tmp, (const char*) "1"))
|
||||
TempIsOTAEnabled = true;
|
||||
}
|
||||
|
||||
if (httpd_query_key_value(PostData, "otarst", tmp, sizeof(tmp)) == ESP_OK)
|
||||
{
|
||||
if (!strcmp((const char*) tmp, (const char*) "1"))
|
||||
TempIsRstEnabled = true;
|
||||
}
|
||||
|
||||
if (httpd_query_key_value(PostData, "otaint", tmp, sizeof(tmp)) == ESP_OK)
|
||||
{
|
||||
uint16_t tp = atoi((const char*) tmp);
|
||||
if (tp < 65535 && tp >= 1)
|
||||
GetSysConf()->OTAAutoInt = tp;
|
||||
}
|
||||
|
||||
if (httpd_query_key_value(PostData, "otaurl", tmp, sizeof(tmp)) == ESP_OK)
|
||||
{
|
||||
UnencodeURL(tmp);
|
||||
strcpy(GetSysConf()->OTAURL, tmp);
|
||||
}
|
||||
|
||||
if (httpd_query_key_value(PostData, "colscheme", tmp, sizeof(tmp)) == ESP_OK)
|
||||
{
|
||||
uint16_t chm = atoi((const char*) tmp);
|
||||
if (chm < 3 && chm >= 1)
|
||||
GetSysConf()->ColorSheme = chm;
|
||||
}
|
||||
|
||||
if (httpd_query_key_value(PostData, "upd", tmp, sizeof(tmp)) == ESP_OK)
|
||||
{
|
||||
if (!strcmp(tmp, (const char*) "prs"))
|
||||
{
|
||||
StartOTA(true);
|
||||
}
|
||||
}
|
||||
if (httpd_query_key_value(PostData, "rtos", tmp, sizeof(tmp)) == ESP_OK)
|
||||
{
|
||||
if (!strcmp(tmp, (const char*) "prs"))
|
||||
{
|
||||
#if (CONFIG_FREERTOS_USE_TRACE_FACILITY == 1)
|
||||
vTaskGetRunTimeStatsCustom(rtstat);
|
||||
httpd_resp_sendstr(req, rtstat);
|
||||
#endif
|
||||
return HTTP_IO_DONE_API;
|
||||
}
|
||||
}
|
||||
|
||||
if (httpd_query_key_value(PostData, "rst", tmp, sizeof(tmp)) == ESP_OK)
|
||||
{
|
||||
if (!strcmp(tmp, (const char*) "prs"))
|
||||
{
|
||||
memcpy(PostData, "/reboot.html", sizeof "/reboot.html");
|
||||
return HTTP_IO_REDIRECT;
|
||||
}
|
||||
}
|
||||
|
||||
if (httpd_query_key_value(PostData, "save", tmp, sizeof(tmp)) == ESP_OK ||
|
||||
httpd_query_key_value(PostData, "apply", tmp, sizeof(tmp)) == ESP_OK)
|
||||
{
|
||||
if (!strcmp(tmp, (const char*) "syst"))
|
||||
{
|
||||
GetSysConf()->Flags1.bIsOTAEnabled = TempIsOTAEnabled;
|
||||
GetSysConf()->Flags1.bIsResetOTAEnabled = TempIsRstEnabled;
|
||||
}
|
||||
|
||||
if (httpd_query_key_value(PostData, "apply", tmp, 5) == ESP_OK)
|
||||
{
|
||||
WriteNVSSysConfig(GetSysConf());
|
||||
return HTTP_IO_DONE;
|
||||
}
|
||||
else if (httpd_query_key_value(PostData, "save", tmp, 5) == ESP_OK)
|
||||
{
|
||||
WriteNVSSysConfig(GetSysConf());
|
||||
memcpy(PostData, "/reboot.html", sizeof "/reboot.html");
|
||||
return HTTP_IO_REDIRECT;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (httpd_query_key_value(PostData, "cmd", tmp, 4) == ESP_OK)
|
||||
{
|
||||
if (!strcmp(tmp, (const char*) "1"))
|
||||
{
|
||||
|
||||
return HTTP_IO_DONE_NOREFRESH;
|
||||
}
|
||||
#if CONFIG_WEBGUIAPP_GPRS_ENABLE
|
||||
else if (!strcmp(tmp, (const char*) "2"))
|
||||
{
|
||||
char resp[256] = {0};
|
||||
ModemSendAT("AT+CCLK?\r", resp, 200);
|
||||
return HTTP_IO_DONE_NOREFRESH;
|
||||
}
|
||||
else if (!strcmp(tmp, (const char*) "3"))
|
||||
{
|
||||
char resp[256] = {0};
|
||||
ModemSendAT("ATD+79022518532;\r", resp, 200);
|
||||
return HTTP_IO_DONE_NOREFRESH;
|
||||
}
|
||||
else if (!strcmp(tmp, (const char*) "4"))
|
||||
{
|
||||
char resp[256] = {0};
|
||||
ModemSendAT("ATH\r", resp, 200);
|
||||
return HTTP_IO_DONE_NOREFRESH;
|
||||
}
|
||||
#endif
|
||||
else if (!strcmp(tmp, (const char*) "5"))
|
||||
{
|
||||
return HTTP_IO_DONE_NOREFRESH;
|
||||
}
|
||||
else if (!strcmp(tmp, (const char*) "6"))
|
||||
{
|
||||
return HTTP_IO_DONE_NOREFRESH;
|
||||
}
|
||||
else if (!strcmp(tmp, (const char*) "7"))
|
||||
{
|
||||
return HTTP_IO_DONE_NOREFRESH;
|
||||
}
|
||||
else if (!strcmp(tmp, (const char*) "8"))
|
||||
{
|
||||
return HTTP_IO_DONE_NOREFRESH;
|
||||
}
|
||||
else if (!strcmp(tmp, (const char*) "9"))
|
||||
{
|
||||
return HTTP_IO_DONE_NOREFRESH;
|
||||
}
|
||||
else if (!strcmp(tmp, (const char*) "10"))
|
||||
{
|
||||
GenerateSystemSettingsJSONFile();
|
||||
return HTTP_IO_DONE_NOREFRESH;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return HTTP_IO_DONE;
|
||||
}
|
||||
|
||||
static HTTP_IO_RESULT HTTPPostReboot(httpd_req_t *req, char *PostData)
|
||||
{
|
||||
char tmp[33];
|
||||
if (httpd_query_key_value(PostData, "rbt", tmp, sizeof(tmp)) == ESP_OK)
|
||||
{
|
||||
if (!strcmp(tmp, (const char*) "prs"))
|
||||
{
|
||||
DelayedRestart();
|
||||
}
|
||||
}
|
||||
return HTTP_IO_DONE;
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
129
src/HTTPServer.c
129
src/HTTPServer.c
|
|
@ -29,12 +29,15 @@ extern espfs_fs_t *fs;
|
|||
const char GZIP_SIGN[] = { 0x1f, 0x8b, 0x08 };
|
||||
|
||||
static esp_err_t GETHandler(httpd_req_t *req);
|
||||
static esp_err_t GETHandler2(httpd_req_t *req);
|
||||
static esp_err_t CheckAuth(httpd_req_t *req);
|
||||
|
||||
struct file_server_data *server_data = NULL;
|
||||
httpd_handle_t server = NULL;
|
||||
static const char *TAG = "HTTPServer";
|
||||
|
||||
const char url_api[] = "/api";
|
||||
|
||||
//Pointer to external user defined rest api handler
|
||||
static int (*HTTPUserRestAPI)(char *url, char *req, int len, char *resp) = NULL;
|
||||
void regHTTPUserRestAPI(int (*api_handler)(char *url, char *req, int len, char *resp))
|
||||
|
|
@ -221,30 +224,11 @@ static esp_err_t POSTHandler(httpd_req_t *req)
|
|||
((struct file_server_data*) req->user_ctx)->base_path,
|
||||
req->uri,
|
||||
sizeof(filepath));
|
||||
http_res = HTTP_IO_DONE;
|
||||
if (!memcmp(filename, url_api, sizeof(url_api)))
|
||||
http_res = HTTPPostSysAPI(req, buf);
|
||||
|
||||
http_res = HTTPPostApp(req, filename, buf);
|
||||
|
||||
if (http_res == HTTP_IO_DONE)
|
||||
return GETHandler(req);
|
||||
|
||||
else if (http_res == HTTP_IO_REDIRECT)
|
||||
{
|
||||
httpd_resp_set_status(req, "307 Temporary Redirect");
|
||||
httpd_resp_set_hdr(req, "Location", filename);
|
||||
httpd_resp_send(req, NULL, 0); // Response body can be empty
|
||||
#if HTTP_SERVER_DEBUG_LEVEL > 0
|
||||
ESP_LOGI(TAG, "Redirect request from POST");
|
||||
#endif
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
else if (http_res == HTTP_IO_DONE_NOREFRESH)
|
||||
{
|
||||
httpd_resp_set_status(req, HTTPD_204);
|
||||
httpd_resp_send(req, NULL, 0); // Response body can be empty
|
||||
return ESP_OK;
|
||||
}
|
||||
else if (http_res == HTTP_IO_DONE_API)
|
||||
if (http_res == HTTP_IO_DONE_API)
|
||||
{
|
||||
httpd_resp_send(req, NULL, 0); // Response body can be empty
|
||||
return ESP_OK;
|
||||
|
|
@ -260,6 +244,103 @@ static esp_err_t POSTHandler(httpd_req_t *req)
|
|||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t GETHandler2(httpd_req_t *req)
|
||||
{
|
||||
#if HTTP_SERVER_DEBUG_LEVEL > 0
|
||||
ESP_LOGI(TAG, "GET request handle");
|
||||
#endif
|
||||
|
||||
//Route to file server GET handler
|
||||
if (memmem(req->uri, strlen(req->uri), "/storage/", sizeof("/storage/") - 1))
|
||||
return download_get_handler(req);
|
||||
|
||||
char filepath[FILE_PATH_MAX];
|
||||
espfs_file_t *file;
|
||||
struct espfs_stat_t stat;
|
||||
const char *filename = get_path_from_uri(filepath,
|
||||
((struct file_server_data*) req->user_ctx)->base_path,
|
||||
req->uri,
|
||||
sizeof(filepath));
|
||||
if (!filename)
|
||||
{
|
||||
ESP_LOGE(TAG, "Filename is too long");
|
||||
/* Respond with 500 Internal Server Error */
|
||||
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR,
|
||||
"Filename too long");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
/* Redirect request to /index.html */
|
||||
if (filename[strlen(filename) - 1] == '/')
|
||||
{
|
||||
httpd_resp_set_status(req, "307 Temporary Redirect");
|
||||
httpd_resp_set_hdr(req, "Location", "/index.html");
|
||||
httpd_resp_send(req, NULL, 0); // Response body can be empty
|
||||
#if HTTP_SERVER_DEBUG_LEVEL > 0
|
||||
ESP_LOGI(TAG, "Redirect request to /index.html");
|
||||
#endif
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
//check auth for all files
|
||||
if (CheckAuth(req) != ESP_OK)
|
||||
{
|
||||
return ESP_FAIL;
|
||||
}
|
||||
//open file
|
||||
file = espfs_fopen(fs, filepath);
|
||||
if (!file)
|
||||
{
|
||||
httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, "File not found");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
//get file info
|
||||
espfs_stat(fs, filepath, &stat);
|
||||
|
||||
#if HTTP_SERVER_DEBUG_LEVEL > 0
|
||||
ESP_LOGI(TAG, "Sending file : %s (%d bytes)...", filename,
|
||||
stat.size);
|
||||
#endif
|
||||
|
||||
set_content_type_from_file(req, filename);
|
||||
httpd_resp_set_hdr(req, "Cache-Control", "max-age=600");
|
||||
httpd_resp_set_hdr(req, "Content-Encoding", "gzip");
|
||||
httpd_resp_set_hdr(req, "Connection", "close");
|
||||
|
||||
/* Retrieve the pointer to scratch buffer for temporary storage */
|
||||
char *chunk = ((struct file_server_data*) req->user_ctx)->scratch;
|
||||
size_t chunksize;
|
||||
do
|
||||
{
|
||||
/* Read file in chunks into the scratch buffer */
|
||||
chunksize = espfs_fread(file, chunk, SCRATCH_BUFSIZE);
|
||||
if (chunksize > 0)
|
||||
{
|
||||
/* Send the buffer contents as HTTP response chunk */
|
||||
if (httpd_resp_send_chunk(req, chunk, chunksize) != ESP_OK)
|
||||
{
|
||||
espfs_fclose(file);
|
||||
ESP_LOGE(TAG, "File sending failed!");
|
||||
/* Abort sending file */
|
||||
httpd_resp_sendstr_chunk(req, NULL);
|
||||
/* Respond with 500 Internal Server Error */
|
||||
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to send file");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
}
|
||||
/* Keep looping till the whole file is sent */
|
||||
}
|
||||
while (chunksize != 0);
|
||||
/* Close file after sending complete */
|
||||
espfs_fclose(file);
|
||||
/* Respond with an empty chunk to signal HTTP response completion */
|
||||
#ifdef CONFIG_EXAMPLE_HTTPD_CONN_CLOSE_HEADER
|
||||
httpd_resp_set_hdr(req, "Connection", "close");
|
||||
#endif
|
||||
httpd_resp_send_chunk(req, NULL, 0);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t GETHandler(httpd_req_t *req)
|
||||
{
|
||||
#if HTTP_SERVER_DEBUG_LEVEL > 0
|
||||
|
|
@ -442,7 +523,7 @@ static httpd_handle_t start_webserver(void)
|
|||
/* URI handler for GET request */
|
||||
httpd_uri_t get = { .uri = "/*",
|
||||
.method = HTTP_GET,
|
||||
.handler = GETHandler,
|
||||
.handler = GETHandler2,
|
||||
.user_ctx = server_data // Pass server data as context
|
||||
};
|
||||
httpd_register_uri_handler(server, &get);
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user