From 437318220b847a914661dada71ef34afd905d1d2 Mon Sep 17 00:00:00 2001 From: Victor Maslov Date: Sat, 30 Jan 2021 12:41:27 +0300 Subject: [PATCH] force_encoding was called on Net::ReadAdapter when url is webmocked --- README.textile | 2 +- lib/fastimage.rb | 24 +++++++++++++----------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/README.textile b/README.textile index b539828..c112c4c 100644 --- a/README.textile +++ b/README.textile @@ -132,7 +132,7 @@ h2. Tests You'll need to @gem install fakeweb@ and possibly also @gem install test-unit@ to be able to run the tests. -bc.. $ ruby test.rb +bc.. $ ruby test/test.rb Run options: # Running tests: diff --git a/lib/fastimage.rb b/lib/fastimage.rb index 3bfceab..47b5d1c 100644 --- a/lib/fastimage.rb +++ b/lib/fastimage.rb @@ -251,10 +251,8 @@ class FastImage Errno::ENETUNREACH, ImageFetchFailure, Net::HTTPBadResponse, EOFError, Errno::ENOENT, OpenSSL::SSL::SSLError raise ImageFetchFailure if @options[:raise_on_failure] - rescue NoMethodError # 1.8.7p248 can raise this due to a net/http bug - raise ImageFetchFailure if @options[:raise_on_failure] rescue UnknownImageType - raise UnknownImageType if @options[:raise_on_failure] + raise if @options[:raise_on_failure] rescue CannotParseImage if @options[:raise_on_failure] if @property == :size @@ -434,8 +432,11 @@ class FastImage end def fetch_using_base64(uri) - data = uri.split(',')[1] - decoded = Base64.decode64(data) + decoded = begin + Base64.decode64(uri.split(',')[1]) + rescue + raise CannotParseImage + end @content_length = decoded.size fetch_using_read StringIO.new(decoded) end @@ -471,19 +472,20 @@ class FastImage # Peeking beyond the end of the input will raise def peek(n) - while @strpos + n - 1 >= @str.size + while @strpos + n > @str.size unused_str = @str[@strpos..-1] - new_string = @read_fiber.resume - raise CannotParseImage if !new_string + new_string = @read_fiber.resume + new_string = @read_fiber.resume if new_string.is_a? Net::ReadAdapter + raise CannotParseImage if !new_string # we are dealing with bytes here, so force the encoding - new_string.force_encoding("ASCII-8BIT") if String.method_defined? :force_encoding + new_string.force_encoding("ASCII-8BIT") if new_string.respond_to? :force_encoding @str = unused_str + new_string @strpos = 0 end - @str[@strpos..(@strpos + n - 1)] + @str[@strpos, n] end def read(n) @@ -501,7 +503,7 @@ class FastImage new_string = @read_fiber.resume raise CannotParseImage if !new_string - new_string.force_encoding("ASCII-8BIT") if String.method_defined? :force_encoding + new_string.force_encoding("ASCII-8BIT") if new_string.respond_to? :force_encoding fetched += new_string.size @str = new_string