Class: Gem::RemoteFetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/rubygems/remote_fetcher.rb

Overview

RemoteFetcher handles the details of fetching gems and gem information from a remote source.

Defined Under Namespace

Classes: FetchError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proxy) ⇒ RemoteFetcher

Initialize a remote fetcher using the source URI and possible proxy information.

proxy

  • [String]: explicit specification of proxy; overrides any environment

    variable setting
    
  • nil: respect environment variables (HTTP_PROXY, HTTP_PROXY_USER,

    HTTP_PROXY_PASS)
    
  • :no_proxy: ignore environment variables and _don’t_ use a proxy



38
39
40
41
42
43
44
45
# File 'lib/rubygems/remote_fetcher.rb', line 38

def initialize(proxy)
  @proxy_uri =
    case proxy
    when :no_proxy then nil
    when nil then get_proxy_from_env
    else URI.parse(proxy.to_str)
    end
end

Class Method Details

.fetcherObject

Cached RemoteFetcher instance.



25
26
27
# File 'lib/rubygems/remote_fetcher.rb', line 25

def self.fetcher
  @fetcher ||= new Gem.configuration[:http_proxy]
end

Instance Method Details

#fetch_path(uri) ⇒ Object

Downloads uri.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rubygems/remote_fetcher.rb', line 48

def fetch_path(uri)
  open_uri_or_path(uri) do |input|
    input.read
  end
rescue Timeout::Error
  raise FetchError, "timed out fetching #{uri}"
rescue IOError, SocketError, SystemCallError => e
  raise FetchError, "#{e.class} reading #{uri}"
rescue
  old_uri = uri
  uri = uri.downcase
  retry if old_uri != uri
  raise
end

#fetch_size(uri) ⇒ Object

Returns the size of uri in bytes.



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rubygems/remote_fetcher.rb', line 64

def fetch_size(uri)
  return File.size(get_file_uri_path(uri)) if file_uri?(uri)
  require 'net/http'
  require 'uri'
  u = URI.parse(uri)
  raise ArgumentError, 'uri is not an HTTP URI' unless URI::HTTP === u
  http = connect_to(u.host, u.port)
  resp = http.head(u.request_uri)
  raise Gem::RemoteSourceException, "HTTP Response #{resp.code}" if resp.code !~ /^2/
  resp['content-length'].to_i
rescue SocketError, SystemCallError, Timeout::Error => e
  raise FetchError, "#{e.message}(#{e.class})"
end