I have tried requesting files via HTTP GET from dozens of other devices (they are also ESP32-S3). It usually works, but occasionally a different file is sent than the one it requested. In other words, it seems that when processing requests from multiple clients, it sometimes sends the response to the wrong destination.
Do you have any idea what might be going on?
Here is my handler
- #define MAX_ASYNC_REQUESTS 4
- #define SCRATCH_BUFSIZE 8192
- static esp_err_t my_handler(httpd_req_t *req)
- {
- char filepath[STORAGE_MAXPATH];
- FILE *fd = NULL;
- struct stat file_stat;
- const char *filename = get_path_from_uri(filepath, req->uri, sizeof filepath);
- if (!filename) {
- httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Filename too long");
- return ESP_FAIL;
- }
- if (stat(filepath, &file_stat) == -1) {
- httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, "File does not exist");
- return ESP_FAIL;
- }
- fd = fopen(filepath, "r");
- if (!fd) {
- httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to read existing file");
- return ESP_FAIL;
- }
- httpd_resp_set_type(req, "application/octet-stream");
- char *chunk = heap_caps_malloc(SCRATCH_BUFSIZE, MALLOC_CAP_SPIRAM);
- if (chunk) {
- size_t chunksize;
- do {
- chunksize = fread(chunk, 1, SCRATCH_BUFSIZE, fd);
- if (chunksize > 0) {
- esp_err_t err = httpd_resp_send_chunk(req, chunk, chunksize);
- if (ESP_OK == err) {
- continue;
- }
- if (ECONNRESET == errno) {
- free(chunk);
- fclose(fd);
- return ESP_OK;
- }
- httpd_resp_sendstr_chunk(req, NULL);
- httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to send file");
- free(chunk);
- fclose(fd);
- return ESP_FAIL;
- }
- } while (chunksize != 0);
- free(chunk);
- }
- fclose(fd);
- httpd_resp_send_chunk(req, NULL, 0);
- return ESP_OK;
- }