fix: decompress gzip/deflate/br responses in parseURL() - #306
Open
pratik-desgn wants to merge 1 commit into
Open
fix: decompress gzip/deflate/br responses in parseURL()#306pratik-desgn wants to merge 1 commit into
pratik-desgn wants to merge 1 commit into
Conversation
parseURL() reads the raw HTTP response body as text via res.on('data'),
so a server that returns a compressed body (with or without an
Accept-Encoding request header - which rss-parser never sent) produces
garbage bytes that the XML parser rejects with
"Non-whitespace before first tag" (rbren#280).
Now requests advertise Accept-Encoding: gzip, deflate, br, and any
response actually returned with a Content-Encoding header gets piped
through the matching zlib decompression stream before being read as
text, regardless of what was requested - matching how fetch()/browsers
already handle this.
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.
Summary
Fixes #280.
parseURL()never sends anAccept-Encodingheader, and reads the raw HTTP response body directly as text (res.on('data')). Per RFC 7231 §5.3.4, a server is allowed to compress a response even without anAccept-Encodingrequest header - and as the issue reports, some servers do this unconditionally regardless of what's requested. When that happens, rss-parser feeds raw gzip bytes to the XML parser and fails withNon-whitespace before first tag.Browsers and
fetch()handle this by transparently decompressing based on the response'sContent-Encoding, independent of what was requested. This PR does the same.Changes
parseURL()now sendsAccept-Encoding: gzip, deflate, brby default (inDEFAULT_HEADERS), for better compliance and so compliant servers can save bandwidth.Content-Encoding: gzip/deflate/brheader, the body is piped through the matchingzlibdecompression stream before being read as text - covering the case in the issue where the server ignoresAccept-Encodingentirely.Test plan
Content-Encoding: gzipheader and no request for it, and asserts it parses correctly. Confirmed it fails with the exactNon-whitespace before first tagerror from the issue when run against the old code, and passes with the fix.