Work in progress - move to httpd

This commit is contained in:
Sebastien
2019-11-26 08:29:19 -05:00
parent c40d805b44
commit 3929f3e809
8 changed files with 1222 additions and 640 deletions

View File

@@ -1,7 +1,7 @@
idf_component_register(SRCS "dns_server.c" "http_server.c" "wifi_manager.c" idf_component_register(SRCS "dns_server.c" "http_server.c" "wifi_manager.c"
INCLUDE_DIRS . INCLUDE_DIRS .
REQUIRES esp_common REQUIRES esp_common
PRIV_REQUIRES newlib freertos spi_flash nvs_flash mdns pthread wpa_supplicant cmd_system PRIV_REQUIRES newlib freertos spi_flash nvs_flash mdns pthread wpa_supplicant cmd_system esp_http_server
EMBED_FILES style.css code.js index.html bootstrap.min.css.gz jquery.min.js.gz popper.min.js.gz bootstrap.min.js.gz EMBED_FILES style.css code.js index.html bootstrap.min.css.gz jquery.min.js.gz popper.min.js.gz bootstrap.min.js.gz
) )

View File

@@ -1,638 +0,0 @@
/*
Copyright (c) 2017-2019 Tony Pottier
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
@file http_server.c
@author Tony Pottier
@brief Defines all functions necessary for the HTTP server to run.
Contains the freeRTOS task for the HTTP listener and all necessary support
function to process requests, decode URLs, serve files, etc. etc.
@note http_server task cannot run without the wifi_manager task!
@see https://idyl.io
@see https://github.com/tonyp7/esp32-wifi-manager
*/
#include "http_server.h"
#include "cmd_system.h"
#include <inttypes.h>
#include "squeezelite-ota.h"
#include "nvs_utilities.h"
#include <stdio.h>
#include <stdlib.h>
#include "cJSON.h"
#include "esp_system.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "config.h"
#define HTTP_STACK_SIZE (5*1024)
/* @brief tag used for ESP serial console messages */
static const char TAG[] = "http_server";
/* @brief task handle for the http server */
static TaskHandle_t task_http_server = NULL;
static StaticTask_t task_http_buffer;
#if RECOVERY_APPLICATION
static StackType_t task_http_stack[HTTP_STACK_SIZE];
#else
static StackType_t EXT_RAM_ATTR task_http_stack[HTTP_STACK_SIZE];
#endif
SemaphoreHandle_t http_server_config_mutex = NULL;
/**
* @brief embedded binary data.
* @see file "component.mk"
* @see https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html#embedding-binary-data
*/
extern const uint8_t style_css_start[] asm("_binary_style_css_start");
extern const uint8_t style_css_end[] asm("_binary_style_css_end");
extern const uint8_t jquery_gz_start[] asm("_binary_jquery_min_js_gz_start");
extern const uint8_t jquery_gz_end[] asm("_binary_jquery_min_js_gz_end");
extern const uint8_t popper_gz_start[] asm("_binary_popper_min_js_gz_start");
extern const uint8_t popper_gz_end[] asm("_binary_popper_min_js_gz_end");
extern const uint8_t bootstrap_js_gz_start[] asm("_binary_bootstrap_min_js_gz_start");
extern const uint8_t bootstrap_js_gz_end[] asm("_binary_bootstrap_min_js_gz_end");
extern const uint8_t bootstrap_css_gz_start[] asm("_binary_bootstrap_min_css_gz_start");
extern const uint8_t bootstrap_css_gz_end[] asm("_binary_bootstrap_min_css_gz_end");
extern const uint8_t code_js_start[] asm("_binary_code_js_start");
extern const uint8_t code_js_end[] asm("_binary_code_js_end");
extern const uint8_t index_html_start[] asm("_binary_index_html_start");
extern const uint8_t index_html_end[] asm("_binary_index_html_end");
/* const http headers stored in ROM */
const static char http_hdr_template[] = "HTTP/1.1 200 OK\nContent-type: %s\nAccept-Ranges: bytes\nContent-Length: %d\nContent-Encoding: %s\nAccess-Control-Allow-Origin: *\n\n";
const static char http_html_hdr[] = "HTTP/1.1 200 OK\nContent-type: text/html\nAccess-Control-Allow-Origin: *\nAccept-Encoding: identity\n\n";
const static char http_css_hdr[] = "HTTP/1.1 200 OK\nContent-type: text/css\nCache-Control: public, max-age=31536000\nAccess-Control-Allow-Origin: *\n\n";
const static char http_js_hdr[] = "HTTP/1.1 200 OK\nContent-type: text/javascript\nAccess-Control-Allow-Origin: *\n\n";
const static char http_400_hdr[] = "HTTP/1.1 400 Bad Request\nContent-Length: 0\n\n";
const static char http_404_hdr[] = "HTTP/1.1 404 Not Found\nContent-Length: 0\n\n";
const static char http_503_hdr[] = "HTTP/1.1 503 Service Unavailable\nContent-Length: 0\n\n";
const static char http_ok_json_no_cache_hdr[] = "HTTP/1.1 200 OK\nContent-type: application/json\nCache-Control: no-store, no-cache, must-revalidate, max-age=0\nPragma: no-cache\nAccess-Control-Allow-Origin: *\nAccept-Encoding: identity\n\n";
const static char http_redirect_hdr_start[] = "HTTP/1.1 302 Found\nLocation: http://";
const static char http_redirect_hdr_end[] = "/\n\n";
void http_server_start() {
ESP_LOGD(TAG, "http_server_start ");
if(task_http_server == NULL) {
task_http_server = xTaskCreateStatic( (TaskFunction_t) &http_server, "http_server", HTTP_STACK_SIZE, NULL,
WIFI_MANAGER_TASK_PRIORITY, task_http_stack, &task_http_buffer);
}
}
void http_server(void *pvParameters) {
http_server_config_mutex = xSemaphoreCreateMutex();
struct netconn *conn, *newconn;
err_t err;
conn = netconn_new(NETCONN_TCP);
netconn_bind(conn, IP_ADDR_ANY, 80);
netconn_listen(conn);
ESP_LOGI(TAG, "HTTP Server listening on 80/tcp");
do {
err = netconn_accept(conn, &newconn);
if(err == ERR_OK) {
http_server_netconn_serve(newconn);
netconn_delete(newconn);
}
else
{
ESP_LOGE(TAG, "Error accepting new connection. Terminating HTTP server");
}
taskYIELD(); /* allows the freeRTOS scheduler to take over if needed. */
} while(err == ERR_OK);
netconn_close(conn);
netconn_delete(conn);
vSemaphoreDelete(http_server_config_mutex);
http_server_config_mutex = NULL;
vTaskDelete( NULL );
}
char* http_server_get_header(char *request, char *header_name, int *len) {
*len = 0;
char *ret = NULL;
char *ptr = NULL;
ptr = strstr(request, header_name);
if(ptr) {
ret = ptr + strlen(header_name);
ptr = ret;
while (*ptr != '\0' && *ptr != '\n' && *ptr != '\r') {
(*len)++;
ptr++;
}
return ret;
}
return NULL;
}
char* http_server_search_header(char *request, char *header_name, int *len, char ** parm_name, char ** next_position, char * bufEnd) {
*len = 0;
char *ret = NULL;
char *ptr = NULL;
int currentLength=0;
ESP_LOGV(TAG, "searching for header name: [%s]", header_name);
ptr = strstr(request, header_name);
if(ptr!=NULL && ptr<bufEnd) {
ret = ptr + strlen(header_name);
ptr = ret;
currentLength=(int)(ptr-request);
ESP_LOGV(TAG, "found string at %d", currentLength);
while (*ptr != '\0' && *ptr != '\n' && *ptr != '\r' && *ptr != ':' && ptr<bufEnd) {
ptr++;
}
if(*ptr==':') {
currentLength=(int)(ptr-ret);
ESP_LOGV(TAG, "Found parameter name end, length : %d", currentLength);
// save the parameter name: the string between header name and ":"
*parm_name=malloc(currentLength+1);
if(*parm_name==NULL) {
ESP_LOGE(TAG, "Unable to allocate memory for new header name");
return NULL;
}
memset(*parm_name, 0x00,currentLength+1);
strncpy(*parm_name,ret,currentLength);
ESP_LOGV(TAG, "Found parameter name : %s ", *parm_name);
ptr++;
while (*ptr == ' ' && ptr<bufEnd) {
ptr++;
}
}
ret=ptr;
while (*ptr != '\0' && *ptr != '\n' && *ptr != '\r'&& ptr<bufEnd) {
(*len)++;
ptr++;
}
// Terminate value inside its actual buffer so we can treat it as individual string
*ptr='\0';
currentLength=(int)(ptr-ret);
ESP_LOGV(TAG, "Found parameter value end, length : %d, value: %s", currentLength,ret );
*next_position=++ptr;
return ret;
}
ESP_LOGD(TAG, "No more match for : %s", header_name);
return NULL;
}
void http_server_send_resource_file(struct netconn *conn,const uint8_t * start, const uint8_t * end, char * content_type,char * encoding) {
uint16_t len=end - start;
size_t buff_length= sizeof(http_hdr_template)+strlen(content_type)+strlen(encoding);
char * http_hdr=malloc(buff_length);
if( http_hdr == NULL) {
ESP_LOGE(TAG, "Cound not allocate %d bytes for headers.",buff_length);
netconn_write(conn, http_503_hdr, sizeof(http_503_hdr) - 1, NETCONN_NOCOPY);
}
else
{
memset(http_hdr,0x00,buff_length);
snprintf(http_hdr, buff_length-1,http_hdr_template,content_type,len,encoding);
netconn_write(conn, http_hdr, strlen(http_hdr), NETCONN_NOCOPY);
ESP_LOGD(TAG, "sending response : %s",http_hdr);
netconn_write(conn, start, end - start, NETCONN_NOCOPY);
free(http_hdr);
}
}
err_t http_server_send_config_json(struct netconn *conn) {
char * json = config_alloc_get_json(false);
if(json!=NULL){
ESP_LOGD(TAG, "config json : %s",json );
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY);
netconn_write(conn, json, strlen(json), NETCONN_NOCOPY);
free(json);
}
else{
ESP_LOGD(TAG, "Error retrieving config json string. ");
netconn_write(conn, http_503_hdr, sizeof(http_503_hdr) - 1, NETCONN_NOCOPY);
}
return ESP_OK;
}
void http_server_process_config(struct netconn *conn, char *inbuf) {
// Here, we are passed a buffer which contains the http request
// netbuf_data(inbuf, (void**)&buf, &buflen);
// err = netconn_recv(conn, &inbuf);
// if(err == ERR_OK) {
//
// /* extract the first line of the request */
// char *save_ptr = buf;
// char *line = strtok_r(save_ptr, new_line, &save_ptr);
// ESP_LOGD(TAG, "Processing line %s",line);
ESP_LOGD(TAG, "Processing request buffer: \n%s",inbuf);
char *last = NULL;
char *ptr = NULL;
last = ptr = inbuf;
bool bHeaders= true;
while(ptr!=NULL && *ptr != '\0') {
// Move to the end of the line, or to the end of the buffer
if(bHeaders) {
while (*ptr != '\0' && *ptr != '\n' && *ptr != '\r') {
ptr++;
}
// terminate the header string
if( *(ptr) == '\0' ) {
ESP_LOGD(TAG, "End of buffer found");
return;
}
*ptr = '\0';
if( *(ptr+1) == '\n' ) {
*(ptr+1)='\0';
ptr+=2;
}
if(ptr==last) {
ESP_LOGD(TAG, "Processing body. ");
break;
}
if(strlen(last)>0) {
ESP_LOGD(TAG, "Found Header Line %s ", last);
//Content-Type: application/json
}
else {
ESP_LOGD(TAG, "Found end of headers");
bHeaders = false;
}
last=ptr;
}
else {
//ESP_LOGD(TAG, "Body content: %s", last);
//cJSON * json = cJSON_Parse(last);
//cJSON_Delete(json);
//todo: implement body json parsing
// right now, body is coming as compressed, so we need some type of decompression to happen.
return;
}
}
return ;
}
void dump_net_buffer(void * buf, u16_t buflen) {
char * curbuf = malloc(buflen+1);
ESP_LOGV(TAG, "netconn buffer, length=%u",buflen);
if(curbuf==NULL) {
ESP_LOGE(TAG, "Unable to show netconn buffer. Malloc failed");
}
memset(curbuf,0x0, buflen+1);
memcpy(curbuf,buf,buflen);
ESP_LOGV(TAG, "netconn buffer content:\n%s",curbuf);
free(curbuf);
}
void http_server_netconn_serve(struct netconn *conn) {
struct netbuf *inbuf;
char *buf = NULL;
u16_t buflen;
err_t err;
ip_addr_t remote_add;
u16_t port;
ESP_LOGV(TAG, "Serving page. Getting device AP address.");
const char new_line[2] = "\n";
char * ap_ip_address= config_alloc_get_default(NVS_TYPE_STR, "ap_ip_address", DEFAULT_AP_IP, 0);
if(ap_ip_address==NULL){
ESP_LOGE(TAG, "Unable to retrieve default AP IP Address");
netconn_write(conn, http_503_hdr, sizeof(http_503_hdr) - 1, NETCONN_NOCOPY);
netconn_close(conn);
return;
}
ESP_LOGV(TAG, "Getting remote device IP address.");
netconn_getaddr(conn, &remote_add, &port, 0);
char * remote_address = strdup(ip4addr_ntoa(ip_2_ip4(&remote_add)));
ESP_LOGD(TAG, "Local Access Point IP address is: %s. Remote device IP address is %s. Receiving request buffer", ap_ip_address, remote_address);
err = netconn_recv(conn, &inbuf);
if(err == ERR_OK) {
ESP_LOGV(TAG, "Getting data buffer.");
netbuf_data(inbuf, (void**)&buf, &buflen);
dump_net_buffer(buf, buflen);
int lenH = 0;
/* extract the first line of the request */
char *save_ptr = buf;
char *line = strtok_r(save_ptr, new_line, &save_ptr);
char *temphost = http_server_get_header(save_ptr, "Host: ", &lenH);
char * host = malloc(lenH+1);
memset(host,0x00,lenH+1);
if(lenH>0){
strlcpy(host,temphost,lenH+1);
}
ESP_LOGD(TAG, "http_server_netconn_serve Host: [%s], host: [%s], Processing line [%s]",remote_address,host,line);
if(line) {
/* captive portal functionality: redirect to access point IP for HOST that are not the access point IP OR the STA IP */
const char * host_name=NULL;
if((err=tcpip_adapter_get_hostname(TCPIP_ADAPTER_IF_STA, &host_name )) !=ESP_OK) {
ESP_LOGE(TAG, "Unable to get host name. Error: %s",esp_err_to_name(err));
}
else {
ESP_LOGI(TAG,"System host name %s, http requested host: %s.",host_name, host);
}
/* determine if Host is from the STA IP address */
wifi_manager_lock_sta_ip_string(portMAX_DELAY);
bool access_from_sta_ip = lenH > 0?strcasestr(host, wifi_manager_get_sta_ip_string()):false;
wifi_manager_unlock_sta_ip_string();
bool access_from_host_name = (host_name!=NULL) && strcasestr(host,host_name);
if(lenH > 0 && !strcasestr(host, ap_ip_address) && !(access_from_sta_ip || access_from_host_name)) {
ESP_LOGI(TAG, "Redirecting host [%s] to AP IP Address : %s",remote_address, ap_ip_address);
netconn_write(conn, http_redirect_hdr_start, sizeof(http_redirect_hdr_start) - 1, NETCONN_NOCOPY);
netconn_write(conn, ap_ip_address, strlen(ap_ip_address), NETCONN_NOCOPY);
netconn_write(conn, http_redirect_hdr_end, sizeof(http_redirect_hdr_end) - 1, NETCONN_NOCOPY);
}
else {
//static stuff
/* default page */
if(strstr(line, "GET / ")) {
netconn_write(conn, http_html_hdr, sizeof(http_html_hdr) - 1, NETCONN_NOCOPY);
netconn_write(conn, index_html_start, index_html_end- index_html_start, NETCONN_NOCOPY);
}
else if(strstr(line, "GET /code.js ")) {
netconn_write(conn, http_js_hdr, sizeof(http_js_hdr) - 1, NETCONN_NOCOPY);
netconn_write(conn, code_js_start, code_js_end - code_js_start, NETCONN_NOCOPY);
}
else if(strstr(line, "GET /style.css ")) {
netconn_write(conn, http_css_hdr, sizeof(http_css_hdr) - 1, NETCONN_NOCOPY);
netconn_write(conn, style_css_start, style_css_end - style_css_start, NETCONN_NOCOPY);
}
else if(strstr(line, "GET /jquery.js ")) {
http_server_send_resource_file(conn,jquery_gz_start, jquery_gz_end, "text/javascript", "gzip" );
}
else if(strstr(line, "GET /popper.js ")) {
http_server_send_resource_file(conn,popper_gz_start, popper_gz_end, "text/javascript", "gzip" );
}
else if(strstr(line, "GET /bootstrap.js ")) {
http_server_send_resource_file(conn,bootstrap_js_gz_start, bootstrap_js_gz_end, "text/javascript", "gzip" );
}
else if(strstr(line, "GET /bootstrap.css ")) {
http_server_send_resource_file(conn,bootstrap_css_gz_start, bootstrap_css_gz_end, "text/css", "gzip" );
}
//dynamic stuff
else if(strstr(line, "GET /scan.json ")) {
ESP_LOGI(TAG, "Starting wifi scan");
wifi_manager_scan_async();
}
else if(strstr(line, "GET /ap.json ")) {
/* if we can get the mutex, write the last version of the AP list */
ESP_LOGI(TAG, "Processing ap.json request");
if(wifi_manager_lock_json_buffer(( TickType_t ) 10)) {
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY);
char *buff = wifi_manager_alloc_get_ap_list_json();
wifi_manager_unlock_json_buffer();
if(buff!=NULL){
netconn_write(conn, buff, strlen(buff), NETCONN_NOCOPY);
free(buff);
}
else {
ESP_LOGD(TAG, "Error retrieving ap list json string. ");
netconn_write(conn, http_503_hdr, sizeof(http_503_hdr) - 1, NETCONN_NOCOPY);
}
}
else {
netconn_write(conn, http_503_hdr, sizeof(http_503_hdr) - 1, NETCONN_NOCOPY);
ESP_LOGE(TAG, "http_server_netconn_serve: GET /ap.json failed to obtain mutex");
}
/* request a wifi scan */
ESP_LOGI(TAG, "Starting wifi scan");
wifi_manager_scan_async();
ESP_LOGI(TAG, "Done serving ap.json");
}
else if(strstr(line, "GET /config.json ")) {
ESP_LOGI(TAG, "Serving config.json");
ESP_LOGI(TAG, "About to get config from flash");
http_server_send_config_json(conn);
ESP_LOGD(TAG, "Done serving config.json");
}
else if(strstr(line, "POST /config.json ")) {
ESP_LOGI(TAG, "Serving POST config.json");
int lenA=0;
char * last_parm=save_ptr;
char * next_parm=save_ptr;
char * last_parm_name=NULL;
bool bErrorFound=false;
bool bOTA=false;
char * otaURL=NULL;
// todo: implement json body parsing
//http_server_process_config(conn,save_ptr);
while(last_parm!=NULL) {
// Search will return
ESP_LOGD(TAG, "Getting parameters from X-Custom headers");
last_parm = http_server_search_header(next_parm, "X-Custom-", &lenA, &last_parm_name,&next_parm,buf+buflen);
if(last_parm!=NULL && last_parm_name!=NULL) {
ESP_LOGI(TAG, "http_server_netconn_serve: POST config.json, config %s=%s", last_parm_name, last_parm);
if(strcmp(last_parm_name, "fwurl")==0) {
// we're getting a request to do an OTA from that URL
ESP_LOGW(TAG, "Found OTA request!");
otaURL=strdup(last_parm);
bOTA=true;
}
else {
ESP_LOGV(TAG, "http_server_netconn_serve: POST config.json Storing parameter");
if(config_set_value(NVS_TYPE_STR, last_parm_name , last_parm) != ESP_OK){
ESP_LOGE(TAG, "Unable to save nvs value.");
}
}
}
if(last_parm_name!=NULL) {
free(last_parm_name);
last_parm_name=NULL;
}
}
if(bErrorFound) {
netconn_write(conn, http_400_hdr, sizeof(http_400_hdr) - 1, NETCONN_NOCOPY); //400 invalid request
}
else {
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY); //200ok
if(bOTA) {
#if RECOVERY_APPLICATION
ESP_LOGW(TAG, "Starting process OTA for url %s",otaURL);
#else
ESP_LOGW(TAG, "Restarting system to process OTA for url %s",otaURL);
#endif
wifi_manager_reboot_ota(otaURL);
free(otaURL);
}
}
ESP_LOGI(TAG, "Done Serving POST config.json");
}
else if(strstr(line, "POST /connect.json ")) {
ESP_LOGI(TAG, "http_server_netconn_serve: POST /connect.json");
bool found = false;
int lenS = 0, lenP = 0, lenN = 0;
char *ssid = NULL, *password = NULL;
ssid = http_server_get_header(save_ptr, "X-Custom-ssid: ", &lenS);
password = http_server_get_header(save_ptr, "X-Custom-pwd: ", &lenP);
char * new_host_name_b = http_server_get_header(save_ptr, "X-Custom-host_name: ", &lenN);
if(lenN > 0){
lenN++;
char * new_host_name = malloc(lenN);
strlcpy(new_host_name, new_host_name_b, lenN);
if(config_set_value(NVS_TYPE_STR, "host_name", new_host_name) != ESP_OK){
ESP_LOGE(TAG, "Unable to save host name configuration");
}
free(new_host_name);
}
if(ssid && lenS <= MAX_SSID_SIZE && password && lenP <= MAX_PASSWORD_SIZE) {
wifi_config_t* config = wifi_manager_get_wifi_sta_config();
memset(config, 0x00, sizeof(wifi_config_t));
memcpy(config->sta.ssid, ssid, lenS);
memcpy(config->sta.password, password, lenP);
ESP_LOGD(TAG, "http_server_netconn_serve: wifi_manager_connect_async() call, with ssid: %s, password: %s", config->sta.ssid, config->sta.password);
wifi_manager_connect_async();
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY); //200ok
found = true;
}
else{
ESP_LOGE(TAG, "SSID or Password invalid");
}
if(!found) {
/* bad request the authentification header is not complete/not the correct format */
netconn_write(conn, http_400_hdr, sizeof(http_400_hdr) - 1, NETCONN_NOCOPY);
ESP_LOGE(TAG, "bad request the authentification header is not complete/not the correct format");
}
ESP_LOGI(TAG, "http_server_netconn_serve: done serving connect.json");
}
else if(strstr(line, "DELETE /connect.json ")) {
ESP_LOGI(TAG, "http_server_netconn_serve: DELETE /connect.json");
/* request a disconnection from wifi and forget about it */
wifi_manager_disconnect_async();
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY); /* 200 ok */
ESP_LOGI(TAG, "http_server_netconn_serve: done serving DELETE /connect.json");
}
else if(strstr(line, "POST /reboot_ota.json ")) {
ESP_LOGI(TAG, "http_server_netconn_serve: POST reboot_ota.json");
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY); /* 200 ok */
wifi_manager_reboot(OTA);
ESP_LOGI(TAG, "http_server_netconn_serve: done serving POST reboot_ota.json");
}
else if(strstr(line, "POST /reboot.json ")) {
ESP_LOGI(TAG, "http_server_netconn_serve: POST reboot.json");
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY); /* 200 ok */
wifi_manager_reboot(RESTART);
ESP_LOGI(TAG, "http_server_netconn_serve: done serving POST reboot.json");
}
else if(strstr(line, "POST /recovery.json ")) {
ESP_LOGI(TAG, "http_server_netconn_serve: POST recovery.json");
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY); /* 200 ok */
wifi_manager_reboot(RECOVERY);
ESP_LOGI(TAG, "http_server_netconn_serve: done serving POST recovery.json");
}
else if(strstr(line, "GET /status.json ")) {
ESP_LOGI(TAG, "Serving status.json");
if(wifi_manager_lock_json_buffer(( TickType_t ) 10)) {
char *buff = wifi_manager_alloc_get_ip_info_json();
wifi_manager_unlock_json_buffer();
if(buff) {
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY);
netconn_write(conn, buff, strlen(buff), NETCONN_NOCOPY);
free(buff);
}
else {
netconn_write(conn, http_503_hdr, sizeof(http_503_hdr) - 1, NETCONN_NOCOPY);
}
}
else {
netconn_write(conn, http_503_hdr, sizeof(http_503_hdr) - 1, NETCONN_NOCOPY);
ESP_LOGE(TAG, "http_server_netconn_serve: GET /status failed to obtain mutex");
}
ESP_LOGI(TAG, "Done Serving status.json");
}
else {
netconn_write(conn, http_400_hdr, sizeof(http_400_hdr) - 1, NETCONN_NOCOPY);
ESP_LOGE(TAG, "bad request from host: %s, request %s",remote_address, line);
}
}
}
else {
ESP_LOGE(TAG, "URL not found processing for remote host : %s",remote_address);
netconn_write(conn, http_404_hdr, sizeof(http_404_hdr) - 1, NETCONN_NOCOPY);
}
free(host);
}
free(ap_ip_address);
free(remote_address);
netconn_close(conn);
netbuf_delete(inbuf);
/* free the buffer */
}
bool http_server_lock_json_object(TickType_t xTicksToWait) {
ESP_LOGD(TAG, "Locking config json object");
if(http_server_config_mutex) {
if( xSemaphoreTake( http_server_config_mutex, xTicksToWait ) == pdTRUE ) {
ESP_LOGV(TAG, "config Json object locked!");
return true;
}
else {
ESP_LOGW(TAG, "Semaphore take failed. Unable to lock config Json object mutex");
return false;
}
}
else {
ESP_LOGW(TAG, "Unable to lock config Json object mutex");
return false;
}
}
void http_server_unlock_json_object() {
ESP_LOGD(TAG, "Unlocking json buffer!");
xSemaphoreGive( http_server_config_mutex );
}
void strreplace(char *src, char *str, char *rep)
{
char *p = strstr(src, str);
if(p)
{
int len = strlen(src)+strlen(rep)-strlen(str);
char r[len];
memset(r, 0, len);
if( p >= src ) {
strncpy(r, src, p-src);
r[p-src]='\0';
strncat(r, rep, strlen(rep));
strncat(r, p+strlen(str), p+strlen(str)-src+strlen(src));
strcpy(src, r);
strreplace(p+strlen(rep), str, rep);
}
}
}

