Fix bug. Sometimes the content type isn't sent.#228
Merged
Conversation
Fixes an regresive bug where Content-Type was not properly set when serving files, which specifically affected Firefox when using "view page source" functionality. The issue primarily manifested in Firefox's view-source feature, but the fix ensures consistent behavior across all browsers and use cases. Changes: Fixed missing contentType headers Optimized string handling by replacing PSTR() with static constant - Optimized string handling by replacing snprintf_P with snprintf - Changed from dynamic buffer formatting to direct header addition using static template - Improved memory efficiency and code readability
mathieucarbou
approved these changes
Jul 15, 2025
mathieucarbou
approved these changes
Jul 15, 2025
There was a problem hiding this comment.
Pull Request Overview
Fixes a regression where the Content-Type header could be omitted when serving files (notably in Firefox’s view-source) and optimizes string handling by replacing PSTR calls with static constants.
- Added
T_inlineandT_attachmentliterals and removedPSTR/snprintf_Pusages - Inverted the
contentTypecheck to correctly set a default when none is provided - Updated
fs.opencalls to use explicitfs::FileOpenModeenum for clarity
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/literals.h | Introduced T_inline and T_attachment constants for Content-Disposition headers |
| src/WebResponses.cpp | Fixed empty contentType logic, swapped snprintf_P for snprintf and static templates |
| src/AsyncWebServerRequest.cpp | Replaced mode string "r" with fs::FileOpenMode::read in fs.open |
Comments suppressed due to low confidence (3)
src/WebResponses.cpp:737
- There are no tests covering the case when contentType is empty. Consider adding a unit test to verify that the default Content-Type is set correctly.
if (*contentType == '\0') {
src/WebResponses.cpp:748
- [nitpick] The variable name
bufis generic. A more descriptive name likedispositionHeaderBufwould improve readability.
snprintf(buf, sizeof(buf), T_attachment, filename);
src/AsyncWebServerRequest.cpp:30
- Using
fs::FileOpenMode::readmay affect compatibility with FS implementations expecting a mode string. Ensure all supported file systems accept this enum or update the documentation accordingly.
File gzFile = fs.open(gzPath, fs::FileOpenMode::read);
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
me-no-dev
approved these changes
Jul 16, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes an regresive bug where Content-Type was not properly set when serving files, which specifically affected Firefox when using "view page source" functionality. The issue primarily manifested in Firefox's view-source feature, but the fix ensures consistent behavior across all browsers and use cases.
Changes:
Fixed missing contentType headers
Optimized string handling by replacing PSTR() with static constant