2016-02-01 15:43:26 +03:00
|
|
|
# frozen_string_literal: true
|
2013-07-10 03:21:36 +04:00
|
|
|
require "net/http"
|
2019-04-22 14:56:16 +03:00
|
|
|
require_relative "user_interaction"
|
2013-07-10 03:21:36 +04:00
|
|
|
|
|
|
|
class Gem::Request
|
2016-10-30 09:32:48 +03:00
|
|
|
extend Gem::UserInteraction
|
2013-07-10 03:21:36 +04:00
|
|
|
include Gem::UserInteraction
|
|
|
|
|
2014-09-14 07:30:02 +04:00
|
|
|
###
|
|
|
|
# Legacy. This is used in tests.
|
2018-11-21 13:20:47 +03:00
|
|
|
def self.create_with_proxy(uri, request_class, last_modified, proxy) # :nodoc:
|
2014-09-14 07:30:02 +04:00
|
|
|
cert_files = get_cert_files
|
|
|
|
proxy ||= get_proxy_from_env(uri.scheme)
|
2019-02-14 15:59:03 +03:00
|
|
|
pool = ConnectionPools.new proxy_uri(proxy), cert_files
|
2014-09-14 07:30:02 +04:00
|
|
|
|
|
|
|
new(uri, request_class, last_modified, pool.pool_for(uri))
|
|
|
|
end
|
|
|
|
|
2018-11-21 13:20:47 +03:00
|
|
|
def self.proxy_uri(proxy) # :nodoc:
|
2019-12-13 14:19:08 +03:00
|
|
|
require "uri"
|
2014-09-14 07:30:02 +04:00
|
|
|
case proxy
|
|
|
|
when :no_proxy then nil
|
|
|
|
when URI::HTTP then proxy
|
|
|
|
else URI.parse(proxy)
|
|
|
|
end
|
|
|
|
end
|
2013-07-10 03:21:36 +04:00
|
|
|
|
2014-09-14 07:30:02 +04:00
|
|
|
def initialize(uri, request_class, last_modified, pool)
|
2013-07-10 03:21:36 +04:00
|
|
|
@uri = uri
|
|
|
|
@request_class = request_class
|
|
|
|
@last_modified = last_modified
|
|
|
|
@requests = Hash.new 0
|
|
|
|
@user_agent = user_agent
|
|
|
|
|
2014-09-14 07:30:02 +04:00
|
|
|
@connection_pool = pool
|
2013-07-10 03:21:36 +04:00
|
|
|
end
|
|
|
|
|
2023-03-16 07:28:31 +03:00
|
|
|
def proxy_uri
|
|
|
|
@connection_pool.proxy_uri
|
|
|
|
end
|
|
|
|
|
|
|
|
def cert_files
|
|
|
|
@connection_pool.cert_files
|
|
|
|
end
|
2014-07-25 11:55:42 +04:00
|
|
|
|
2014-09-14 07:30:02 +04:00
|
|
|
def self.get_cert_files
|
2022-04-01 14:36:59 +03:00
|
|
|
pattern = File.expand_path("./ssl_certs/*/*.pem", __dir__)
|
2014-09-14 07:30:02 +04:00
|
|
|
Dir.glob(pattern)
|
2013-07-10 03:21:36 +04:00
|
|
|
end
|
|
|
|
|
2014-09-14 07:30:02 +04:00
|
|
|
def self.configure_connection_for_https(connection, cert_files)
|
2021-05-28 13:47:49 +03:00
|
|
|
raise Gem::Exception.new("OpenSSL is not available. Install OpenSSL and rebuild Ruby (preferred) or use non-HTTPS sources") unless Gem::HAVE_OPENSSL
|
2020-12-08 10:33:39 +03:00
|
|
|
|
2013-07-10 03:21:36 +04:00
|
|
|
connection.use_ssl = true
|
|
|
|
connection.verify_mode =
|
|
|
|
Gem.configuration.ssl_verify_mode || OpenSSL::SSL::VERIFY_PEER
|
|
|
|
store = OpenSSL::X509::Store.new
|
|
|
|
|
2018-11-21 13:20:47 +03:00
|
|
|
if Gem.configuration.ssl_client_cert
|
2013-07-10 03:21:36 +04:00
|
|
|
pem = File.read Gem.configuration.ssl_client_cert
|
|
|
|
connection.cert = OpenSSL::X509::Certificate.new pem
|
|
|
|
connection.key = OpenSSL::PKey::RSA.new pem
|
|
|
|
end
|
|
|
|
|
2014-02-04 04:48:31 +04:00
|
|
|
store.set_default_paths
|
2014-09-14 07:30:02 +04:00
|
|
|
cert_files.each do |ssl_cert_file|
|
|
|
|
store.add_file ssl_cert_file
|
|
|
|
end
|
2013-07-10 03:21:36 +04:00
|
|
|
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
|
2016-10-30 09:32:48 +03:00
|
|
|
|
|
|
|
connection.verify_callback = proc do |preverify_ok, store_context|
|
|
|
|
verify_certificate store_context unless preverify_ok
|
|
|
|
|
|
|
|
preverify_ok
|
|
|
|
end
|
|
|
|
|
2014-09-14 07:30:02 +04:00
|
|
|
connection
|
2013-07-10 03:21:36 +04:00
|
|
|
end
|
|
|
|
|
2018-11-21 13:20:47 +03:00
|
|
|
def self.verify_certificate(store_context)
|
2016-10-30 09:32:48 +03:00
|
|
|
depth = store_context.error_depth
|
|
|
|
error = store_context.error_string
|
|
|
|
number = store_context.error
|
|
|
|
cert = store_context.current_cert
|
|
|
|
|
|
|
|
ui.alert_error "SSL verification error at depth #{depth}: #{error} (#{number})"
|
|
|
|
|
|
|
|
extra_message = verify_certificate_message number, cert
|
|
|
|
|
|
|
|
ui.alert_error extra_message if extra_message
|
|
|
|
end
|
|
|
|
|
2018-11-21 13:20:47 +03:00
|
|
|
def self.verify_certificate_message(error_number, cert)
|
2016-10-30 09:32:48 +03:00
|
|
|
return unless cert
|
|
|
|
case error_number
|
|
|
|
when OpenSSL::X509::V_ERR_CERT_HAS_EXPIRED then
|
2021-10-10 14:28:14 +03:00
|
|
|
require "time"
|
2016-10-30 09:32:48 +03:00
|
|
|
"Certificate #{cert.subject} expired at #{cert.not_after.iso8601}"
|
|
|
|
when OpenSSL::X509::V_ERR_CERT_NOT_YET_VALID then
|
2021-10-10 14:28:14 +03:00
|
|
|
require "time"
|
2016-10-30 09:32:48 +03:00
|
|
|
"Certificate #{cert.subject} not valid until #{cert.not_before.iso8601}"
|
|
|
|
when OpenSSL::X509::V_ERR_CERT_REJECTED then
|
|
|
|
"Certificate #{cert.subject} is rejected"
|
|
|
|
when OpenSSL::X509::V_ERR_CERT_UNTRUSTED then
|
|
|
|
"Certificate #{cert.subject} is not trusted"
|
|
|
|
when OpenSSL::X509::V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT then
|
|
|
|
"Certificate #{cert.issuer} is not trusted"
|
|
|
|
when OpenSSL::X509::V_ERR_INVALID_CA then
|
|
|
|
"Certificate #{cert.subject} is an invalid CA certificate"
|
|
|
|
when OpenSSL::X509::V_ERR_INVALID_PURPOSE then
|
|
|
|
"Certificate #{cert.subject} has an invalid purpose"
|
|
|
|
when OpenSSL::X509::V_ERR_SELF_SIGNED_CERT_IN_CHAIN then
|
|
|
|
"Root certificate is not trusted (#{cert.subject})"
|
2018-11-03 15:31:51 +03:00
|
|
|
when OpenSSL::X509::V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY then
|
2016-10-30 09:32:48 +03:00
|
|
|
"You must add #{cert.issuer} to your local trusted store"
|
2018-11-03 15:31:51 +03:00
|
|
|
when
|
|
|
|
OpenSSL::X509::V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE then
|
|
|
|
"Cannot verify certificate issued by #{cert.issuer}"
|
2016-10-30 09:32:48 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-10 03:21:36 +04:00
|
|
|
##
|
|
|
|
# Creates or an HTTP connection based on +uri+, or retrieves an existing
|
|
|
|
# connection, using a proxy if needed.
|
|
|
|
|
|
|
|
def connection_for(uri)
|
2014-09-14 07:30:02 +04:00
|
|
|
@connection_pool.checkout
|
2020-12-08 10:33:39 +03:00
|
|
|
rescue Gem::HAVE_OPENSSL ? OpenSSL::SSL::SSLError : Errno::EHOSTDOWN,
|
2013-07-10 03:21:36 +04:00
|
|
|
Errno::EHOSTDOWN => e
|
2021-08-22 13:06:02 +03:00
|
|
|
raise Gem::RemoteFetcher::FetchError.new(e.message, uri)
|
2013-07-10 03:21:36 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def fetch
|
|
|
|
request = @request_class.new @uri.request_uri
|
|
|
|
|
2018-11-21 13:20:47 +03:00
|
|
|
unless @uri.nil? || @uri.user.nil? || @uri.user.empty?
|
2014-02-04 04:48:31 +04:00
|
|
|
request.basic_auth Gem::UriFormatter.new(@uri.user).unescape,
|
|
|
|
Gem::UriFormatter.new(@uri.password).unescape
|
2013-07-10 03:21:36 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
request.add_field "User-Agent", @user_agent
|
|
|
|
request.add_field "Connection", "keep-alive"
|
|
|
|
request.add_field "Keep-Alive", "30"
|
|
|
|
|
2018-11-21 13:20:47 +03:00
|
|
|
if @last_modified
|
2020-12-08 10:33:39 +03:00
|
|
|
require "time"
|
2013-10-16 04:14:16 +04:00
|
|
|
request.add_field "If-Modified-Since", @last_modified.httpdate
|
2013-07-10 03:21:36 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
yield request if block_given?
|
|
|
|
|
2014-09-14 07:30:02 +04:00
|
|
|
perform_request request
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Returns a proxy URI for the given +scheme+ if one is set in the
|
|
|
|
# environment variables.
|
|
|
|
|
2018-11-21 13:20:47 +03:00
|
|
|
def self.get_proxy_from_env(scheme = "http")
|
2023-03-22 06:47:40 +03:00
|
|
|
downcase_scheme = scheme.downcase
|
|
|
|
upcase_scheme = scheme.upcase
|
|
|
|
env_proxy = ENV["#{downcase_scheme}_proxy"] || ENV["#{upcase_scheme}_PROXY"]
|
2014-09-14 07:30:02 +04:00
|
|
|
|
|
|
|
no_env_proxy = env_proxy.nil? || env_proxy.empty?
|
|
|
|
|
2019-02-14 15:59:03 +03:00
|
|
|
if no_env_proxy
|
2023-03-22 07:14:27 +03:00
|
|
|
return ["https", "http"].include?(downcase_scheme) ? :no_proxy : get_proxy_from_env("http")
|
2019-02-14 15:59:03 +03:00
|
|
|
end
|
2014-09-14 07:30:02 +04:00
|
|
|
|
2019-12-13 14:19:08 +03:00
|
|
|
require "uri"
|
2014-09-14 07:30:02 +04:00
|
|
|
uri = URI(Gem::UriFormatter.new(env_proxy).normalize)
|
|
|
|
|
2022-08-09 05:16:07 +03:00
|
|
|
if uri && uri.user.nil? && uri.password.nil?
|
2023-03-22 06:47:40 +03:00
|
|
|
user = ENV["#{downcase_scheme}_proxy_user"] || ENV["#{upcase_scheme}_PROXY_USER"]
|
|
|
|
password = ENV["#{downcase_scheme}_proxy_pass"] || ENV["#{upcase_scheme}_PROXY_PASS"]
|
2014-09-14 07:30:02 +04:00
|
|
|
|
|
|
|
uri.user = Gem::UriFormatter.new(user).escape
|
|
|
|
uri.password = Gem::UriFormatter.new(password).escape
|
|
|
|
end
|
|
|
|
|
|
|
|
uri
|
|
|
|
end
|
|
|
|
|
2018-11-21 13:20:47 +03:00
|
|
|
def perform_request(request) # :nodoc:
|
2013-07-10 03:21:36 +04:00
|
|
|
connection = connection_for @uri
|
|
|
|
|
|
|
|
retried = false
|
|
|
|
bad_response = false
|
|
|
|
|
|
|
|
begin
|
|
|
|
@requests[connection.object_id] += 1
|
|
|
|
|
2022-06-23 12:22:36 +03:00
|
|
|
verbose "#{request.method} #{Gem::Uri.redact(@uri)}"
|
2013-07-10 03:21:36 +04:00
|
|
|
|
|
|
|
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
|
2016-02-01 15:43:26 +03:00
|
|
|
data = String.new
|
2013-07-10 03:21:36 +04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2014-09-14 07:30:02 +04:00
|
|
|
verbose "#{response.code} #{response.message}"
|
2013-07-10 03:21:36 +04:00
|
|
|
rescue Net::HTTPBadResponse
|
2014-09-14 07:30:02 +04:00
|
|
|
verbose "bad response"
|
2013-07-10 03:21:36 +04:00
|
|
|
|
|
|
|
reset connection
|
|
|
|
|
2021-08-22 13:06:02 +03:00
|
|
|
raise Gem::RemoteFetcher::FetchError.new("too many bad responses", @uri) if bad_response
|
2013-07-10 03:21:36 +04:00
|
|
|
|
|
|
|
bad_response = true
|
|
|
|
retry
|
2015-11-19 09:16:19 +03:00
|
|
|
rescue Net::HTTPFatalError
|
|
|
|
verbose "fatal error"
|
|
|
|
|
2021-08-22 13:06:02 +03:00
|
|
|
raise Gem::RemoteFetcher::FetchError.new("fatal error", @uri)
|
2023-03-16 07:36:06 +03:00
|
|
|
# HACK: work around EOFError bug in Net::HTTP
|
2013-07-10 03:21:36 +04:00
|
|
|
# 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]
|
2014-09-14 07:30:02 +04:00
|
|
|
verbose "connection reset after #{requests} requests, retrying"
|
2013-07-10 03:21:36 +04:00
|
|
|
|
2021-08-22 13:06:02 +03:00
|
|
|
raise Gem::RemoteFetcher::FetchError.new("too many connection resets", @uri) if retried
|
2013-07-10 03:21:36 +04:00
|
|
|
|
|
|
|
reset connection
|
|
|
|
|
|
|
|
retried = true
|
|
|
|
retry
|
|
|
|
end
|
|
|
|
|
|
|
|
response
|
2014-09-14 07:30:02 +04:00
|
|
|
ensure
|
|
|
|
@connection_pool.checkin connection
|
2013-07-10 03:21:36 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Resets HTTP connection +connection+.
|
|
|
|
|
|
|
|
def reset(connection)
|
|
|
|
@requests.delete connection.object_id
|
|
|
|
|
|
|
|
connection.finish
|
|
|
|
connection.start
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_agent
|
2016-02-01 15:43:26 +03:00
|
|
|
ua = "RubyGems/#{Gem::VERSION} #{Gem::Platform.local}".dup
|
2013-07-10 03:21:36 +04:00
|
|
|
|
|
|
|
ruby_version = RUBY_VERSION
|
|
|
|
ruby_version += "dev" if RUBY_PATCHLEVEL == -1
|
|
|
|
|
|
|
|
ua << " Ruby/#{ruby_version} (#{RUBY_RELEASE_DATE}"
|
2018-11-21 13:20:47 +03:00
|
|
|
if RUBY_PATCHLEVEL >= 0
|
2013-07-10 03:21:36 +04:00
|
|
|
ua << " patchlevel #{RUBY_PATCHLEVEL}"
|
2023-03-16 09:09:24 +03:00
|
|
|
else
|
2013-07-10 03:21:36 +04:00
|
|
|
ua << " revision #{RUBY_REVISION}"
|
|
|
|
end
|
|
|
|
ua << ")"
|
|
|
|
|
2019-08-04 15:39:55 +03:00
|
|
|
ua << " #{RUBY_ENGINE}" if RUBY_ENGINE != "ruby"
|
2013-07-10 03:21:36 +04:00
|
|
|
|
|
|
|
ua
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-22 14:56:16 +03:00
|
|
|
require_relative "request/http_pool"
|
|
|
|
require_relative "request/https_pool"
|
|
|
|
require_relative "request/connection_pools"
|