Handle protocol relative urls

This commit is contained in:
Denny Abraham Cheriyan 2017-02-12 15:55:32 -08:00
parent e8aec702fa
commit 88e2098d65
2 changed files with 13 additions and 1 deletions

View File

@ -239,7 +239,9 @@ class FastImage
begin
newly_parsed_uri = Addressable::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
@ -282,6 +284,10 @@ class FastImage
end
end
def protocol_relative_url?(url)
url.start_with?("//")
end
def proxy_uri
begin
if @options[:proxy]

View File

@ -232,6 +232,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