Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-deflate raises Zlib::BufError on URLs that can otherwise be decoded #621

Open
Gargron opened this issue Oct 4, 2020 · 1 comment
Open
Labels

Comments

@Gargron
Copy link

Gargron commented Oct 4, 2020

Example request:

url = 'https://m.huffingtonpost.es/entry/el-supremo-anula-la-sentencia-contra-otegi-y-los-demas-acusados-en-el-caso-bateragune_es_5f24056ac5b6a34284b99a0a?25f'

HTTP.use(:auto_inflate)
        .follow
        .headers('Accept-Encoding' => 'gzip')
        .get(url)

immediately raises:

Traceback (most recent call last):
        ...
        7: from vendor/bundle/ruby/2.6.0/gems/http-4.4.1/lib/http/chainable.rb:20:in `get'
        6: from vendor/bundle/ruby/2.6.0/gems/http-4.4.1/lib/http/client.rb:34:in `request'
        5: from vendor/bundle/ruby/2.6.0/gems/http-4.4.1/lib/http/redirector.rb:59:in `perform'
        4: from vendor/bundle/ruby/2.6.0/gems/http-4.4.1/lib/http/response.rb:94:in `flush'
        3: from vendor/bundle/ruby/2.6.0/gems/http-4.4.1/lib/http/response/body.rb:51:in `to_s'
        2: from vendor/bundle/ruby/2.6.0/gems/http-4.4.1/lib/http/response/inflater.rb:19:in `readpartial'
        1: from vendor/bundle/ruby/2.6.0/gems/http-4.4.1/lib/http/response/inflater.rb:19:in `finish'
Zlib::BufError (buffer error)

If you fetch the URL without use(:auto_inflate), read the body into a string, then feed it into Zlib in full, then it's correctly decoded without errors:

res = HTTP.follow.headers('Accept-Encoding' => 'gzip').get(url)
zlib = Zlib::Inflate.new(32 + Zlib::MAX_WBITS)
zlib.inflate(res.to_s)
zlib.finish
zlib.close

It must be related to how the chunks are read but I don't know enough about Zlib to understand why.

@ixti ixti added the Bug label Oct 11, 2020
@Bonias
Copy link
Contributor

Bonias commented Mar 19, 2021

The error is raised in the Redirector (lib/http/redirector.rb:59) when it tries to flush the body of the first response:

res = HTTP.headers('Accept-Encoding' => 'gzip').get(url)
zlib = Zlib::Inflate.new(32 + Zlib::MAX_WBITS)
zlib.inflate(res.to_s)
zlib.finish # => Zlib::BufError: buffer error

res.to_s # => " "
res.headers['Content-Encoding'] # => "gzip"

IMO returned response is faulty. It shouldn't contain Content-Encoding header if body is not compressed.
On the other hand it should be easy to "fix" it on http-rb side by skipping decompression when body is flushed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants