Fixes #20 Unable to handle permanent redirection, for redirects that have relative urls

This commit is contained in:
Stephen Sykes 2013-09-19 19:20:43 +03:00
parent 63b97f7ef0
commit 151a770b87
2 changed files with 13 additions and 1 deletions

View File

@ -209,7 +209,13 @@ class FastImage
if res.is_a?(Net::HTTPRedirection) && @redirect_count < 4
@redirect_count += 1
begin
@parsed_uri = URI.parse(res['Location'])
newly_parsed_uri = URI.parse(res['Location'])
# The new location may be relative - check for that
if newly_parsed_uri.scheme != "http" && newly_parsed_uri.scheme != "https"
@parsed_uri.path = res['Location']
else
@parsed_uri = newly_parsed_uri
end
rescue URI::InvalidURIError
else
fetch_using_http_from_parsed_uri

View File

@ -186,6 +186,12 @@ class FastImageTest < Test::Unit::TestCase
end
end
def test_should_handle_permanent_redirect_with_relative_url
url = "http://example.nowhere/foo.jpeg"
register_redirect(url, "/" + GoodFixtures.keys.first)
assert_equal GoodFixtures[GoodFixtures.keys.first][1], FastImage.size(url, :raise_on_failure=>true)
end
def register_redirect(from, to)
resp = Net::HTTPMovedPermanently.new(1.0, 302, "Moved")
resp['Location'] = to