wifi_manager_http_server.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #ifdef NETWORK_HTTP_SERVER_LOG_LEVEL
  2. #define LOG_LOCAL_LEVEL NETWORK_HTTP_SERVER_LOG_LEVEL
  3. #endif
  4. /*
  5. * Squeezelite for esp32
  6. *
  7. * (c) Sebastien 2019
  8. * Philippe G. 2019, philippe_44@outlook.com
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. #include "http_server_handlers.h"
  25. #include "esp_log.h"
  26. #include "esp_http_server.h"
  27. #include <inttypes.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include "esp_system.h"
  31. #include "freertos/FreeRTOS.h"
  32. #include "freertos/task.h"
  33. #include "messaging.h"
  34. #include "platform_esp32.h"
  35. #include "trace.h"
  36. #include "tools.h"
  37. static const char TAG[] = "http_server";
  38. EXT_RAM_ATTR static httpd_handle_t _server;
  39. EXT_RAM_ATTR static int _port;
  40. EXT_RAM_ATTR rest_server_context_t *rest_context;
  41. EXT_RAM_ATTR RingbufHandle_t messaging;
  42. httpd_handle_t http_get_server(int *port) {
  43. if (port) *port = _port;
  44. return _server;
  45. }
  46. void register_common_handlers(httpd_handle_t server){
  47. httpd_uri_t css_get = { .uri = "/css/*", .method = HTTP_GET, .handler = resource_filehandler, .user_ctx = rest_context };
  48. httpd_register_uri_handler(server, &css_get);
  49. httpd_uri_t js_get = { .uri = "/js/*", .method = HTTP_GET, .handler = resource_filehandler, .user_ctx = rest_context };
  50. httpd_register_uri_handler(server, &js_get);
  51. httpd_uri_t icon_get = { .uri = "/icons*", .method = HTTP_GET, .handler = resource_filehandler, .user_ctx = rest_context };
  52. httpd_register_uri_handler(server, &icon_get);
  53. httpd_uri_t png_get = { .uri = "/favicon*", .method = HTTP_GET, .handler = resource_filehandler, .user_ctx = rest_context };
  54. httpd_register_uri_handler(server, &png_get);
  55. }
  56. void register_regular_handlers(httpd_handle_t server){
  57. httpd_uri_t root_get = { .uri = "/", .method = HTTP_GET, .handler = root_get_handler, .user_ctx = rest_context };
  58. httpd_register_uri_handler(server, &root_get);
  59. httpd_uri_t scan_get = { .uri = "/scan.json", .method = HTTP_GET, .handler = ap_scan_handler, .user_ctx = rest_context };
  60. httpd_register_uri_handler(server, &scan_get);
  61. httpd_uri_t config_get = { .uri = "/config.json", .method = HTTP_GET, .handler = config_get_handler, .user_ctx = rest_context };
  62. httpd_register_uri_handler(server, &config_get);
  63. httpd_uri_t status_get = { .uri = "/status.json", .method = HTTP_GET, .handler = status_get_handler, .user_ctx = rest_context };
  64. httpd_register_uri_handler(server, &status_get);
  65. httpd_uri_t messages_get = { .uri = "/messages.json", .method = HTTP_GET, .handler = messages_get_handler, .user_ctx = rest_context };
  66. httpd_register_uri_handler(server, &messages_get);
  67. httpd_uri_t commands_get = { .uri = "/commands.json", .method = HTTP_GET, .handler = console_cmd_get_handler, .user_ctx = rest_context };
  68. httpd_register_uri_handler(server, &commands_get);
  69. httpd_uri_t commands_post = { .uri = "/commands.json", .method = HTTP_POST, .handler = console_cmd_post_handler, .user_ctx = rest_context };
  70. httpd_register_uri_handler(server, &commands_post);
  71. httpd_uri_t config_post = { .uri = "/config.json", .method = HTTP_POST, .handler = config_post_handler, .user_ctx = rest_context };
  72. httpd_register_uri_handler(server, &config_post);
  73. httpd_uri_t connect_post = { .uri = "/connect.json", .method = HTTP_POST, .handler = connect_post_handler, .user_ctx = rest_context };
  74. httpd_register_uri_handler(server, &connect_post);
  75. httpd_uri_t reboot_ota_post = { .uri = "/reboot_ota.json", .method = HTTP_POST, .handler = reboot_ota_post_handler, .user_ctx = rest_context };
  76. httpd_register_uri_handler(server, &reboot_ota_post);
  77. httpd_uri_t reboot_post = { .uri = "/reboot.json", .method = HTTP_POST, .handler = reboot_post_handler, .user_ctx = rest_context };
  78. httpd_register_uri_handler(server, &reboot_post);
  79. httpd_uri_t recovery_post = { .uri = "/recovery.json", .method = HTTP_POST, .handler = recovery_post_handler, .user_ctx = rest_context };
  80. httpd_register_uri_handler(server, &recovery_post);
  81. httpd_uri_t connect_delete = { .uri = "/connect.json", .method = HTTP_DELETE, .handler = connect_delete_handler, .user_ctx = rest_context };
  82. httpd_register_uri_handler(server, &connect_delete);
  83. if(is_recovery_running){
  84. httpd_uri_t flash_post = { .uri = "/flash.json", .method = HTTP_POST, .handler = flash_post_handler, .user_ctx = rest_context };
  85. httpd_register_uri_handler(server, &flash_post);
  86. }
  87. // from https://github.com/tripflex/wifi-captive-portal/blob/master/src/mgos_wifi_captive_portal.c
  88. // https://unix.stackexchange.com/questions/432190/why-isnt-androids-captive-portal-detection-triggering-a-browser-window
  89. // Known HTTP GET requests to check for Captive Portal
  90. ///kindle-wifi/wifiredirect.html Kindle when requested with com.android.captiveportallogin
  91. ///kindle-wifi/wifistub.html Kindle before requesting with captive portal login window (maybe for detection?)
  92. 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+)
  93. httpd_register_uri_handler(server, &connect_redirect_1);
  94. httpd_uri_t connect_redirect_2 = { .uri = "/generate_204", .method = HTTP_GET, .handler = redirect_200_ev_handler, .user_ctx = rest_context };// Android
  95. httpd_register_uri_handler(server, &connect_redirect_2);
  96. httpd_uri_t connect_redirect_3 = { .uri = "/gen_204", .method = HTTP_GET, .handler = redirect_ev_handler, .user_ctx = rest_context };// Android 9.0
  97. httpd_register_uri_handler(server, &connect_redirect_3);
  98. // httpd_uri_t connect_redirect_4 = { .uri = "/ncsi.txt", .method = HTTP_GET, .handler = redirect_ev_handler, .user_ctx = rest_context };// Windows
  99. // httpd_register_uri_handler(server, &connect_redirect_4);
  100. httpd_uri_t connect_redirect_5 = { .uri = "/hotspot-detect.html", .method = HTTP_GET, .handler = redirect_ev_handler, .user_ctx = rest_context }; // iOS 8/9
  101. httpd_register_uri_handler(server, &connect_redirect_5);
  102. 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
  103. httpd_register_uri_handler(server, &connect_redirect_6);
  104. httpd_uri_t connect_redirect_7 = { .uri = "/hotspotdetect.html", .method = HTTP_GET, .handler = redirect_ev_handler, .user_ctx = rest_context }; // iOS
  105. httpd_register_uri_handler(server, &connect_redirect_7);
  106. httpd_uri_t connect_redirect_8 = { .uri = "/success.txt", .method = HTTP_GET, .handler = redirect_ev_handler, .user_ctx = rest_context }; // OSX
  107. httpd_register_uri_handler(server, &connect_redirect_8);
  108. httpd_uri_t configurator_post = { .uri = "/config.pro", .method = HTTP_POST, .handler = configurator_post_handler, .user_ctx = rest_context };
  109. httpd_register_uri_handler(server, &configurator_post);
  110. httpd_uri_t configurator_get = { .uri = "/config.pro", .method = HTTP_POST, .handler = configurator_get_handler, .user_ctx = rest_context };
  111. httpd_register_uri_handler(server, &configurator_get);
  112. ESP_LOGD(TAG,"Registering default error handler for 404");
  113. httpd_register_err_handler(server, HTTPD_404_NOT_FOUND,&err_handler);
  114. }
  115. esp_err_t http_server_start()
  116. {
  117. ESP_LOGI(TAG, "Initializing HTTP Server");
  118. MEMTRACE_PRINT_DELTA_MESSAGE("Registering messaging subscriber");
  119. messaging = messaging_register_subscriber(10, "http_server");
  120. MEMTRACE_PRINT_DELTA_MESSAGE("Allocating RAM for server context");
  121. rest_context = malloc_init_external( sizeof(rest_server_context_t));
  122. if(rest_context==NULL){
  123. ESP_LOGE(TAG,"No memory for http context");
  124. return ESP_FAIL;
  125. }
  126. strlcpy(rest_context->base_path, "/res/", sizeof(rest_context->base_path));
  127. httpd_config_t config = HTTPD_DEFAULT_CONFIG();
  128. config.max_uri_handlers = 30;
  129. config.max_open_sockets = 3;
  130. config.lru_purge_enable = true;
  131. config.backlog_conn = 1;
  132. config.uri_match_fn = httpd_uri_match_wildcard;
  133. config.task_priority = ESP_TASK_PRIO_MIN;
  134. _port = config.server_port;
  135. //todo: use the endpoint below to configure session token?
  136. // config.open_fn
  137. MEMTRACE_PRINT_DELTA_MESSAGE( "Starting HTTP Server");
  138. esp_err_t err= httpd_start(&_server, &config);
  139. if(err != ESP_OK){
  140. ESP_LOGE_LOC(TAG,"Start server failed");
  141. }
  142. else {
  143. MEMTRACE_PRINT_DELTA_MESSAGE( "HTTP Server started. Registering common handlers");
  144. register_common_handlers(_server);
  145. MEMTRACE_PRINT_DELTA_MESSAGE("Registering regular handlers");
  146. register_regular_handlers(_server);
  147. MEMTRACE_PRINT_DELTA_MESSAGE("HTTP Server regular handlers registered");
  148. }
  149. return err;
  150. }
  151. /* Function to free context */
  152. void adder_free_func(void *ctx)
  153. {
  154. ESP_LOGD(TAG, "/adder Free Context function called");
  155. free(ctx);
  156. }
  157. void stop_webserver(httpd_handle_t server)
  158. {
  159. // Stop the httpd server
  160. httpd_stop(server);
  161. }