|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "rubygems/request" |
| 4 | +require "rubygems/remote_fetcher" |
| 5 | + |
| 6 | +module Bundler |
| 7 | + class Fetcher |
| 8 | + # Hands out pools of configured Gem::Net::HTTP connections, compatible |
| 9 | + # with the checkout/checkin interface that Gem::Request expects. |
| 10 | + # Connection policy (proxy, SSL, timeouts) follows Bundler settings |
| 11 | + # first, falling back to RubyGems configuration. |
| 12 | + class ConnectionPools |
| 13 | + # A fixed-size pool of connections to a single host, sharing the |
| 14 | + # interface of Gem::Request::HTTPPool. |
| 15 | + class Pool |
| 16 | + attr_reader :cert_files, :proxy_uri |
| 17 | + |
| 18 | + def initialize(connections, uri, proxy_uri, size) |
| 19 | + @connections = connections |
| 20 | + @uri = uri |
| 21 | + @proxy_uri = proxy_uri |
| 22 | + @cert_files = connections.cert_files |
| 23 | + @queue = Thread::SizedQueue.new(size) |
| 24 | + size.times { @queue.push(nil) } |
| 25 | + end |
| 26 | + |
| 27 | + def checkout |
| 28 | + @queue.pop || @connections.build_connection(@uri, @proxy_uri) |
| 29 | + end |
| 30 | + |
| 31 | + def checkin(connection) |
| 32 | + @queue.push(connection) |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + attr_reader :override_headers, :cert_files |
| 37 | + |
| 38 | + def initialize(size:, timeout:) |
| 39 | + @size = size |
| 40 | + @timeout = timeout |
| 41 | + @override_headers = {} |
| 42 | + @cert_files = Gem::Request.get_cert_files |
| 43 | + @pools = {} |
| 44 | + @pool_mutex = Thread::Mutex.new |
| 45 | + end |
| 46 | + |
| 47 | + # Performs a GET request through Gem::Request, which handles resetting |
| 48 | + # and retrying stale connections, and returns the raw response. |
| 49 | + def request(uri, headers = nil) |
| 50 | + Gem::Request.new(uri, Gem::Net::HTTP::Get, nil, pool_for(uri)).fetch do |request| |
| 51 | + @override_headers.each {|key, value| request[key] = value } |
| 52 | + headers&.each {|key, value| request[key] = value } |
| 53 | + end |
| 54 | + end |
| 55 | + |
| 56 | + def pool_for(uri) |
| 57 | + key = [uri.scheme, uri.hostname, uri.port] |
| 58 | + @pool_mutex.synchronize do |
| 59 | + @pools[key] ||= Pool.new(self, uri, proxy_for(uri), @size) |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + # The proxy that will be used for +uri+, or nil. RubyGems configuration |
| 64 | + # takes precedence over the environment, like Gem::RemoteFetcher. |
| 65 | + # Unlike Gem::Request, an https URI falls back to `http_proxy` when no |
| 66 | + # https-specific proxy is set, which is what the previous |
| 67 | + # net-http-persistent based transport did. |
| 68 | + def proxy_for(uri) |
| 69 | + proxy = if config_proxy = Gem.configuration[:http_proxy] |
| 70 | + Gem::Request.proxy_uri(config_proxy) |
| 71 | + else |
| 72 | + env_proxy_for(uri) |
| 73 | + end |
| 74 | + return unless proxy |
| 75 | + return unless Gem::URI::Generic.use_proxy?(uri.hostname, nil, uri.port, no_proxy_env) |
| 76 | + proxy |
| 77 | + end |
| 78 | + |
| 79 | + def build_connection(uri, proxy_uri) # :nodoc: |
| 80 | + args = [uri.hostname, uri.port] |
| 81 | + args += if proxy_uri |
| 82 | + [proxy_uri.hostname, proxy_uri.port, |
| 83 | + Gem::UriFormatter.new(proxy_uri.user).unescape, |
| 84 | + Gem::UriFormatter.new(proxy_uri.password).unescape] |
| 85 | + else |
| 86 | + [nil, nil] |
| 87 | + end |
| 88 | + |
| 89 | + connection = Gem::Request::ConnectionPools.client.new(*args) |
| 90 | + configure_ssl(connection) if uri.scheme == "https" |
| 91 | + connection.open_timeout = @timeout |
| 92 | + connection.read_timeout = @timeout |
| 93 | + connection.start |
| 94 | + connection |
| 95 | + end |
| 96 | + |
| 97 | + private |
| 98 | + |
| 99 | + def env_proxy_for(uri) |
| 100 | + proxy = Gem::Request.get_proxy_from_env(uri.scheme) |
| 101 | + proxy = Gem::Request.get_proxy_from_env("http") if proxy == :no_proxy && uri.scheme == "https" |
| 102 | + proxy == :no_proxy ? nil : proxy |
| 103 | + end |
| 104 | + |
| 105 | + def no_proxy_env |
| 106 | + ENV["no_proxy"] || ENV["NO_PROXY"] || "" |
| 107 | + end |
| 108 | + |
| 109 | + def configure_ssl(connection) |
| 110 | + connection.use_ssl = true |
| 111 | + connection.verify_mode = Bundler.settings[:ssl_verify_mode] || OpenSSL::SSL::VERIFY_PEER |
| 112 | + connection.cert_store = bundler_cert_store |
| 113 | + |
| 114 | + ssl_client_cert = Bundler.settings[:ssl_client_cert] || |
| 115 | + (Gem.configuration.ssl_client_cert if |
| 116 | + Gem.configuration.respond_to?(:ssl_client_cert)) |
| 117 | + return unless ssl_client_cert |
| 118 | + |
| 119 | + pem = File.read(ssl_client_cert) |
| 120 | + connection.cert = OpenSSL::X509::Certificate.new(pem) |
| 121 | + connection.key = OpenSSL::PKey.read(pem) |
| 122 | + end |
| 123 | + |
| 124 | + def bundler_cert_store |
| 125 | + store = OpenSSL::X509::Store.new |
| 126 | + ssl_ca_cert = Bundler.settings[:ssl_ca_cert] || |
| 127 | + (Gem.configuration.ssl_ca_cert if |
| 128 | + Gem.configuration.respond_to?(:ssl_ca_cert)) |
| 129 | + if ssl_ca_cert |
| 130 | + if File.directory? ssl_ca_cert |
| 131 | + store.add_path ssl_ca_cert |
| 132 | + else |
| 133 | + store.add_file ssl_ca_cert |
| 134 | + end |
| 135 | + else |
| 136 | + store.set_default_paths |
| 137 | + cert_files.each {|c| store.add_file c } |
| 138 | + end |
| 139 | + store |
| 140 | + end |
| 141 | + end |
| 142 | + end |
| 143 | +end |
0 commit comments