http_server_handlers.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182
  1. /*
  2. Copyright (c) 2017-2019 Tony Pottier
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in all
  10. copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. SOFTWARE.
  18. @file http_server.c
  19. @author Tony Pottier
  20. @brief Defines all functions necessary for the HTTP server to run.
  21. Contains the freeRTOS task for the HTTP listener and all necessary support
  22. function to process requests, decode URLs, serve files, etc. etc.
  23. @note http_server task cannot run without the wifi_manager task!
  24. @see https://idyl.io
  25. @see https://github.com/tonyp7/esp32-wifi-manager
  26. */
  27. #include "http_server_handlers.h"
  28. #include "esp_http_server.h"
  29. #include "cmd_system.h"
  30. #include <inttypes.h>
  31. #include "squeezelite-ota.h"
  32. #include "nvs_utilities.h"
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include "cJSON.h"
  36. #include "esp_system.h"
  37. #include "freertos/FreeRTOS.h"
  38. #include "freertos/task.h"
  39. #include "platform_config.h"
  40. #include "sys/param.h"
  41. #include "esp_vfs.h"
  42. #include "messaging.h"
  43. #include "platform_esp32.h"
  44. #include "trace.h"
  45. #include "esp_console.h"
  46. #include "argtable3/argtable3.h"
  47. #include "platform_console.h"
  48. #include "accessors.h"
  49. #include "webapp/webpack.h"
  50. #define HTTP_STACK_SIZE (5*1024)
  51. const char str_na[]="N/A";
  52. #define STR_OR_NA(s) s?s:str_na
  53. /* @brief tag used for ESP serial console messages */
  54. static const char TAG[] = "httpd_handlers";
  55. /* @brief task handle for the http server */
  56. SemaphoreHandle_t http_server_config_mutex = NULL;
  57. extern RingbufHandle_t messaging;
  58. #define AUTH_TOKEN_SIZE 50
  59. typedef struct session_context {
  60. char * auth_token;
  61. bool authenticated;
  62. char * sess_ip_address;
  63. u16_t port;
  64. } session_context_t;
  65. union sockaddr_aligned {
  66. struct sockaddr sa;
  67. struct sockaddr_storage st;
  68. struct sockaddr_in sin;
  69. struct sockaddr_in6 sin6;
  70. } aligned_sockaddr_t;
  71. esp_err_t post_handler_buff_receive(httpd_req_t * req);
  72. static const char redirect_payload1[]="<html><head><title>Redirecting to Captive Portal</title><meta http-equiv='refresh' content='0; url=";
  73. static const char redirect_payload2[]="'></head><body><p>Please wait, refreshing. If page does not refresh, click <a href='";
  74. static const char redirect_payload3[]="'>here</a> to login.</p></body></html>";
  75. /**
  76. * @brief embedded binary data.
  77. * @see file "component.mk"
  78. * @see https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html#embedding-binary-data
  79. */
  80. esp_err_t redirect_processor(httpd_req_t *req, httpd_err_code_t error);
  81. char * alloc_get_http_header(httpd_req_t * req, const char * key){
  82. char* buf = NULL;
  83. size_t buf_len;
  84. /* Get header value string length and allocate memory for length + 1,
  85. * extra byte for null termination */
  86. buf_len = httpd_req_get_hdr_value_len(req, key) + 1;
  87. if (buf_len > 1) {
  88. buf = malloc(buf_len);
  89. /* Copy null terminated value string into buffer */
  90. if (httpd_req_get_hdr_value_str(req, "Host", buf, buf_len) == ESP_OK) {
  91. ESP_LOGD_LOC(TAG, "Found header => %s: %s",key, buf);
  92. }
  93. }
  94. return buf;
  95. }
  96. char * http_alloc_get_socket_address(httpd_req_t *req, u8_t local, in_port_t * portl) {
  97. socklen_t len;
  98. union sockaddr_aligned addr;
  99. len = sizeof(addr);
  100. ip_addr_t * ip_addr=NULL;
  101. char * ipstr = malloc(INET6_ADDRSTRLEN);
  102. memset(ipstr,0x0,INET6_ADDRSTRLEN);
  103. typedef int (*getaddrname_fn_t)(int s, struct sockaddr *name, socklen_t *namelen);
  104. getaddrname_fn_t get_addr = NULL;
  105. int s = httpd_req_to_sockfd(req);
  106. if(s == -1) {
  107. free(ipstr);
  108. return strdup("httpd_req_to_sockfd error");
  109. }
  110. ESP_LOGV_LOC(TAG,"httpd socket descriptor: %u", s);
  111. get_addr = local?&lwip_getsockname:&lwip_getpeername;
  112. if(get_addr(s, (struct sockaddr *)&addr, &len) <0){
  113. ESP_LOGE_LOC(TAG,"Failed to retrieve socket address");
  114. sprintf(ipstr,"N/A (0.0.0.%u)",local);
  115. }
  116. else {
  117. if (addr.sin.sin_family!= AF_INET) {
  118. ip_addr = (ip_addr_t *)&(addr.sin6.sin6_addr);
  119. inet_ntop(addr.sa.sa_family, ip_addr, ipstr, INET6_ADDRSTRLEN);
  120. ESP_LOGV_LOC(TAG,"Processing an IPV6 address : %s", ipstr);
  121. *portl = addr.sin6.sin6_port;
  122. unmap_ipv4_mapped_ipv6(ip_2_ip4(ip_addr), ip_2_ip6(ip_addr));
  123. }
  124. else {
  125. ip_addr = (ip_addr_t *)&(addr.sin.sin_addr);
  126. inet_ntop(addr.sa.sa_family, ip_addr, ipstr, INET6_ADDRSTRLEN);
  127. ESP_LOGV_LOC(TAG,"Processing an IPV6 address : %s", ipstr);
  128. *portl = addr.sin.sin_port;
  129. }
  130. inet_ntop(AF_INET, ip_addr, ipstr, INET6_ADDRSTRLEN);
  131. ESP_LOGV_LOC(TAG,"Retrieved ip address:port = %s:%u",ipstr, *portl);
  132. }
  133. return ipstr;
  134. }
  135. bool is_captive_portal_host_name(httpd_req_t *req){
  136. const char * host_name=NULL;
  137. const char * ap_host_name=NULL;
  138. char * ap_ip_address=NULL;
  139. bool request_contains_hostname = false;
  140. esp_err_t hn_err =ESP_OK, err=ESP_OK;
  141. ESP_LOGD_LOC(TAG, "Getting adapter host name");
  142. if((err = tcpip_adapter_get_hostname(TCPIP_ADAPTER_IF_STA, &host_name )) !=ESP_OK) {
  143. ESP_LOGE_LOC(TAG, "Unable to get host name. Error: %s",esp_err_to_name(err));
  144. }
  145. else {
  146. ESP_LOGD_LOC(TAG, "Host name is %s",host_name);
  147. }
  148. ESP_LOGD_LOC(TAG, "Getting host name from request");
  149. char *req_host = alloc_get_http_header(req, "Host");
  150. if(tcpip_adapter_is_netif_up(TCPIP_ADAPTER_IF_AP)){
  151. ESP_LOGD_LOC(TAG, "Soft AP is enabled. getting ip info");
  152. // Access point is up and running. Get the current IP address
  153. tcpip_adapter_ip_info_t ip_info;
  154. esp_err_t ap_ip_err = tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_AP, &ip_info);
  155. if(ap_ip_err != ESP_OK){
  156. ESP_LOGE_LOC(TAG, "Unable to get local AP ip address. Error: %s",esp_err_to_name(ap_ip_err));
  157. }
  158. else {
  159. ESP_LOGD_LOC(TAG, "getting host name for TCPIP_ADAPTER_IF_AP");
  160. if((hn_err = tcpip_adapter_get_hostname(TCPIP_ADAPTER_IF_AP, &ap_host_name )) !=ESP_OK) {
  161. ESP_LOGE_LOC(TAG, "Unable to get host name. Error: %s",esp_err_to_name(hn_err));
  162. err=err==ESP_OK?hn_err:err;
  163. }
  164. else {
  165. ESP_LOGD_LOC(TAG, "Soft AP Host name is %s",ap_host_name);
  166. }
  167. ap_ip_address = malloc(IP4ADDR_STRLEN_MAX);
  168. memset(ap_ip_address, 0x00, IP4ADDR_STRLEN_MAX);
  169. if(ap_ip_address){
  170. ESP_LOGD_LOC(TAG, "Converting soft ip address to string");
  171. ip4addr_ntoa_r(&ip_info.ip, ap_ip_address, IP4ADDR_STRLEN_MAX);
  172. ESP_LOGD_LOC(TAG,"TCPIP_ADAPTER_IF_AP is up and has ip address %s ", ap_ip_address);
  173. }
  174. }
  175. }
  176. if((request_contains_hostname = (host_name!=NULL) && (req_host!=NULL) && strcasestr(req_host,host_name)) == true){
  177. ESP_LOGD_LOC(TAG,"http request host = system host name %s", req_host);
  178. }
  179. else if((request_contains_hostname = (ap_host_name!=NULL) && (req_host!=NULL) && strcasestr(req_host,ap_host_name)) == true){
  180. ESP_LOGD_LOC(TAG,"http request host = AP system host name %s", req_host);
  181. }
  182. FREE_AND_NULL(ap_ip_address);
  183. FREE_AND_NULL(req_host);
  184. return request_contains_hostname;
  185. }
  186. /* Custom function to free context */
  187. void free_ctx_func(void *ctx)
  188. {
  189. session_context_t * context = (session_context_t *)ctx;
  190. if(context){
  191. ESP_LOGD(TAG, "Freeing up socket context");
  192. FREE_AND_NULL(context->auth_token);
  193. FREE_AND_NULL(context->sess_ip_address);
  194. free(context);
  195. }
  196. }
  197. session_context_t* get_session_context(httpd_req_t *req){
  198. bool newConnection=false;
  199. if (! req->sess_ctx) {
  200. ESP_LOGD(TAG,"New connection context. Allocating session buffer");
  201. req->sess_ctx = malloc(sizeof(session_context_t));
  202. memset(req->sess_ctx,0x00,sizeof(session_context_t));
  203. req->free_ctx = free_ctx_func;
  204. newConnection = true;
  205. // get the remote IP address only once per session
  206. }
  207. session_context_t *ctx_data = (session_context_t*)req->sess_ctx;
  208. FREE_AND_NULL(ctx_data->sess_ip_address);
  209. ctx_data->sess_ip_address = http_alloc_get_socket_address(req, 0, &ctx_data->port);
  210. if(newConnection){
  211. ESP_LOGI(TAG, "serving %s to peer %s port %u", req->uri, ctx_data->sess_ip_address , ctx_data->port);
  212. }
  213. return (session_context_t *)req->sess_ctx;
  214. }
  215. bool is_user_authenticated(httpd_req_t *req){
  216. session_context_t *ctx_data = get_session_context(req);
  217. if(ctx_data->authenticated){
  218. ESP_LOGD_LOC(TAG,"User is authenticated.");
  219. return true;
  220. }
  221. ESP_LOGD(TAG,"Heap internal:%zu (min:%zu) external:%zu (min:%zu)",
  222. heap_caps_get_free_size(MALLOC_CAP_INTERNAL),
  223. heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL),
  224. heap_caps_get_free_size(MALLOC_CAP_SPIRAM),
  225. heap_caps_get_minimum_free_size(MALLOC_CAP_SPIRAM));
  226. // todo: ask for user to authenticate
  227. return false;
  228. }
  229. /* Copies the full path into destination buffer and returns
  230. * pointer to requested file name */
  231. static const char* get_path_from_uri(char *dest, const char *uri, size_t destsize)
  232. {
  233. size_t pathlen = strlen(uri);
  234. memset(dest,0x0,destsize);
  235. const char *quest = strchr(uri, '?');
  236. if (quest) {
  237. pathlen = MIN(pathlen, quest - uri);
  238. }
  239. const char *hash = strchr(uri, '#');
  240. if (hash) {
  241. pathlen = MIN(pathlen, hash - uri);
  242. }
  243. if ( pathlen + 1 > destsize) {
  244. /* Full path string won't fit into destination buffer */
  245. return NULL;
  246. }
  247. strlcpy(dest , uri, pathlen + 1);
  248. // strip trailing blanks
  249. char * sr = dest+pathlen;
  250. while(*sr== ' ') *sr-- = '\0';
  251. char * last_fs = strchr(dest,'/');
  252. if(!last_fs) ESP_LOGD_LOC(TAG,"no / found in %s", dest);
  253. char * p=last_fs;
  254. while(p && *(++p)!='\0'){
  255. if(*p == '/') {
  256. last_fs=p;
  257. }
  258. }
  259. /* Return pointer to path, skipping the base */
  260. return last_fs? ++last_fs: dest;
  261. }
  262. #define IS_FILE_EXT(filename, ext) \
  263. (strcasecmp(&filename[strlen(filename) - sizeof(ext) + 1], ext) == 0)
  264. /* Set HTTP response content type according to file extension */
  265. static esp_err_t set_content_type_from_file(httpd_req_t *req, const char *filename)
  266. {
  267. if(strlen(filename) ==0){
  268. // for root page, etc.
  269. return httpd_resp_set_type(req, HTTPD_TYPE_TEXT);
  270. } else if (IS_FILE_EXT(filename, ".pdf")) {
  271. return httpd_resp_set_type(req, "application/pdf");
  272. } else if (IS_FILE_EXT(filename, ".html")) {
  273. return httpd_resp_set_type(req, HTTPD_TYPE_TEXT);
  274. } else if (IS_FILE_EXT(filename, ".jpeg")) {
  275. return httpd_resp_set_type(req, "image/jpeg");
  276. } else if (IS_FILE_EXT(filename, ".png")) {
  277. return httpd_resp_set_type(req, "image/png");
  278. } else if (IS_FILE_EXT(filename, ".ico")) {
  279. return httpd_resp_set_type(req, "image/x-icon");
  280. } else if (IS_FILE_EXT(filename, ".css")) {
  281. return httpd_resp_set_type(req, "text/css");
  282. } else if (IS_FILE_EXT(filename, ".js")) {
  283. return httpd_resp_set_type(req, "text/javascript");
  284. } else if (IS_FILE_EXT(filename, ".json")) {
  285. return httpd_resp_set_type(req, HTTPD_TYPE_JSON);
  286. }
  287. /* This is a limited set only */
  288. /* For any other type always set as plain text */
  289. return httpd_resp_set_type(req, "text/plain");
  290. }
  291. static esp_err_t set_content_type_from_req(httpd_req_t *req)
  292. {
  293. char filepath[FILE_PATH_MAX];
  294. const char *filename = get_path_from_uri(filepath, req->uri, sizeof(filepath));
  295. if (!filename) {
  296. ESP_LOGE_LOC(TAG, "Filename is too long");
  297. /* Respond with 500 Internal Server Error */
  298. httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Filename too long");
  299. return ESP_FAIL;
  300. }
  301. /* If name has trailing '/', respond with directory contents */
  302. if (filename[strlen(filename) - 1] == '/' && strlen(filename)>1) {
  303. httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Browsing files forbidden.");
  304. return ESP_FAIL;
  305. }
  306. set_content_type_from_file(req, filename);
  307. return ESP_OK;
  308. }
  309. int resource_get_index(const char * fileName){
  310. for(int i=0;resource_lookups[i][0]!='\0';i++){
  311. if(strstr(resource_lookups[i], fileName)){
  312. return i;
  313. }
  314. }
  315. return -1;
  316. }
  317. esp_err_t root_get_handler(httpd_req_t *req){
  318. esp_err_t err = ESP_OK;
  319. ESP_LOGD_LOC(TAG, "serving [%s]", req->uri);
  320. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  321. httpd_resp_set_hdr(req, "Accept-Encoding", "identity");
  322. if(!is_user_authenticated(req)){
  323. // todo: send password entry page and return
  324. }
  325. int idx=-1;
  326. if((idx=resource_get_index("index.html"))>=0){
  327. const size_t file_size = (resource_map_end[idx] - resource_map_start[idx]);
  328. httpd_resp_set_hdr(req, "Content-Encoding", "gzip");
  329. err = set_content_type_from_req(req);
  330. if(err == ESP_OK){
  331. httpd_resp_send(req, (const char *)resource_map_start[idx], file_size);
  332. }
  333. }
  334. else{
  335. httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "index.html not found");
  336. return ESP_FAIL;
  337. }
  338. ESP_LOGD_LOC(TAG, "done serving [%s]", req->uri);
  339. return err;
  340. }
  341. esp_err_t resource_filehandler(httpd_req_t *req){
  342. char filepath[FILE_PATH_MAX];
  343. ESP_LOGD_LOC(TAG, "serving [%s]", req->uri);
  344. const char *filename = get_path_from_uri(filepath, req->uri, sizeof(filepath));
  345. if (!filename) {
  346. ESP_LOGE_LOC(TAG, "Filename is too long");
  347. /* Respond with 500 Internal Server Error */
  348. httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Filename too long");
  349. return ESP_FAIL;
  350. }
  351. /* If name has trailing '/', respond with directory contents */
  352. if (filename[strlen(filename) - 1] == '/') {
  353. httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Browsing files forbidden.");
  354. return ESP_FAIL;
  355. }
  356. int idx=-1;
  357. if((idx=resource_get_index(filename))>=0){
  358. set_content_type_from_file(req, filename);
  359. if(strstr(resource_lookups[idx], ".gz")) {
  360. httpd_resp_set_hdr(req, "Content-Encoding", "gzip");
  361. }
  362. const size_t file_size = (resource_map_end[idx] - resource_map_start[idx]);
  363. httpd_resp_send(req, (const char *)resource_map_start[idx], file_size);
  364. }
  365. else {
  366. ESP_LOGE_LOC(TAG, "Unknown resource [%s] from path [%s] ", filename,filepath);
  367. /* Respond with 404 Not Found */
  368. httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, "File does not exist");
  369. return ESP_FAIL;
  370. }
  371. ESP_LOGD_LOC(TAG, "Resource sending complete");
  372. return ESP_OK;
  373. }
  374. esp_err_t ap_scan_handler(httpd_req_t *req){
  375. const char empty[] = "{}";
  376. ESP_LOGD_LOC(TAG, "serving [%s]", req->uri);
  377. if(!is_user_authenticated(req)){
  378. // todo: redirect to login page
  379. // return ESP_OK;
  380. }
  381. wifi_manager_scan_async();
  382. esp_err_t err = set_content_type_from_req(req);
  383. if(err == ESP_OK){
  384. httpd_resp_send(req, (const char *)empty, HTTPD_RESP_USE_STRLEN);
  385. }
  386. return err;
  387. }
  388. esp_err_t console_cmd_get_handler(httpd_req_t *req){
  389. ESP_LOGD_LOC(TAG, "serving [%s]", req->uri);
  390. if(!is_user_authenticated(req)){
  391. // todo: redirect to login page
  392. // return ESP_OK;
  393. }
  394. /* if we can get the mutex, write the last version of the AP list */
  395. esp_err_t err = set_content_type_from_req(req);
  396. cJSON * cmdlist = get_cmd_list();
  397. char * json_buffer = cJSON_Print(cmdlist);
  398. if(json_buffer){
  399. httpd_resp_send(req, (const char *)json_buffer, HTTPD_RESP_USE_STRLEN);
  400. free(json_buffer);
  401. }
  402. else{
  403. ESP_LOGD_LOC(TAG, "Error retrieving command json string. ");
  404. httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Unable to format command");
  405. }
  406. cJSON_Delete(cmdlist);
  407. ESP_LOGD_LOC(TAG, "done serving [%s]", req->uri);
  408. return err;
  409. }
  410. esp_err_t console_cmd_post_handler(httpd_req_t *req){
  411. char success[]="{\"Result\" : \"Success\" }";
  412. char failed[]="{\"Result\" : \"Failed\" }";
  413. ESP_LOGD_LOC(TAG, "serving [%s]", req->uri);
  414. //bool bOTA=false;
  415. //char * otaURL=NULL;
  416. esp_err_t err = post_handler_buff_receive(req);
  417. if(err!=ESP_OK){
  418. return err;
  419. }
  420. if(!is_user_authenticated(req)){
  421. // todo: redirect to login page
  422. // return ESP_OK;
  423. }
  424. err = set_content_type_from_req(req);
  425. if(err != ESP_OK){
  426. return err;
  427. }
  428. char *command= ((rest_server_context_t *)(req->user_ctx))->scratch;
  429. cJSON *root = cJSON_Parse(command);
  430. if(root == NULL){
  431. ESP_LOGE_LOC(TAG, "Parsing command. Received content was: %s",command);
  432. httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Malformed command json. Unable to parse content.");
  433. return ESP_FAIL;
  434. }
  435. char * root_str = cJSON_Print(root);
  436. if(root_str!=NULL){
  437. ESP_LOGD(TAG, "Processing command item: \n%s", root_str);
  438. free(root_str);
  439. }
  440. cJSON *item=cJSON_GetObjectItemCaseSensitive(root, "command");
  441. if(!item){
  442. ESP_LOGE_LOC(TAG, "Command not found. Received content was: %s",command);
  443. httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Malformed command json. Unable to parse content.");
  444. err = ESP_FAIL;
  445. }
  446. else{
  447. // navigate to the first child of the config structure
  448. if(run_command(cJSON_GetStringValue(item))!=ESP_OK){
  449. httpd_resp_send(req, (const char *)failed, strlen(failed));
  450. }
  451. else {
  452. httpd_resp_send(req, (const char *)success, strlen(success));
  453. }
  454. }
  455. ESP_LOGD_LOC(TAG, "done serving [%s]", req->uri);
  456. return err;
  457. }
  458. esp_err_t ap_get_handler(httpd_req_t *req){
  459. ESP_LOGD_LOC(TAG, "serving [%s]", req->uri);
  460. if(!is_user_authenticated(req)){
  461. // todo: redirect to login page
  462. // return ESP_OK;
  463. }
  464. /* if we can get the mutex, write the last version of the AP list */
  465. esp_err_t err = set_content_type_from_req(req);
  466. if( err == ESP_OK && wifi_manager_lock_json_buffer(( TickType_t ) 200/portTICK_PERIOD_MS)){
  467. char *buff = wifi_manager_alloc_get_ap_list_json();
  468. wifi_manager_unlock_json_buffer();
  469. if(buff!=NULL){
  470. httpd_resp_send(req, (const char *)buff, HTTPD_RESP_USE_STRLEN);
  471. free(buff);
  472. }
  473. else {
  474. ESP_LOGD_LOC(TAG, "Error retrieving ap list json string. ");
  475. httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Unable to retrieve AP list");
  476. }
  477. }
  478. else {
  479. httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "AP list unavailable");
  480. ESP_LOGE_LOC(TAG, "GET /ap.json failed to obtain mutex");
  481. }
  482. ESP_LOGD_LOC(TAG, "done serving [%s]", req->uri);
  483. return err;
  484. }
  485. esp_err_t config_get_handler(httpd_req_t *req){
  486. ESP_LOGD_LOC(TAG, "serving [%s]", req->uri);
  487. if(!is_user_authenticated(req)){
  488. // todo: redirect to login page
  489. // return ESP_OK;
  490. }
  491. esp_err_t err = set_content_type_from_req(req);
  492. if(err == ESP_OK){
  493. char * json = config_alloc_get_json(false);
  494. if(json==NULL){
  495. ESP_LOGD_LOC(TAG, "Error retrieving config json string. ");
  496. httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Error retrieving configuration object");
  497. err=ESP_FAIL;
  498. }
  499. else {
  500. ESP_LOGD_LOC(TAG, "config json : %s",json );
  501. cJSON * gplist=get_gpio_list(false);
  502. char * gpliststr=cJSON_PrintUnformatted(gplist);
  503. httpd_resp_sendstr_chunk(req,"{ \"gpio\":");
  504. httpd_resp_sendstr_chunk(req,gpliststr);
  505. httpd_resp_sendstr_chunk(req,", \"config\":");
  506. httpd_resp_sendstr_chunk(req, (const char *)json);
  507. httpd_resp_sendstr_chunk(req,"}");
  508. httpd_resp_sendstr_chunk(req,NULL);
  509. free(gpliststr);
  510. free(json);
  511. }
  512. }
  513. return err;
  514. }
  515. esp_err_t post_handler_buff_receive(httpd_req_t * req){
  516. esp_err_t err = ESP_OK;
  517. int total_len = req->content_len;
  518. int cur_len = 0;
  519. char *buf = ((rest_server_context_t *)(req->user_ctx))->scratch;
  520. int received = 0;
  521. if (total_len >= SCRATCH_BUFSIZE) {
  522. /* Respond with 500 Internal Server Error */
  523. ESP_LOGE_LOC(TAG,"Received content was too long. ");
  524. httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR , "Content too long");
  525. err = ESP_FAIL;
  526. }
  527. while (err == ESP_OK && cur_len < total_len) {
  528. received = httpd_req_recv(req, buf + cur_len, total_len);
  529. if (received <= 0) {
  530. /* Respond with 500 Internal Server Error */
  531. ESP_LOGE_LOC(TAG,"Not all data was received. ");
  532. httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR , "Not all data was received");
  533. err = ESP_FAIL;
  534. }
  535. else {
  536. cur_len += received;
  537. }
  538. }
  539. if(err == ESP_OK) {
  540. buf[total_len] = '\0';
  541. }
  542. return err;
  543. }
  544. esp_err_t config_post_handler(httpd_req_t *req){
  545. ESP_LOGD_LOC(TAG, "serving [%s]", req->uri);
  546. bool bOTA=false;
  547. char * otaURL=NULL;
  548. esp_err_t err = post_handler_buff_receive(req);
  549. if(err!=ESP_OK){
  550. return err;
  551. }
  552. if(!is_user_authenticated(req)){
  553. // todo: redirect to login page
  554. // return ESP_OK;
  555. }
  556. err = set_content_type_from_req(req);
  557. if(err != ESP_OK){
  558. return err;
  559. }
  560. char *buf = ((rest_server_context_t *)(req->user_ctx))->scratch;
  561. cJSON *root = cJSON_Parse(buf);
  562. if(root == NULL){
  563. ESP_LOGE_LOC(TAG, "Parsing config json failed. Received content was: %s",buf);
  564. httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Malformed config json. Unable to parse content.");
  565. return ESP_FAIL;
  566. }
  567. char * root_str = cJSON_Print(root);
  568. if(root_str!=NULL){
  569. ESP_LOGD(TAG, "Processing config item: \n%s", root_str);
  570. free(root_str);
  571. }
  572. cJSON *item=cJSON_GetObjectItemCaseSensitive(root, "config");
  573. if(!item){
  574. ESP_LOGE_LOC(TAG, "Parsing config json failed. Received content was: %s",buf);
  575. httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Malformed config json. Unable to parse content.");
  576. err = ESP_FAIL;
  577. }
  578. else{
  579. // navigate to the first child of the config structure
  580. if(item->child) item=item->child;
  581. }
  582. while (item && err == ESP_OK)
  583. {
  584. cJSON *prev_item = item;
  585. item=item->next;
  586. char * entry_str = cJSON_Print(prev_item);
  587. if(entry_str!=NULL){
  588. ESP_LOGD_LOC(TAG, "Processing config item: \n%s", entry_str);
  589. free(entry_str);
  590. }
  591. if(prev_item->string==NULL) {
  592. ESP_LOGD_LOC(TAG,"Config value does not have a name");
  593. httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Malformed config json. Value does not have a name.");
  594. err = ESP_FAIL;
  595. }
  596. if(err == ESP_OK){
  597. ESP_LOGD_LOC(TAG,"Found config value name [%s]", prev_item->string);
  598. nvs_type_t item_type= config_get_item_type(prev_item);
  599. if(item_type!=0){
  600. void * val = config_safe_alloc_get_entry_value(item_type, prev_item);
  601. if(val!=NULL){
  602. if(strcmp(prev_item->string, "fwurl")==0) {
  603. if(item_type!=NVS_TYPE_STR){
  604. ESP_LOGE_LOC(TAG,"Firmware url should be type %d. Found type %d instead.",NVS_TYPE_STR,item_type );
  605. httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Malformed config json. Wrong type for firmware URL.");
  606. err = ESP_FAIL;
  607. }
  608. else {
  609. // we're getting a request to do an OTA from that URL
  610. ESP_LOGW_LOC(TAG, "Found OTA request!");
  611. otaURL=strdup(val);
  612. bOTA=true;
  613. }
  614. }
  615. else {
  616. if(config_set_value(item_type, prev_item->string , val) != ESP_OK){
  617. ESP_LOGE_LOC(TAG,"Unable to store value for [%s]", prev_item->string);
  618. httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR , "Unable to store config value");
  619. err = ESP_FAIL;
  620. }
  621. else {
  622. ESP_LOGD_LOC(TAG,"Successfully set value for [%s]",prev_item->string);
  623. }
  624. }
  625. free(val);
  626. }
  627. else {
  628. char messageBuffer[101]={};
  629. ESP_LOGE_LOC(TAG,"Value not found for [%s]", prev_item->string);
  630. snprintf(messageBuffer,sizeof(messageBuffer),"Malformed config json. Missing value for entry %s.",prev_item->string);
  631. httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, messageBuffer);
  632. err = ESP_FAIL;
  633. }
  634. }
  635. else {
  636. ESP_LOGE_LOC(TAG,"Unable to determine the type of config value [%s]", prev_item->string);
  637. httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Malformed config json. Missing value for entry.");
  638. err = ESP_FAIL;
  639. }
  640. }
  641. }
  642. if(err==ESP_OK){
  643. httpd_resp_sendstr(req, "{ \"result\" : \"OK\" }");
  644. messaging_post_message(MESSAGING_INFO,MESSAGING_CLASS_SYSTEM,"Save Success");
  645. }
  646. cJSON_Delete(root);
  647. if(bOTA) {
  648. if(is_recovery_running){
  649. ESP_LOGW_LOC(TAG, "Starting process OTA for url %s",otaURL);
  650. }
  651. else {
  652. ESP_LOGW_LOC(TAG, "Restarting system to process OTA for url %s",otaURL);
  653. }
  654. wifi_manager_reboot_ota(otaURL);
  655. free(otaURL);
  656. }
  657. return err;
  658. }
  659. esp_err_t connect_post_handler(httpd_req_t *req){
  660. ESP_LOGD_LOC(TAG, "serving [%s]", req->uri);
  661. char success[]="{}";
  662. char * ssid=NULL;
  663. char * password=NULL;
  664. char * host_name=NULL;
  665. esp_err_t err = post_handler_buff_receive(req);
  666. if(err!=ESP_OK){
  667. return err;
  668. }
  669. err = set_content_type_from_req(req);
  670. if(err != ESP_OK){
  671. return err;
  672. }
  673. char *buf = ((rest_server_context_t *)(req->user_ctx))->scratch;
  674. if(!is_user_authenticated(req)){
  675. // todo: redirect to login page
  676. // return ESP_OK;
  677. }
  678. cJSON *root = cJSON_Parse(buf);
  679. if(root==NULL){
  680. httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR , "JSON parsing error.");
  681. return ESP_FAIL;
  682. }
  683. cJSON * ssid_object = cJSON_GetObjectItem(root, "ssid");
  684. if(ssid_object !=NULL){
  685. ssid = strdup(ssid_object->valuestring);
  686. }
  687. cJSON * password_object = cJSON_GetObjectItem(root, "pwd");
  688. if(password_object !=NULL){
  689. password = strdup(password_object->valuestring);
  690. }
  691. cJSON * host_name_object = cJSON_GetObjectItem(root, "host_name");
  692. if(host_name_object !=NULL){
  693. host_name = strdup(host_name_object->valuestring);
  694. }
  695. cJSON_Delete(root);
  696. if(host_name!=NULL){
  697. if(config_set_value(NVS_TYPE_STR, "host_name", host_name) != ESP_OK){
  698. ESP_LOGW_LOC(TAG, "Unable to save host name configuration");
  699. }
  700. }
  701. if(ssid !=NULL && strlen(ssid) <= MAX_SSID_SIZE && strlen(password) <= MAX_PASSWORD_SIZE ){
  702. wifi_config_t* config = wifi_manager_get_wifi_sta_config();
  703. memset(config, 0x00, sizeof(wifi_config_t));
  704. strlcpy((char *)config->sta.ssid, ssid, sizeof(config->sta.ssid)+1);
  705. if(password){
  706. strlcpy((char *)config->sta.password, password, sizeof(config->sta.password)+1);
  707. }
  708. ESP_LOGD_LOC(TAG, "http_server_netconn_serve: wifi_manager_connect_async() call, with ssid: %s, password: %s", config->sta.ssid, config->sta.password);
  709. wifi_manager_connect_async();
  710. httpd_resp_send(req, (const char *)success, strlen(success));
  711. }
  712. else {
  713. httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Malformed json. Missing or invalid ssid/password.");
  714. err = ESP_FAIL;
  715. }
  716. FREE_AND_NULL(ssid);
  717. FREE_AND_NULL(password);
  718. FREE_AND_NULL(host_name);
  719. return err;
  720. }
  721. esp_err_t connect_delete_handler(httpd_req_t *req){
  722. char success[]="{}";
  723. ESP_LOGD_LOC(TAG, "serving [%s]", req->uri);
  724. if(!is_user_authenticated(req)){
  725. // todo: redirect to login page
  726. // return ESP_OK;
  727. }
  728. esp_err_t err = set_content_type_from_req(req);
  729. if(err != ESP_OK){
  730. return err;
  731. }
  732. httpd_resp_send(req, (const char *)success, strlen(success));
  733. wifi_manager_disconnect_async();
  734. return ESP_OK;
  735. }
  736. esp_err_t reboot_ota_post_handler(httpd_req_t *req){
  737. char success[]="{}";
  738. ESP_LOGD_LOC(TAG, "serving [%s]", req->uri);
  739. if(!is_user_authenticated(req)){
  740. // todo: redirect to login page
  741. // return ESP_OK;
  742. }
  743. esp_err_t err = set_content_type_from_req(req);
  744. if(err != ESP_OK){
  745. return err;
  746. }
  747. httpd_resp_send(req, (const char *)success, strlen(success));
  748. wifi_manager_reboot(OTA);
  749. return ESP_OK;
  750. }
  751. esp_err_t reboot_post_handler(httpd_req_t *req){
  752. ESP_LOGD_LOC(TAG, "serving [%s]", req->uri);
  753. char success[]="{}";
  754. if(!is_user_authenticated(req)){
  755. // todo: redirect to login page
  756. // return ESP_OK;
  757. }
  758. esp_err_t err = set_content_type_from_req(req);
  759. if(err != ESP_OK){
  760. return err;
  761. }
  762. httpd_resp_send(req, (const char *)success, strlen(success));
  763. wifi_manager_reboot(RESTART);
  764. return ESP_OK;
  765. }
  766. esp_err_t recovery_post_handler(httpd_req_t *req){
  767. ESP_LOGD_LOC(TAG, "serving [%s]", req->uri);
  768. char success[]="{}";
  769. if(!is_user_authenticated(req)){
  770. // todo: redirect to login page
  771. // return ESP_OK;
  772. }
  773. esp_err_t err = set_content_type_from_req(req);
  774. if(err != ESP_OK){
  775. return err;
  776. }
  777. httpd_resp_send(req, (const char *)success, strlen(success));
  778. wifi_manager_reboot(RECOVERY);
  779. return ESP_OK;
  780. }
  781. esp_err_t flash_post_handler(httpd_req_t *req){
  782. esp_err_t err =ESP_OK;
  783. if(is_recovery_running){
  784. ESP_LOGD_LOC(TAG, "serving [%s]", req->uri);
  785. char success[]="File uploaded. Flashing started.";
  786. if(!is_user_authenticated(req)){
  787. // todo: redirect to login page
  788. // return ESP_OK;
  789. }
  790. err = httpd_resp_set_type(req, HTTPD_TYPE_TEXT);
  791. if(err != ESP_OK){
  792. return err;
  793. }
  794. char * binary_buffer = malloc(req->content_len);
  795. if(binary_buffer == NULL){
  796. ESP_LOGE(TAG, "File too large : %d bytes", req->content_len);
  797. /* Respond with 400 Bad Request */
  798. httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST,
  799. "Binary file too large. Unable to allocate memory!");
  800. return ESP_FAIL;
  801. }
  802. ESP_LOGI(TAG, "Receiving ota binary file");
  803. /* Retrieve the pointer to scratch buffer for temporary storage */
  804. char *buf = ((rest_server_context_t *)(req->user_ctx))->scratch;
  805. char *head=binary_buffer;
  806. int received;
  807. /* Content length of the request gives
  808. * the size of the file being uploaded */
  809. int remaining = req->content_len;
  810. while (remaining > 0) {
  811. ESP_LOGI(TAG, "Remaining size : %d", remaining);
  812. /* Receive the file part by part into a buffer */
  813. if ((received = httpd_req_recv(req, buf, MIN(remaining, SCRATCH_BUFSIZE))) <= 0) {
  814. if (received == HTTPD_SOCK_ERR_TIMEOUT) {
  815. /* Retry if timeout occurred */
  816. continue;
  817. }
  818. FREE_RESET(binary_buffer);
  819. ESP_LOGE(TAG, "File reception failed!");
  820. /* Respond with 500 Internal Server Error */
  821. httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to receive file");
  822. err = ESP_FAIL;
  823. goto bail_out;
  824. }
  825. /* Write buffer content to file on storage */
  826. if (received ) {
  827. memcpy(head,buf,received );
  828. head+=received;
  829. }
  830. /* Keep track of remaining size of
  831. * the file left to be uploaded */
  832. remaining -= received;
  833. }
  834. /* Close file upon upload completion */
  835. ESP_LOGI(TAG, "File reception complete. Invoking OTA process.");
  836. err = start_ota(NULL, binary_buffer, req->content_len);
  837. if(err!=ESP_OK){
  838. httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "OTA processing failed");
  839. goto bail_out;
  840. }
  841. //todo: handle this in ajax. For now, just send the root page
  842. httpd_resp_send(req, (const char *)success, strlen(success));
  843. }
  844. bail_out:
  845. return err;
  846. }
  847. char * get_ap_ip_address(){
  848. static char ap_ip_address[IP4ADDR_STRLEN_MAX]={};
  849. tcpip_adapter_ip_info_t ip_info;
  850. esp_err_t err=ESP_OK;
  851. memset(ap_ip_address, 0x00, sizeof(ap_ip_address));
  852. ESP_LOGD_LOC(TAG, "checking if soft AP is enabled");
  853. if(tcpip_adapter_is_netif_up(TCPIP_ADAPTER_IF_AP)){
  854. ESP_LOGD_LOC(TAG, "Soft AP is enabled. getting ip info");
  855. // Access point is up and running. Get the current IP address
  856. err = tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_AP, &ip_info);
  857. if(err != ESP_OK){
  858. ESP_LOGE_LOC(TAG, "Unable to get local AP ip address. Error: %s",esp_err_to_name(err));
  859. }
  860. else {
  861. ESP_LOGV_LOC(TAG, "Converting soft ip address to string");
  862. ip4addr_ntoa_r(&ip_info.ip, ap_ip_address, IP4ADDR_STRLEN_MAX);
  863. ESP_LOGD_LOC(TAG,"TCPIP_ADAPTER_IF_AP is up and has ip address %s ", ap_ip_address);
  864. }
  865. }
  866. else{
  867. ESP_LOGD_LOC(TAG,"AP Is not enabled. Returning blank string");
  868. }
  869. return ap_ip_address;
  870. }
  871. esp_err_t process_redirect(httpd_req_t *req, const char * status){
  872. const char location_prefix[] = "http://";
  873. char * ap_ip_address=get_ap_ip_address();
  874. char * remote_ip=NULL;
  875. in_port_t port=0;
  876. char *redirect_url = NULL;
  877. ESP_LOGD_LOC(TAG, "Getting remote socket address");
  878. remote_ip = http_alloc_get_socket_address(req,0, &port);
  879. size_t buf_size = strlen(redirect_payload1) +strlen(redirect_payload2) + strlen(redirect_payload3) +2*(strlen(location_prefix)+strlen(ap_ip_address))+1;
  880. char * redirect=malloc(buf_size);
  881. if(strcasestr(status,"302")){
  882. size_t url_buf_size = strlen(location_prefix) + strlen(ap_ip_address)+1;
  883. redirect_url = malloc(url_buf_size);
  884. memset(redirect_url,0x00,url_buf_size);
  885. snprintf(redirect_url, buf_size,"%s%s/",location_prefix, ap_ip_address);
  886. ESP_LOGW_LOC(TAG, "Redirecting host [%s] to %s (from uri %s)",remote_ip, redirect_url,req->uri);
  887. httpd_resp_set_hdr(req,"Location",redirect_url);
  888. snprintf(redirect, buf_size,"OK");
  889. }
  890. else {
  891. snprintf(redirect, buf_size,"%s%s%s%s%s%s%s",redirect_payload1, location_prefix, ap_ip_address,redirect_payload2, location_prefix, ap_ip_address,redirect_payload3);
  892. ESP_LOGW_LOC(TAG, "Responding to host [%s] (from uri %s) with redirect html page %s",remote_ip, req->uri,redirect);
  893. }
  894. httpd_resp_set_type(req, HTTPD_TYPE_TEXT);
  895. httpd_resp_set_hdr(req,"Cache-Control","no-cache");
  896. httpd_resp_set_status(req, status);
  897. httpd_resp_send(req, redirect, HTTPD_RESP_USE_STRLEN);
  898. FREE_AND_NULL(redirect);
  899. FREE_AND_NULL(redirect_url);
  900. FREE_AND_NULL(remote_ip);
  901. return ESP_OK;
  902. }
  903. esp_err_t redirect_200_ev_handler(httpd_req_t *req){
  904. ESP_LOGD_LOC(TAG,"Processing known redirect url %s",req->uri);
  905. process_redirect(req,"200 OK");
  906. return ESP_OK;
  907. }
  908. esp_err_t redirect_processor(httpd_req_t *req, httpd_err_code_t error){
  909. esp_err_t err=ESP_OK;
  910. const char * host_name=NULL;
  911. const char * ap_host_name=NULL;
  912. char * user_agent=NULL;
  913. char * remote_ip=NULL;
  914. char * sta_ip_address=NULL;
  915. char * ap_ip_address=get_ap_ip_address();
  916. char * socket_local_address=NULL;
  917. bool request_contains_hostname = false;
  918. bool request_contains_ap_ip_address = false;
  919. bool request_is_sta_ip_address = false;
  920. bool connected_to_ap_ip_interface = false;
  921. bool connected_to_sta_ip_interface = false;
  922. bool useragentiscaptivenetwork = false;
  923. in_port_t port=0;
  924. ESP_LOGV_LOC(TAG, "Getting remote socket address");
  925. remote_ip = http_alloc_get_socket_address(req,0, &port);
  926. ESP_LOGW_LOC(TAG, "%s requested invalid URL: [%s]",remote_ip, req->uri);
  927. if(wifi_manager_lock_sta_ip_string(portMAX_DELAY)){
  928. sta_ip_address = strdup(wifi_manager_get_sta_ip_string());
  929. wifi_manager_unlock_sta_ip_string();
  930. }
  931. else {
  932. ESP_LOGE(TAG,"Unable to obtain local IP address from WiFi Manager.");
  933. httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR , NULL);
  934. }
  935. ESP_LOGV_LOC(TAG, "Getting host name from request");
  936. char *req_host = alloc_get_http_header(req, "Host");
  937. user_agent = alloc_get_http_header(req,"User-Agent");
  938. if((useragentiscaptivenetwork = (user_agent!=NULL && strcasestr(user_agent,"CaptiveNetworkSupport"))==true)){
  939. ESP_LOGW_LOC(TAG,"Found user agent that supports captive networks! [%s]",user_agent);
  940. }
  941. esp_err_t hn_err = ESP_OK;
  942. ESP_LOGV_LOC(TAG, "Getting adapter host name");
  943. if((hn_err = tcpip_adapter_get_hostname(TCPIP_ADAPTER_IF_STA, &host_name )) !=ESP_OK) {
  944. ESP_LOGE_LOC(TAG, "Unable to get host name. Error: %s",esp_err_to_name(hn_err));
  945. err=err==ESP_OK?hn_err:err;
  946. }
  947. else {
  948. ESP_LOGV_LOC(TAG, "Host name is %s",host_name);
  949. }
  950. in_port_t loc_port=0;
  951. ESP_LOGV_LOC(TAG, "Getting local socket address");
  952. socket_local_address= http_alloc_get_socket_address(req,1, &loc_port);
  953. ESP_LOGD_LOC(TAG, "Peer IP: %s [port %u], System AP IP address: %s, System host: %s. Requested Host: [%s], uri [%s]",STR_OR_NA(remote_ip), port, STR_OR_NA(ap_ip_address), STR_OR_NA(host_name), STR_OR_NA(req_host), req->uri);
  954. /* captive portal functionality: redirect to access point IP for HOST that are not the access point IP OR the STA IP */
  955. /* determine if Host is from the STA IP address */
  956. if((request_contains_hostname = (host_name!=NULL) && (req_host!=NULL) && strcasestr(req_host,host_name)) == true){
  957. ESP_LOGD_LOC(TAG,"http request host = system host name %s", req_host);
  958. }
  959. else if((request_contains_hostname = (ap_host_name!=NULL) && (req_host!=NULL) && strcasestr(req_host,ap_host_name)) == true){
  960. ESP_LOGD_LOC(TAG,"http request host = AP system host name %s", req_host);
  961. }
  962. if((request_contains_ap_ip_address = (ap_ip_address!=NULL) && (req_host!=NULL) && strcasestr(req_host,ap_ip_address))== true){
  963. ESP_LOGD_LOC(TAG,"http request host is access point ip address %s", req_host);
  964. }
  965. if((connected_to_ap_ip_interface = (ap_ip_address!=NULL) && (socket_local_address!=NULL) && strcasestr(socket_local_address,ap_ip_address))==true){
  966. ESP_LOGD_LOC(TAG,"http request is connected to access point interface IP %s", ap_ip_address);
  967. }
  968. if((request_is_sta_ip_address = (sta_ip_address!=NULL) && (req_host!=NULL) && strcasestr(req_host,sta_ip_address))==true){
  969. ESP_LOGD_LOC(TAG,"http request host is WiFi client ip address %s", req_host);
  970. }
  971. if((connected_to_sta_ip_interface = (sta_ip_address!=NULL) && (socket_local_address!=NULL) && strcasestr(sta_ip_address,socket_local_address))==true){
  972. ESP_LOGD_LOC(TAG,"http request is connected to WiFi client ip address %s", sta_ip_address);
  973. }
  974. if((error == 0) || (error == HTTPD_404_NOT_FOUND && connected_to_ap_ip_interface && !(request_contains_ap_ip_address || request_contains_hostname ))) {
  975. process_redirect(req,"302 Found");
  976. }
  977. else {
  978. ESP_LOGD_LOC(TAG,"URL not found, and not processing captive portal so throw regular 404 error");
  979. httpd_resp_send_err(req, error, NULL);
  980. }
  981. FREE_AND_NULL(socket_local_address);
  982. FREE_AND_NULL(req_host);
  983. FREE_AND_NULL(user_agent);
  984. FREE_AND_NULL(sta_ip_address);
  985. FREE_AND_NULL(remote_ip);
  986. return err;
  987. }
  988. esp_err_t redirect_ev_handler(httpd_req_t *req){
  989. return redirect_processor(req,0);
  990. }
  991. esp_err_t messages_get_handler(httpd_req_t *req){
  992. ESP_LOGD_LOC(TAG, "serving [%s]", req->uri);
  993. if(!is_user_authenticated(req)){
  994. // todo: redirect to login page
  995. // return ESP_OK;
  996. }
  997. esp_err_t err = set_content_type_from_req(req);
  998. if(err != ESP_OK){
  999. return err;
  1000. }
  1001. cJSON * json_messages= messaging_retrieve_messages(messaging);
  1002. if(json_messages!=NULL){
  1003. char * json_text= cJSON_Print(json_messages);
  1004. httpd_resp_send(req, (const char *)json_text, strlen(json_text));
  1005. free(json_text);
  1006. cJSON_Delete(json_messages);
  1007. }
  1008. else {
  1009. httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR , "Unable to retrieve messages");
  1010. }
  1011. return ESP_OK;
  1012. }
  1013. esp_err_t status_get_handler(httpd_req_t *req){
  1014. ESP_LOGD_LOC(TAG, "serving [%s]", req->uri);
  1015. if(!is_user_authenticated(req)){
  1016. // todo: redirect to login page
  1017. // return ESP_OK;
  1018. }
  1019. esp_err_t err = set_content_type_from_req(req);
  1020. if(err != ESP_OK){
  1021. return err;
  1022. }
  1023. if(wifi_manager_lock_json_buffer(( TickType_t ) 200/portTICK_PERIOD_MS)) {
  1024. char *buff = wifi_manager_alloc_get_ip_info_json();
  1025. wifi_manager_unlock_json_buffer();
  1026. if(buff) {
  1027. httpd_resp_send(req, (const char *)buff, strlen(buff));
  1028. free(buff);
  1029. }
  1030. else {
  1031. httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR , "Empty status object");
  1032. }
  1033. }
  1034. else {
  1035. httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR , "Error retrieving status object");
  1036. }
  1037. // update status for next status call
  1038. wifi_manager_update_status();
  1039. return ESP_OK;
  1040. }
  1041. esp_err_t err_handler(httpd_req_t *req, httpd_err_code_t error){
  1042. esp_err_t err = ESP_OK;
  1043. if(error != HTTPD_404_NOT_FOUND){
  1044. err = httpd_resp_send_err(req, error, NULL);
  1045. }
  1046. else {
  1047. err = redirect_processor(req,error);
  1048. }
  1049. return err;
  1050. }