esp_https_server.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2018 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef _ESP_HTTPS_SERVER_H_
  15. #define _ESP_HTTPS_SERVER_H_
  16. #include <stdbool.h>
  17. #include "esp_err.h"
  18. #include "esp_http_server.h"
  19. typedef enum {
  20. HTTPD_SSL_TRANSPORT_SECURE, // SSL Enabled
  21. HTTPD_SSL_TRANSPORT_INSECURE // SSL disabled
  22. } httpd_ssl_transport_mode_t;
  23. /**
  24. * HTTPS server config struct
  25. *
  26. * Please use HTTPD_SSL_CONFIG_DEFAULT() to initialize it.
  27. */
  28. struct httpd_ssl_config {
  29. /**
  30. * Underlying HTTPD server config
  31. *
  32. * Parameters like task stack size and priority can be adjusted here.
  33. */
  34. httpd_config_t httpd;
  35. /** CA certificate */
  36. const uint8_t *cacert_pem;
  37. /** CA certificate byte length */
  38. size_t cacert_len;
  39. /** Private key */
  40. const uint8_t *prvtkey_pem;
  41. /** Private key byte length */
  42. size_t prvtkey_len;
  43. /** Transport Mode (default secure) */
  44. httpd_ssl_transport_mode_t transport_mode;
  45. /** Port used when transport mode is secure (default 443) */
  46. uint16_t port_secure;
  47. /** Port used when transport mode is insecure (default 80) */
  48. uint16_t port_insecure;
  49. };
  50. typedef struct httpd_ssl_config httpd_ssl_config_t;
  51. /**
  52. * Default config struct init
  53. *
  54. * (http_server default config had to be copied for customization)
  55. *
  56. * Notes:
  57. * - port is set when starting the server, according to 'transport_mode'
  58. * - one socket uses ~ 40kB RAM with SSL, we reduce the default socket count to 4
  59. * - SSL sockets are usually long-lived, closing LRU prevents pool exhaustion DOS
  60. * - Stack size may need adjustments depending on the user application
  61. */
  62. #define HTTPD_SSL_CONFIG_DEFAULT() { \
  63. .httpd = { \
  64. .task_priority = tskIDLE_PRIORITY+5, \
  65. .stack_size = 10240, \
  66. .server_port = 0, \
  67. .ctrl_port = 32768, \
  68. .max_open_sockets = 4, \
  69. .max_uri_handlers = 8, \
  70. .max_resp_headers = 8, \
  71. .backlog_conn = 5, \
  72. .lru_purge_enable = true, \
  73. .recv_wait_timeout = 5, \
  74. .send_wait_timeout = 5, \
  75. .global_user_ctx = NULL, \
  76. .global_user_ctx_free_fn = NULL, \
  77. .global_transport_ctx = NULL, \
  78. .global_transport_ctx_free_fn = NULL, \
  79. .open_fn = NULL, \
  80. .close_fn = NULL, \
  81. }, \
  82. .transport_mode = HTTPD_SSL_TRANSPORT_SECURE, \
  83. .port_secure = 443, \
  84. .port_insecure = 80, \
  85. }
  86. /**
  87. * Create a SSL capable HTTP server (secure mode may be disabled in config)
  88. *
  89. * @param[in,out] config - server config, must not be const. Does not have to stay valid after
  90. * calling this function.
  91. * @param[out] handle - storage for the server handle, must be a valid pointer
  92. * @return success
  93. */
  94. esp_err_t httpd_ssl_start(httpd_handle_t *handle, httpd_ssl_config_t *config);
  95. /**
  96. * Stop the server. Blocks until the server is shut down.
  97. *
  98. * @param[in] handle
  99. */
  100. void httpd_ssl_stop(httpd_handle_t handle);
  101. #endif // _ESP_HTTPS_SERVER_H_