esp_https_server.h 4.5 KB

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