Project

General

Profile

Actions

Bug #11058

closed

[PATCH] Net::HTTPResponse#read_body raises TypeError when receiving empty gzipped response body

Added by ktsj (Kazuki Tsujimoto) about 9 years ago. Updated over 8 years ago.

Status:
Closed
Target version:
-
ruby -v:
ruby 2.3.0dev (2015-04-11 master 50228) [x86_64-linux]
[ruby-core:<unknown>]

Description

Following code raises TypeError.

require 'net/http'

def dummy_io(str)
  str = str.gsub(/\n/, "\r\n")
  Net::BufferedIO.new(StringIO.new(str))
end

io = dummy_io(<<EOS)
HTTP/1.1 200 OK
Connection: close
Content-Encoding: deflate
Content-Length: 0

EOS

res = Net::HTTPResponse.read_new(io)
res.decode_content = true

res.reading_body io, true do
  res.read_body
end

There are two problems.

(1) In Net::HTTPResponse#inflater, e may be nil and causes TypeError if inflate_body_io.finish failed.

begin
  yield inflate_body_io
ensure
  e = $!
  begin
    inflate_body_io.finish
  rescue
    raise e
  end
end

(2) Zlib::ZStream#finish raises Zlib::BufError when inflating an empty string, so we should avoid calling #finish in such case.

require 'zlib'
inflate = Zlib::Inflate.new
inflate.inflate('')
inflate.finish #=> Zlib::BufError: buffer error

Files

Updated by naruse (Yui NARUSE) over 8 years ago

Almost are ok.
One thing in 0002-lib-net-http-response.rb-Net-HTTPResponse-Inflater.patch, use @inflate.total_in instead of @bytes_read.
After fix that, please commit them by yourself

Updated by ktsj (Kazuki Tsujimoto) over 8 years ago

Oops, I overlooked Inflate#total_in.
Thank you for your reviewing.

Actions #3

Updated by ktsj (Kazuki Tsujimoto) over 8 years ago

  • Status changed from Assigned to Closed

Applied in changeset r51464.


  • lib/net/http/response.rb (Net::HTTPResponse#inflater):
    fix TypeError. An exception object might be nil.
    [ruby-core:68846] [Bug #11058]
Actions #4

Updated by nagachika (Tomoyuki Chikanaga) over 8 years ago

  • Backport changed from 2.0.0: REQUIRED, 2.1: REQUIRED, 2.2: REQUIRED to 2.0.0: REQUIRED, 2.1: REQUIRED, 2.2: DONE

Backported into ruby_2_2 branch at r51555.

Actions #5

Updated by usa (Usaku NAKAMURA) over 8 years ago

  • Backport changed from 2.0.0: REQUIRED, 2.1: REQUIRED, 2.2: DONE to 2.0.0: REQUIRED, 2.1: DONE, 2.2: DONE

ruby_2_1 r51609 merged revision(s) 46681,51464,51465.
note: r46681 was not backported into ruby_2_1, so it is also backported with these changes.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0