Merge pull request #99 from Nakilon/master

better redirect handling
This commit is contained in:
Stephen Sykes 2018-04-20 13:54:26 +03:00 committed by GitHub
commit c7cad12fa0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 9 deletions

View File

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

View File

@ -247,6 +247,12 @@ class FastImageTest < Test::Unit::TestCase
assert_equal GoodFixtures[GoodFixtures.keys.first][1], FastImage.size(url, :raise_on_failure=>true)
end
def test_should_handle_permanent_redirect_with_complex_relative_url
register_redirect(TestUrl, "/photo.gne?rb=1&short=Vv4Und")
register_redirect("#{TestUrl}photo.gne?rb=1&short=Vv4Und", "/" + 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