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
- Create http server and add some endpoint (you can copy from my example)
- 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);
}
- 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
- file_serving example: Demonstrate use of connection close header from request headers Closes: https://github.com/espressif/esp-idf/issues/5775 — committed to espressif/esp-idf by shubhamkulkarni97 3 years ago
- file_serving example: Demonstrate use of connection close header from request headers Closes: https://github.com/espressif/esp-idf/issues/5775 — committed to ch45/esp32 by shubhamkulkarni97 3 years ago
Hello, I meet the same problem. Andy i try below config.
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);