fix(webserver): reset _contentLength after _streamFileCore send#12385
Conversation
After calling send() in _streamFileCore, _contentLength retained the file size value and was incorrectly reused in subsequent response handlers, causing ERR_CONTENT_LENGTH_MISMATCH in the browser. Reset _contentLength to CONTENT_LENGTH_NOT_SET after send() to prevent leaking file size into unrelated responses.
👋 Hello Sick-E, we appreciate your contribution to this project! 📘 Please review the project's Contributions Guide for key guidelines on code, documentation, testing, and more. 🖊️ Please also make sure you have read and signed the Contributor License Agreement for this project. Click to see more instructions ...
Review and merge process you can expect ...
|
Move .idea/ to IDE section in .gitignore Description (body): Relocated .idea/ entry from the bottom of the file to the IDE-related section alongside .vscode/ and .vs/.
There was a problem hiding this comment.
Pull request overview
This pull request fixes a bug where the _contentLength member variable in the WebServer class was leaking between requests. When _streamFileCore served a file, it set _contentLength to the file size but never reset it, causing subsequent request handlers to inherit this value. This resulted in ERR_CONTENT_LENGTH_MISMATCH errors in browsers.
Changes:
- Adds a
resetContentLength()method to reset_contentLengthtoCONTENT_LENGTH_NOT_SET - Calls
resetContentLength()at the end of_streamFileCore()to prevent content length leakage - Updates
.gitignoreto include/.idea/directory for JetBrains IDEs
Reviewed changes
Copilot reviewed 1 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| libraries/WebServer/src/WebServer.cpp | Implements resetContentLength() method and calls it after send() in _streamFileCore() to fix content length leakage bug |
| .gitignore | Adds /.idea/ to ignore JetBrains IDE files and updates comment to reflect support for additional IDEs |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…_streamFileCore Description (body): Replaced dedicated resetContentLength() method with a direct call to setContentLength(CONTENT_LENGTH_NOT_SET) at the end of _streamFileCore, reusing existing API without requiring a new declaration in WebServer.h.
Memory usage test (comparing PR against master branch)The table below shows the summary of memory usage change (decrease - increase) in bytes and percentage for each target.
Click to expand the detailed deltas report [usage change in BYTES]
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Test Results 91 files 91 suites 30m 53s ⏱️ Results for commit 9123263. ♻️ This comment has been updated with latest results. |
…essif#12385) * fix(WebServer): reset _contentLength after _streamFileCore send After calling send() in _streamFileCore, _contentLength retained the file size value and was incorrectly reused in subsequent response handlers, causing ERR_CONTENT_LENGTH_MISMATCH in the browser. Reset _contentLength to CONTENT_LENGTH_NOT_SET after send() to prevent leaking file size into unrelated responses. * Commit message (subject): Move .idea/ to IDE section in .gitignore Description (body): Relocated .idea/ entry from the bottom of the file to the IDE-related section alongside .vscode/ and .vs/. * fix(webserver): reset _contentLength to CONTENT_LENGTH_NOT_SET after _streamFileCore Description (body): Replaced dedicated resetContentLength() method with a direct call to setContentLength(CONTENT_LENGTH_NOT_SET) at the end of _streamFileCore, reusing existing API without requiring a new declaration in WebServer.h.
Description of Change
_streamFileCorecallssetContentLength(fileSize)beforesend(), which sets the member variable_contentLengthto the file size. Aftersend()completes,_contentLengthis never reset, causing it to leak into subsequent response handlers. Any handler callingsend()after astreamFileresponse will incorrectly inherit the previous file's size asContent-Length, resulting inERR_CONTENT_LENGTH_MISMATCHin the browser.Fix: reset
_contentLengthtoCONTENT_LENGTH_NOT_SETat the end of_streamFileCore.Test Scenarios
Tested on ESP32 with framework-arduinoespressif32 via PlatformIO.
Reproduction scenario:
<link rel="icon">pointing to a file served viastreamFile_contentLengthis set to favicon file sizesend(200, "text/plain", "Success")without explicitly callingsetContentLengthContent-Lengthequal to favicon size →ERR_CONTENT_LENGTH_MISMATCHAfter fix:
_contentLengthis reset after eachstreamFileresponse, subsequent handlers receive correctContent-Length.Related links
None