View File

@@ -0,0 +1,699 @@
/*
Copyright (c) 2017-2019 Tony Pottier
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
@file http_server.c
@author Tony Pottier
@brief Defines all functions necessary for the HTTP server to run.
Contains the freeRTOS task for the HTTP listener and all necessary support
function to process requests, decode URLs, serve files, etc. etc.
@note http_server task cannot run without the wifi_manager task!
@see https://idyl.io
@see https://github.com/tonyp7/esp32-wifi-manager
*/
#include "http_server_handlers.h"
#include "esp_http_server.h"
#include "cmd_system.h"
#include <inttypes.h>
#include "squeezelite-ota.h"
#include "nvs_utilities.h"
#include <stdio.h>
#include <stdlib.h>
#include "cJSON.h"
#include "esp_system.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "config.h"
#define HTTP_STACK_SIZE (5*1024)
#define FREE_AND_NULL(p) if(p!=NULL){ free(p); p=NULL;}
/* @brief tag used for ESP serial console messages */
static const char TAG[] = "http_server";
/* @brief task handle for the http server */
static TaskHandle_t task_http_server = NULL;
static StaticTask_t task_http_buffer;
#if RECOVERY_APPLICATION
static StackType_t task_http_stack[HTTP_STACK_SIZE];
#else
static StackType_t EXT_RAM_ATTR task_http_stack[HTTP_STACK_SIZE];
#endif
SemaphoreHandle_t http_server_config_mutex = NULL;
#define AUTH_TOKEN_SIZE 50
typedef struct session_context {
char * auth_token;
bool authenticated;
} session_context_t;
/**
* @brief embedded binary data.
* @see file "component.mk"
* @see https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html#embedding-binary-data
*/
extern const uint8_t style_css_start[] asm("_binary_style_css_start");
extern const uint8_t style_css_end[] asm("_binary_style_css_end");
extern const uint8_t jquery_gz_start[] asm("_binary_jquery_min_js_gz_start");
extern const uint8_t jquery_gz_end[] asm("_binary_jquery_min_js_gz_end");
extern const uint8_t popper_gz_start[] asm("_binary_popper_min_js_gz_start");
extern const uint8_t popper_gz_end[] asm("_binary_popper_min_js_gz_end");
extern const uint8_t bootstrap_js_gz_start[] asm("_binary_bootstrap_min_js_gz_start");
extern const uint8_t bootstrap_js_gz_end[] asm("_binary_bootstrap_min_js_gz_end");
extern const uint8_t bootstrap_css_gz_start[] asm("_binary_bootstrap_min_css_gz_start");
extern const uint8_t bootstrap_css_gz_end[] asm("_binary_bootstrap_min_css_gz_end");
extern const uint8_t code_js_start[] asm("_binary_code_js_start");
extern const uint8_t code_js_end[] asm("_binary_code_js_end");
extern const uint8_t index_html_start[] asm("_binary_index_html_start");
extern const uint8_t index_html_end[] asm("_binary_index_html_end");
//
//
///* const http headers stored in ROM */
//const static char http_hdr_template[] = "HTTP/1.1 200 OK\nContent-type: %s\nAccept-Ranges: bytes\nContent-Length: %d\nContent-Encoding: %s\nAccess-Control-Allow-Origin: *\n\n";
//const static char http_html_hdr[] = "HTTP/1.1 200 OK\nContent-type: text/html\nAccess-Control-Allow-Origin: *\nAccept-Encoding: identity\n\n";
//const static char http_css_hdr[] = "HTTP/1.1 200 OK\nContent-type: text/css\nCache-Control: public, max-age=31536000\nAccess-Control-Allow-Origin: *\n\n";
//const static char http_js_hdr[] = "HTTP/1.1 200 OK\nContent-type: text/javascript\nAccess-Control-Allow-Origin: *\n\n";
//const static char http_400_hdr[] = "HTTP/1.1 400 Bad Request\nContent-Length: 0\n\n";
//const static char http_404_hdr[] = "HTTP/1.1 404 Not Found\nContent-Length: 0\n\n";
//const static char http_503_hdr[] = "HTTP/1.1 503 Service Unavailable\nContent-Length: 0\n\n";
//const static char http_ok_json_no_cache_hdr[] = "HTTP/1.1 200 OK\nContent-type: application/json\nCache-Control: no-store, no-cache, must-revalidate, max-age=0\nPragma: no-cache\nAccess-Control-Allow-Origin: *\nAccept-Encoding: identity\n\n";
//const static char http_redirect_hdr_start[] = "HTTP/1.1 302 Found\nLocation: http://";
//const static char http_redirect_hdr_end[] = "/\n\n";
/* Custom function to free context */
void free_ctx_func(void *ctx)
{
if(ctx){
if(ctx->auth_token) free(auth_token);
free(ctx);
}
}
bool is_user_authenticated(httpd_req_t *req){
if (! req->sess_ctx) {
req->sess_ctx = malloc(sizeof(session_context_t));
memset(req->sess_ctx,0x00,sizeof(session_context_t));
req->free_ctx = free_ctx_func;
}
session_context_t *ctx_data = (session_context_t*)req->sess_ctx;
if(ctx_data->authenticated){
ESP_LOGD(TAG,"User is authenticated.");
return true;
}
// todo: ask for user to authenticate
return false;
}
/* Copies the full path into destination buffer and returns
* pointer to path (skipping the preceding base path) */
static const char* get_path_from_uri(char *dest, const char *base_path, const char *uri, size_t destsize)
{
const size_t base_pathlen = strlen(base_path);
size_t pathlen = strlen(uri);
const char *quest = strchr(uri, '?');
if (quest) {
pathlen = MIN(pathlen, quest - uri);
}
const char *hash = strchr(uri, '#');
if (hash) {
pathlen = MIN(pathlen, hash - uri);
}
if (base_pathlen + pathlen + 1 > destsize) {
/* Full path string won't fit into destination buffer */
return NULL;
}
/* Construct full path (base + path) */
strcpy(dest, base_path);
strlcpy(dest + base_pathlen, uri, pathlen + 1);
/* Return pointer to path, skipping the base */
return dest + base_pathlen;
}
#define IS_FILE_EXT(filename, ext) \
(strcasecmp(&filename[strlen(filename) - sizeof(ext) + 1], ext) == 0)
/* Set HTTP response content type according to file extension */
static esp_err_t set_content_type_from_file(httpd_req_t *req, const char *filename)
{
if (IS_FILE_EXT(filename, ".pdf")) {
return httpd_resp_set_type(req, "application/pdf");
} else if (IS_FILE_EXT(filename, ".html")) {
return httpd_resp_set_type(req, "text/html");
} else if (IS_FILE_EXT(filename, ".jpeg")) {
return httpd_resp_set_type(req, "image/jpeg");
} else if (IS_FILE_EXT(filename, ".ico")) {
return httpd_resp_set_type(req, "image/x-icon");
} else if (IS_FILE_EXT(filename, ".ico")) {
return httpd_resp_set_type(req, "image/x-icon");
} else if (IS_FILE_EXT(filename, ".css")) {
return httpd_resp_set_type(req, "text/css");
} else if (IS_FILE_EXT(filename, ".js")) {
return httpd_resp_set_type(req, "text/javascript");
} else if (IS_FILE_EXT(filename, ".json")) {
return httpd_resp_set_type(req, "application/json");
}
/* This is a limited set only */
/* For any other type always set as plain text */
return httpd_resp_set_type(req, "text/plain");
}
static esp_err_t set_content_type_from_req(httpd_req_t *req)
{
char filepath[FILE_PATH_MAX];
const char *filename = get_path_from_uri(filepath, "/" ,
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;
}
/* If name has trailing '/', respond with directory contents */
if (filename[strlen(filename) - 1] == '/') {
httpd_resp_send_err(req, HTTPD_405_METHOD_NOT_ALLOWED, "Browsing files forbidden.");
return ESP_FAIL;
}
return ESP_OK;
}
esp_err_t root_get_handler(httpd_req_t *req){
ESP_LOGI(TAG, "serving [%s]", req->uri);
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
httpd_resp_set_hdr(req, "Accept-Encoding", "identity");
if(!is_user_authenticated(httpd_req_t *req)){
// todo: send password entry page and return
}
const size_t file_size = (index_html_end - index_html_start);
esp_err_t err = set_content_type_from_req(req);
if(err == ESP_OK){
httpd_resp_send(req, (const char *)index_html_start, file_size);
}
return err;
}
esp_err_t resource_filehandler(httpd_req_t *req){
char filepath[FILE_PATH_MAX];
ESP_LOGI(TAG, "serving [%s]", req->uri);
const char *filename = get_path_from_uri(filepath, "/res/" ,
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;
}
/* If name has trailing '/', respond with directory contents */
if (filename[strlen(filename) - 1] == '/') {
httpd_resp_send_err(req, HTTPD_405_METHOD_NOT_ALLOWED, "Browsing files forbidden.");
return ESP_FAIL;
}
if(strstr(line, "GET /code.js ")) {
const size_t file_size = (code_js_end - code_js_start);
set_content_type_from_file(req, filename);
httpd_resp_send(req, (const char *)code_js_start, file_size);
}
else if(strstr(line, "GET /style.css ")) {
set_content_type_from_file(req, filename);
const size_t file_size = (style_css_end - style_css_start);
httpd_resp_send(req, (const char *)style_css_start, file_size);
} else if(strstr(line, "GET /jquery.js ")) {
set_content_type_from_file(req, filename);
httpd_resp_set_hdr(req, "Content-Encoding", "gzip");
const size_t file_size = (jquery_gz_end - jquery_gz_start);
httpd_resp_send(req, (const char *)jquery_gz_start, file_size);
}else if(strstr(line, "GET /popper.js")) {
set_content_type_from_file(req, filename);
httpd_resp_set_hdr(req, "Content-Encoding", "gzip");
const size_t file_size = (popper_gz_end - popper_gz_start);
httpd_resp_send(req, (const char *)popper_gz_start, file_size);
}
else if(strstr(line, "GET /bootstrap.js ")) {
set_content_type_from_file(req, filename);
httpd_resp_set_hdr(req, "Content-Encoding", "gzip");
const size_t file_size = (bootstrap_js_gz_end - bootstrap_js_gz_start);
httpd_resp_send(req, (const char *)bootstrap_js_gz_start, file_size);
}
else if(strstr(line, "GET /bootstrap.css")) {
set_content_type_from_file(req, filename);
httpd_resp_set_hdr(req, "Content-Encoding", "gzip");
const size_t file_size = (bootstrap_css_gz_end - bootstrap_css_gz_start);
httpd_resp_send(req, (const char *)bootstrap_css_gz_start, file_size);
}
else {
ESP_LOGE(TAG, "Unknown resource: %s", filepath);
/* Respond with 404 Not Found */
httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, "File does not exist");
return ESP_FAIL;
}
ESP_LOGI(TAG, "Resource sending complete");
return ESP_OK;
}
esp_err_t ap_scan_handler(httpd_req_t *req){
const char empty[] = "{}";
ESP_LOGI(TAG, "serving [%s]", req->uri);
if(!is_user_authenticated(httpd_req_t *req)){
// todo: redirect to login page
// return ESP_OK;
}
wifi_manager_scan_async();
esp_err_t err = set_content_type_from_req(req);
if(err == ESP_OK){
httpd_resp_send(req, (const char *)empty, strlen(empty));
}
return err;
}
esp_err_t ap_get_handler(httpd_req_t *req){
ESP_LOGI(TAG, "serving [%s]", req->uri);
if(!is_user_authenticated(httpd_req_t *req)){
// todo: redirect to login page
// return ESP_OK;
}
/* if we can get the mutex, write the last version of the AP list */
esp_err_t err = set_content_type_from_req(req);
if(err == ESP_OK wifi_manager_lock_json_buffer(( TickType_t ) 10)){
char *buff = wifi_manager_alloc_get_ap_list_json();
wifi_manager_unlock_json_buffer();
if(buff!=NULL){
httpd_resp_send(req, (const char *)buff, strlen(buff));
free(buff);
}
else {
ESP_LOGD(TAG, "Error retrieving ap list json string. ");
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Unable to retrieve AP list");
}
}
else {
httpd_resp_send_err(req, HTTPD_503_NOT_FOUND, "AP list unavailable");
ESP_LOGE(TAG, "GET /ap.json failed to obtain mutex");
}
return err;
}
esp_err_t config_get_handler(httpd_req_t *req){
ESP_LOGI(TAG, "serving [%s]", req->uri);
if(!is_user_authenticated(httpd_req_t *req)){
// todo: redirect to login page
// return ESP_OK;
}
esp_err_t err = set_content_type_from_req(req);
if(err == ESP_OK){
char * json = config_alloc_get_json(false);
if(json==NULL){
ESP_LOGD(TAG, "Error retrieving config json string. ");
httpd_resp_send_err(req, HTTPD_503_NOT_FOUND, "Error retrieving configuration object");
err=ESP_FAIL;
}
else {
ESP_LOGD(TAG, "config json : %s",json );
httpd_resp_send(req, (const char *)json, strlen(json));
free(json);
}
}
return err;
}
esp_err_t post_handler_buff_receive(httpd_req_t * req){
esp_err_t err = ESP_OK;
int total_len = req->content_len;
int cur_len = 0;
char *buf = ((rest_server_context_t *)(req->user_ctx))->scratch;
int received = 0;
if (total_len >= SCRATCH_BUFSIZE) {
/* Respond with 500 Internal Server Error */
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "content too long");
return ESP_FAIL;
}
while (cur_len < total_len) {
received = httpd_req_recv(req, buf + cur_len, total_len);
if (received <= 0) {
/* Respond with 500 Internal Server Error */
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to post control value");
return ESP_FAIL;
}
cur_len += received;
}
buf[total_len] = '\0';
}
esp_err_t config_post_handler(httpd_req_t *req){
ESP_LOGI(TAG, "serving [%s]", req->uri);
bool bOTA=false;
char * otaURL=NULL;
esp_err_t err = post_handler_buff_receive(req);
if(err!=ESP_OK){
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "content too long");
return err;
}
if(!is_user_authenticated(httpd_req_t *req)){
// todo: redirect to login page
// return ESP_OK;
}
char *buf = ((rest_server_context_t *)(req->user_ctx))->scratch;
cJSON *root = cJSON_Parse(buf);
cJSON *item=root->next;
while (item && err == ESP_OK)
{
cJSON *prev_item = item;
item=item->next;
if(prev_item->name==NULL) {
ESP_LOGE(TAG,"Config value does not have a name");
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Malformed config json. Value does not have a name.");
err = ESP_FAIL;
}
if(err == ESP_OK){
ESP_LOGI(TAG,"Found config value name [%s]", prev_item->name);
nvs_type_t item_type= config_get_item_type(prev_item);
if(item_type!=0){
void * val = config_safe_alloc_get_entry_value(item_type, prev_item);
if(val!=NULL){
if(strcmp(prev_item->name, "fwurl")==0) {
if(item_type!=NVS_TYPE_STR){
ESP_LOGE(TAG,"Firmware url should be type %d. Found type %d instead.",NVS_TYPE_STR,item_type );
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Malformed config json. Wrong type for firmware URL.");
err = ESP_FAIL;
}
else {
// we're getting a request to do an OTA from that URL
ESP_LOGW(TAG, "Found OTA request!");
otaURL=strdup(val);
bOTA=true;
}
}
else {
if(config_set_value(item_type, last_parm_name , last_parm) != ESP_OK){
ESP_LOGE(TAG,"Unable to store value for [%s]", prev_item->name);
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Unable to store config value");
err = ESP_FAIL;
}
else {
ESP_LOGI(TAG,"Successfully set value for [%s]",prev_item->name);
}
}
free(val);
}
else {
ESP_LOGE(TAG,"Value not found for [%s]", prev_item->name);
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Malformed config json. Missing value for entry.");
err = ESP_FAIL;
}
}
else {
ESP_LOGE(TAG,"Unable to determine the type of config value [%s]", prev_item->name);
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Malformed config json. Missing value for entry.");
err = ESP_FAIL;
}
}
}
if(err==ESP_OK){
set_content_type_from_req(req);
httpd_resp_sendstr(req, "{ok}");
}
cJSON_Delete(root);
if(bOTA) {
#if RECOVERY_APPLICATION
ESP_LOGW(TAG, "Starting process OTA for url %s",otaURL);
#else
ESP_LOGW(TAG, "Restarting system to process OTA for url %s",otaURL);
#endif
wifi_manager_reboot_ota(otaURL);
free(otaURL);
}
return err;
}
esp_err_t connect_post_handler(httpd_req_t *req){
ESP_LOGI(TAG, "serving [%s]", req->uri);
char success[]="{}";
char * ssid=NULL;
char * password=NULL;
char * host_name;
set_content_type_from_req(req);
esp_err_t err = post_handler_buff_receive(req);
if(err!=ESP_OK){
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "content too long");
return err;
}
char *buf = ((rest_server_context_t *)(req->user_ctx))->scratch;
if(!is_user_authenticated(httpd_req_t *req)){
// todo: redirect to login page
// return ESP_OK;
}
cJSON *root = cJSON_Parse(buf);
if(root==NULL){
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "content too long");
return ESP_FAIL;
}
cJSON * ssid_object = cJSON_GetObjectItem(root, "ssid");
if(ssid_object !=NULL){
ssid = (char *)config_safe_alloc_get_entry_value(ssid_object);
}
cJSON * password_object = cJSON_GetObjectItem(root, "pwd");
if(password_object !=NULL){
password = (char *)config_safe_alloc_get_entry_value(password_object);
}
cJSON * host_name_object = cJSON_GetObjectItem(root, "host_name");
if(host_name_object !=NULL){
host_name = (char *)config_safe_alloc_get_entry_value(host_name_object);
}
cJSON_Delete(root);
if(host_name!=NULL){
if(config_set_value(NVS_TYPE_STR, "host_name", host_name) != ESP_OK){
ESP_LOGW(TAG, "Unable to save host name configuration");
}
}
if(ssid !=NULL && strlen(ssid) <= MAX_SSID_SIZE && strlen(password) <= MAX_PASSWORD_SIZE ){
wifi_config_t* config = wifi_manager_get_wifi_sta_config();
memset(config, 0x00, sizeof(wifi_config_t));
strlcpy(config->sta.ssid, ssid, sizeof(config->sta.ssid)+1);
if(password){
strlcpy(config->sta.password, password, sizeof(config->sta.password)+1);
}
ESP_LOGD(TAG, "http_server_netconn_serve: wifi_manager_connect_async() call, with ssid: %s, password: %s", config->sta.ssid, config->sta.password);
wifi_manager_connect_async();
httpd_resp_send(req, (const char *)success, strlen(success));
}
else {
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Malformed json. Missing or invalid ssid/password.");
err = ESP_FAIL;
}
FREE_AND_NULL(ssid);
FREE_AND_NULL(password);
FREE_AND_NULL(host_name);
return err;
}
esp_err_t connect_delete_handler(httpd_req_t *req){
char success[]="{}";
ESP_LOGI(TAG, "serving [%s]", req->uri);
if(!is_user_authenticated(httpd_req_t *req)){
// todo: redirect to login page
// return ESP_OK;
}
set_content_type_from_req(req);
httpd_resp_send(req, (const char *)success, strlen(success));
wifi_manager_disconnect_async();
return ESP_OK;
}
esp_err_t reboot_ota_post_handler(httpd_req_t *req){
char success[]="{}";
ESP_LOGI(TAG, "serving [%s]", req->uri);
if(!is_user_authenticated(httpd_req_t *req)){
// todo: redirect to login page
// return ESP_OK;
}
set_content_type_from_req(req);
httpd_resp_send(req, (const char *)success, strlen(success));
wifi_manager_reboot(OTA);
return ESP_OK;
}
esp_err_t reboot_post_handler(httpd_req_t *req){
ESP_LOGI(TAG, "serving [%s]", req->uri);
char success[]="{}";
if(!is_user_authenticated(httpd_req_t *req)){
// todo: redirect to login page
// return ESP_OK;
}
set_content_type_from_req(req);
httpd_resp_send(req, (const char *)success, strlen(success));
wifi_manager_reboot(RESTART);
return ESP_OK;
}
esp_err_t recovery_post_handler(httpd_req_t *req){
ESP_LOGI(TAG, "serving [%s]", req->uri);
char success[]="{}";
if(!is_user_authenticated(httpd_req_t *req)){
// todo: redirect to login page
// return ESP_OK;
}
set_content_type_from_req(req);
httpd_resp_send(req, (const char *)success, strlen(success));
wifi_manager_reboot(RECOVERY);
return ESP_OK;
}
esp_err_t status_post_handler(httpd_req_t *req){
ESP_LOGI(TAG, "serving [%s]", req->uri);
char success[]="{}";
if(!is_user_authenticated(httpd_req_t *req)){
// todo: redirect to login page
// return ESP_OK;
}
set_content_type_from_req(req);
if(wifi_manager_lock_json_buffer(( TickType_t ) 10)) {
char *buff = wifi_manager_alloc_get_ip_info_json();
wifi_manager_unlock_json_buffer();
if(buff) {
httpd_resp_send(req, (const char *)buff, strlen(buff));
free(buff);
}
else {
httpd_resp_send_err(req, HTTPD_503_NOT_FOUND, "Empty status object");
}
}
else {
httpd_resp_send_err(req, HTTPD_503_NOT_FOUND, "Error retrieving status object");
}
return ESP_OK;
}
void http_server_netconn_serve(struct netconn *conn) {
struct netbuf *inbuf;
char *buf = NULL;
u16_t buflen;
err_t err;
ip_addr_t remote_add;
u16_t port;
ESP_LOGV(TAG, "Serving page. Getting device AP address.");
const char new_line[2] = "\n";
char * ap_ip_address= config_alloc_get_default(NVS_TYPE_STR, "ap_ip_address", DEFAULT_AP_IP, 0);
if(ap_ip_address==NULL){
ESP_LOGE(TAG, "Unable to retrieve default AP IP Address");
netconn_write(conn, http_503_hdr, sizeof(http_503_hdr) - 1, NETCONN_NOCOPY);
netconn_close(conn);
return;
}
ESP_LOGV(TAG, "Getting remote device IP address.");
netconn_getaddr(conn, &remote_add, &port, 0);
char * remote_address = strdup(ip4addr_ntoa(ip_2_ip4(&remote_add)));
ESP_LOGD(TAG, "Local Access Point IP address is: %s. Remote device IP address is %s. Receiving request buffer", ap_ip_address, remote_address);
err = netconn_recv(conn, &inbuf);
if(err == ERR_OK) {
ESP_LOGV(TAG, "Getting data buffer.");
netbuf_data(inbuf, (void**)&buf, &buflen);
dump_net_buffer(buf, buflen);
int lenH = 0;
/* extract the first line of the request */
char *save_ptr = buf;
char *line = strtok_r(save_ptr, new_line, &save_ptr);
char *temphost = http_server_get_header(save_ptr, "Host: ", &lenH);
char * host = malloc(lenH+1);
memset(host,0x00,lenH+1);
if(lenH>0){
strlcpy(host,temphost,lenH+1);
}
ESP_LOGD(TAG, "http_server_netconn_serve Host: [%s], host: [%s], Processing line [%s]",remote_address,host,line);
if(line) {
/* captive portal functionality: redirect to access point IP for HOST that are not the access point IP OR the STA IP */
const char * host_name=NULL;
if((err=tcpip_adapter_get_hostname(TCPIP_ADAPTER_IF_STA, &host_name )) !=ESP_OK) {
ESP_LOGE(TAG, "Unable to get host name. Error: %s",esp_err_to_name(err));
}
else {
ESP_LOGI(TAG,"System host name %s, http requested host: %s.",host_name, host);
}
/* determine if Host is from the STA IP address */
wifi_manager_lock_sta_ip_string(portMAX_DELAY);
bool access_from_sta_ip = lenH > 0?strcasestr(host, wifi_manager_get_sta_ip_string()):false;
wifi_manager_unlock_sta_ip_string();
bool access_from_host_name = (host_name!=NULL) && strcasestr(host,host_name);
if(lenH > 0 && !strcasestr(host, ap_ip_address) && !(access_from_sta_ip || access_from_host_name)) {
ESP_LOGI(TAG, "Redirecting host [%s] to AP IP Address : %s",remote_address, ap_ip_address);
netconn_write(conn, http_redirect_hdr_start, sizeof(http_redirect_hdr_start) - 1, NETCONN_NOCOPY);
netconn_write(conn, ap_ip_address, strlen(ap_ip_address), NETCONN_NOCOPY);
netconn_write(conn, http_redirect_hdr_end, sizeof(http_redirect_hdr_end) - 1, NETCONN_NOCOPY);
}
else {
//static stuff
}
void strreplace(char *src, char *str, char *rep)
{
char *p = strstr(src, str);
if(p)
{
int len = strlen(src)+strlen(rep)-strlen(str);
char r[len];
memset(r, 0, len);
if( p >= src ) {
strncpy(r, src, p-src);
r[p-src]='\0';
strncat(r, rep, strlen(rep));
strncat(r, p+strlen(str), p+strlen(str)-src+strlen(src));
strcpy(src, r);
strreplace(p+strlen(rep), str, rep);
}
}
}

View File

@@ -62,6 +62,26 @@ function to process requests, decode URLs, serve files, etc. etc.
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
esp_err_t root_get_handler(httpd_req_t *req);
esp_err_t resource_filehandler(httpd_req_t *req);
esp_err_t resource_filehandler(httpd_req_t *req);
esp_err_t resource_filehandler(httpd_req_t *req);
esp_err_t resource_filehandler(httpd_req_t *req);
esp_err_t resource_filehandler(httpd_req_t *req);
esp_err_t resource_filehandler(httpd_req_t *req);
esp_err_t ap_get_handler(httpd_req_t *req);
esp_err_t config_get_handler(httpd_req_t *req);
esp_err_t config_post_handler(httpd_req_t *req);
esp_err_t connect_post_handler(httpd_req_t *req);
esp_err_t connect_delete_handler(httpd_req_t *req);
esp_err_t reboot_ota_post_handler(httpd_req_t *req);
esp_err_t reboot_post_handler(httpd_req_t *req);
esp_err_t recovery_post_handler(httpd_req_t *req);
esp_err_t status_post_handler(httpd_req_t *req);
esp_err_t ap_scan_handler(httpd_req_t *req);
/** /**
* @brief RTOS task for the HTTP server. Do not start manually. * @brief RTOS task for the HTTP server. Do not start manually.

View File

@@ -37,7 +37,6 @@ Contains the freeRTOS task and all necessary support
#include <stdbool.h> #include <stdbool.h>
#include "dns_server.h" #include "dns_server.h"
#include "http_server.h"
#include "esp_system.h" #include "esp_system.h"
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/task.h" #include "freertos/task.h"
@@ -62,6 +61,7 @@ Contains the freeRTOS task and all necessary support
#include "cJSON.h" #include "cJSON.h"
#include "nvs_utilities.h" #include "nvs_utilities.h"
#include "cmd_system.h" #include "cmd_system.h"
#include "http_server_handlers.h"
#ifndef RECOVERY_APPLICATION #ifndef RECOVERY_APPLICATION
#define RECOVERY_APPLICATION 0 #define RECOVERY_APPLICATION 0

View File

@@ -0,0 +1,472 @@
/*
* Squeezelite for esp32
*
* (c) Sebastien 2019
* Philippe G. 2019, philippe_44@outlook.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
#include "http_server_handlers.h"
#include "esp_log.h"
#include "esp_http_server.h"
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include "esp_system.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "config.h"
static const char TAG[] = "http_server";
static httpd_handle_t server = NULL;
#define FILE_PATH_MAX (ESP_VFS_PATH_MAX + 128)
#define SCRATCH_BUFSIZE (10240)
typedef struct rest_server_context {
char base_path[ESP_VFS_PATH_MAX + 1];
char scratch[SCRATCH_BUFSIZE];
} rest_server_context_t;
#define ESP_LOGE_LOC(t,str, ...) ESP_LOGE(t, "%s(%d): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__)
#define ESP_LOGI_LOC(t,str, ...) ESP_LOGI(t, "%s(%d): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__)
#define ESP_LOGD_LOC(t,str, ...) ESP_LOGD(t, "%s(%d): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__)
esp_err_t http_server_start()
{
ESP_LOGI(REST_TAG, "Initializing HTTP Server");
rest_server_context_t *rest_context = calloc(1, sizeof(rest_server_context_t));
if(rest_context==NULL){
ESP_LOGE(TAG,"No memory for http context");
return ESP_FAIL;
}
strlcpy(rest_context->base_path, "/res/", sizeof(rest_context->base_path));
httpd_handle_t server = NULL;
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
config.uri_match_fn = httpd_uri_match_wildcard;
ESP_LOGI(REST_TAG, "Starting HTTP Server");
esp_err_t err= httpd_start(&server, &config);
if(err != ESP_OK){
ESP_LOGE_LOC(TAG,"Start server failed");
}
else {
httpd_uri_t root_get = { .uri = "/", .method = HTTP_GET, .handler = root_get_handler, .user_ctx = rest_context };
httpd_register_uri_handler(server, &root_get);
httpd_uri_t res_get = { .uri = "/res/*", .method = HTTP_GET, .handler = resource_filehandler, .user_ctx = rest_context };
httpd_register_uri_handler(server, &res_get);
httpd_uri_t ap_get = { .uri = "/ap.json", .method = HTTP_GET, .handler = ap_get_handler, .user_ctx = rest_context };
httpd_register_uri_handler(server, &ap_get);
httpd_uri_t scan_get = { .uri = "/scan.json", .method = HTTP_GET, .handler = ap_scan_handler, .user_ctx = rest_context };
httpd_register_uri_handler(server, &scan_get);
httpd_uri_t config_get = { .uri = "/config.json", .method = HTTP_GET, .handler = config_get_handler, .user_ctx = rest_context };
httpd_register_uri_handler(server, &config_get);
httpd_uri_t status_get = { .uri = "/status.json", .method = HTTP_GET, .handler = status_get_handler, .user_ctx = rest_context };
httpd_register_uri_handler(server, &status_get);
httpd_uri_t config_post = { .uri = "/config.json", .method = HTTP_POST, .handler = config_post_handler, .user_ctx = rest_context };
httpd_register_uri_handler(server, &config_post);
httpd_uri_t connect_post = { .uri = "/connect.json", .method = HTTP_POST, .handler = connect_post_handler, .user_ctx = rest_context };
httpd_register_uri_handler(server, &connect_post);
httpd_uri_t reboot_ota_post = { .uri = "/reboot_ota.json", .method = HTTP_POST, .handler = reboot_ota_post_handler, .user_ctx = rest_context };
httpd_register_uri_handler(server, &reboot_ota_post);
httpd_uri_t reboot_post = { .uri = "/reboot.json", .method = HTTP_POST, .handler = reboot_post_handler, .user_ctx = rest_context };
httpd_register_uri_handler(server, &reboot_post);
httpd_uri_t recovery_post = { .uri = "/recovery.json", .method = HTTP_POST, .handler = recovery_post_handler, .user_ctx = rest_context };
httpd_register_uri_handler(server, &recovery_post);
httpd_uri_t connect_delete = { .uri = "/connect.json", .method = HTTP_DELETE, .handler = connect_delete_handler, .user_ctx = rest_context };
httpd_register_uri_handler(server, &connect_delete);
}
return err;
}
/* Function to free context */
void adder_free_func(void *ctx)
{
ESP_LOGI(TAG, "/adder Free Context function called");
free(ctx);
}
/* This handler keeps accumulating data that is posted to it into a per
* socket/session context. And returns the result.
*/
esp_err_t adder_post_handler(httpd_req_t *req)
{
/* Log total visitors */
unsigned *visitors = (unsigned *)req->user_ctx;
ESP_LOGI(TAG, "/adder visitor count = %d", ++(*visitors));
char buf[10];
char outbuf[50];
int ret;
/* Read data received in the request */
ret = httpd_req_recv(req, buf, sizeof(buf));
if (ret <= 0) {
if (ret == HTTPD_SOCK_ERR_TIMEOUT) {
httpd_resp_send_408(req);
}
return ESP_FAIL;
}
buf[ret] = '\0';
int val = atoi(buf);
ESP_LOGI(TAG, "/adder handler read %d", val);
/* Create session's context if not already available */
if (! req->sess_ctx) {
ESP_LOGI(TAG, "/adder allocating new session");
req->sess_ctx = malloc(sizeof(int));
req->free_ctx = adder_free_func;
*(int *)req->sess_ctx = 0;
}
/* Add the received data to the context */
int *adder = (int *)req->sess_ctx;
*adder += val;
/* Respond with the accumulated value */
snprintf(outbuf, sizeof(outbuf),"%d", *adder);
httpd_resp_send(req, outbuf, strlen(outbuf));
return ESP_OK;
}
/* This handler gets the present value of the accumulator */
esp_err_t adder_get_handler(httpd_req_t *req)
{
/* Log total visitors */
unsigned *visitors = (unsigned *)req->user_ctx;
ESP_LOGI(TAG, "/adder visitor count = %d", ++(*visitors));
char outbuf[50];
/* Create session's context if not already available */
if (! req->sess_ctx) {
ESP_LOGI(TAG, "/adder GET allocating new session");
req->sess_ctx = malloc(sizeof(int));
req->free_ctx = adder_free_func;
*(int *)req->sess_ctx = 0;
}
ESP_LOGI(TAG, "/adder GET handler send %d", *(int *)req->sess_ctx);
/* Respond with the accumulated value */
snprintf(outbuf, sizeof(outbuf),"%d", *((int *)req->sess_ctx));
httpd_resp_send(req, outbuf, strlen(outbuf));
return ESP_OK;
}
/* This handler resets the value of the accumulator */
esp_err_t adder_put_handler(httpd_req_t *req)
{
/* Log total visitors */
unsigned *visitors = (unsigned *)req->user_ctx;
ESP_LOGI(TAG, "/adder visitor count = %d", ++(*visitors));
char buf[10];
char outbuf[50];
int ret;
/* Read data received in the request */
ret = httpd_req_recv(req, buf, sizeof(buf));
if (ret <= 0) {
if (ret == HTTPD_SOCK_ERR_TIMEOUT) {
httpd_resp_send_408(req);
}
return ESP_FAIL;
}
buf[ret] = '\0';
int val = atoi(buf);
ESP_LOGI(TAG, "/adder PUT handler read %d", val);
/* Create session's context if not already available */
if (! req->sess_ctx) {
ESP_LOGI(TAG, "/adder PUT allocating new session");
req->sess_ctx = malloc(sizeof(int));
req->free_ctx = adder_free_func;
}
*(int *)req->sess_ctx = val;
/* Respond with the reset value */
snprintf(outbuf, sizeof(outbuf),"%d", *((int *)req->sess_ctx));
httpd_resp_send(req, outbuf, strlen(outbuf));
return ESP_OK;
}
/* Maintain a variable which stores the number of times
* the "/adder" URI has been visited */
static unsigned visitors = 0;
httpd_handle_t start_webserver(void)
{
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
// Start the httpd server
ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port);
if (httpd_start(&server, &config) == ESP_OK) {
// Set URI handlers
ESP_LOGI(TAG, "Registering URI handlers");
httpd_register_uri_handler(server, &adder_get);
httpd_register_uri_handler(server, &adder_put);
httpd_register_uri_handler(server, &adder_post);
return server;
}
ESP_LOGI(TAG, "Error starting server!");
return NULL;
}
void stop_webserver(httpd_handle_t server)
{
// Stop the httpd server
httpd_stop(server);
}
if(strstr(line, "GET / ")) {
netconn_write(conn, http_html_hdr, sizeof(http_html_hdr) - 1, NETCONN_NOCOPY);
netconn_write(conn, index_html_start, index_html_end- index_html_start, NETCONN_NOCOPY);
}
else if(strstr(line, "GET /code.js ")) {
netconn_write(conn, http_js_hdr, sizeof(http_js_hdr) - 1, NETCONN_NOCOPY);
netconn_write(conn, code_js_start, code_js_end - code_js_start, NETCONN_NOCOPY);
}
else if(strstr(line, "GET /style.css ")) {
netconn_write(conn, http_css_hdr, sizeof(http_css_hdr) - 1, NETCONN_NOCOPY);
netconn_write(conn, style_css_start, style_css_end - style_css_start, NETCONN_NOCOPY);
}
else if(strstr(line, "GET /jquery.js ")) {
http_server_send_resource_file(conn,jquery_gz_start, jquery_gz_end, "text/javascript", "gzip" );
}
else if(strstr(line, "GET /popper.js ")) {
http_server_send_resource_file(conn,popper_gz_start, popper_gz_end, "text/javascript", "gzip" );
}
else if(strstr(line, "GET /bootstrap.js ")) {
http_server_send_resource_file(conn,bootstrap_js_gz_start, bootstrap_js_gz_end, "text/javascript", "gzip" );
}
else if(strstr(line, "GET /bootstrap.css ")) {
http_server_send_resource_file(conn,bootstrap_css_gz_start, bootstrap_css_gz_end, "text/css", "gzip" );
}
//dynamic stuff
else if(strstr(line, "GET /scan.json ")) {
ESP_LOGI(TAG, "Starting wifi scan");
wifi_manager_scan_async();
}
else if(strstr(line, "GET /ap.json ")) {
/* if we can get the mutex, write the last version of the AP list */
ESP_LOGI(TAG, "Processing ap.json request");
if(wifi_manager_lock_json_buffer(( TickType_t ) 10)) {
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY);
char *buff = wifi_manager_alloc_get_ap_list_json();
wifi_manager_unlock_json_buffer();
if(buff!=NULL){
netconn_write(conn, buff, strlen(buff), NETCONN_NOCOPY);
free(buff);
}
else {
ESP_LOGD(TAG, "Error retrieving ap list json string. ");
netconn_write(conn, http_503_hdr, sizeof(http_503_hdr) - 1, NETCONN_NOCOPY);
}
}
else {
netconn_write(conn, http_503_hdr, sizeof(http_503_hdr) - 1, NETCONN_NOCOPY);
ESP_LOGE(TAG, "http_server_netconn_serve: GET /ap.json failed to obtain mutex");
}
/* request a wifi scan */
ESP_LOGI(TAG, "Starting wifi scan");
wifi_manager_scan_async();
ESP_LOGI(TAG, "Done serving ap.json");
}
else if(strstr(line, "GET /config.json ")) {
ESP_LOGI(TAG, "Serving config.json");
ESP_LOGI(TAG, "About to get config from flash");
http_server_send_config_json(conn);
ESP_LOGD(TAG, "Done serving config.json");
}
else if(strstr(line, "POST /config.json ")) {
ESP_LOGI(TAG, "Serving POST config.json");
int lenA=0;
char * last_parm=save_ptr;
char * next_parm=save_ptr;
char * last_parm_name=NULL;
bool bErrorFound=false;
bool bOTA=false;
char * otaURL=NULL;
// todo: implement json body parsing
//http_server_process_config(conn,save_ptr);
while(last_parm!=NULL) {
// Search will return
ESP_LOGD(TAG, "Getting parameters from X-Custom headers");
last_parm = http_server_search_header(next_parm, "X-Custom-", &lenA, &last_parm_name,&next_parm,buf+buflen);
if(last_parm!=NULL && last_parm_name!=NULL) {
ESP_LOGI(TAG, "http_server_netconn_serve: POST config.json, config %s=%s", last_parm_name, last_parm);
if(strcmp(last_parm_name, "fwurl")==0) {
// we're getting a request to do an OTA from that URL
ESP_LOGW(TAG, "Found OTA request!");
otaURL=strdup(last_parm);
bOTA=true;
}
else {
ESP_LOGV(TAG, "http_server_netconn_serve: POST config.json Storing parameter");
if(config_set_value(NVS_TYPE_STR, last_parm_name , last_parm) != ESP_OK){
ESP_LOGE(TAG, "Unable to save nvs value.");
}
}
}
if(last_parm_name!=NULL) {
free(last_parm_name);
last_parm_name=NULL;
}
}
if(bErrorFound) {
netconn_write(conn, http_400_hdr, sizeof(http_400_hdr) - 1, NETCONN_NOCOPY); //400 invalid request
}
else {
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY); //200ok
if(bOTA) {
#if RECOVERY_APPLICATION
ESP_LOGW(TAG, "Starting process OTA for url %s",otaURL);
#else
ESP_LOGW(TAG, "Restarting system to process OTA for url %s",otaURL);
#endif
wifi_manager_reboot_ota(otaURL);
free(otaURL);
}
}
ESP_LOGI(TAG, "Done Serving POST config.json");
}
else if(strstr(line, "POST /connect.json ")) {
ESP_LOGI(TAG, "http_server_netconn_serve: POST /connect.json");
bool found = false;
int lenS = 0, lenP = 0, lenN = 0;
char *ssid = NULL, *password = NULL;
ssid = http_server_get_header(save_ptr, "X-Custom-ssid: ", &lenS);
password = http_server_get_header(save_ptr, "X-Custom-pwd: ", &lenP);
char * new_host_name_b = http_server_get_header(save_ptr, "X-Custom-host_name: ", &lenN);
if(lenN > 0){
lenN++;
char * new_host_name = malloc(lenN);
strlcpy(new_host_name, new_host_name_b, lenN);
if(config_set_value(NVS_TYPE_STR, "host_name", new_host_name) != ESP_OK){
ESP_LOGE(TAG, "Unable to save host name configuration");
}
free(new_host_name);
}
if(ssid && lenS <= MAX_SSID_SIZE && password && lenP <= MAX_PASSWORD_SIZE) {
wifi_config_t* config = wifi_manager_get_wifi_sta_config();
memset(config, 0x00, sizeof(wifi_config_t));
memcpy(config->sta.ssid, ssid, lenS);
memcpy(config->sta.password, password, lenP);
ESP_LOGD(TAG, "http_server_netconn_serve: wifi_manager_connect_async() call, with ssid: %s, password: %s", config->sta.ssid, config->sta.password);
wifi_manager_connect_async();
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY); //200ok
found = true;
}
else{
ESP_LOGE(TAG, "SSID or Password invalid");
}
if(!found) {
/* bad request the authentification header is not complete/not the correct format */
netconn_write(conn, http_400_hdr, sizeof(http_400_hdr) - 1, NETCONN_NOCOPY);
ESP_LOGE(TAG, "bad request the authentification header is not complete/not the correct format");
}
ESP_LOGI(TAG, "http_server_netconn_serve: done serving connect.json");
}
else if(strstr(line, "DELETE /connect.json ")) {
ESP_LOGI(TAG, "http_server_netconn_serve: DELETE /connect.json");
/* request a disconnection from wifi and forget about it */
wifi_manager_disconnect_async();
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY); /* 200 ok */
ESP_LOGI(TAG, "http_server_netconn_serve: done serving DELETE /connect.json");
}
else if(strstr(line, "POST /reboot_ota.json ")) {
ESP_LOGI(TAG, "http_server_netconn_serve: POST reboot_ota.json");
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY); /* 200 ok */
wifi_manager_reboot(OTA);
ESP_LOGI(TAG, "http_server_netconn_serve: done serving POST reboot_ota.json");
}
else if(strstr(line, "POST /reboot.json ")) {
ESP_LOGI(TAG, "http_server_netconn_serve: POST reboot.json");
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY); /* 200 ok */
wifi_manager_reboot(RESTART);
ESP_LOGI(TAG, "http_server_netconn_serve: done serving POST reboot.json");
}
else if(strstr(line, "POST /recovery.json ")) {
ESP_LOGI(TAG, "http_server_netconn_serve: POST recovery.json");
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY); /* 200 ok */
wifi_manager_reboot(RECOVERY);
ESP_LOGI(TAG, "http_server_netconn_serve: done serving POST recovery.json");
}
else if(strstr(line, "GET /status.json ")) {
ESP_LOGI(TAG, "Serving status.json");
if(wifi_manager_lock_json_buffer(( TickType_t ) 10)) {
char *buff = wifi_manager_alloc_get_ip_info_json();
wifi_manager_unlock_json_buffer();
if(buff) {
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY);
netconn_write(conn, buff, strlen(buff), NETCONN_NOCOPY);
free(buff);
}
else {
netconn_write(conn, http_503_hdr, sizeof(http_503_hdr) - 1, NETCONN_NOCOPY);
}
}
else {
netconn_write(conn, http_503_hdr, sizeof(http_503_hdr) - 1, NETCONN_NOCOPY);
ESP_LOGE(TAG, "http_server_netconn_serve: GET /status failed to obtain mutex");
}
ESP_LOGI(TAG, "Done Serving status.json");
}
else {
netconn_write(conn, http_400_hdr, sizeof(http_400_hdr) - 1, NETCONN_NOCOPY);
ESP_LOGE(TAG, "bad request from host: %s, request %s",remote_address, line);
}
}
}
else {
ESP_LOGE(TAG, "URL not found processing for remote host : %s",remote_address);
netconn_write(conn, http_404_hdr, sizeof(http_404_hdr) - 1, NETCONN_NOCOPY);
}
free(host);
}

