http_server.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 "wifi_manager.h"
  34. #include "freertos/FreeRTOS.h"
  35. #include "freertos/task.h"
  36. #include "freertos/event_groups.h"
  37. #include "esp_wifi.h"
  38. #include "esp_event_loop.h"
  39. #include "nvs_flash.h"
  40. #include "esp_log.h"
  41. #include "driver/gpio.h"
  42. #include "mdns.h"
  43. #include "lwip/api.h"
  44. #include "lwip/err.h"
  45. #include "lwip/netdb.h"
  46. #include "lwip/opt.h"
  47. #include "lwip/memp.h"
  48. #include "lwip/ip.h"
  49. #include "lwip/raw.h"
  50. #include "lwip/udp.h"
  51. #include "lwip/priv/api_msg.h"
  52. #include "lwip/priv/tcp_priv.h"
  53. #include "lwip/priv/tcpip_priv.h"
  54. #ifdef __cplusplus
  55. extern "C" {
  56. #endif
  57. /**
  58. * @brief RTOS task for the HTTP server. Do not start manually.
  59. * @see void http_server_start()
  60. */
  61. void CODE_RAM_LOCATION http_server(void *pvParameters);
  62. /* @brief helper function that processes one HTTP request at a time */
  63. void CODE_RAM_LOCATION http_server_netconn_serve(struct netconn *conn);
  64. /* @brief create the task for the http server */
  65. void CODE_RAM_LOCATION http_server_start();
  66. /**
  67. * @brief gets a char* pointer to the first occurence of header_name withing the complete http request request.
  68. *
  69. * For optimization purposes, no local copy is made. memcpy can then be used in coordination with len to extract the
  70. * data.
  71. *
  72. * @param request the full HTTP raw request.
  73. * @param header_name the header that is being searched.
  74. * @param len the size of the header value if found.
  75. * @return pointer to the beginning of the header value.
  76. */
  77. char* CODE_RAM_LOCATION http_server_get_header(char *request, char *header_name, int *len);
  78. void CODE_RAM_LOCATION strreplace(char *src, char *str, char *rep);
  79. /* @brief lock the json config object */
  80. bool http_server_lock_json_object(TickType_t xTicksToWait);
  81. /* @brief unlock the json config object */
  82. void http_server_unlock_json_object();
  83. #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"); }
  84. #ifdef __cplusplus
  85. }
  86. #endif
  87. #endif