123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- #ifdef NETWORK_HTTP_SERVER_LOG_LEVEL
- #define LOG_LOCAL_LEVEL NETWORK_HTTP_SERVER_LOG_LEVEL
- #endif
- #include "http_server_handlers.h"
- #include "esp_log.h"
- #include "esp_http_server.h"
- #include <inttypes.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include "esp_system.h"
- #include "freertos/FreeRTOS.h"
- #include "freertos/task.h"
- #include "messaging.h"
- #include "platform_esp32.h"
- #include "trace.h"
- #include "tools.h"
- static const char TAG[] = "http_server";
- EXT_RAM_ATTR static httpd_handle_t _server;
- EXT_RAM_ATTR static int _port;
- EXT_RAM_ATTR rest_server_context_t *rest_context;
- EXT_RAM_ATTR RingbufHandle_t messaging;
- httpd_handle_t http_get_server(int *port) {
- if (port) *port = _port;
- return _server;
- }
- void register_common_handlers(httpd_handle_t server){
- httpd_uri_t css_get = { .uri = "/css/*", .method = HTTP_GET, .handler = resource_filehandler, .user_ctx = rest_context };
- httpd_register_uri_handler(server, &css_get);
- httpd_uri_t js_get = { .uri = "/js/*", .method = HTTP_GET, .handler = resource_filehandler, .user_ctx = rest_context };
- httpd_register_uri_handler(server, &js_get);
- httpd_uri_t icon_get = { .uri = "/icons*", .method = HTTP_GET, .handler = resource_filehandler, .user_ctx = rest_context };
- httpd_register_uri_handler(server, &icon_get);
- httpd_uri_t png_get = { .uri = "/favicon*", .method = HTTP_GET, .handler = resource_filehandler, .user_ctx = rest_context };
- httpd_register_uri_handler(server, &png_get);
- }
- void register_regular_handlers(httpd_handle_t server){
- httpd_uri_t root_get = { .uri = "/", .method = HTTP_GET, .handler = root_get_handler, .user_ctx = rest_context };
- httpd_register_uri_handler(server, &root_get);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- httpd_uri_t connect_redirect_1 = { .uri = "/mobile/status.php", .method = HTTP_GET, .handler = redirect_200_ev_handler, .user_ctx = rest_context };
- httpd_register_uri_handler(server, &connect_redirect_1);
- httpd_uri_t connect_redirect_2 = { .uri = "/generate_204", .method = HTTP_GET, .handler = redirect_200_ev_handler, .user_ctx = rest_context };
- httpd_register_uri_handler(server, &connect_redirect_2);
- httpd_uri_t connect_redirect_3 = { .uri = "/gen_204", .method = HTTP_GET, .handler = redirect_ev_handler, .user_ctx = rest_context };
- httpd_register_uri_handler(server, &connect_redirect_3);
- httpd_uri_t connect_redirect_5 = { .uri = "/hotspot-detect.html", .method = HTTP_GET, .handler = redirect_ev_handler, .user_ctx = rest_context };
- httpd_register_uri_handler(server, &connect_redirect_5);
- httpd_uri_t connect_redirect_6 = { .uri = "/library/test/success.html", .method = HTTP_GET, .handler = redirect_ev_handler, .user_ctx = rest_context };
- httpd_register_uri_handler(server, &connect_redirect_6);
- httpd_uri_t connect_redirect_7 = { .uri = "/hotspotdetect.html", .method = HTTP_GET, .handler = redirect_ev_handler, .user_ctx = rest_context };
- httpd_register_uri_handler(server, &connect_redirect_7);
- httpd_uri_t connect_redirect_8 = { .uri = "/success.txt", .method = HTTP_GET, .handler = redirect_ev_handler, .user_ctx = rest_context };
- httpd_register_uri_handler(server, &connect_redirect_8);
- httpd_uri_t config_post = { .uri = "/data.bin", .method = HTTP_POST, .handler = data_post_handler, .user_ctx = rest_context };
- httpd_register_uri_handler(server, &config_post);
-
- ESP_LOGD(TAG,"Registering default error handler for 404");
- httpd_register_err_handler(server, HTTPD_404_NOT_FOUND,&err_handler);
- }
- esp_err_t http_server_start()
- {
-
- ESP_LOGI(TAG, "Initializing HTTP Server");
- messaging = messaging_register_subscriber(10, "http_server");
- rest_context = malloc_init_external( sizeof(rest_server_context_t));
- if(rest_context==NULL){
- ESP_LOGE(TAG,"No memory for http context");
- return ESP_FAIL;
- }
- strlcpy(rest_context->base_path, "/res/", sizeof(rest_context->base_path));
- httpd_config_t config = HTTPD_DEFAULT_CONFIG();
- config.max_uri_handlers = 30;
- config.max_open_sockets = 3;
- config.lru_purge_enable = false;
- config.backlog_conn = 1;
- config.uri_match_fn = httpd_uri_match_wildcard;
- config.task_priority = ESP_TASK_PRIO_MIN;
- _port = config.server_port;
-
-
- esp_err_t err= httpd_start(&_server, &config);
- if(err != ESP_OK){
- ESP_LOGE_LOC(TAG,"Start server failed");
- }
- else {
- register_common_handlers(_server);
- register_regular_handlers(_server);
- }
- return err;
- }
- void adder_free_func(void *ctx)
- {
- ESP_LOGD(TAG, "/adder Free Context function called");
- free(ctx);
- }
- void stop_webserver(httpd_handle_t server)
- {
-
- httpd_stop(server);
- }
|