View File

@@ -704,6 +704,33 @@ esp_err_t config_set_value(nvs_type_t nvs_type, const char *key, void * value){
return result; return result;
} }
esp_err_t config_set_value(nvs_type_t nvs_type, const char *key, void * value){
esp_err_t result = ESP_OK;
if(!config_lock(LOCK_MAX_WAIT/portTICK_PERIOD_MS)){
ESP_LOGE(TAG, "Unable to lock config after %d ms",LOCK_MAX_WAIT);
result = ESP_FAIL;
}
cJSON * entry = config_set_value_safe(nvs_type, key, value);
if(entry == NULL){
result = ESP_FAIL;
}
else{
char * entry_str = cJSON_PrintUnformatted(entry);
if(entry_str!=NULL){
ESP_LOGV(TAG,"config_set_value result: \n%s",entry_str);
free(entry_str);
}
else {
ESP_LOGV(TAG,"config_set_value completed");
}
}
config_unlock();
return result;
}
IMPLEMENT_SET_DEFAULT(uint8_t,NVS_TYPE_U8); IMPLEMENT_SET_DEFAULT(uint8_t,NVS_TYPE_U8);
IMPLEMENT_SET_DEFAULT(int8_t,NVS_TYPE_I8); IMPLEMENT_SET_DEFAULT(int8_t,NVS_TYPE_I8);
IMPLEMENT_SET_DEFAULT(uint16_t,NVS_TYPE_U16); IMPLEMENT_SET_DEFAULT(uint16_t,NVS_TYPE_U16);

View File

@@ -38,4 +38,6 @@ void * config_alloc_get(nvs_type_t nvs_type, const char *key) ;
bool wait_for_commit(); bool wait_for_commit();
char * config_alloc_get_json(bool bFormatted); char * config_alloc_get_json(bool bFormatted);
esp_err_t config_set_value(nvs_type_t nvs_type, const char *key, void * value); esp_err_t config_set_value(nvs_type_t nvs_type, const char *key, void * value);
nvs_type_t config_get_item_type(cJSON * entry);
void * config_safe_alloc_get_entry_value(nvs_type_t nvs_type, cJSON * entry);