Browse Source

fix(http): don't terminate HTTP responses twice

Several handlers sent an extra response terminator after the response
was already complete:

- send_file() and the two file-serving loops in server_file.cpp end with
  a zero-length chunk (fread() returns 0 on the last pass), but the
  callers sent another httpd_resp_send_chunk(req, NULL, 0). Every static
  file / web UI asset therefore ended with "0\r\n\r\n0\r\n\r\n".
- handler_current_date (/date, polled by the overview page) and four
  /ota responses use httpd_resp_send() (Content-Length response) and then
  called httpd_resp_send_chunk(req, NULL, 0). Because no chunk was sent
  before, ESP-IDF writes a complete second response header block plus a
  terminating chunk after the first response.

Captured from a device with a raw socket:

  GET /date       -> "...2026-10-14 09:15:11HTTP/1.1 200 OK\r\n
                      Content-Type: text/html\r\nTransfer-Encoding: chunked
                      \r\n...\r\n\r\n0\r\n\r\n"
  GET /index.html -> body ends with "\r\n0\r\n\r\n0\r\n\r\n"

The stray bytes are left on the keep-alive connection, so clients have
to discard the connection (or misparse the next response), which costs a
reconnect per request on a server limited to a few sockets.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Victor Gladkikh 1 tháng trước cách đây
mục cha
commit
5112daab65

+ 2 - 8
code/components/jomjol_fileserver_ota/server_file.cpp

@@ -421,15 +421,12 @@ static esp_err_t send_datafile(httpd_req_t *req, bool send_full_file)
             return ESP_FAIL;
         }
 
-        /* Keep looping till the whole file is sent */
+        /* Keep looping till the whole file is sent (the last, empty chunk terminates the response) */
     } while (chunksize != 0);
 
     /* Close file after sending complete */
     fclose(fd);
     ESP_LOGI(TAG, "File sending complete");
-
-    /* Respond with an empty chunk to signal HTTP response completion */
-    httpd_resp_send_chunk(req, NULL, 0);
     return ESP_OK;
 }
 
@@ -515,15 +512,12 @@ static esp_err_t send_logfile(httpd_req_t *req, bool send_full_file)
             return ESP_FAIL;
         }
 
-        /* Keep looping till the whole file is sent */
+        /* Keep looping till the whole file is sent (the last, empty chunk terminates the response) */
     } while (chunksize != 0);
 
     /* Close file after sending complete */
     fclose(fd);
     ESP_LOGD(TAG, "File sending complete");
-
-    /* Respond with an empty chunk to signal HTTP response completion */
-    httpd_resp_send_chunk(req, NULL, 0);
     return ESP_OK;
 }
 

+ 0 - 5
code/components/jomjol_fileserver_ota/server_ota.cpp

@@ -385,8 +385,6 @@ esp_err_t handler_ota_update(httpd_req_t *req)
 
         httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
         httpd_resp_send(req, zw.c_str(), strlen(zw.c_str()));
-        /* Respond with an empty chunk to signal HTTP response completion */
-        httpd_resp_send_chunk(req, NULL, 0);
 
         ESP_LOGD(TAG, "Done empty directory /firmware");
         return ESP_OK;
@@ -448,7 +446,6 @@ esp_err_t handler_ota_update(httpd_req_t *req)
         unzip_file(in, out + "/");
         std::string zw = "Web Interface Update Successfull!\nNo reboot necessary";
         httpd_resp_send(req, zw.c_str(), strlen(zw.c_str()));
-        httpd_resp_sendstr_chunk(req, NULL);
         return ESP_OK;
     }
 
@@ -474,14 +471,12 @@ esp_err_t handler_ota_update(httpd_req_t *req)
         std::string zw = "file deleted\n";
         ESP_LOGD(TAG, "%s", zw.c_str());
         httpd_resp_send(req, zw.c_str(), strlen(zw.c_str()));
-        httpd_resp_send_chunk(req, NULL, 0);
         return ESP_OK;
     }
 
     std::string zw = "ota without parameter - should not be the case!";
     httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
     httpd_resp_send(req, zw.c_str(), strlen(zw.c_str()));
-    httpd_resp_send_chunk(req, NULL, 0);
 
     LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "ota without parameter - should not be the case!");
 

+ 0 - 3
code/components/jomjol_flowcontroll/MainFlowControl.cpp

@@ -1514,9 +1514,6 @@ esp_err_t handler_current_date(httpd_req_t *req)
     httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
     httpd_resp_send(req, formatedDateAndTime.c_str(), formatedDateAndTime.length());
 
-    /* Respond with an empty chunk to signal HTTP response completion */
-    httpd_resp_sendstr_chunk(req, NULL);
-
 #ifdef DEBUG_DETAIL_ON
     LogFile.WriteHeapInfo("handler_uptime - End");
 #endif

+ 3 - 10
code/main/server_main.cpp

@@ -290,9 +290,7 @@ esp_err_t hello_main_handler(httpd_req_t *req)
         return ESP_FAIL;
     }
 
-    res = send_file(req, filetosend);
-    /* Respond with an empty chunk to signal HTTP response completion */
-    httpd_resp_send_chunk(req, NULL, 0);
+    res = send_file(req, filetosend);   // send_file() already terminates the chunked response
 
     if (res != ESP_OK)
         return res;
@@ -324,13 +322,8 @@ esp_err_t img_tmp_handler(httpd_req_t *req)
     filetosend = filetosend + "/img_tmp/" + std::string(filename);
     ESP_LOGD(TAG, "File to upload: %s", filetosend.c_str());
 
-    esp_err_t res = send_file(req, filetosend); 
-    if (res != ESP_OK)
-        return res;
-
-    /* Respond with an empty chunk to signal HTTP response completion */
-    httpd_resp_send_chunk(req, NULL, 0);
-    return ESP_OK;
+    // send_file() already terminates the chunked response
+    return send_file(req, filetosend);
 }