http_server_handlers.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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.h
  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. #ifndef HTTP_SERVER_H_INCLUDED
  28. #define HTTP_SERVER_H_INCLUDED
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <stdlib.h>
  32. #include <stdbool.h>
  33. #include "esp_http_server.h"
  34. #include "wifi_manager.h"
  35. #include "freertos/FreeRTOS.h"
  36. #include "freertos/task.h"
  37. #include "freertos/event_groups.h"
  38. #include "esp_wifi.h"
  39. #include <esp_event.h>
  40. #include "nvs_flash.h"
  41. #include "esp_log.h"
  42. #include "driver/gpio.h"
  43. #include "mdns.h"
  44. #include "lwip/api.h"
  45. #include "lwip/err.h"
  46. #include "lwip/netdb.h"
  47. #include "lwip/opt.h"
  48. #include "lwip/memp.h"
  49. #include "lwip/ip.h"
  50. #include "lwip/raw.h"
  51. #include "lwip/udp.h"
  52. #include "lwip/priv/api_msg.h"
  53. #include "lwip/priv/tcp_priv.h"
  54. #include "lwip/priv/tcpip_priv.h"
  55. #include "esp_vfs.h"
  56. #include "esp_console.h"
  57. #ifdef __cplusplus
  58. extern "C" {
  59. #endif
  60. #define ESP_LOGE_LOC(t,str, ...) ESP_LOGE(t, "%s(%d): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__)
  61. #define ESP_LOGI_LOC(t,str, ...) ESP_LOGI(t, "%s(%d): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__)
  62. #define ESP_LOGD_LOC(t,str, ...) ESP_LOGD(t, "%s(%d): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__)
  63. #define ESP_LOGW_LOC(t,str, ...) ESP_LOGW(t, "%s(%d): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__)
  64. #define ESP_LOGV_LOC(t,str, ...) ESP_LOGV(t, "%s(%d): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__)
  65. esp_err_t root_get_handler(httpd_req_t *req);
  66. esp_err_t resource_filehandler(httpd_req_t *req);
  67. esp_err_t resource_filehandler(httpd_req_t *req);
  68. esp_err_t resource_filehandler(httpd_req_t *req);
  69. esp_err_t resource_filehandler(httpd_req_t *req);
  70. esp_err_t resource_filehandler(httpd_req_t *req);
  71. esp_err_t resource_filehandler(httpd_req_t *req);
  72. esp_err_t ap_get_handler(httpd_req_t *req);
  73. esp_err_t config_get_handler(httpd_req_t *req);
  74. esp_err_t config_post_handler(httpd_req_t *req);
  75. esp_err_t connect_post_handler(httpd_req_t *req);
  76. esp_err_t connect_delete_handler(httpd_req_t *req);
  77. esp_err_t reboot_ota_post_handler(httpd_req_t *req);
  78. esp_err_t reboot_post_handler(httpd_req_t *req);
  79. esp_err_t recovery_post_handler(httpd_req_t *req);
  80. esp_err_t flash_post_handler(httpd_req_t *req);
  81. esp_err_t status_get_handler(httpd_req_t *req);
  82. esp_err_t messages_get_handler(httpd_req_t *req);
  83. esp_err_t console_cmd_get_handler(httpd_req_t *req);
  84. esp_err_t console_cmd_post_handler(httpd_req_t *req);
  85. esp_err_t ap_scan_handler(httpd_req_t *req);
  86. esp_err_t redirect_ev_handler(httpd_req_t *req);
  87. esp_err_t redirect_200_ev_handler(httpd_req_t *req);
  88. esp_err_t err_handler(httpd_req_t *req, httpd_err_code_t error);
  89. #define SCRATCH_BUFSIZE (10240)
  90. #define FILE_PATH_MAX (ESP_VFS_PATH_MAX + 128)
  91. typedef struct rest_server_context {
  92. char base_path[ESP_VFS_PATH_MAX + 1];
  93. char scratch[SCRATCH_BUFSIZE];
  94. } rest_server_context_t;
  95. /**
  96. * @brief RTOS task for the HTTP server. Do not start manually.
  97. * @see void http_server_start()
  98. */
  99. void http_server(void *pvParameters);
  100. /* @brief helper function that processes one HTTP request at a time */
  101. void http_server_netconn_serve(struct netconn *conn);
  102. /* @brief create the task for the http server */
  103. esp_err_t http_server_start();
  104. /**
  105. * @brief gets a char* pointer to the first occurence of header_name withing the complete http request request.
  106. *
  107. * For optimization purposes, no local copy is made. memcpy can then be used in coordination with len to extract the
  108. * data.
  109. *
  110. * @param request the full HTTP raw request.
  111. * @param header_name the header that is being searched.
  112. * @param len the size of the header value if found.
  113. * @return pointer to the beginning of the header value.
  114. */
  115. char* http_server_get_header(char *request, char *header_name, int *len);
  116. void strreplace(char *src, char *str, char *rep);
  117. /* @brief lock the json config object */
  118. bool http_server_lock_json_object(TickType_t xTicksToWait);
  119. /* @brief unlock the json config object */
  120. void http_server_unlock_json_object();
  121. #define PROTECTED_JSON_CALL(a) if(http_server_lock_json_object( portMAX_DELAY )){ \ a; http_server_unlocklock_json_object(); } else{ ESP_LOGE(TAG, "could not get access to json mutex in wifi_scan"); }
  122. #ifdef __cplusplus
  123. }
  124. #endif
  125. #endif