Merge pull request #42 from lupus78/web_post

AP, web interface parm.html page handle submits via POST
pull/46/head
Paweł Jałocha 2021-06-10 11:40:56 +01:00 zatwierdzone przez GitHub
commit a35802e4c0
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
2 zmienionych plików z 88 dodań i 49 usunięć

Wyświetl plik

@ -59,7 +59,7 @@ static void End_Control_Row(httpd_req_t *Req)
static void ParmForm_Info(httpd_req_t *Req) static void ParmForm_Info(httpd_req_t *Req)
{ {
httpd_resp_sendstr_chunk(Req, "<h2>Info</h2>"); httpd_resp_sendstr_chunk(Req, "<h2>Info</h2>");
httpd_resp_sendstr_chunk(Req, "<form action=\"/parm.html\" method=\"get\" id=\"Info\">\n"); httpd_resp_sendstr_chunk(Req, "<form action=\"/parm.html\" method=\"POST\" id=\"Info\">\n");
Begin_Control_Row(Req, "Pilot"); Begin_Control_Row(Req, "Pilot");
httpd_resp_sendstr_chunk(Req, "<input type=\"text\" name=\"Pilot\" size=\"10\" value=\""); httpd_resp_sendstr_chunk(Req, "<input type=\"text\" name=\"Pilot\" size=\"10\" value=\"");
@ -111,7 +111,7 @@ static void ParmForm_Acft(httpd_req_t *Req)
{ char Line[16]; { char Line[16];
httpd_resp_sendstr_chunk(Req, "<h2>Aircraft</h2>"); httpd_resp_sendstr_chunk(Req, "<h2>Aircraft</h2>");
httpd_resp_sendstr_chunk(Req, "<form action=\"/parm.html\" method=\"get\" id=\"Acft\">\n"); httpd_resp_sendstr_chunk(Req, "<form action=\"/parm.html\" method=\"POST\" id=\"Acft\">\n");
Begin_Control_Row(Req, "Address"); Begin_Control_Row(Req, "Address");
httpd_resp_sendstr_chunk(Req, "<input type=\"text\" name=\"Address\" size=\"10\" value=\"0x"); httpd_resp_sendstr_chunk(Req, "<input type=\"text\" name=\"Address\" size=\"10\" value=\"0x");
@ -146,7 +146,7 @@ static void ParmForm_GPS(httpd_req_t *Req) // produce HTML form for GPS paramet
httpd_resp_sendstr_chunk(Req, "<h2>GPS</h2>"); httpd_resp_sendstr_chunk(Req, "<h2>GPS</h2>");
#endif #endif
#endif #endif
httpd_resp_sendstr_chunk(Req, "<form action=\"/parm.html\" method=\"get\" id=\"GPS\">\n"); httpd_resp_sendstr_chunk(Req, "<form action=\"/parm.html\" method=\"POST\" id=\"GPS\">\n");
Begin_Control_Row(Req, "Nav. rate [Hz]"); Begin_Control_Row(Req, "Nav. rate [Hz]");
@ -195,7 +195,7 @@ static void ParmForm_Other(httpd_req_t *Req) // produce HTML form for aircraft
{ char Line[16]; int Len; { char Line[16]; int Len;
httpd_resp_sendstr_chunk(Req, "<h2>Other</h2>"); httpd_resp_sendstr_chunk(Req, "<h2>Other</h2>");
httpd_resp_sendstr_chunk(Req, "<form action=\"/parm.html\" method=\"get\" id=\"Other\">\n"); httpd_resp_sendstr_chunk(Req, "<form action=\"/parm.html\" method=\"POST\" id=\"Other\">\n");
Begin_Control_Row(Req, "Freq. plan"); Begin_Control_Row(Req, "Freq. plan");
const char *FreqPlanTable[6] = { "Auto", "Europe/Africa", "USA/Canada", "Australia/Chile", "New Zeeland", "Izrael" }; const char *FreqPlanTable[6] = { "Auto", "Europe/Africa", "USA/Canada", "Australia/Chile", "New Zeeland", "Izrael" };
@ -252,7 +252,7 @@ static void ParmForm_Stratux(httpd_req_t *Req) // Connection to Stratux WiFi par
{ char Line[16]; int Len; { char Line[16]; int Len;
httpd_resp_sendstr_chunk(Req, "<h2>Stratux</h2>"); httpd_resp_sendstr_chunk(Req, "<h2>Stratux</h2>");
httpd_resp_sendstr_chunk(Req, "<form action=\"/parm.html\" method=\"get\" id=\"Stratux\">\n"); httpd_resp_sendstr_chunk(Req, "<form action=\"/parm.html\" method=\"POST\" id=\"Stratux\">\n");
Begin_Control_Row(Req, "SSID"); Begin_Control_Row(Req, "SSID");
httpd_resp_sendstr_chunk(Req, "<input type=\"text\" name=\"StratuxWIFI\" size=\"10\" value=\""); httpd_resp_sendstr_chunk(Req, "<input type=\"text\" name=\"StratuxWIFI\" size=\"10\" value=\"");
@ -302,7 +302,7 @@ static void ParmForm_AP(httpd_req_t *Req) // Wi-Fi access point parameters { cha
{ char Line[16]; int Len; { char Line[16]; int Len;
httpd_resp_sendstr_chunk(Req, "<h2>Wi-Fi AP</h2>"); httpd_resp_sendstr_chunk(Req, "<h2>Wi-Fi AP</h2>");
httpd_resp_sendstr_chunk(Req, "<form action=\"/parm.html\" method=\"get\" id=\"AP\">\n"); httpd_resp_sendstr_chunk(Req, "<form action=\"/parm.html\" method=\"POST\" id=\"AP\">\n");
Begin_Control_Row(Req, "SSID"); Begin_Control_Row(Req, "SSID");
httpd_resp_sendstr_chunk(Req, "<input type=\"text\" name=\"APname\" size=\"10\" value=\""); httpd_resp_sendstr_chunk(Req, "<input type=\"text\" name=\"APname\" size=\"10\" value=\"");
@ -341,6 +341,17 @@ static void ParmForm_AP(httpd_req_t *Req) // Wi-Fi access point parameters { cha
httpd_resp_sendstr_chunk(Req, "</form>\n"); } httpd_resp_sendstr_chunk(Req, "</form>\n"); }
#endif #endif
static void ParmForm_Restart(httpd_req_t *Req)
{
httpd_resp_sendstr_chunk(Req, "\
<form action=\"/parm.html\" method=\"POST\" onsubmit=\"return confirm('Are you sure to restart?')\">\n\
<input type=\"submit\" value=\"Restart\">\n\
<input type=\"hidden\" name=\"Restart\" value=\"1\">\n\
</form>\n");
}
// ============================================================================================================ // ============================================================================================================
static void Table_GPS(httpd_req_t *Req) static void Table_GPS(httpd_req_t *Req)
@ -656,6 +667,7 @@ static void Html_Start(httpd_req_t *Req, const char *Title, const uint8_t Active
httpd_resp_sendstr_chunk(Req, Title); httpd_resp_sendstr_chunk(Req, Title);
httpd_resp_sendstr_chunk(Req, "</title>\n\ httpd_resp_sendstr_chunk(Req, "</title>\n\
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n\ <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n\
<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />\n\
<style>\n\ <style>\n\
html {margin: 0px;}\n\ html {margin: 0px;}\n\
body {margin: 8px;}\n\ body {margin: 8px;}\n\
@ -699,31 +711,69 @@ static void Html_End(httpd_req_t *Req)
// ============================================================================================================ // ============================================================================================================
static esp_err_t parm_get_handler(httpd_req_t *Req) static esp_err_t parm_post_handler(httpd_req_t *Req)
{ // char Line[32]; int Len; {
bool Restart=0; bool Restart=0;
uint16_t URLlen=httpd_req_get_url_query_len(Req); /* Destination buffer for content of HTTP POST request.
if(URLlen) * httpd_req_recv() accepts char* only, but content could
{ char *URL = (char *)malloc(URLlen+1); * as well be any binary data (needs type casting).
httpd_req_get_url_query_str(Req, URL, URLlen+1); * In case of string data, null termination will be absent, and
#ifdef DEBUG_PRINT * content length would give length of string */
xSemaphoreTake(CONS_Mutex, portMAX_DELAY); char content[1024];
Format_String(CONS_UART_Write, "parm_get_handler() => ["); char *URL = (char *)malloc(Req->content_len+1);
Format_SignDec(CONS_UART_Write, URLlen);
Format_String(CONS_UART_Write, "] ");
Format_String(CONS_UART_Write, URL);
Format_String(CONS_UART_Write, "\n");
xSemaphoreGive(CONS_Mutex);
#endif
char *Line=URL;
Restart = strstr(Line,"Restart=1");
for( ; ; )
{ Parameters.ReadLine(Line);
Line = strchr(Line, '&'); if(Line==0) break;
Line++; }
free(URL);
Parameters.WriteToNVS(); }
/* Truncate if content length larger than the buffer */
size_t recv_size = std::min(Req->content_len, sizeof(content));
int ret = httpd_req_recv(Req, URL, recv_size);
if (ret <= 0) { /* 0 return value indicates connection closed */
/* Check if timeout occurred */
if (ret == HTTPD_SOCK_ERR_TIMEOUT) {
/* In case of timeout one can choose to retry calling
* httpd_req_recv(), but to keep it simple, here we
* respond with an HTTP 408 (Request Timeout) error */
httpd_resp_send_408(Req);
}
/* In case of error, returning ESP_FAIL will
* ensure that the underlying socket is closed */
return ESP_FAIL;
}
#ifdef DEBUG_PRINT
xSemaphoreTake(CONS_Mutex, portMAX_DELAY);
Format_String(CONS_UART_Write, "parm_post_handler() => [");
Format_UnsDec(CONS_UART_Write, Req->content_len, 1);
Format_String(CONS_UART_Write, "] ");
Format_String(CONS_UART_Write, URL);
Format_String(CONS_UART_Write, "\n");
xSemaphoreGive(CONS_Mutex);
#endif
char *Line=URL;
Restart = strstr(Line,"Restart=1");
for( ; ; )
{ Parameters.ReadLine(Line);
Line = strchr(Line, '&'); if(Line==0) break;
Line++; }
free(URL);
Parameters.WriteToNVS();
if(Restart)
{
#ifdef WITH_SPIFFS
FlashLog_SaveReq=1;
#endif
vTaskDelay(1000);
esp_restart(); }
// redirect to get handler
httpd_resp_set_type(Req, "text/html");
httpd_resp_set_status(Req, "302 Found");
httpd_resp_set_hdr(Req, "Location", "/parm.html");
httpd_resp_send(Req, NULL, 0);
return ESP_OK;
}
static esp_err_t parm_get_handler(httpd_req_t *Req)
{
Html_Start(Req, "OGN-Tracker configuration", 2); Html_Start(Req, "OGN-Tracker configuration", 2);
ParmForm_Acft(Req); ParmForm_Acft(Req);
@ -737,28 +787,10 @@ static esp_err_t parm_get_handler(httpd_req_t *Req)
#endif #endif
ParmForm_Other(Req); ParmForm_Other(Req);
httpd_resp_sendstr_chunk(Req, "\ ParmForm_Restart(Req);
<form action=\"/parm.html\" method=\"get\">\n\
<input type=\"submit\" value=\"Reset to defaults\">\n\
<input type=\"hidden\" name=\"Defaults\" value=\"1\">\n\
</form>\n");
httpd_resp_sendstr_chunk(Req, "\
<form action=\"/parm.html\" method=\"get\">\n\
<input type=\"submit\" value=\"Restart\">\n\
<input type=\"hidden\" name=\"Restart\" value=\"1\">\n\
</form>\n");
Html_End(Req); Html_End(Req);
if(Restart)
{
#ifdef WITH_SPIFFS
FlashLog_SaveReq=1;
#endif
vTaskDelay(1000);
esp_restart(); }
return ESP_OK; } return ESP_OK; }
static esp_err_t top_get_handler(httpd_req_t *Req) static esp_err_t top_get_handler(httpd_req_t *Req)
@ -1045,6 +1077,12 @@ static const httpd_uri_t HTTPparm =
.handler = parm_get_handler, .handler = parm_get_handler,
.user_ctx = 0 }; .user_ctx = 0 };
static const httpd_uri_t HTTPparmPost =
{ .uri = "/parm.html",
.method = HTTP_POST,
.handler = parm_post_handler,
.user_ctx = 0 };
static const httpd_uri_t HTTPlog = static const httpd_uri_t HTTPlog =
{ .uri = "/log.html", { .uri = "/log.html",
.method = HTTP_GET, .method = HTTP_GET,
@ -1061,6 +1099,7 @@ esp_err_t HTTP_Start(int MaxSockets, int Port)
esp_err_t Err=httpd_start(&HTTPserver, &Config); if(Err!=ESP_OK) return Err; esp_err_t Err=httpd_start(&HTTPserver, &Config); if(Err!=ESP_OK) return Err;
httpd_register_uri_handler(HTTPserver, &HTTPtop); // top URL httpd_register_uri_handler(HTTPserver, &HTTPtop); // top URL
httpd_register_uri_handler(HTTPserver, &HTTPparm); // parameters URL httpd_register_uri_handler(HTTPserver, &HTTPparm); // parameters URL
httpd_register_uri_handler(HTTPserver, &HTTPparmPost); // parameters URL
httpd_register_uri_handler(HTTPserver, &HTTPlog); // log files URL httpd_register_uri_handler(HTTPserver, &HTTPlog); // log files URL
httpd_register_uri_handler(HTTPserver, &HTTPlogo); // OGN logo httpd_register_uri_handler(HTTPserver, &HTTPlogo); // OGN logo
return Err; } return Err; }

Wyświetl plik

@ -632,7 +632,7 @@ CONFIG_ESP_HTTP_CLIENT_ENABLE_HTTPS=y
# #
# HTTP Server # HTTP Server
# #
CONFIG_HTTPD_MAX_REQ_HDR_LEN=512 CONFIG_HTTPD_MAX_REQ_HDR_LEN=1024
CONFIG_HTTPD_MAX_URI_LEN=512 CONFIG_HTTPD_MAX_URI_LEN=512
CONFIG_HTTPD_ERR_RESP_NO_DELAY=y CONFIG_HTTPD_ERR_RESP_NO_DELAY=y
CONFIG_HTTPD_PURGE_BUF_LEN=32 CONFIG_HTTPD_PURGE_BUF_LEN=32