Only escape location in redirect if needed. Fixes #103

This commit is contained in:
Stephen Sykes 2018-05-08 22:12:16 +03:00
parent c4ffaf827c
commit 15bc4111c4
2 changed files with 18 additions and 1 deletions

View File

@ -242,6 +242,17 @@ class FastImage
fetch_using_http_from_parsed_uri
end
# Some invalid locations need escaping
def escaped_location(location)
begin
URI(location)
rescue URI::InvalidURIError
URI.escape(location)
else
location
end
end
def fetch_using_http_from_parsed_uri
http_header = {'Accept-Encoding' => 'identity'}.merge(@options[:http_header])
@ -250,7 +261,7 @@ class FastImage
if res.is_a?(Net::HTTPRedirection) && @redirect_count < 4
@redirect_count += 1
begin
@parsed_uri = URI.join(@parsed_uri, URI.escape(res['Location']))
@parsed_uri = URI.join(@parsed_uri, escaped_location(res['Location']))
rescue URI::InvalidURIError
else
fetch_using_http_from_parsed_uri

View File

@ -254,6 +254,12 @@ class FastImageTest < Test::Unit::TestCase
assert_equal GoodFixtures[GoodFixtures.keys.first][1], FastImage.size(TestUrl, :raise_on_failure=>true)
end
def test_should_handle_permanent_redirect_with_encoded_url
register_redirect(TestUrl, "/pho%20to.gne")
register_redirect("#{TestUrl}pho%20to.gne", "/" + GoodFixtures.keys.first)
assert_equal GoodFixtures[GoodFixtures.keys.first][1], FastImage.size(TestUrl, :raise_on_failure=>true)
end
def register_redirect(from, to)
resp = Net::HTTPMovedPermanently.new(1.0, 302, "Moved")
resp['Location'] = to