Method: NicInfo::Main#get_file_via_http

Defined in:
lib/nicinfo/nicinfo_main.rb

#get_file_via_http(url, file_name, try) ⇒ Object

Do an HTTP GET of a file



376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/nicinfo/nicinfo_main.rb', line 376

def get_file_via_http url, file_name, try

  @config.logger.trace("Downloading " + url + " to " + file_name )
  uri = URI.parse( URI::encode( url ) )
  req = Net::HTTP::Get.new(uri.request_uri)
  req["User-Agent"] = NicInfo::VERSION_LABEL
  req["Accept"] = NicInfo::JSON_CONTENT_TYPE
  req["Connection"] = "close"
  http = Net::HTTP.new( uri.host, uri.port )
  if uri.scheme == "https"
    http.use_ssl=true
    http.verify_mode=OpenSSL::SSL::VERIFY_NONE
  end
  res = http.start do |http_req|
    http_req.request(req)
  end

  case res
    when Net::HTTPSuccess
      File.write(file_name, res.body)
    else
      if res.code == "301" or res.code == "302" or res.code == "303" or res.code == "307" or res.code == "308"
        res.error! if try >= 5
        location = res["location"]
        return get_file_via_http( location, file_name, try + 1)
      end
      res.error!
  end
end