Merge pull request #78 from dennyac/handle-protocol-relative-uls

Handle protocol relative urls
This commit is contained in:
Stephen Sykes 2017-02-22 18:54:34 +02:00 committed by GitHub
commit 955d77a7e9
2 changed files with 13 additions and 1 deletions

View File

@ -249,7 +249,9 @@ class FastImage
begin
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"
if protocol_relative_url?(res['Location'])
@parsed_uri = newly_parsed_uri.tap { |obj| obj.scheme = @parsed_uri.scheme }
elsif newly_parsed_uri.scheme != "http" && newly_parsed_uri.scheme != "https"
@parsed_uri.path = res['Location']
else
@parsed_uri = newly_parsed_uri
@ -292,6 +294,10 @@ class FastImage
end
end
def protocol_relative_url?(url)
url.start_with?("//")
end
def proxy_uri
begin
if @options[:proxy]

View File

@ -235,6 +235,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_protocol_relative_url
url = "http://example.nowhere/foo.jpeg"
register_redirect(url, "//example.nowhere/" + 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