http_server.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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.h"
  28. #include "cmd_system.h"
  29. #include <inttypes.h>
  30. #include "squeezelite-ota.h"
  31. #include "nvs_utilities.h"
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include "cJSON.h"
  35. #define NVS_PARTITION_NAME "nvs"
  36. /* @brief tag used for ESP serial console messages */
  37. static const char TAG[] = "http_server";
  38. cJSON * nvs_json=NULL;
  39. /* @brief task handle for the http server */
  40. static TaskHandle_t task_http_server = NULL;
  41. /**
  42. * @brief embedded binary data.
  43. * @see file "component.mk"
  44. * @see https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html#embedding-binary-data
  45. */
  46. extern const uint8_t style_css_start[] asm("_binary_style_css_start");
  47. extern const uint8_t style_css_end[] asm("_binary_style_css_end");
  48. extern const uint8_t jquery_gz_start[] asm("_binary_jquery_min_js_gz_start");
  49. extern const uint8_t jquery_gz_end[] asm("_binary_jquery_min_js_gz_end");
  50. extern const uint8_t popper_gz_start[] asm("_binary_popper_min_js_gz_start");
  51. extern const uint8_t popper_gz_end[] asm("_binary_popper_min_js_gz_end");
  52. extern const uint8_t bootstrap_js_gz_start[] asm("_binary_bootstrap_min_js_gz_start");
  53. extern const uint8_t bootstrap_js_gz_end[] asm("_binary_bootstrap_min_js_gz_end");
  54. extern const uint8_t bootstrap_css_gz_start[] asm("_binary_bootstrap_min_css_gz_start");
  55. extern const uint8_t bootstrap_css_gz_end[] asm("_binary_bootstrap_min_css_gz_end");
  56. extern const uint8_t code_js_start[] asm("_binary_code_js_start");
  57. extern const uint8_t code_js_end[] asm("_binary_code_js_end");
  58. extern const uint8_t index_html_start[] asm("_binary_index_html_start");
  59. extern const uint8_t index_html_end[] asm("_binary_index_html_end");
  60. /* const http headers stored in ROM */
  61. 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";
  62. const static char http_html_hdr[] = "HTTP/1.1 200 OK\nContent-type: text/html\nAccess-Control-Allow-Origin: *\n\n";
  63. 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";
  64. const static char http_js_hdr[] = "HTTP/1.1 200 OK\nContent-type: text/javascript\nAccess-Control-Allow-Origin: *\n\n";
  65. const static char http_400_hdr[] = "HTTP/1.1 400 Bad Request\nContent-Length: 0\n\n";
  66. const static char http_404_hdr[] = "HTTP/1.1 404 Not Found\nContent-Length: 0\n\n";
  67. const static char http_503_hdr[] = "HTTP/1.1 503 Service Unavailable\nContent-Length: 0\n\n";
  68. 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: *\n\n";
  69. const static char http_redirect_hdr_start[] = "HTTP/1.1 302 Found\nLocation: http://";
  70. const static char http_redirect_hdr_end[] = "/\n\n";
  71. void http_server_start(){
  72. if(task_http_server == NULL){
  73. xTaskCreate(&http_server, "http_server", 1024*5, NULL, WIFI_MANAGER_TASK_PRIORITY, &task_http_server);
  74. }
  75. }
  76. void http_server(void *pvParameters) {
  77. struct netconn *conn, *newconn;
  78. err_t err;
  79. conn = netconn_new(NETCONN_TCP);
  80. netconn_bind(conn, IP_ADDR_ANY, 80);
  81. netconn_listen(conn);
  82. ESP_LOGI(TAG, "HTTP Server listening on 80/tcp");
  83. do {
  84. err = netconn_accept(conn, &newconn);
  85. if (err == ERR_OK) {
  86. http_server_netconn_serve(newconn);
  87. netconn_delete(newconn);
  88. }
  89. else
  90. {
  91. ESP_LOGE(TAG,"Error accepting new connection. Terminating HTTP server");
  92. }
  93. taskYIELD(); /* allows the freeRTOS scheduler to take over if needed. */
  94. } while(err == ERR_OK);
  95. netconn_close(conn);
  96. netconn_delete(conn);
  97. vTaskDelete( NULL );
  98. }
  99. char* http_server_get_header(char *request, char *header_name, int *len) {
  100. *len = 0;
  101. char *ret = NULL;
  102. char *ptr = NULL;
  103. ptr = strstr(request, header_name);
  104. if (ptr) {
  105. ret = ptr + strlen(header_name);
  106. ptr = ret;
  107. while (*ptr != '\0' && *ptr != '\n' && *ptr != '\r') {
  108. (*len)++;
  109. ptr++;
  110. }
  111. return ret;
  112. }
  113. return NULL;
  114. }
  115. char* http_server_search_header(char *request, char *header_name, int *len, char ** parm_name, char ** next_position, char * bufEnd) {
  116. *len = 0;
  117. char *ret = NULL;
  118. char *ptr = NULL;
  119. int currentLength=0;
  120. ESP_LOGD(TAG, "header name: [%s]\nRequest: %s", header_name, request);
  121. ptr = strstr(request, header_name);
  122. if (ptr!=NULL && ptr<bufEnd) {
  123. ret = ptr + strlen(header_name);
  124. ptr = ret;
  125. currentLength=(int)(ptr-request);
  126. ESP_LOGD(TAG, "found string at %d", currentLength);
  127. while (*ptr != '\0' && *ptr != '\n' && *ptr != '\r' && *ptr != ':' && ptr<bufEnd) {
  128. ptr++;
  129. }
  130. if(*ptr==':'){
  131. currentLength=(int)(ptr-ret);
  132. ESP_LOGD(TAG, "Found parameter name end, length : %d", currentLength);
  133. // save the parameter name: the string between header name and ":"
  134. *parm_name=malloc(currentLength+1);
  135. if(*parm_name==NULL){
  136. ESP_LOGE(TAG, "Unable to allocate memory for new header name");
  137. return NULL;
  138. }
  139. memset(*parm_name, 0x00,currentLength+1);
  140. strncpy(*parm_name,ret,currentLength);
  141. ESP_LOGD(TAG, "Found parameter name : %s ", *parm_name);
  142. ptr++;
  143. while (*ptr == ' ' && ptr<bufEnd) {
  144. ptr++;
  145. }
  146. }
  147. ret=ptr;
  148. while (*ptr != '\0' && *ptr != '\n' && *ptr != '\r'&& ptr<bufEnd) {
  149. (*len)++;
  150. ptr++;
  151. }
  152. // Terminate value inside its actual buffer so we can treat it as individual string
  153. *ptr='\0';
  154. currentLength=(int)(ptr-ret);
  155. ESP_LOGD(TAG, "Found parameter value end, length : %d, value: %s", currentLength,ret );
  156. *next_position=++ptr;
  157. return ret;
  158. }
  159. ESP_LOGD(TAG, "No more match for : %s", header_name);
  160. return NULL;
  161. }
  162. void http_server_send_resource_file(struct netconn *conn,const uint8_t * start, const uint8_t * end, char * content_type,char * encoding){
  163. uint16_t len=end - start;
  164. size_t buff_length= sizeof(http_hdr_template)+strlen(content_type)+strlen(encoding);
  165. char * http_hdr=malloc(buff_length);
  166. if( http_hdr == NULL){
  167. ESP_LOGE(TAG,"Cound not allocate %d bytes for headers.",buff_length);
  168. netconn_write(conn, http_503_hdr, sizeof(http_503_hdr) - 1, NETCONN_NOCOPY);
  169. }
  170. else
  171. {
  172. memset(http_hdr,0x00,buff_length);
  173. snprintf(http_hdr, buff_length-1,http_hdr_template,content_type,len,encoding);
  174. netconn_write(conn, http_hdr, strlen(http_hdr), NETCONN_NOCOPY);
  175. ESP_LOGD(TAG,"sending response : %s",http_hdr);
  176. netconn_write(conn, start, end - start, NETCONN_NOCOPY);
  177. free(http_hdr);
  178. }
  179. }
  180. #define NUM_BUFFER_LEN 101
  181. err_t http_server_nvs_dump(struct netconn *conn, nvs_type_t nvs_type, bool * bFirst){
  182. nvs_entry_info_t info;
  183. char * num_buffer = NULL;
  184. if(nvs_json!=NULL){
  185. cJSON_Delete(nvs_json);
  186. nvs_json=NULL;
  187. }
  188. nvs_json = cJSON_CreateObject();
  189. num_buffer = malloc(NUM_BUFFER_LEN);
  190. nvs_iterator_t it = nvs_entry_find(NVS_PARTITION_NAME, NULL, nvs_type);
  191. if (it == NULL) {
  192. ESP_LOGW(TAG, "No nvs entry found in %s",NVS_PARTITION_NAME );
  193. }
  194. while (it != NULL){
  195. nvs_entry_info(it, &info);
  196. memset(num_buffer,0x00,NUM_BUFFER_LEN);
  197. if(strstr(info.namespace_name, current_namespace)){
  198. void * value = get_nvs_value_alloc(nvs_type,info.key);
  199. if(value==NULL)
  200. {
  201. ESP_LOGE(TAG,"nvs read failed.");
  202. netconn_write(conn, http_503_hdr, sizeof(http_503_hdr) - 1, NETCONN_NOCOPY); //200ok
  203. free(num_buffer);
  204. cJSON_Delete(nvs_json);
  205. return ESP_FAIL;
  206. }
  207. switch (nvs_type) {
  208. case NVS_TYPE_I8:
  209. snprintf(num_buffer, NUM_BUFFER_LEN-1, "%i", *(int8_t*)value);
  210. break;
  211. case NVS_TYPE_I16:
  212. snprintf(num_buffer, NUM_BUFFER_LEN-1, "%i", *(int16_t*)value);
  213. break;
  214. case NVS_TYPE_I32:
  215. snprintf(num_buffer, NUM_BUFFER_LEN-1, "%i", *(int32_t*)value);
  216. break;
  217. case NVS_TYPE_U8:
  218. snprintf(num_buffer, NUM_BUFFER_LEN-1, "%u", *(uint8_t*)value);
  219. break;
  220. case NVS_TYPE_U16:
  221. snprintf(num_buffer, NUM_BUFFER_LEN-1, "%u", *(uint16_t*)value);
  222. break;
  223. case NVS_TYPE_U32:
  224. snprintf(num_buffer, NUM_BUFFER_LEN-1, "%u", *(uint32_t*)value);
  225. break;
  226. case NVS_TYPE_STR:
  227. // string will be processed directly below
  228. break;
  229. case NVS_TYPE_I64:
  230. case NVS_TYPE_U64:
  231. default:
  232. ESP_LOGE(TAG, "nvs type %u not supported", nvs_type);
  233. break;
  234. }
  235. cJSON_AddItemToObject(nvs_json, info.key, cJSON_CreateString((nvs_type==NVS_TYPE_STR)?(char *)value:num_buffer));
  236. free(value );
  237. }
  238. it = nvs_entry_next(it);
  239. }
  240. ESP_LOGD(TAG,"config json : %s\n", cJSON_Print(nvs_json));
  241. netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY);
  242. netconn_write(conn, cJSON_Print(nvs_json), strlen(cJSON_Print(nvs_json)), NETCONN_NOCOPY);
  243. cJSON_Delete(nvs_json);
  244. free(num_buffer);
  245. return ESP_OK;
  246. }
  247. void http_server_netconn_serve(struct netconn *conn) {
  248. struct netbuf *inbuf;
  249. char *buf = NULL;
  250. u16_t buflen;
  251. err_t err;
  252. const char new_line[2] = "\n";
  253. err = netconn_recv(conn, &inbuf);
  254. if (err == ERR_OK) {
  255. netbuf_data(inbuf, (void**)&buf, &buflen);
  256. /* extract the first line of the request */
  257. char *save_ptr = buf;
  258. char *line = strtok_r(save_ptr, new_line, &save_ptr);
  259. ESP_LOGD(TAG,"Processing line %s",line);
  260. if(line) {
  261. /* captive portal functionality: redirect to access point IP for HOST that are not the access point IP OR the STA IP */
  262. int lenH = 0;
  263. char *host = http_server_get_header(save_ptr, "Host: ", &lenH);
  264. const char * host_name=NULL;
  265. if((err=tcpip_adapter_get_hostname(TCPIP_ADAPTER_IF_STA, &host_name )) !=ESP_OK){
  266. ESP_LOGE(TAG,"Unable to get host name. Error: %s",esp_err_to_name(err));
  267. }
  268. /* determine if Host is from the STA IP address */
  269. wifi_manager_lock_sta_ip_string(portMAX_DELAY);
  270. bool access_from_sta_ip = lenH > 0?strstr(host, wifi_manager_get_sta_ip_string()):false;
  271. wifi_manager_unlock_sta_ip_string();
  272. bool access_from_host_name = (host_name!=NULL) && strstr(host, host_name);
  273. if (lenH > 0 && !strstr(host, DEFAULT_AP_IP) && !(access_from_sta_ip || access_from_host_name)) {
  274. ESP_LOGI(TAG,"Redirecting to default AP IP Address : %s", DEFAULT_AP_IP);
  275. netconn_write(conn, http_redirect_hdr_start, sizeof(http_redirect_hdr_start) - 1, NETCONN_NOCOPY);
  276. netconn_write(conn, DEFAULT_AP_IP, sizeof(DEFAULT_AP_IP) - 1, NETCONN_NOCOPY);
  277. netconn_write(conn, http_redirect_hdr_end, sizeof(http_redirect_hdr_end) - 1, NETCONN_NOCOPY);
  278. }
  279. else{
  280. //static stuff
  281. /* default page */
  282. if(strstr(line, "GET / ")) {
  283. netconn_write(conn, http_html_hdr, sizeof(http_html_hdr) - 1, NETCONN_NOCOPY);
  284. netconn_write(conn, index_html_start, index_html_end- index_html_start, NETCONN_NOCOPY);
  285. }
  286. else if(strstr(line, "GET /code.js ")) {
  287. netconn_write(conn, http_js_hdr, sizeof(http_js_hdr) - 1, NETCONN_NOCOPY);
  288. netconn_write(conn, code_js_start, code_js_end - code_js_start, NETCONN_NOCOPY);
  289. }
  290. else if(strstr(line, "GET /style.css ")) {
  291. netconn_write(conn, http_css_hdr, sizeof(http_css_hdr) - 1, NETCONN_NOCOPY);
  292. netconn_write(conn, style_css_start, style_css_end - style_css_start, NETCONN_NOCOPY);
  293. }
  294. else if(strstr(line, "GET /jquery.js ")) {
  295. http_server_send_resource_file(conn,jquery_gz_start, jquery_gz_end, "text/javascript", "gzip" );
  296. }
  297. else if(strstr(line, "GET /popper.js ")) {
  298. http_server_send_resource_file(conn,popper_gz_start, popper_gz_end, "text/javascript", "gzip" );
  299. }
  300. else if(strstr(line, "GET /bootstrap.js ")) {
  301. http_server_send_resource_file(conn,bootstrap_js_gz_start, bootstrap_js_gz_end, "text/javascript", "gzip" );
  302. }
  303. else if(strstr(line, "GET /bootstrap.css ")) {
  304. http_server_send_resource_file(conn,bootstrap_css_gz_start, bootstrap_css_gz_end, "text/css", "gzip" );
  305. }
  306. //dynamic stuff
  307. else if(strstr(line, "GET /ap.json ")) {
  308. /* if we can get the mutex, write the last version of the AP list */
  309. ESP_LOGI(TAG,"Processing ap.json request");
  310. if(wifi_manager_lock_json_buffer(( TickType_t ) 10)){
  311. netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY);
  312. char *buff = wifi_manager_get_ap_list_json();
  313. netconn_write(conn, buff, strlen(buff), NETCONN_NOCOPY);
  314. wifi_manager_unlock_json_buffer();
  315. }
  316. else{
  317. netconn_write(conn, http_503_hdr, sizeof(http_503_hdr) - 1, NETCONN_NOCOPY);
  318. ESP_LOGE(TAG, "http_server_netconn_serve: GET /ap.json failed to obtain mutex");
  319. }
  320. /* request a wifi scan */
  321. ESP_LOGI(TAG,"Starting wifi scan");
  322. wifi_manager_scan_async();
  323. }
  324. else if(strstr(line, "GET /config.json ")){
  325. ESP_LOGI(TAG,"Serving config.json");
  326. ESP_LOGI(TAG, "About to get config from flash");
  327. bool bFirst=true;
  328. http_server_nvs_dump(conn,NVS_TYPE_STR , &bFirst);
  329. ESP_LOGD(TAG,"Done serving config.json");
  330. }
  331. else if(strstr(line, "POST /config.json ")){
  332. ESP_LOGI(TAG,"Serving POST config.json");
  333. int lenA=0;
  334. char * last_parm=save_ptr;
  335. char * next_parm=save_ptr;
  336. char * last_parm_name=NULL;
  337. bool bErrorFound=false;
  338. bool bOTA=false;
  339. char * otaURL=NULL;
  340. // make sure we terminate the netconn string
  341. save_ptr[buflen-1]='\0';
  342. while(last_parm!=NULL){
  343. // Search will return
  344. ESP_LOGI(TAG, "Getting parameters from X-Custom headers");
  345. last_parm = http_server_search_header(next_parm, "X-Custom-", &lenA, &last_parm_name,&next_parm,buf+buflen);
  346. if(last_parm!=NULL && last_parm_name!=NULL){
  347. ESP_LOGI(TAG, "http_server_netconn_serve: config.json/ call, found parameter %s=%s, length %i", last_parm_name, last_parm, lenA);
  348. if(strcmp(last_parm_name, "fwurl")==0){
  349. // we're getting a request to do an OTA from that URL
  350. ESP_LOGI(TAG, "OTA parameter found!");
  351. otaURL=strdup(last_parm);
  352. bOTA=true;
  353. }
  354. else {
  355. ESP_LOGD(TAG, "http_server_netconn_serve: config.json/ Storing parameter");
  356. err= store_nvs_value(NVS_TYPE_STR, last_parm_name , last_parm);
  357. if(err!=ESP_OK) ESP_LOGE(TAG,"Unable to save nvs value. Error: %s",esp_err_to_name(err));
  358. }
  359. }
  360. if(last_parm_name!=NULL) {
  361. free(last_parm_name);
  362. last_parm_name=NULL;
  363. }
  364. }
  365. if(bErrorFound){
  366. netconn_write(conn, http_400_hdr, sizeof(http_400_hdr) - 1, NETCONN_NOCOPY); //400 invalid request
  367. }
  368. else{
  369. if(bOTA){
  370. ESP_LOGI(TAG, "Restarting to process OTA for url %s",otaURL);
  371. netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY); //200ok
  372. start_ota(otaURL,false);
  373. free(otaURL);
  374. }
  375. }
  376. }
  377. else if(strstr(line, "POST /connect.json ")) {
  378. ESP_LOGI(TAG, "http_server_netconn_serve: POST /connect.json");
  379. bool found = false;
  380. int lenS = 0, lenP = 0;
  381. char *ssid = NULL, *password = NULL;
  382. ssid = http_server_get_header(save_ptr, "X-Custom-ssid: ", &lenS);
  383. password = http_server_get_header(save_ptr, "X-Custom-pwd: ", &lenP);
  384. if(ssid && lenS <= MAX_SSID_SIZE && password && lenP <= MAX_PASSWORD_SIZE){
  385. wifi_config_t* config = wifi_manager_get_wifi_sta_config();
  386. memset(config, 0x00, sizeof(wifi_config_t));
  387. memcpy(config->sta.ssid, ssid, lenS);
  388. memcpy(config->sta.password, password, lenP);
  389. ESP_LOGD(TAG, "http_server_netconn_serve: wifi_manager_connect_async() call, with ssid: %s, password: %s", ssid, password);
  390. wifi_manager_connect_async();
  391. netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY); //200ok
  392. found = true;
  393. }
  394. if(!found){
  395. /* bad request the authentification header is not complete/not the correct format */
  396. netconn_write(conn, http_400_hdr, sizeof(http_400_hdr) - 1, NETCONN_NOCOPY);
  397. ESP_LOGE(TAG, "bad request the authentification header is not complete/not the correct format");
  398. }
  399. }
  400. else if(strstr(line, "DELETE /connect.json ")) {
  401. ESP_LOGI(TAG, "http_server_netconn_serve: DELETE /connect.json");
  402. /* request a disconnection from wifi and forget about it */
  403. wifi_manager_disconnect_async();
  404. netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY); /* 200 ok */
  405. }
  406. else if(strstr(line, "POST /reboot.json ")){
  407. guided_restart_ota();
  408. }
  409. else if(strstr(line, "POST /recovery.json ")){
  410. guided_factory();
  411. }
  412. else if(strstr(line, "GET /status.json ")){
  413. ESP_LOGI(TAG,"Serving status.json");
  414. if(wifi_manager_lock_json_buffer(( TickType_t ) 10)){
  415. char *buff = wifi_manager_get_ip_info_json();
  416. if(buff){
  417. netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY);
  418. netconn_write(conn, buff, strlen(buff), NETCONN_NOCOPY);
  419. }
  420. else{
  421. netconn_write(conn, http_503_hdr, sizeof(http_503_hdr) - 1, NETCONN_NOCOPY);
  422. }
  423. wifi_manager_unlock_json_buffer();
  424. }
  425. else{
  426. netconn_write(conn, http_503_hdr, sizeof(http_503_hdr) - 1, NETCONN_NOCOPY);
  427. ESP_LOGE(TAG, "http_server_netconn_serve: GET /status failed to obtain mutex");
  428. }
  429. }
  430. else{
  431. netconn_write(conn, http_400_hdr, sizeof(http_400_hdr) - 1, NETCONN_NOCOPY);
  432. ESP_LOGE(TAG, "bad request");
  433. }
  434. }
  435. }
  436. else{
  437. ESP_LOGE(TAG, "URL Not found. Sending 404.");
  438. netconn_write(conn, http_404_hdr, sizeof(http_404_hdr) - 1, NETCONN_NOCOPY);
  439. }
  440. }
  441. /* free the buffer */
  442. netbuf_delete(inbuf);
  443. }
  444. void strreplace(char *src, char *str, char *rep)
  445. {
  446. char *p = strstr(src, str);
  447. if (p)
  448. {
  449. int len = strlen(src)+strlen(rep)-strlen(str);
  450. char r[len];
  451. memset(r, 0, len);
  452. if ( p >= src ){
  453. strncpy(r, src, p-src);
  454. r[p-src]='\0';
  455. strncat(r, rep, strlen(rep));
  456. strncat(r, p+strlen(str), p+strlen(str)-src+strlen(src));
  457. strcpy(src, r);
  458. strreplace(p+strlen(rep), str, rep);
  459. }
  460. }
  461. }