Skip to content

Commit

Permalink
Merge 69089318-download-cache-redirects to master
Browse files Browse the repository at this point in the history
[Completes #69089318]
  • Loading branch information
nebhale committed Apr 14, 2014
2 parents e9a6334 + d114b29 commit ded1e56
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/java_buildpack/util/cache/download_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,16 @@ def evict(uri)
Timeout::Error
].freeze

REDIRECT_TYPES = [
Net::HTTPMovedPermanently,
Net::HTTPFound,
Net::HTTPSeeOther,
Net::HTTPTemporaryRedirect
].freeze

TIMEOUT_SECONDS = 10.freeze

private_constant :FAILURE_LIMIT, :HTTP_ERRORS, :TIMEOUT_SECONDS
private_constant :FAILURE_LIMIT, :HTTP_ERRORS, :REDIRECT_TYPES, :TIMEOUT_SECONDS

def attempt(http, request, cached_file)
downloaded = false
Expand All @@ -117,6 +124,8 @@ def attempt(http, request, cached_file)
downloaded = true
elsif response.is_a? Net::HTTPNotModified
@logger.debug { 'Cached copy up to date' }
elsif redirect?(response)
downloaded = update URI(response['Location']), cached_file
else
fail InferredNetworkFailure, "Bad response: #{response}"
end
Expand Down Expand Up @@ -204,6 +213,10 @@ def proxy(uri)
Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password)
end

def redirect?(response)
REDIRECT_TYPES.any? { |t| response.is_a? t }
end

def request(uri, cached_file)
request = Net::HTTP::Get.new(uri.request_uri)

Expand Down
11 changes: 11 additions & 0 deletions spec/java_buildpack/util/cache/download_cache_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@
expect_complete_cache mutable_cache_root
end

it 'should follow redirects' do
stub_request(:get, uri)
.to_return(status: 301, headers: { Location: uri_secure })
stub_request(:get, uri_secure)
.to_return(status: 200, body: 'foo-cached', headers: { Etag: 'foo-etag', 'Last-Modified' => 'foo-last-modified' })

expect { |b| download_cache.get uri, &b }.to yield_with_args(be_a(File), true)
.and yield_file_with_content(/foo-cached/)
expect_complete_cache mutable_cache_root
end

it 'should retry failed downloads' do
stub_request(:get, uri)
.to_raise(SocketError)
Expand Down

0 comments on commit ded1e56

Please sign in to comment.