fix: handle unknown Content-Type in res.set() falling back to original value#7106
Closed
AkaHarshit wants to merge 1 commit intoexpressjs:masterfrom
Closed
fix: handle unknown Content-Type in res.set() falling back to original value#7106AkaHarshit wants to merge 1 commit intoexpressjs:masterfrom
AkaHarshit wants to merge 1 commit intoexpressjs:masterfrom
Conversation
…l value
When mime.contentType() returns false for unrecognized types,
res.set('Content-Type', value) was setting the header to the
literal string 'false'. This change falls back to the original
value, matching how res.type() already handles this case.
Fixes: expressjs#7034
Contributor
|
Duplicate of #7035 |
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.
Problem
When calling
res.set('Content-Type', value)where the value doesn't contain a/(like a bare extension or shorthand),mime.contentType(value)is used to resolve the full MIME type. However, ifmime.contentType()returnsfalse(for unrecognized types), theContent-Typeheader is set to the literal string"false"instead of keeping the original value.Reproduction:
Requesting
/returnsContent-Type: falsein the response headers.Solution
In
res.set()(lib/response.js), added a fallback to the original value whenmime.contentType()returnsfalse:This mirrors how
res.type()already handles the same scenario:Why It Matters
Content-Type: falseheaders being sent to clientsres.set('Content-Type')behavior consistent withres.type()Testing
Added two new test cases to
test/res.set.js:should fall back to original value for unknown Content-Type— Verifies thatres.set('Content-Type', 'some-custom-type')preserves the original value instead of setting'false'should resolve known Content-Type shorthands— Verifies thatres.set('Content-Type', 'html')correctly resolves totext/html; charset=utf-8Full test suite passes: 1249 passing, 0 failing.
Linked Issue
Fixes: #7034