mirror of
https://github.com/espressif/esp-idf.git
synced 2025-11-18 10:31:09 +00:00
feature: Added user callback for esp_https_server
- Can be used to get connection or client information (SSL context) - E.g. Client certificate, Socket FD, Connection state, etc. - Added example callback for getting client certificate information in 'https_server/simple' example Closes https://github.com/espressif/esp-idf/issues/7479
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
#include <stdbool.h>
|
||||
#include "esp_err.h"
|
||||
#include "esp_http_server.h"
|
||||
#include "esp_tls.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
@@ -20,6 +21,22 @@ typedef enum {
|
||||
HTTPD_SSL_TRANSPORT_INSECURE // SSL disabled
|
||||
} httpd_ssl_transport_mode_t;
|
||||
|
||||
/**
|
||||
* @brief Callback data struct, contains the ESP-TLS connection handle
|
||||
*/
|
||||
typedef struct esp_https_server_user_cb_arg {
|
||||
const esp_tls_t *tls;
|
||||
} esp_https_server_user_cb_arg_t;
|
||||
|
||||
/**
|
||||
* @brief Callback function prototype
|
||||
* Can be used to get connection or client information (SSL context)
|
||||
* E.g. Client certificate, Socket FD, Connection state, etc.
|
||||
*
|
||||
* @param user_cb Callback data struct
|
||||
*/
|
||||
typedef void esp_https_server_user_cb(esp_https_server_user_cb_arg_t *user_cb);
|
||||
|
||||
/**
|
||||
* HTTPS server config struct
|
||||
*
|
||||
@@ -66,6 +83,9 @@ struct httpd_ssl_config {
|
||||
|
||||
/** Enable tls session tickets */
|
||||
bool session_tickets;
|
||||
|
||||
/** User callback for esp_https_server */
|
||||
esp_https_server_user_cb *user_cb;
|
||||
};
|
||||
|
||||
typedef struct httpd_ssl_config httpd_ssl_config_t;
|
||||
@@ -113,6 +133,7 @@ typedef struct httpd_ssl_config httpd_ssl_config_t;
|
||||
.port_secure = 443, \
|
||||
.port_insecure = 80, \
|
||||
.session_tickets = false, \
|
||||
.user_cb = NULL, \
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -15,6 +15,7 @@ const static char *TAG = "esp_https_server";
|
||||
typedef struct httpd_ssl_ctx {
|
||||
esp_tls_cfg_server_t *tls_cfg;
|
||||
httpd_open_func_t open_fn;
|
||||
esp_https_server_user_cb *user_cb;
|
||||
} httpd_ssl_ctx_t;
|
||||
|
||||
/**
|
||||
@@ -119,6 +120,13 @@ static esp_err_t httpd_ssl_open(httpd_handle_t server, int sockfd)
|
||||
if (global_ctx->open_fn) {
|
||||
(global_ctx->open_fn)(server, sockfd);
|
||||
}
|
||||
|
||||
if (global_ctx->user_cb) {
|
||||
esp_https_server_user_cb_arg_t user_cb_data = {0};
|
||||
user_cb_data.tls = tls;
|
||||
(global_ctx->user_cb)((void *)&user_cb_data);
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
fail:
|
||||
esp_tls_server_session_delete(tls);
|
||||
@@ -172,6 +180,7 @@ static httpd_ssl_ctx_t *create_secure_context(const struct httpd_ssl_config *con
|
||||
}
|
||||
|
||||
ssl_ctx->tls_cfg = cfg;
|
||||
ssl_ctx->user_cb = config->user_cb;
|
||||
/* cacert = CA which signs client cert, or client cert itself , which is mapped to client_verify_cert_pem */
|
||||
if(config->client_verify_cert_pem != NULL) {
|
||||
cfg->cacert_buf = (unsigned char *)malloc(config->client_verify_cert_len);
|
||||
|
||||
Reference in New Issue
Block a user