class Gem::RemoteFetcher
RemoteFetcher
处理从远程源获取 gem 包和 gem 信息的细节。
属性
headers[RW]
公共类方法
fetcher() 点击切换源代码
缓存的 RemoteFetcher
实例。
# File rubygems/remote_fetcher.rb, line 55 def self.fetcher @fetcher ||= new Gem.configuration[:http_proxy] end
new(proxy=nil, dns=nil, headers={}) 点击切换源代码
使用源 URI 和可能的代理信息初始化一个远程获取器。
proxy
-
[String]:代理的明确指定;覆盖任何环境设置
variable setting
-
nil:遵循环境变量(HTTP_PROXY,HTTP_PROXY_USER)
HTTP_PROXY_PASS)
-
:no_proxy
: 忽略环境变量并且_不_使用代理
headers
:一组额外的 HTTP 标头,当发送到服务器时
fetching the gem.
# File rubygems/remote_fetcher.rb, line 75 def initialize(proxy=nil, dns=nil, headers={}) require_relative "core_ext/tcpsocket_init" if Gem.configuration.ipv4_fallback_enabled require_relative "vendored_net_http" require_relative "vendor/uri/lib/uri" Socket.do_not_reverse_lookup = true @proxy = proxy @pools = {} @pool_lock = Thread::Mutex.new @cert_files = Gem::Request.get_cert_files @headers = headers end
公共实例方法
cache_update_path(uri, path = nil, update = true) 点击切换源代码
如有必要,将 uri
下载到 path
。如果没有给定路径,它只是传递数据。
# File rubygems/remote_fetcher.rb, line 286 def cache_update_path(uri, path = nil, update = true) mtime = begin path && File.stat(path).mtime rescue StandardError nil end data = fetch_path(uri, mtime) if data.nil? # indicates the server returned 304 Not Modified return Gem.read_binary(path) end if update && path Gem.write_binary(path, data) end data end
close_all() 点击切换源代码
# File rubygems/remote_fetcher.rb, line 326 def close_all @pools.each_value(&:close_all) end
download(spec, source_uri, install_dir = Gem.dir) 点击切换源代码
将 gem spec
从 source_uri
移动到缓存目录,除非它已经在那里。如果 source_uri 是本地的,gem 缓存目录副本总是会被替换。
# File rubygems/remote_fetcher.rb, line 112 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) && !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" begin FileUtils.mkdir_p cache_dir rescue StandardError nil end unless File.exist? cache_dir source_uri = Gem::Uri.new(source_uri) scheme = source_uri.scheme # Gem::URI.parse gets confused by MS Windows paths with forward slashes. scheme = nil if /^[a-z]$/i.match?(scheme) # 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}" 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}" 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) 点击切换源代码
给定一个名称和要求,将此 gem 下载到缓存中并返回文件名。如果无法找到该 gem,则返回 nil。
# File rubygems/remote_fetcher.rb, line 97 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, *_) 点击切换源代码
文件获取器。由 fetch_path
调度。请改用它。
# File rubygems/remote_fetcher.rb, line 204 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) 点击切换源代码
HTTP 获取器。由 fetch_path
调度。请改用它。
# File rubygems/remote_fetcher.rb, line 211 def fetch_http(uri, last_modified = nil, head = false, depth = 0) fetch_type = head ? Gem::Net::HTTP::Head : Gem::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 Gem::Net::HTTPOK, Gem::Net::HTTPNotModified then response.uri = uri head ? response : response.body when Gem::Net::HTTPMovedPermanently, Gem::Net::HTTPFound, Gem::Net::HTTPSeeOther, Gem::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 = Gem::Uri.new 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.message} #{response.code}", uri) end end
也别名为:fetch_https
fetch_path(uri, mtime = nil, head = false) 点击切换源代码
下载 uri
并将其作为字符串返回。
# File rubygems/remote_fetcher.rb, line 245 def fetch_path(uri, mtime = nil, head = false) uri = Gem::Uri.new uri unless uri.scheme raise ArgumentError, "uri scheme is invalid: #{uri.scheme.inspect}" end data = send "fetch_#{uri.scheme}", uri, mtime, head if data && !head && 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 Gem::Timeout::Error, IOError, SocketError, SystemCallError, *(OpenSSL::SSL::SSLError if Gem::HAVE_OPENSSL) => e raise FetchError.new("#{e.class}: #{e}", uri) end
fetch_s3(uri, mtime = nil, head = false) 点击切换源代码
# File rubygems/remote_fetcher.rb, line 268 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.message, "s3://#{uri.host}") end fetch_https public_uri, mtime, head end
https?(uri) 点击切换源代码
# File rubygems/remote_fetcher.rb, line 322 def https?(uri) uri.scheme.casecmp("https").zero? end
request(uri, request_class, last_modified = nil) { |req| ... } 点击切换源代码
在 uri
上执行 request_class
类型的 Gem::Net::HTTP
请求,返回一个 Gem::Net::HTTP
响应对象。 request 维护一个持久连接表,以减少连接开销。
# File rubygems/remote_fetcher.rb, line 311 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) 点击切换源代码
我们在这里有自己的签名代码,以避免依赖 aws-sdk gem
# File rubygems/remote_fetcher.rb, line 278 def s3_uri_signer(uri) Gem::S3URISigner.new(uri) end
私有实例方法
pools_for(proxy) 点击切换源代码
# File rubygems/remote_fetcher.rb, line 336 def pools_for(proxy) @pool_lock.synchronize do @pools[proxy] ||= Gem::Request::ConnectionPools.new proxy, @cert_files end end
proxy_for(proxy, uri) 点击切换源代码
# File rubygems/remote_fetcher.rb, line 332 def proxy_for(proxy, uri) Gem::Request.proxy_uri(proxy || Gem::Request.get_proxy_from_env(uri.scheme)) end