esp_https_server.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. typedef enum {
  23. HTTPD_SSL_TRANSPORT_SECURE, // SSL Enabled
  24. HTTPD_SSL_TRANSPORT_INSECURE // SSL disabled
  25. } httpd_ssl_transport_mode_t;
  26. /**
  27. * HTTPS server config struct
  28. *
  29. * Please use HTTPD_SSL_CONFIG_DEFAULT() to initialize it.
  30. */
  31. struct httpd_ssl_config {
  32. /**
  33. * Underlying HTTPD server config
  34. *
  35. * Parameters like task stack size and priority can be adjusted here.
  36. */
  37. httpd_config_t httpd;
  38. /** CA certificate */
  39. const uint8_t *cacert_pem;
  40. /** CA certificate byte length */
  41. size_t cacert_len;
  42. /** Private key */
  43. const uint8_t *prvtkey_pem;
  44. /** Private key byte length */
  45. size_t prvtkey_len;
  46. /** Transport Mode (default secure) */
  47. httpd_ssl_transport_mode_t transport_mode;
  48. /** Port used when transport mode is secure (default 443) */
  49. uint16_t port_secure;
  50. /** Port used when transport mode is insecure (default 80) */
  51. uint16_t port_insecure;
  52. };
  53. typedef struct httpd_ssl_config httpd_ssl_config_t;
  54. /**
  55. * Default config struct init
  56. *
  57. * (http_server default config had to be copied for customization)
  58. *
  59. * Notes:
  60. * - port is set when starting the server, according to 'transport_mode'
  61. * - one socket uses ~ 40kB RAM with SSL, we reduce the default socket count to 4
  62. * - SSL sockets are usually long-lived, closing LRU prevents pool exhaustion DOS
  63. * - Stack size may need adjustments depending on the user application
  64. */
  65. #define HTTPD_SSL_CONFIG_DEFAULT() { \
  66. .httpd = { \
  67. .task_priority = tskIDLE_PRIORITY+5, \
  68. .stack_size = 10240, \
  69. .core_id = tskNO_AFFINITY, \
  70. .server_port = 0, \
  71. .ctrl_port = 32768, \
  72. .max_open_sockets = 4, \
  73. .max_uri_handlers = 8, \
  74. .max_resp_headers = 8, \
  75. .backlog_conn = 5, \
  76. .lru_purge_enable = true, \
  77. .recv_wait_timeout = 5, \
  78. .send_wait_timeout = 5, \
  79. .global_user_ctx = NULL, \
  80. .global_user_ctx_free_fn = NULL, \
  81. .global_transport_ctx = NULL, \
  82. .global_transport_ctx_free_fn = NULL, \
  83. .open_fn = NULL, \
  84. .close_fn = NULL, \
  85. .uri_match_fn = NULL \
  86. }, \
  87. .cacert_pem = NULL, \
  88. .cacert_len = 0, \
  89. .prvtkey_pem = NULL, \
  90. .prvtkey_len = 0, \
  91. .transport_mode = HTTPD_SSL_TRANSPORT_SECURE, \
  92. .port_secure = 443, \
  93. .port_insecure = 80, \
  94. }
  95. /**
  96. * Create a SSL capable HTTP server (secure mode may be disabled in config)
  97. *
  98. * @param[in,out] config - server config, must not be const. Does not have to stay valid after
  99. * calling this function.
  100. * @param[out] handle - storage for the server handle, must be a valid pointer
  101. * @return success
  102. */
  103. esp_err_t httpd_ssl_start(httpd_handle_t *handle, httpd_ssl_config_t *config);
  104. /**
  105. * Stop the server. Blocks until the server is shut down.
  106. *
  107. * @param[in] handle
  108. */
  109. void httpd_ssl_stop(httpd_handle_t handle);
  110. #ifdef __cplusplus
  111. }
  112. #endif
  113. #endif // _ESP_HTTPS_SERVER_H_