Skip to content

Commit

Permalink
Merge pull request #116 from PikachuEXE/fix-incorrect-redirect-respon…
Browse files Browse the repository at this point in the history
…se-handling

Fix handling of redirect response without Location header
  • Loading branch information
sdsykes committed Jun 22, 2020
2 parents 8386e11 + 45521bf commit 099c5e4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/fastimage.rb
Expand Up @@ -296,7 +296,10 @@ def fetch_using_http_from_parsed_uri
if res.is_a?(Net::HTTPRedirection) && @redirect_count < 4
@redirect_count += 1
begin
@parsed_uri = URI.join(@parsed_uri, escaped_location(res['Location']))
location = res['Location']
raise ImageFetchFailure if location.nil? || location.empty?

@parsed_uri = URI.join(@parsed_uri, escaped_location(location))
rescue URI::InvalidURIError
else
fetch_using_http_from_parsed_uri
Expand Down
7 changes: 7 additions & 0 deletions test/test.rb
Expand Up @@ -300,6 +300,13 @@ def test_should_handle_permanent_redirect_with_encoded_url
assert_equal GoodFixtures[GoodFixtures.keys.first][1], FastImage.size(TestUrl, :raise_on_failure=>true)
end

def test_should_handle_permanent_redirect_with_missing_location
register_redirect(TestUrl, nil)
assert_raises(FastImage::ImageFetchFailure) do
FastImage.size(TestUrl, :raise_on_failure=>true)
end
end

def register_redirect(from, to)
resp = Net::HTTPMovedPermanently.new(1.0, 302, "Moved")
resp['Location'] = to
Expand Down

0 comments on commit 099c5e4

Please sign in to comment.