Module: BooticCli::Utils

Defined in:
lib/bootic_cli/utils.rb

Constant Summary collapse

REQUEST_OPTS =
{
  open_timeout: 5,
  read_timeout: 5
}.freeze
MAX_FETCH_ATTEMPTS =
3

Class Method Summary collapse

Class Method Details

.fetch_http_file(href, attempt: 1, skip_verify: false) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/bootic_cli/utils.rb', line 12

def self.fetch_http_file(href, attempt: 1, skip_verify: false)
  uri = URI.parse(href)
  opts = REQUEST_OPTS.merge({
    verify_mode: skip_verify ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER,
    use_ssl: uri.port == 443
  })

  Net::HTTP.start(uri.host, uri.port, opts) do |http|
    resp = http.get(uri.path)
    raise "Invalid response: #{resp.code}" unless resp.code.to_i == 200
    StringIO.new(resp.body)
  end
rescue Net::OpenTimeout, Net::ReadTimeout => e
  raise if attempt > MAX_FETCH_ATTEMPTS # max attempts
  # puts "#{e.class} for #{File.basename(uri.path)}! Retrying request..."
  fetch_http_file(href, attempt: attempt + 1)
rescue OpenSSL::SSL::SSLError => e
  # retry but skipping verification
  fetch_http_file(href, attempt: attempt + 1, skip_verify: true)
end