*** include/esp_https_server.h.orig 2022-01-26 16:39:00.814829244 +0100 --- include/esp_https_server.h 2022-02-05 14:41:49.377180907 +0100 *************** *** 21,33 **** --- 21,47 ---- HTTPD_SSL_TRANSPORT_INSECURE // SSL disabled } httpd_ssl_transport_mode_t; + typedef enum { + HTTPD_SSL_USER_CB_CREATE, + HTTPD_SSL_USER_CB_DELETE + } httpd_ssl_user_cb_type_t; + /** * @brief Callback data struct, contains the ESP-TLS connection handle */ typedef struct esp_https_server_user_cb_arg { + httpd_ssl_user_cb_type_t user_cb_type; const esp_tls_t *tls; + int sock_fd; } esp_https_server_user_cb_arg_t; + struct httpd_ssl_ctx; + + typedef struct httpd_ssl_close_arg { + esp_tls_t *tls; + struct httpd_ssl_ctx *global_ctx; + } httpd_ssl_close_arg_t; + /** * @brief Callback function prototype * Can be used to get connection or client information (SSL context) *** src/https_server.c.orig 2022-01-26 16:39:00.814829244 +0100 --- src/https_server.c 2022-02-05 16:29:43.974165271 +0100 *************** *** 26,33 **** static void httpd_ssl_close(void *ctx) { assert(ctx != NULL); ! esp_tls_server_session_delete(ctx); ! ESP_LOGD(TAG, "Secure socket closed"); } /** --- 26,49 ---- static void httpd_ssl_close(void *ctx) { assert(ctx != NULL); ! ! ESP_LOGI(TAG, "%s(%p)", __FUNCTION__, ctx); ! ! httpd_ssl_close_arg_t *arg = (httpd_ssl_close_arg_t *)ctx; ! httpd_ssl_ctx_t *global_ctx = arg->global_ctx; ! esp_tls_t *tls = arg->tls; ! ! ESP_LOGI(TAG, "%s call esp_tls_server_session_delete(tls %p)", __FUNCTION__, tls); ! esp_tls_server_session_delete(tls); ! ! ESP_LOGI(TAG, "%s call user_cb global_ctx %p", __FUNCTION__, global_ctx); ! esp_https_server_user_cb_arg_t user_cb_data = {0}; ! user_cb_data.user_cb_type = HTTPD_SSL_USER_CB_DELETE; ! user_cb_data.tls = tls; ! (global_ctx->user_cb)((void *)&user_cb_data); ! ! ESP_LOGI(TAG, "%s free %p", __FUNCTION__, ctx); ! free(ctx); } /** *************** *** 39,45 **** */ static int httpd_ssl_pending(httpd_handle_t server, int sockfd) { ! esp_tls_t *tls = httpd_sess_get_transport_ctx(server, sockfd); assert(tls != NULL); return esp_tls_get_bytes_avail(tls); } --- 55,63 ---- */ static int httpd_ssl_pending(httpd_handle_t server, int sockfd) { ! httpd_ssl_close_arg_t *arg = httpd_sess_get_transport_ctx(server, sockfd); ! assert(arg != NULL); ! esp_tls_t *tls = arg->tls; assert(tls != NULL); return esp_tls_get_bytes_avail(tls); } *************** *** 56,62 **** */ static int httpd_ssl_recv(httpd_handle_t server, int sockfd, char *buf, size_t buf_len, int flags) { ! esp_tls_t *tls = httpd_sess_get_transport_ctx(server, sockfd); assert(tls != NULL); return esp_tls_conn_read(tls, buf, buf_len); } --- 74,82 ---- */ static int httpd_ssl_recv(httpd_handle_t server, int sockfd, char *buf, size_t buf_len, int flags) { ! httpd_ssl_close_arg_t *arg = httpd_sess_get_transport_ctx(server, sockfd); ! assert(arg != NULL); ! esp_tls_t *tls = arg->tls; assert(tls != NULL); return esp_tls_conn_read(tls, buf, buf_len); } *************** *** 73,79 **** */ static int httpd_ssl_send(httpd_handle_t server, int sockfd, const char *buf, size_t buf_len, int flags) { ! esp_tls_t *tls = httpd_sess_get_transport_ctx(server, sockfd); assert(tls != NULL); return esp_tls_conn_write(tls, buf, buf_len); } --- 93,101 ---- */ static int httpd_ssl_send(httpd_handle_t server, int sockfd, const char *buf, size_t buf_len, int flags) { ! httpd_ssl_close_arg_t *arg = httpd_sess_get_transport_ctx(server, sockfd); ! assert(arg != NULL); ! esp_tls_t *tls = arg->tls; assert(tls != NULL); return esp_tls_conn_write(tls, buf, buf_len); } *************** *** 105,112 **** goto fail; } ! // Store the SSL session into the context field of the HTTPD session object ! httpd_sess_set_transport_ctx(server, sockfd, tls, httpd_ssl_close); // Set rx/tx/pending override functions httpd_sess_set_send_override(server, sockfd, httpd_ssl_send); --- 127,142 ---- goto fail; } ! // Pass a new structure containing the global context and the tls pointer to httpd_ssl_close ! // Store it in the context field of the HTTPD session object ! // Note: allocated memory will be freed by httpd_ssl_close ! httpd_ssl_close_arg_t *arg = (httpd_ssl_close_arg_t *)calloc(1, sizeof(httpd_ssl_close_arg_t)); ! arg->tls = tls; ! arg->global_ctx = global_ctx; ! // ESP_LOGI(TAG, "%s: malloc(%d) -> %p, global_ctx %p tls %p", __FUNCTION__, sizeof(httpd_ssl_close_arg_t), arg, global_ctx, tls); ! ! httpd_sess_set_transport_ctx(server, sockfd, arg, httpd_ssl_close); ! // ESP_LOGI(TAG, "%s: httpd_sess_set_transport_ctx(%p, %d, %p, httpd_ssl_close_uf)", __FUNCTION__, server, sockfd, arg); // Set rx/tx/pending override functions httpd_sess_set_send_override(server, sockfd, httpd_ssl_send); *************** *** 124,129 **** --- 154,162 ---- if (global_ctx->user_cb) { esp_https_server_user_cb_arg_t user_cb_data = {0}; user_cb_data.tls = tls; + user_cb_data.user_cb_type = HTTPD_SSL_USER_CB_CREATE; + user_cb_data.sock_fd = sockfd; + (global_ctx->user_cb)((void *)&user_cb_data); }