Does gzip decompression if the server sends compressed files. Closes #42.

This commit is contained in:
Stephen Sykes 2014-12-05 14:13:58 +02:00
parent 2d989507f3
commit cef7cae610
4 changed files with 33 additions and 0 deletions

View File

@ -237,6 +237,21 @@ class FastImage
end
end
case res['content-encoding']
when 'deflate', 'gzip', 'x-gzip'
begin
gzip = Zlib::GzipReader.new(FiberStream.new(read_fiber))
rescue FiberError, Zlib::GzipFile::Error
raise CannotParseImage
end
read_fiber = Fiber.new do
while data = gzip.readline
Fiber.yield data
end
end
end
parse_packets FiberStream.new(read_fiber)
break # needed to actively quit out of the fetch

BIN
test/fixtures/gzipped.jpg vendored Normal file

Binary file not shown.

BIN
test/fixtures/truncated_gzipped.jpg vendored Normal file

Binary file not shown.

View File

@ -56,6 +56,12 @@ BadFixtures.each do |fn|
FakeWeb.register_uri(:get, "#{TestUrl}#{fn}", :body => File.join(FixturePath, fn))
end
GzipTestImg = "gzipped.jpg"
FakeWeb.register_uri(:get, "#{TestUrl}#{GzipTestImg}", :body => File.join(FixturePath, GzipTestImg), :content_encoding => "gzip")
GzipTestImgTruncated = "truncated_gzipped.jpg"
FakeWeb.register_uri(:get, "#{TestUrl}#{GzipTestImgTruncated}", :body => File.join(FixturePath, GzipTestImgTruncated), :content_encoding => "gzip")
GzipTestImgSize = [970, 450]
class FastImageTest < Test::Unit::TestCase
def test_should_report_type_correctly
GoodFixtures.each do |fn, info|
@ -258,4 +264,16 @@ class FastImageTest < Test::Unit::TestCase
assert_equal info[1], FastImage.size(stringio)
end
end
def test_gzipped_file
url = "http://example.nowhere/#{GzipTestImg}"
assert_equal([970, 450], FastImage.size(url))
end
def test_truncated_gzipped_file
url = "http://example.nowhere/#{GzipTestImgTruncated}"
assert_raises(FastImage::SizeNotFound) do
FastImage.size(url, :raise_on_failure => true)
end
end
end