wifi_manager_http_server.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * Squeezelite for esp32
  3. *
  4. * (c) Sebastien 2019
  5. * Philippe G. 2019, philippe_44@outlook.com
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. #include "http_server_handlers.h"
  22. #include "esp_log.h"
  23. #include "esp_http_server.h"
  24. #include <inttypes.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include "esp_system.h"
  28. #include "freertos/FreeRTOS.h"
  29. #include "freertos/task.h"
  30. #include "config.h"
  31. static const char TAG[] = "http_server";
  32. static httpd_handle_t _server = NULL;
  33. rest_server_context_t *rest_context = NULL;
  34. void register_common_handlers(httpd_handle_t server){
  35. httpd_uri_t res_get = { .uri = "/res/*", .method = HTTP_GET, .handler = resource_filehandler, .user_ctx = rest_context };
  36. httpd_register_uri_handler(server, &res_get);
  37. }
  38. void register_regular_handlers(httpd_handle_t server){
  39. httpd_uri_t root_get = { .uri = "/", .method = HTTP_GET, .handler = root_get_handler, .user_ctx = rest_context };
  40. httpd_register_uri_handler(server, &root_get);
  41. httpd_uri_t ap_get = { .uri = "/ap.json", .method = HTTP_GET, .handler = ap_get_handler, .user_ctx = rest_context };
  42. httpd_register_uri_handler(server, &ap_get);
  43. httpd_uri_t scan_get = { .uri = "/scan.json", .method = HTTP_GET, .handler = ap_scan_handler, .user_ctx = rest_context };
  44. httpd_register_uri_handler(server, &scan_get);
  45. httpd_uri_t config_get = { .uri = "/config.json", .method = HTTP_GET, .handler = config_get_handler, .user_ctx = rest_context };
  46. httpd_register_uri_handler(server, &config_get);
  47. httpd_uri_t status_get = { .uri = "/status.json", .method = HTTP_GET, .handler = status_get_handler, .user_ctx = rest_context };
  48. httpd_register_uri_handler(server, &status_get);
  49. httpd_uri_t config_post = { .uri = "/config.json", .method = HTTP_POST, .handler = config_post_handler, .user_ctx = rest_context };
  50. httpd_register_uri_handler(server, &config_post);
  51. httpd_uri_t connect_post = { .uri = "/connect.json", .method = HTTP_POST, .handler = connect_post_handler, .user_ctx = rest_context };
  52. httpd_register_uri_handler(server, &connect_post);
  53. httpd_uri_t reboot_ota_post = { .uri = "/reboot_ota.json", .method = HTTP_POST, .handler = reboot_ota_post_handler, .user_ctx = rest_context };
  54. httpd_register_uri_handler(server, &reboot_ota_post);
  55. httpd_uri_t reboot_post = { .uri = "/reboot.json", .method = HTTP_POST, .handler = reboot_post_handler, .user_ctx = rest_context };
  56. httpd_register_uri_handler(server, &reboot_post);
  57. httpd_uri_t recovery_post = { .uri = "/recovery.json", .method = HTTP_POST, .handler = recovery_post_handler, .user_ctx = rest_context };
  58. httpd_register_uri_handler(server, &recovery_post);
  59. httpd_uri_t connect_delete = { .uri = "/connect.json", .method = HTTP_DELETE, .handler = connect_delete_handler, .user_ctx = rest_context };
  60. httpd_register_uri_handler(server, &connect_delete);
  61. // from https://github.com/tripflex/wifi-captive-portal/blob/master/src/mgos_wifi_captive_portal.c
  62. // https://unix.stackexchange.com/questions/432190/why-isnt-androids-captive-portal-detection-triggering-a-browser-window
  63. // Known HTTP GET requests to check for Captive Portal
  64. ///kindle-wifi/wifiredirect.html Kindle when requested with com.android.captiveportallogin
  65. ///kindle-wifi/wifistub.html Kindle before requesting with captive portal login window (maybe for detection?)
  66. httpd_uri_t connect_redirect_1 = { .uri = "/mobile/status.php", .method = HTTP_GET, .handler = redirect_200_ev_handler, .user_ctx = rest_context };// Android 8.0 (Samsung s9+)
  67. httpd_register_uri_handler(server, &connect_redirect_1);
  68. httpd_uri_t connect_redirect_2 = { .uri = "/generate_204", .method = HTTP_GET, .handler = redirect_200_ev_handler, .user_ctx = rest_context };// Android
  69. httpd_register_uri_handler(server, &connect_redirect_2);
  70. httpd_uri_t connect_redirect_3 = { .uri = "/gen_204", .method = HTTP_GET, .handler = redirect_ev_handler, .user_ctx = rest_context };// Android 9.0
  71. httpd_register_uri_handler(server, &connect_redirect_3);
  72. // httpd_uri_t connect_redirect_4 = { .uri = "/ncsi.txt", .method = HTTP_GET, .handler = redirect_ev_handler, .user_ctx = rest_context };// Windows
  73. // httpd_register_uri_handler(server, &connect_redirect_4);
  74. httpd_uri_t connect_redirect_5 = { .uri = "/hotspot-detect.html", .method = HTTP_GET, .handler = redirect_ev_handler, .user_ctx = rest_context }; // iOS 8/9
  75. httpd_register_uri_handler(server, &connect_redirect_5);
  76. httpd_uri_t connect_redirect_6 = { .uri = "/library/test/success.html", .method = HTTP_GET, .handler = redirect_ev_handler, .user_ctx = rest_context };// iOS 8/9
  77. httpd_register_uri_handler(server, &connect_redirect_6);
  78. httpd_uri_t connect_redirect_7 = { .uri = "/hotspotdetect.html", .method = HTTP_GET, .handler = redirect_ev_handler, .user_ctx = rest_context }; // iOS
  79. httpd_register_uri_handler(server, &connect_redirect_7);
  80. httpd_uri_t connect_redirect_8 = { .uri = "/success.txt", .method = HTTP_GET, .handler = redirect_ev_handler, .user_ctx = rest_context }; // OSX
  81. httpd_register_uri_handler(server, &connect_redirect_8);
  82. ESP_LOGD(TAG,"Registering default error handler for 404");
  83. httpd_register_err_handler(server, HTTPD_404_NOT_FOUND,&err_handler);
  84. }
  85. esp_err_t http_server_start()
  86. {
  87. ESP_LOGI(TAG, "Initializing HTTP Server");
  88. rest_context = calloc(1, sizeof(rest_server_context_t));
  89. if(rest_context==NULL){
  90. ESP_LOGE(TAG,"No memory for http context");
  91. return ESP_FAIL;
  92. }
  93. strlcpy(rest_context->base_path, "/res/", sizeof(rest_context->base_path));
  94. httpd_config_t config = HTTPD_DEFAULT_CONFIG();
  95. config.max_uri_handlers = 20;
  96. config.max_open_sockets = 5;
  97. config.uri_match_fn = httpd_uri_match_wildcard;
  98. //todo: use the endpoint below to configure session token?
  99. // config.open_fn
  100. ESP_LOGI(TAG, "Starting HTTP Server");
  101. esp_err_t err= httpd_start(&_server, &config);
  102. if(err != ESP_OK){
  103. ESP_LOGE_LOC(TAG,"Start server failed");
  104. }
  105. else {
  106. register_common_handlers(_server);
  107. register_regular_handlers(_server);
  108. }
  109. return err;
  110. }
  111. /* Function to free context */
  112. void adder_free_func(void *ctx)
  113. {
  114. ESP_LOGI(TAG, "/adder Free Context function called");
  115. free(ctx);
  116. }
  117. void stop_webserver(httpd_handle_t server)
  118. {
  119. // Stop the httpd server
  120. httpd_stop(server);
  121. }
  122. #if 0
  123. if(strstr(line, "GET / ")) {
  124. netconn_write(conn, http_html_hdr, sizeof(http_html_hdr) - 1, NETCONN_NOCOPY);
  125. netconn_write(conn, index_html_start, index_html_end- index_html_start, NETCONN_NOCOPY);
  126. }
  127. else if(strstr(line, "GET /code.js ")) {
  128. netconn_write(conn, http_js_hdr, sizeof(http_js_hdr) - 1, NETCONN_NOCOPY);
  129. netconn_write(conn, code_js_start, code_js_end - code_js_start, NETCONN_NOCOPY);
  130. }
  131. else if(strstr(line, "GET /style.css ")) {
  132. netconn_write(conn, http_css_hdr, sizeof(http_css_hdr) - 1, NETCONN_NOCOPY);
  133. netconn_write(conn, style_css_start, style_css_end - style_css_start, NETCONN_NOCOPY);
  134. }
  135. else if(strstr(line, "GET /jquery.js ")) {
  136. http_server_send_resource_file(conn,jquery_gz_start, jquery_gz_end, "text/javascript", "gzip" );
  137. }
  138. else if(strstr(line, "GET /popper.js ")) {
  139. http_server_send_resource_file(conn,popper_gz_start, popper_gz_end, "text/javascript", "gzip" );
  140. }
  141. else if(strstr(line, "GET /bootstrap.js ")) {
  142. http_server_send_resource_file(conn,bootstrap_js_gz_start, bootstrap_js_gz_end, "text/javascript", "gzip" );
  143. }
  144. else if(strstr(line, "GET /bootstrap.css ")) {
  145. http_server_send_resource_file(conn,bootstrap_css_gz_start, bootstrap_css_gz_end, "text/css", "gzip" );
  146. }
  147. //dynamic stuff
  148. else if(strstr(line, "GET /scan.json ")) {
  149. ESP_LOGI(TAG, "Starting wifi scan");
  150. wifi_manager_scan_async();
  151. }
  152. else if(strstr(line, "GET /ap.json ")) {
  153. /* if we can get the mutex, write the last version of the AP list */
  154. ESP_LOGI(TAG, "Processing ap.json request");
  155. if(wifi_manager_lock_json_buffer(( TickType_t ) 10)) {
  156. netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY);
  157. char *buff = wifi_manager_alloc_get_ap_list_json();
  158. wifi_manager_unlock_json_buffer();
  159. if(buff!=NULL){
  160. netconn_write(conn, buff, strlen(buff), NETCONN_NOCOPY);
  161. free(buff);
  162. }
  163. else {
  164. ESP_LOGD(TAG, "Error retrieving ap list json string. ");
  165. netconn_write(conn, http_503_hdr, sizeof(http_503_hdr) - 1, NETCONN_NOCOPY);
  166. }
  167. }
  168. else {
  169. netconn_write(conn, http_503_hdr, sizeof(http_503_hdr) - 1, NETCONN_NOCOPY);
  170. ESP_LOGE(TAG, "http_server_netconn_serve: GET /ap.json failed to obtain mutex");
  171. }
  172. /* request a wifi scan */
  173. ESP_LOGI(TAG, "Starting wifi scan");
  174. wifi_manager_scan_async();
  175. ESP_LOGI(TAG, "Done serving ap.json");
  176. }
  177. else if(strstr(line, "GET /config.json ")) {
  178. ESP_LOGI(TAG, "Serving config.json");
  179. ESP_LOGI(TAG, "About to get config from flash");
  180. http_server_send_config_json(conn);
  181. ESP_LOGD(TAG, "Done serving config.json");
  182. }
  183. else if(strstr(line, "POST /config.json ")) {
  184. ESP_LOGI(TAG, "Serving POST config.json");
  185. int lenA=0;
  186. char * last_parm=save_ptr;
  187. char * next_parm=save_ptr;
  188. char * last_parm_name=NULL;
  189. bool bErrorFound=false;
  190. bool bOTA=false;
  191. char * otaURL=NULL;
  192. // todo: implement json body parsing
  193. //http_server_process_config(conn,save_ptr);
  194. while(last_parm!=NULL) {
  195. // Search will return
  196. ESP_LOGD(TAG, "Getting parameters from X-Custom headers");
  197. last_parm = http_server_search_header(next_parm, "X-Custom-", &lenA, &last_parm_name,&next_parm,buf+buflen);
  198. if(last_parm!=NULL && last_parm_name!=NULL) {
  199. ESP_LOGI(TAG, "http_server_netconn_serve: POST config.json, config %s=%s", last_parm_name, last_parm);
  200. if(strcmp(last_parm_name, "fwurl")==0) {
  201. // we're getting a request to do an OTA from that URL
  202. ESP_LOGW(TAG, "Found OTA request!");
  203. otaURL=strdup(last_parm);
  204. bOTA=true;
  205. }
  206. else {
  207. ESP_LOGV(TAG, "http_server_netconn_serve: POST config.json Storing parameter");
  208. if(config_set_value(NVS_TYPE_STR, last_parm_name , last_parm) != ESP_OK){
  209. ESP_LOGE(TAG, "Unable to save nvs value.");
  210. }
  211. }
  212. }
  213. if(last_parm_name!=NULL) {
  214. free(last_parm_name);
  215. last_parm_name=NULL;
  216. }
  217. }
  218. if(bErrorFound) {
  219. netconn_write(conn, http_400_hdr, sizeof(http_400_hdr) - 1, NETCONN_NOCOPY); //400 invalid request
  220. }
  221. else {
  222. netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY); //200ok
  223. if(bOTA) {
  224. #if RECOVERY_APPLICATION
  225. ESP_LOGW(TAG, "Starting process OTA for url %s",otaURL);
  226. #else
  227. ESP_LOGW(TAG, "Restarting system to process OTA for url %s",otaURL);
  228. #endif
  229. wifi_manager_reboot_ota(otaURL);
  230. free(otaURL);
  231. }
  232. }
  233. ESP_LOGI(TAG, "Done Serving POST config.json");
  234. }
  235. else if(strstr(line, "POST /connect.json ")) {
  236. ESP_LOGI(TAG, "http_server_netconn_serve: POST /connect.json");
  237. bool found = false;
  238. int lenS = 0, lenP = 0, lenN = 0;
  239. char *ssid = NULL, *password = NULL;
  240. ssid = http_server_get_header(save_ptr, "X-Custom-ssid: ", &lenS);
  241. password = http_server_get_header(save_ptr, "X-Custom-pwd: ", &lenP);
  242. char * new_host_name_b = http_server_get_header(save_ptr, "X-Custom-host_name: ", &lenN);
  243. if(lenN > 0){
  244. lenN++;
  245. char * new_host_name = malloc(lenN);
  246. strlcpy(new_host_name, new_host_name_b, lenN);
  247. if(config_set_value(NVS_TYPE_STR, "host_name", new_host_name) != ESP_OK){
  248. ESP_LOGE(TAG, "Unable to save host name configuration");
  249. }
  250. free(new_host_name);
  251. }
  252. if(ssid && lenS <= MAX_SSID_SIZE && password && lenP <= MAX_PASSWORD_SIZE) {
  253. wifi_config_t* config = wifi_manager_get_wifi_sta_config();
  254. memset(config, 0x00, sizeof(wifi_config_t));
  255. memcpy(config->sta.ssid, ssid, lenS);
  256. memcpy(config->sta.password, password, lenP);
  257. ESP_LOGD(TAG, "http_server_netconn_serve: wifi_manager_connect_async() call, with ssid: %s, password: %s", config->sta.ssid, config->sta.password);
  258. wifi_manager_connect_async();
  259. netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY); //200ok
  260. found = true;
  261. }
  262. else{
  263. ESP_LOGE(TAG, "SSID or Password invalid");
  264. }
  265. if(!found) {
  266. /* bad request the authentification header is not complete/not the correct format */
  267. netconn_write(conn, http_400_hdr, sizeof(http_400_hdr) - 1, NETCONN_NOCOPY);
  268. ESP_LOGE(TAG, "bad request the authentification header is not complete/not the correct format");
  269. }
  270. ESP_LOGI(TAG, "http_server_netconn_serve: done serving connect.json");
  271. }
  272. else if(strstr(line, "DELETE /connect.json ")) {
  273. ESP_LOGI(TAG, "http_server_netconn_serve: DELETE /connect.json");
  274. /* request a disconnection from wifi and forget about it */
  275. wifi_manager_disconnect_async();
  276. netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY); /* 200 ok */
  277. ESP_LOGI(TAG, "http_server_netconn_serve: done serving DELETE /connect.json");
  278. }
  279. else if(strstr(line, "POST /reboot_ota.json ")) {
  280. ESP_LOGI(TAG, "http_server_netconn_serve: POST reboot_ota.json");
  281. netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY); /* 200 ok */
  282. wifi_manager_reboot(OTA);
  283. ESP_LOGI(TAG, "http_server_netconn_serve: done serving POST reboot_ota.json");
  284. }
  285. else if(strstr(line, "POST /reboot.json ")) {
  286. ESP_LOGI(TAG, "http_server_netconn_serve: POST reboot.json");
  287. netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY); /* 200 ok */
  288. wifi_manager_reboot(RESTART);
  289. ESP_LOGI(TAG, "http_server_netconn_serve: done serving POST reboot.json");
  290. }
  291. else if(strstr(line, "POST /recovery.json ")) {
  292. ESP_LOGI(TAG, "http_server_netconn_serve: POST recovery.json");
  293. netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY); /* 200 ok */
  294. wifi_manager_reboot(RECOVERY);
  295. ESP_LOGI(TAG, "http_server_netconn_serve: done serving POST recovery.json");
  296. }
  297. else if(strstr(line, "GET /status.json ")) {
  298. ESP_LOGI(TAG, "Serving status.json");
  299. if(wifi_manager_lock_json_buffer(( TickType_t ) 10)) {
  300. char *buff = wifi_manager_alloc_get_ip_info_json();
  301. wifi_manager_unlock_json_buffer();
  302. if(buff) {
  303. netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY);
  304. netconn_write(conn, buff, strlen(buff), NETCONN_NOCOPY);
  305. free(buff);
  306. }
  307. else {
  308. netconn_write(conn, http_503_hdr, sizeof(http_503_hdr) - 1, NETCONN_NOCOPY);
  309. }
  310. }
  311. else {
  312. netconn_write(conn, http_503_hdr, sizeof(http_503_hdr) - 1, NETCONN_NOCOPY);
  313. ESP_LOGE(TAG, "http_server_netconn_serve: GET /status failed to obtain mutex");
  314. }
  315. ESP_LOGI(TAG, "Done Serving status.json");
  316. }
  317. else {
  318. netconn_write(conn, http_400_hdr, sizeof(http_400_hdr) - 1, NETCONN_NOCOPY);
  319. ESP_LOGE(TAG, "bad request from host: %s, request %s",remote_address, line);
  320. }
  321. }
  322. }
  323. else {
  324. ESP_LOGE(TAG, "URL not found processing for remote host : %s",remote_address);
  325. netconn_write(conn, http_404_hdr, sizeof(http_404_hdr) - 1, NETCONN_NOCOPY);
  326. }
  327. free(host);
  328. }
  329. #endif