Skip to content

Commit ded1e56

Browse files
committed
Merge 69089318-download-cache-redirects to master
[Completes #69089318]
2 parents e9a6334 + d114b29 commit ded1e56

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

lib/java_buildpack/util/cache/download_cache.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,16 @@ def evict(uri)
100100
Timeout::Error
101101
].freeze
102102

103+
REDIRECT_TYPES = [
104+
Net::HTTPMovedPermanently,
105+
Net::HTTPFound,
106+
Net::HTTPSeeOther,
107+
Net::HTTPTemporaryRedirect
108+
].freeze
109+
103110
TIMEOUT_SECONDS = 10.freeze
104111

105-
private_constant :FAILURE_LIMIT, :HTTP_ERRORS, :TIMEOUT_SECONDS
112+
private_constant :FAILURE_LIMIT, :HTTP_ERRORS, :REDIRECT_TYPES, :TIMEOUT_SECONDS
106113

107114
def attempt(http, request, cached_file)
108115
downloaded = false
@@ -117,6 +124,8 @@ def attempt(http, request, cached_file)
117124
downloaded = true
118125
elsif response.is_a? Net::HTTPNotModified
119126
@logger.debug { 'Cached copy up to date' }
127+
elsif redirect?(response)
128+
downloaded = update URI(response['Location']), cached_file
120129
else
121130
fail InferredNetworkFailure, "Bad response: #{response}"
122131
end
@@ -204,6 +213,10 @@ def proxy(uri)
204213
Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password)
205214
end
206215

216+
def redirect?(response)
217+
REDIRECT_TYPES.any? { |t| response.is_a? t }
218+
end
219+
207220
def request(uri, cached_file)
208221
request = Net::HTTP::Get.new(uri.request_uri)
209222

spec/java_buildpack/util/cache/download_cache_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,17 @@
8585
expect_complete_cache mutable_cache_root
8686
end
8787

88+
it 'should follow redirects' do
89+
stub_request(:get, uri)
90+
.to_return(status: 301, headers: { Location: uri_secure })
91+
stub_request(:get, uri_secure)
92+
.to_return(status: 200, body: 'foo-cached', headers: { Etag: 'foo-etag', 'Last-Modified' => 'foo-last-modified' })
93+
94+
expect { |b| download_cache.get uri, &b }.to yield_with_args(be_a(File), true)
95+
.and yield_file_with_content(/foo-cached/)
96+
expect_complete_cache mutable_cache_root
97+
end
98+
8899
it 'should retry failed downloads' do
89100
stub_request(:get, uri)
90101
.to_raise(SocketError)

0 commit comments

Comments
 (0)