From efed5ee33fa4fd02916e96c5a7ba79241fd25dc0 Mon Sep 17 00:00:00 2001 From: Harshit Malpani Date: Wed, 14 Sep 2022 11:06:40 +0530 Subject: [PATCH] esp_http_server: Add linger in httpd_config_t Closes: https://github.com/espressif/esp-idf/issues/9514 --- components/esp_http_server/include/esp_http_server.h | 5 +++++ components/esp_http_server/src/httpd_sess.c | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/components/esp_http_server/include/esp_http_server.h b/components/esp_http_server/include/esp_http_server.h index 1c8b78583f..1f790b5ff8 100644 --- a/components/esp_http_server/include/esp_http_server.h +++ b/components/esp_http_server/include/esp_http_server.h @@ -40,6 +40,8 @@ initializer that should be kept in sync .global_user_ctx_free_fn = NULL, \ .global_transport_ctx = NULL, \ .global_transport_ctx_free_fn = NULL, \ + .enable_so_linger = false, \ + .linger_timeout = 0, \ .open_fn = NULL, \ .close_fn = NULL, \ .uri_match_fn = NULL \ @@ -185,6 +187,9 @@ typedef struct httpd_config { */ httpd_free_ctx_fn_t global_transport_ctx_free_fn; + bool enable_so_linger; /*!< bool to enable/disable linger */ + int linger_timeout; /*!< linger timeout (in seconds) */ + /** * Custom session opening callback. * diff --git a/components/esp_http_server/src/httpd_sess.c b/components/esp_http_server/src/httpd_sess.c index dea8e67d1a..66c30260e2 100644 --- a/components/esp_http_server/src/httpd_sess.c +++ b/components/esp_http_server/src/httpd_sess.c @@ -353,6 +353,13 @@ void httpd_sess_delete(struct httpd_data *hd, struct sock_db *session) } ESP_LOGD(TAG, LOG_FMT("fd = %d"), session->fd); + if (hd->config.enable_so_linger) { + struct linger so_linger = { + .l_onoff = true, + .l_linger = hd->config.linger_timeout, + }; + setsockopt(session->fd, SOL_SOCKET, SO_LINGER, &so_linger, sizeof(struct linger)); + } // Call close function if defined if (hd->config.close_fn) {