feat: check Content-Length header early when size limit is set#1889
Open
singhvishalkr wants to merge 2 commits into
Open
feat: check Content-Length header early when size limit is set#1889singhvishalkr wants to merge 2 commits into
singhvishalkr wants to merge 2 commits into
Conversation
Currently node-fetch sends the Host header as the last header when it is automatically added. Browsers and other HTTP libraries send Host as the first header. This change prepends the Host header to match browser behavior. If the user provides a custom Host header, it is still placed first. Fixes node-fetch#1570
When the request specifies a size limit, the Content-Length response header is now checked before consuming the body. If Content-Length exceeds the limit, the request is rejected immediately with a max-size error instead of waiting to stream the entire response. This provides faster failure for oversized responses when the server sends a valid Content-Length header. Closes node-fetch#879
71739b2 to
8ce96d3
Compare
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.
When the request specifies a \size\ limit,
ode-fetch\ currently waits until it has streamed enough body content to exceed the limit before throwing. If the server sends a valid \Content-Length\ header, we can check it upfront and fail immediately without consuming any body data.
This change adds an early check in the response handler: if
equest.size > 0\ and the \Content-Length\ header indicates a response larger than the limit, the fetch is rejected with a \max-size\ error right away.
This optimization benefits scenarios where the server provides accurate content length information. When no \Content-Length\ is present or when the header value is smaller than the limit, the existing streaming check still applies.
I added a test endpoint /size/content-length\ that returns a small body with a large \Content-Length\ header, and a test to verify the early rejection.
Closes #879