6868# #verify_callback :: For server certificate verification
6969# #verify_depth :: Depth of certificate verification
7070# #verify_mode :: How connections should be verified
71+ # #verify_hostname :: Use hostname verification for server certificate
72+ # during the handshake
7173#
7274# == Proxies
7375#
@@ -174,7 +176,7 @@ class Gem::Net::HTTP::Persistent
174176 ##
175177 # The version of Gem::Net::HTTP::Persistent you are using
176178
177- VERSION = '4.0.2 '
179+ VERSION = '4.0.4 '
178180
179181 ##
180182 # Error class for errors raised by Gem::Net::HTTP::Persistent. Various
@@ -449,6 +451,21 @@ def self.detect_idle_timeout uri, max = 10
449451
450452 attr_reader :verify_mode
451453
454+ ##
455+ # HTTPS verify_hostname.
456+ #
457+ # If a client sets this to true and enables SNI with SSLSocket#hostname=,
458+ # the hostname verification on the server certificate is performed
459+ # automatically during the handshake using
460+ # OpenSSL::SSL.verify_certificate_identity().
461+ #
462+ # You can set +verify_hostname+ as true to use hostname verification
463+ # during the handshake.
464+ #
465+ # NOTE: This works with Ruby > 3.0.
466+
467+ attr_reader :verify_hostname
468+
452469 ##
453470 # Creates a new Gem::Net::HTTP::Persistent.
454471 #
@@ -508,6 +525,7 @@ def initialize name: nil, proxy: nil, pool_size: DEFAULT_POOL_SIZE
508525 @verify_callback = nil
509526 @verify_depth = nil
510527 @verify_mode = nil
528+ @verify_hostname = nil
511529 @cert_store = nil
512530
513531 @generation = 0 # incremented when proxy Gem::URI changes
@@ -607,13 +625,23 @@ def connection_for uri
607625
608626 return yield connection
609627 rescue Errno ::ECONNREFUSED
610- address = http . proxy_address || http . address
611- port = http . proxy_port || http . port
628+ if http . proxy?
629+ address = http . proxy_address
630+ port = http . proxy_port
631+ else
632+ address = http . address
633+ port = http . port
634+ end
612635
613636 raise Error , "connection refused: #{ address } :#{ port } "
614637 rescue Errno ::EHOSTDOWN
615- address = http . proxy_address || http . address
616- port = http . proxy_port || http . port
638+ if http . proxy?
639+ address = http . proxy_address
640+ port = http . proxy_port
641+ else
642+ address = http . address
643+ port = http . port
644+ end
617645
618646 raise Error , "host down: #{ address } :#{ port } "
619647 ensure
@@ -948,8 +976,10 @@ def ssl connection
948976 connection . min_version = @min_version if @min_version
949977 connection . max_version = @max_version if @max_version
950978
951- connection . verify_depth = @verify_depth
952- connection . verify_mode = @verify_mode
979+ connection . verify_depth = @verify_depth
980+ connection . verify_mode = @verify_mode
981+ connection . verify_hostname = @verify_hostname if
982+ @verify_hostname != nil && connection . respond_to? ( :verify_hostname= )
953983
954984 if OpenSSL ::SSL ::VERIFY_PEER == OpenSSL ::SSL ::VERIFY_NONE and
955985 not Object . const_defined? ( :I_KNOW_THAT_OPENSSL_VERIFY_PEER_EQUALS_VERIFY_NONE_IS_WRONG ) then
@@ -1058,6 +1088,15 @@ def verify_mode= verify_mode
10581088 reconnect_ssl
10591089 end
10601090
1091+ ##
1092+ # Sets the HTTPS verify_hostname.
1093+
1094+ def verify_hostname = verify_hostname
1095+ @verify_hostname = verify_hostname
1096+
1097+ reconnect_ssl
1098+ end
1099+
10611100 ##
10621101 # SSL verification callback.
10631102
@@ -1070,4 +1109,3 @@ def verify_callback= callback
10701109
10711110require_relative 'persistent/connection'
10721111require_relative 'persistent/pool'
1073-
0 commit comments