Class: Gem::Request
- Inherits:
-
Object
- Object
- Gem::Request
- Includes:
- UserInteraction
- Defined in:
- lib/rubygems/request.rb
Defined Under Namespace
Classes: ConnectionPools, HTTPPool, HTTPSPool
Class Method Summary collapse
- .configure_connection_for_https(connection, cert_files) ⇒ Object
-
.create_with_proxy(uri, request_class, last_modified, proxy) ⇒ Object
Legacy.
- .get_cert_files ⇒ Object
-
.get_proxy_from_env(scheme = 'http') ⇒ Object
Returns a proxy URI for the given
schemeif one is set in the environment variables. -
.proxy_uri(proxy) ⇒ Object
:nodoc:.
Instance Method Summary collapse
- #cert_files ⇒ Object
-
#connection_for(uri) ⇒ Object
Creates or an HTTP connection based on
uri, or retrieves an existing connection, using a proxy if needed. - #fetch {|request| ... } ⇒ Object
-
#initialize(uri, request_class, last_modified, pool) ⇒ Request
constructor
A new instance of Request.
-
#perform_request(request) ⇒ Object
:nodoc:.
- #proxy_uri ⇒ Object
-
#reset(connection) ⇒ Object
Resets HTTP connection
connection. - #user_agent ⇒ Object
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
Constructor Details
#initialize(uri, request_class, last_modified, pool) ⇒ Request
Returns a new instance of Request.
28 29 30 31 32 33 34 35 36 |
# File 'lib/rubygems/request.rb', line 28 def initialize(uri, request_class, last_modified, pool) @uri = uri @request_class = request_class @last_modified = last_modified @requests = Hash.new 0 @user_agent = user_agent @connection_pool = pool end |
Class Method Details
.configure_connection_for_https(connection, cert_files) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/rubygems/request.rb', line 46 def self.configure_connection_for_https(connection, cert_files) require 'net/https' connection.use_ssl = true connection.verify_mode = Gem.configuration.ssl_verify_mode || OpenSSL::SSL::VERIFY_PEER store = OpenSSL::X509::Store.new if Gem.configuration.ssl_client_cert then pem = File.read Gem.configuration.ssl_client_cert connection.cert = OpenSSL::X509::Certificate.new pem connection.key = OpenSSL::PKey::RSA.new pem end store.set_default_paths cert_files.each do |ssl_cert_file| store.add_file ssl_cert_file end if Gem.configuration.ssl_ca_cert if File.directory? Gem.configuration.ssl_ca_cert store.add_path Gem.configuration.ssl_ca_cert else store.add_file Gem.configuration.ssl_ca_cert end end connection.cert_store = store connection rescue LoadError => e raise unless (e.respond_to?(:path) && e.path == 'openssl') || e. =~ / -- openssl$/ raise Gem::Exception.new( 'Unable to require openssl, install OpenSSL and rebuild ruby (preferred) or use non-HTTPS sources') end |
.create_with_proxy(uri, request_class, last_modified, proxy) ⇒ Object
Legacy. This is used in tests.
12 13 14 15 16 17 18 |
# File 'lib/rubygems/request.rb', line 12 def self.create_with_proxy uri, request_class, last_modified, proxy # :nodoc: cert_files = get_cert_files proxy ||= get_proxy_from_env(uri.scheme) pool = ConnectionPools.new proxy_uri(proxy), cert_files new(uri, request_class, last_modified, pool.pool_for(uri)) end |
.get_cert_files ⇒ Object
41 42 43 44 |
# File 'lib/rubygems/request.rb', line 41 def self.get_cert_files pattern = File.("./ssl_certs/*.pem", File.dirname(__FILE__)) Dir.glob(pattern) end |
.get_proxy_from_env(scheme = 'http') ⇒ Object
Returns a proxy URI for the given scheme if one is set in the environment variables.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/rubygems/request.rb', line 116 def self.get_proxy_from_env scheme = 'http' _scheme = scheme.downcase _SCHEME = scheme.upcase env_proxy = ENV["#{_scheme}_proxy"] || ENV["#{_SCHEME}_PROXY"] no_env_proxy = env_proxy.nil? || env_proxy.empty? return get_proxy_from_env 'http' if no_env_proxy and _scheme != 'http' return :no_proxy if no_env_proxy uri = URI(Gem::UriFormatter.new(env_proxy).normalize) if uri and uri.user.nil? and uri.password.nil? then user = ENV["#{_scheme}_proxy_user"] || ENV["#{_SCHEME}_PROXY_USER"] password = ENV["#{_scheme}_proxy_pass"] || ENV["#{_SCHEME}_PROXY_PASS"] uri.user = Gem::UriFormatter.new(user).escape uri.password = Gem::UriFormatter.new(password).escape end uri end |
.proxy_uri(proxy) ⇒ Object
:nodoc:
20 21 22 23 24 25 26 |
# File 'lib/rubygems/request.rb', line 20 def self.proxy_uri proxy # :nodoc: case proxy when :no_proxy then nil when URI::HTTP then proxy else URI.parse(proxy) end end |
Instance Method Details
#cert_files ⇒ Object
39 |
# File 'lib/rubygems/request.rb', line 39 def cert_files; @connection_pool.cert_files; end |
#connection_for(uri) ⇒ Object
Creates or an HTTP connection based on uri, or retrieves an existing connection, using a proxy if needed.
84 85 86 87 88 89 |
# File 'lib/rubygems/request.rb', line 84 def connection_for(uri) @connection_pool.checkout rescue defined?(OpenSSL::SSL) ? OpenSSL::SSL::SSLError : Errno::EHOSTDOWN, Errno::EHOSTDOWN => e raise Gem::RemoteFetcher::FetchError.new(e., uri) end |
#fetch {|request| ... } ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/rubygems/request.rb', line 91 def fetch request = @request_class.new @uri.request_uri unless @uri.nil? || @uri.user.nil? || @uri.user.empty? then request.basic_auth Gem::UriFormatter.new(@uri.user).unescape, Gem::UriFormatter.new(@uri.password).unescape end request.add_field 'User-Agent', @user_agent request.add_field 'Connection', 'keep-alive' request.add_field 'Keep-Alive', '30' if @last_modified then request.add_field 'If-Modified-Since', @last_modified.httpdate end yield request if block_given? perform_request request end |
#perform_request(request) ⇒ Object
:nodoc:
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 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/rubygems/request.rb', line 139 def perform_request request # :nodoc: connection = connection_for @uri retried = false bad_response = false begin @requests[connection.object_id] += 1 verbose "#{request.method} #{@uri}" file_name = File.basename(@uri.path) # perform download progress reporter only for gems if request.response_body_permitted? && file_name =~ /\.gem$/ reporter = ui.download_reporter response = connection.request(request) do |incomplete_response| if Net::HTTPOK === incomplete_response reporter.fetch(file_name, incomplete_response.content_length) downloaded = 0 data = '' incomplete_response.read_body do |segment| data << segment downloaded += segment.length reporter.update(downloaded) end reporter.done if incomplete_response.respond_to? :body= incomplete_response.body = data else incomplete_response.instance_variable_set(:@body, data) end end end else response = connection.request request end verbose "#{response.code} #{response.message}" rescue Net::HTTPBadResponse verbose "bad response" reset connection raise Gem::RemoteFetcher::FetchError.new('too many bad responses', @uri) if bad_response bad_response = true retry # HACK work around EOFError bug in Net::HTTP # NOTE Errno::ECONNABORTED raised a lot on Windows, and make impossible # to install gems. rescue EOFError, Timeout::Error, Errno::ECONNABORTED, Errno::ECONNRESET, Errno::EPIPE requests = @requests[connection.object_id] verbose "connection reset after #{requests} requests, retrying" raise Gem::RemoteFetcher::FetchError.new('too many connection resets', @uri) if retried reset connection retried = true retry end response ensure @connection_pool.checkin connection end |
#proxy_uri ⇒ Object
38 |
# File 'lib/rubygems/request.rb', line 38 def proxy_uri; @connection_pool.proxy_uri; end |
#reset(connection) ⇒ Object
Resets HTTP connection connection.
213 214 215 216 217 218 |
# File 'lib/rubygems/request.rb', line 213 def reset(connection) @requests.delete connection.object_id connection.finish connection.start end |
#user_agent ⇒ Object
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/rubygems/request.rb', line 220 def user_agent ua = "RubyGems/#{Gem::VERSION} #{Gem::Platform.local}" ruby_version = RUBY_VERSION ruby_version += 'dev' if RUBY_PATCHLEVEL == -1 ua << " Ruby/#{ruby_version} (#{RUBY_RELEASE_DATE}" if RUBY_PATCHLEVEL >= 0 then ua << " patchlevel #{RUBY_PATCHLEVEL}" elsif defined?(RUBY_REVISION) then ua << " revision #{RUBY_REVISION}" end ua << ")" ua << " #{RUBY_ENGINE}" if defined?(RUBY_ENGINE) and RUBY_ENGINE != 'ruby' ua end |