http_server.c 19 KB

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