Class: Gem::RemoteFetcher
- Inherits:
-
Object
- Object
- Gem::RemoteFetcher
- Includes:
- UriParsing, UserInteraction
- Defined in:
- lib/rubygems/remote_fetcher.rb,
lib/rubygems/test_utilities.rb
Overview
:stopdoc:
Defined Under Namespace
Classes: FetchError, UnknownHostError
Instance Attribute Summary collapse
-
#headers ⇒ Object
Returns the value of attribute headers.
Class Method Summary collapse
-
.fetcher ⇒ Object
Cached RemoteFetcher instance.
- .fetcher=(fetcher) ⇒ Object
Instance Method Summary collapse
-
#cache_update_path(uri, path = nil, update = true) ⇒ Object
Downloads
uri
topath
if necessary. - #close_all ⇒ Object
-
#download(spec, source_uri, install_dir = Gem.dir) ⇒ Object
Moves the gem
spec
fromsource_uri
to the cache dir unless it is already there. -
#download_to_cache(dependency) ⇒ Object
Given a name and requirement, downloads this gem into cache and returns the filename.
-
#fetch_file(uri, *_) ⇒ Object
File Fetcher.
-
#fetch_http(uri, last_modified = nil, head = false, depth = 0) ⇒ Object
(also: #fetch_https)
HTTP Fetcher.
-
#fetch_path(uri, mtime = nil, head = false) ⇒ Object
Downloads
uri
and returns it as a String. - #fetch_s3(uri, mtime = nil, head = false) ⇒ Object
- #https?(uri) ⇒ Boolean
-
#initialize(proxy = nil, dns = nil, headers = {}) ⇒ RemoteFetcher
constructor
Initialize a remote fetcher using the source URI and possible proxy information.
-
#request(uri, request_class, last_modified = nil) ⇒ Object
Performs a Net::HTTP request of type
request_class
onuri
returning a Net::HTTP response object. -
#s3_uri_signer(uri) ⇒ Object
we have our own signing code here to avoid a dependency on the aws-sdk gem.
Methods included from UserInteraction
#alert, #alert_error, #alert_warning, #ask, #ask_for_password, #ask_yes_no, #choose_from_list, #say, #terminate_interaction, #verbose
Methods included from DefaultUserInteraction
ui, #ui, ui=, #ui=, use_ui, #use_ui
Methods included from Text
#clean_text, #format_text, #levenshtein_distance, #min3, #truncate_text
Constructor Details
#initialize(proxy = nil, dns = nil, headers = {}) ⇒ 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
headers
: A set of additional HTTP headers to be sent to the server when
fetching the gem.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/rubygems/remote_fetcher.rb', line 78 def initialize(proxy=nil, dns=nil, headers={}) require 'net/http' require 'stringio' require 'uri' Socket.do_not_reverse_lookup = true @proxy = proxy @pools = {} @pool_lock = Mutex.new @cert_files = Gem::Request.get_cert_files @headers = headers end |
Instance Attribute Details
#headers ⇒ Object
Returns the value of attribute headers.
62 63 64 |
# File 'lib/rubygems/remote_fetcher.rb', line 62 def headers @headers end |
Class Method Details
.fetcher ⇒ Object
Cached RemoteFetcher instance.
58 59 60 |
# File 'lib/rubygems/remote_fetcher.rb', line 58 def self.fetcher @fetcher ||= self.new Gem.configuration[:http_proxy] end |
.fetcher=(fetcher) ⇒ Object
169 170 171 |
# File 'lib/rubygems/test_utilities.rb', line 169 def self.fetcher=(fetcher) @fetcher = fetcher end |
Instance Method Details
#cache_update_path(uri, path = nil, update = true) ⇒ Object
Downloads uri
to path
if necessary. If no path is given, it just passes the data.
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/rubygems/remote_fetcher.rb', line 291 def cache_update_path(uri, path = nil, update = true) mtime = path && File.stat(path).mtime rescue nil data = fetch_path(uri, mtime) if data == nil # indicates the server returned 304 Not Modified return Gem.read_binary(path) end if update and path Gem.write_binary(path, data) end data end |
#close_all ⇒ Object
327 328 329 |
# File 'lib/rubygems/remote_fetcher.rb', line 327 def close_all @pools.each_value {|pool| pool.close_all } end |
#download(spec, source_uri, install_dir = Gem.dir) ⇒ Object
Moves the gem spec
from source_uri
to the cache dir unless it is already there. If the source_uri is local the gem cache dir copy is always replaced.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/rubygems/remote_fetcher.rb', line 115 def download(spec, source_uri, install_dir = Gem.dir) install_cache_dir = File.join install_dir, "cache" cache_dir = if Dir.pwd == install_dir # see fetch_command install_dir elsif File.writable?(install_cache_dir) || (File.writable?(install_dir) && (not File.exist?(install_cache_dir))) install_cache_dir else File.join Gem.user_dir, "cache" end gem_file_name = File.basename spec.cache_file local_gem_path = File.join cache_dir, gem_file_name require "fileutils" FileUtils.mkdir_p cache_dir rescue nil unless File.exist? cache_dir source_uri = parse_uri(source_uri) scheme = source_uri.scheme # URI.parse gets confused by MS Windows paths with forward slashes. scheme = nil if scheme =~ /^[a-z]$/i # REFACTOR: split this up and dispatch on scheme (eg download_http) # REFACTOR: be sure to clean up fake fetcher when you do this... cleaner case scheme when 'http', 'https', 's3' then unless File.exist? local_gem_path begin verbose "Downloading gem #{gem_file_name}" remote_gem_path = source_uri + "gems/#{gem_file_name}" self.cache_update_path remote_gem_path, local_gem_path rescue FetchError raise if spec.original_platform == spec.platform alternate_name = "#{spec.original_name}.gem" verbose "Failed, downloading gem #{alternate_name}" remote_gem_path = source_uri + "gems/#{alternate_name}" self.cache_update_path remote_gem_path, local_gem_path end end when 'file' then begin path = source_uri.path path = File.dirname(path) if File.extname(path) == '.gem' remote_gem_path = Gem::Util.correct_for_windows_path(File.join(path, 'gems', gem_file_name)) FileUtils.cp(remote_gem_path, local_gem_path) rescue Errno::EACCES local_gem_path = source_uri.to_s end verbose "Using local gem #{local_gem_path}" when nil then # TODO test for local overriding cache source_path = if Gem.win_platform? && source_uri.scheme && !source_uri.path.include?(':') "#{source_uri.scheme}:#{source_uri.path}" else source_uri.path end source_path = Gem::UriFormatter.new(source_path).unescape begin FileUtils.cp source_path, local_gem_path unless File.identical?(source_path, local_gem_path) rescue Errno::EACCES local_gem_path = source_uri.to_s end verbose "Using local gem #{local_gem_path}" else raise ArgumentError, "unsupported URI scheme #{source_uri.scheme}" end local_gem_path end |
#download_to_cache(dependency) ⇒ Object
Given a name and requirement, downloads this gem into cache and returns the filename. Returns nil if the gem cannot be located. – Should probably be integrated with #download below, but that will be a larger, more encompassing effort. -erikh
100 101 102 103 104 105 106 107 108 |
# File 'lib/rubygems/remote_fetcher.rb', line 100 def download_to_cache(dependency) found, _ = Gem::SpecFetcher.fetcher.spec_for_dependency dependency return if found.empty? spec, source = found.max_by {|(s,_)| s.version } download spec, source.uri end |
#fetch_file(uri, *_) ⇒ Object
File Fetcher. Dispatched by fetch_path
. Use it instead.
203 204 205 |
# File 'lib/rubygems/remote_fetcher.rb', line 203 def fetch_file(uri, *_) Gem.read_binary Gem::Util.correct_for_windows_path uri.path end |
#fetch_http(uri, last_modified = nil, head = false, depth = 0) ⇒ Object Also known as: fetch_https
HTTP Fetcher. Dispatched by fetch_path
. Use it instead.
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/rubygems/remote_fetcher.rb', line 210 def fetch_http(uri, last_modified = nil, head = false, depth = 0) fetch_type = head ? Net::HTTP::Head : Net::HTTP::Get response = request uri, fetch_type, last_modified do |req| headers.each {|k,v| req.add_field(k,v) } end case response when Net::HTTPOK, Net::HTTPNotModified then response.uri = uri head ? response : response.body when Net::HTTPMovedPermanently, Net::HTTPFound, Net::HTTPSeeOther, Net::HTTPTemporaryRedirect then raise FetchError.new('too many redirects', uri) if depth > 10 unless location = response['Location'] raise FetchError.new("redirecting but no redirect location was given", uri) end location = parse_uri location if https?(uri) && !https?(location) raise FetchError.new("redirecting to non-https resource: #{location}", uri) end fetch_http(location, last_modified, head, depth + 1) else raise FetchError.new("bad response #{response.} #{response.code}", uri) end end |
#fetch_path(uri, mtime = nil, head = false) ⇒ Object
Downloads uri
and returns it as a String.
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/rubygems/remote_fetcher.rb', line 244 def fetch_path(uri, mtime = nil, head = false) uri = parse_uri uri unless uri.scheme raise ArgumentError, "uri scheme is invalid: #{uri.scheme.inspect}" end data = send "fetch_#{uri.scheme}", uri, mtime, head if data and !head and uri.to_s.end_with?(".gz") begin data = Gem::Util.gunzip data rescue Zlib::GzipFile::Error raise FetchError.new("server did not return a valid file", uri) end end data rescue Timeout::Error raise UnknownHostError.new('timed out', uri) rescue IOError, SocketError, SystemCallError, *(OpenSSL::SSL::SSLError if Gem::HAVE_OPENSSL) => e if e. =~ /getaddrinfo/ raise UnknownHostError.new('no such name', uri) else raise FetchError.new("#{e.class}: #{e}", uri) end end |
#fetch_s3(uri, mtime = nil, head = false) ⇒ Object
273 274 275 276 277 278 279 280 |
# File 'lib/rubygems/remote_fetcher.rb', line 273 def fetch_s3(uri, mtime = nil, head = false) begin public_uri = s3_uri_signer(uri).sign rescue Gem::S3URISigner::ConfigurationError, Gem::S3URISigner::InstanceProfileError => e raise FetchError.new(e., "s3://#{uri.host}") end fetch_https public_uri, mtime, head end |
#https?(uri) ⇒ Boolean
323 324 325 |
# File 'lib/rubygems/remote_fetcher.rb', line 323 def https?(uri) uri.scheme.downcase == 'https' end |
#request(uri, request_class, last_modified = nil) ⇒ Object
Performs a Net::HTTP request of type request_class
on uri
returning a Net::HTTP response object. request maintains a table of persistent connections to reduce connect overhead.
312 313 314 315 316 317 318 319 320 321 |
# File 'lib/rubygems/remote_fetcher.rb', line 312 def request(uri, request_class, last_modified = nil) proxy = proxy_for @proxy, uri pool = pools_for(proxy).pool_for uri request = Gem::Request.new uri, request_class, last_modified, pool request.fetch do |req| yield req if block_given? end end |
#s3_uri_signer(uri) ⇒ Object
we have our own signing code here to avoid a dependency on the aws-sdk gem
283 284 285 |
# File 'lib/rubygems/remote_fetcher.rb', line 283 def s3_uri_signer(uri) Gem::S3URISigner.new(uri) end |