Skip to content

Commit d1fcf9b

Browse files
committed
Merge 54290822-no-external-connection to master
[Completes #54290822]
2 parents b8ec258 + 2d794ed commit d1fcf9b

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

lib/java_buildpack/util/download_cache.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17+
require 'java_buildpack/diagnostics'
1718
require 'java_buildpack/util'
1819
require 'net/http'
1920
require 'tmpdir'
@@ -32,6 +33,7 @@ class DownloadCache
3233
def initialize(cache_root = Dir.tmpdir)
3334
Dir.mkdir(cache_root) unless File.exists? cache_root
3435
@cache_root = cache_root
36+
@logger = JavaBuildpack::Diagnostics::LoggerFactory.get_logger
3537
end
3638

3739
# Retrieves an item from the cache. Retrieval of the item uses the following algorithm:
@@ -90,6 +92,21 @@ def evict(uri)
9092

9193
private
9294

95+
HTTP_ERRORS = [
96+
EOFError,
97+
Errno::ECONNREFUSED,
98+
Errno::ECONNRESET,
99+
Errno::EHOSTUNREACH,
100+
Errno::EINVAL,
101+
Errno::EPIPE,
102+
Errno::ETIMEDOUT,
103+
Net::HTTPBadResponse,
104+
Net::HTTPHeaderSyntaxError,
105+
Net::ProtocolError,
106+
SocketError,
107+
Timeout::Error
108+
]
109+
93110
def delete_file(filename)
94111
File.delete filename if File.exists? filename
95112
end
@@ -103,6 +120,10 @@ def download(filenames, uri)
103120
write_response(filenames, response)
104121
end
105122
end
123+
124+
rescue *HTTP_ERRORS
125+
puts 'FAIL'
126+
raise "Unable to download from #{uri}"
106127
end
107128

108129
def filenames(uri)
@@ -151,6 +172,9 @@ def update(filenames, uri)
151172
write_response(filenames, response) unless response.code == '304'
152173
end
153174
end
175+
176+
rescue *HTTP_ERRORS
177+
@logger.warn "Unable to update from #{uri}. Using cached version."
154178
end
155179

156180
def use_ssl?(uri)

spec/java_buildpack/util/download_cache_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ module JavaBuildpack::Util
2121

2222
describe DownloadCache do
2323

24+
before do
25+
$stderr = StringIO.new
26+
end
27+
2428
it 'should download from a uri if the cached file does not exist' do
2529
stub_request(:get, 'http://foo-uri/').to_return(
2630
status: 200,
@@ -40,6 +44,14 @@ module JavaBuildpack::Util
4044
end
4145
end
4246

47+
it 'should raise error if download cannot be completed' do
48+
stub_request(:get, 'http://foo-uri/').to_raise(SocketError)
49+
50+
Dir.mktmpdir do |root|
51+
expect { DownloadCache.new(root).get('http://foo-uri/') {} }.to raise_error
52+
end
53+
end
54+
4355
it 'should download from a uri if the cached file exists and etag exists' do
4456
stub_request(:get, 'http://foo-uri/').with(
4557
headers: {
@@ -66,6 +78,17 @@ module JavaBuildpack::Util
6678
end
6779
end
6880

81+
it 'should use cached copy if update cannot be completed' do
82+
stub_request(:get, 'http://foo-uri/').to_raise(SocketError)
83+
84+
Dir.mktmpdir do |root|
85+
touch root, 'cached', 'foo-cached'
86+
touch root, 'etag', 'foo-etag'
87+
88+
DownloadCache.new(root).get('http://foo-uri/') {}
89+
end
90+
end
91+
6992
it 'should download from a uri if the cached file exists and last modified exists' do
7093
stub_request(:get, 'http://foo-uri/').with(
7194
headers: {

0 commit comments

Comments
 (0)