wifi_manager_http_server.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 "_esp_http_server.h"
  25. #include <inttypes.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include "esp_system.h"
  29. #include "freertos/FreeRTOS.h"
  30. #include "freertos/task.h"
  31. #include "messaging.h"
  32. #include "platform_esp32.h"
  33. static const char TAG[] = "http_server";
  34. static httpd_handle_t _server = NULL;
  35. rest_server_context_t *rest_context = NULL;
  36. RingbufHandle_t messaging=NULL;
  37. void register_common_handlers(httpd_handle_t server){
  38. httpd_uri_t res_get = { .uri = "/res/*", .method = HTTP_GET, .handler = resource_filehandler, .user_ctx = rest_context };
  39. httpd_register_uri_handler(server, &res_get);
  40. }
  41. void register_regular_handlers(httpd_handle_t server){
  42. httpd_uri_t root_get = { .uri = "/", .method = HTTP_GET, .handler = root_get_handler, .user_ctx = rest_context };
  43. httpd_register_uri_handler(server, &root_get);
  44. httpd_uri_t ap_get = { .uri = "/ap.json", .method = HTTP_GET, .handler = ap_get_handler, .user_ctx = rest_context };
  45. httpd_register_uri_handler(server, &ap_get);
  46. httpd_uri_t scan_get = { .uri = "/scan.json", .method = HTTP_GET, .handler = ap_scan_handler, .user_ctx = rest_context };
  47. httpd_register_uri_handler(server, &scan_get);
  48. httpd_uri_t config_get = { .uri = "/config.json", .method = HTTP_GET, .handler = config_get_handler, .user_ctx = rest_context };
  49. httpd_register_uri_handler(server, &config_get);
  50. httpd_uri_t status_get = { .uri = "/status.json", .method = HTTP_GET, .handler = status_get_handler, .user_ctx = rest_context };
  51. httpd_register_uri_handler(server, &status_get);
  52. httpd_uri_t messages_get = { .uri = "/messages.json", .method = HTTP_GET, .handler = messages_get_handler, .user_ctx = rest_context };
  53. httpd_register_uri_handler(server, &messages_get);
  54. httpd_uri_t commands_get = { .uri = "/commands.json", .method = HTTP_GET, .handler = console_cmd_get_handler, .user_ctx = rest_context };
  55. httpd_register_uri_handler(server, &commands_get);
  56. httpd_uri_t commands_post = { .uri = "/commands.json", .method = HTTP_POST, .handler = console_cmd_post_handler, .user_ctx = rest_context };
  57. httpd_register_uri_handler(server, &commands_post);
  58. httpd_uri_t config_post = { .uri = "/config.json", .method = HTTP_POST, .handler = config_post_handler, .user_ctx = rest_context };
  59. httpd_register_uri_handler(server, &config_post);
  60. httpd_uri_t connect_post = { .uri = "/connect.json", .method = HTTP_POST, .handler = connect_post_handler, .user_ctx = rest_context };
  61. httpd_register_uri_handler(server, &connect_post);
  62. httpd_uri_t reboot_ota_post = { .uri = "/reboot_ota.json", .method = HTTP_POST, .handler = reboot_ota_post_handler, .user_ctx = rest_context };
  63. httpd_register_uri_handler(server, &reboot_ota_post);
  64. httpd_uri_t reboot_post = { .uri = "/reboot.json", .method = HTTP_POST, .handler = reboot_post_handler, .user_ctx = rest_context };
  65. httpd_register_uri_handler(server, &reboot_post);
  66. httpd_uri_t recovery_post = { .uri = "/recovery.json", .method = HTTP_POST, .handler = recovery_post_handler, .user_ctx = rest_context };
  67. httpd_register_uri_handler(server, &recovery_post);
  68. httpd_uri_t connect_delete = { .uri = "/connect.json", .method = HTTP_DELETE, .handler = connect_delete_handler, .user_ctx = rest_context };
  69. httpd_register_uri_handler(server, &connect_delete);
  70. if(is_recovery_running){
  71. httpd_uri_t flash_post = { .uri = "/flash.json", .method = HTTP_POST, .handler = flash_post_handler, .user_ctx = rest_context };
  72. httpd_register_uri_handler(server, &flash_post);
  73. }
  74. // from https://github.com/tripflex/wifi-captive-portal/blob/master/src/mgos_wifi_captive_portal.c
  75. // https://unix.stackexchange.com/questions/432190/why-isnt-androids-captive-portal-detection-triggering-a-browser-window
  76. // Known HTTP GET requests to check for Captive Portal
  77. ///kindle-wifi/wifiredirect.html Kindle when requested with com.android.captiveportallogin
  78. ///kindle-wifi/wifistub.html Kindle before requesting with captive portal login window (maybe for detection?)
  79. 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+)
  80. httpd_register_uri_handler(server, &connect_redirect_1);
  81. httpd_uri_t connect_redirect_2 = { .uri = "/generate_204", .method = HTTP_GET, .handler = redirect_200_ev_handler, .user_ctx = rest_context };// Android
  82. httpd_register_uri_handler(server, &connect_redirect_2);
  83. httpd_uri_t connect_redirect_3 = { .uri = "/gen_204", .method = HTTP_GET, .handler = redirect_ev_handler, .user_ctx = rest_context };// Android 9.0
  84. httpd_register_uri_handler(server, &connect_redirect_3);
  85. // httpd_uri_t connect_redirect_4 = { .uri = "/ncsi.txt", .method = HTTP_GET, .handler = redirect_ev_handler, .user_ctx = rest_context };// Windows
  86. // httpd_register_uri_handler(server, &connect_redirect_4);
  87. httpd_uri_t connect_redirect_5 = { .uri = "/hotspot-detect.html", .method = HTTP_GET, .handler = redirect_ev_handler, .user_ctx = rest_context }; // iOS 8/9
  88. httpd_register_uri_handler(server, &connect_redirect_5);
  89. 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
  90. httpd_register_uri_handler(server, &connect_redirect_6);
  91. httpd_uri_t connect_redirect_7 = { .uri = "/hotspotdetect.html", .method = HTTP_GET, .handler = redirect_ev_handler, .user_ctx = rest_context }; // iOS
  92. httpd_register_uri_handler(server, &connect_redirect_7);
  93. httpd_uri_t connect_redirect_8 = { .uri = "/success.txt", .method = HTTP_GET, .handler = redirect_ev_handler, .user_ctx = rest_context }; // OSX
  94. httpd_register_uri_handler(server, &connect_redirect_8);
  95. ESP_LOGD(TAG,"Registering default error handler for 404");
  96. httpd_register_err_handler(server, HTTPD_404_NOT_FOUND,&err_handler);
  97. }
  98. esp_err_t http_server_start()
  99. {
  100. ESP_LOGI(TAG, "Initializing HTTP Server");
  101. messaging = messaging_register_subscriber(10, "http_server");
  102. rest_context = calloc(1, sizeof(rest_server_context_t));
  103. if(rest_context==NULL){
  104. ESP_LOGE(TAG,"No memory for http context");
  105. return ESP_FAIL;
  106. }
  107. strlcpy(rest_context->base_path, "/res/", sizeof(rest_context->base_path));
  108. httpd_config_t config = HTTPD_DEFAULT_CONFIG();
  109. config.max_uri_handlers = 25;
  110. config.max_open_sockets = 8;
  111. config.uri_match_fn = httpd_uri_match_wildcard;
  112. //todo: use the endpoint below to configure session token?
  113. // config.open_fn
  114. ESP_LOGD(TAG, "Starting HTTP Server");
  115. esp_err_t err= __httpd_start(&_server, &config);
  116. if(err != ESP_OK){
  117. ESP_LOGE_LOC(TAG,"Start server failed");
  118. }
  119. else {
  120. register_common_handlers(_server);
  121. register_regular_handlers(_server);
  122. }
  123. return err;
  124. }
  125. /* Function to free context */
  126. void adder_free_func(void *ctx)
  127. {
  128. ESP_LOGD(TAG, "/adder Free Context function called");
  129. free(ctx);
  130. }
  131. void stop_webserver(httpd_handle_t server)
  132. {
  133. // Stop the httpd server
  134. httpd_stop(server);
  135. }