esp-idf: HTTP server problem (IDFGH-3874)

Environment

Development Kit:ESP32-Wrover-Kit Kit version DevKitC Module or chip used:ESP32-WROVER-B IDF version: 4.2 Build System: idf.py Compiler version xtensa-esp32-elf-gcc (crosstool-NG esp-2020r2) 8.2.0 Operating System: macOS Using an IDE?: Visual studio code Power Supply: USB

Problem Description

I am trying to create http server with websocket. I created a few endpoints. One for websocket connection and other to test HTTP GET POST requests. But when I try to make GET or POST request it open new socket and does not close it. I have a limited amount of socket connections and and that is the problem.

Expected Behavior

GET, POST request do not open new socket.

Steps to reproduce

  1. Create http server and add some endpoint (you can copy from my example)
  2. Add callback functions
        httpd_config_t config = HTTPD_DEFAULT_CONFIG();
        config.uri_match_fn = httpd_uri_match_wildcard;
        config.open_fn = on_open_socket;
        config.close_fn = on_close_socket;
esp_err_t on_open_socket(httpd_handle_t hd, int sockfd)
{
    clients++;
    ESP_LOGI(TAG, "socket opened: %d", sockfd);
    ESP_LOGI(TAG, "Clients: %d", clients);
    return ESP_OK;
}

void on_close_socket(httpd_handle_t hd, int sockfd)
{
    ESP_LOGE(TAG, "socket closed: %d", sockfd);
    if (clients > 0)
    {
       clients--;
    }
    
    ESP_LOGE(TAG, "Clients: %d", clients);
}
  1. Use postman to make request.

Here i make one GET request to endpoint ‘/’

I (3022) WS HTTP SERVER: Server started.
I (15732) WS HTTP SERVER: socket opened: 57
I (15732) WS HTTP SERVER: Clients: 1
I (15732) WS HTTP SERVER: URL: /

Code to reproduce this issue

https://gist.github.com/benas1989/6780d76f88d8d316b994996bc3b87f4b

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 18 (4 by maintainers)

Commits related to this issue

Most upvoted comments

Hello, I meet the same problem. Andy i try below config.

  1. config.lru_purge_enable = true;
  2. httpd_resp_set_hdr(req, “Connection”, “close”);
  3. httpd_sess_trigger_close(req->handle, httpd_req_to_sockfd(req)); It works. The most important is the lru_purge_enable. Thanks All.

My mistake! Connection close is working IF you put it before the send command: httpd_resp_set_hdr(req, “Connection”, “close”); httpd_resp_send(req, config, HTTPD_RESP_USE_STRLEN);