fix: preserve writeHead statusText and custom headers (fixes #254)#274
Open
Vansh1811 wants to merge 1 commit intoexpressjs:masterfrom
Open
fix: preserve writeHead statusText and custom headers (fixes #254)#274Vansh1811 wants to merge 1 commit intoexpressjs:masterfrom
Vansh1811 wants to merge 1 commit intoexpressjs:masterfrom
Conversation
…tive
When a user calls res.writeHead(200, undefined, {'foo': 'bar'}), the
compression middleware was overwriting those custom headers because:
1. on-headers drops headers when statusText is undefined (treats args[1]
as undefined, not looking at args[2])
2. compression calls this.writeHead(this.statusCode) without the
original statusText/headers, losing them
Fix: intercept res.writeHead to capture user-supplied args and replay
them in res.write/res.end instead of calling writeHead with only the
status code. This avoids relying on private res._header property.
Fixes expressjs#254
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 a user calls
res.writeHead(200, undefined, {'foo': 'bar'}), the compression middleware was silently losing the custom headers. This happens because:on-headersdrops headers whenstatusTextisundefined— it checkstypeof arguments[1] === 'string'to determine the header index, treatingundefinedas "no statusText", but then looks atarguments[1](which isundefined) instead ofarguments[2]for headersthis.writeHead(this.statusCode)inres.writeandres.endwithout replaying the originalstatusText/headers, overwriting whatever the user setSolution
Intercept
res.writeHeadin the compression middleware to capture user-supplied arguments. Inres.writeandres.end, if the user previously calledwriteHeadexplicitly, replay those captured arguments instead of callingthis.writeHead(this.statusCode)alone.This approach:
res._headerproperty (unlike other proposed fixes)writeHeadsignatures:(code),(code, message),(code, message, headers), and(code, undefined, headers)Testing
To reproduce the bug before this fix:
Before:
x-customheader was missing. After this fix, it is correctly sent.Fixes #254