Skip to content

Commit 7463571

Browse files
authored
Merge pull request #355 from ruby/claude/exciting-davinci-ba31c6
Stop loading resolv from net/http
2 parents ccaf6d9 + 5b42d75 commit 7463571

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

‎lib/net/http.rb‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
require 'net/protocol'
2424
require 'uri'
25-
require 'resolv'
2625
autoload :OpenSSL, 'openssl'
2726

2827
module Net #:nodoc:
@@ -1539,6 +1538,29 @@ def use_ssl=(flag)
15391538

15401539
SSL_IVNAMES = SSL_ATTRIBUTES.map { |a| "@#{a}".to_sym }.freeze # :nodoc:
15411540

1541+
# Resolv::IPv4::Regex and Resolv::IPv6::Regex, copied from resolv 0.7.1 rather
1542+
# than required, so that net/http does not load a DNS resolver just to keep an
1543+
# IP address literal out of the Server Name Indication.
1544+
# https://github.com/ruby/resolv/blob/v0.7.1/lib/resolv.rb
1545+
ipv4_octet = /0|1(?:[0-9][0-9]?)?|2(?:[0-4][0-9]?|5[0-5]?|[6-9])?|[3-9][0-9]?/
1546+
hex16 = /[0-9A-Fa-f]{1,4}/
1547+
hex16_group = /(?:#{hex16}(?::#{hex16})*)?/
1548+
dotted_quad = /\d+\.\d+\.\d+\.\d+/
1549+
zone_id = /%[-0-9A-Za-z._~]+/
1550+
1551+
IPV4_ADDRESS = /\A(?:#{ipv4_octet})\.(?:#{ipv4_octet})\.(?:#{ipv4_octet})\.(?:#{ipv4_octet})\z/ # :nodoc:
1552+
1553+
IPV6_ADDRESS = /\A(?:
1554+
(?:#{hex16}:){7}#{hex16} # a:b:c:d:e:f:g:h
1555+
| #{hex16_group}::#{hex16_group} # a::b
1556+
| (?:#{hex16}:){6}#{dotted_quad} # a:b:c:d:e:f:w.x.y.z
1557+
| #{hex16_group}::(?:#{hex16}:)*#{dotted_quad} # a::b:w.x.y.z
1558+
| [Ff][Ee]80(?::#{hex16}){7}#{zone_id} # fe80:b:c:d:e:f:g:h%em1
1559+
| [Ff][Ee]80:(?:#{hex16_group}::#{hex16_group}|:#{hex16_group})?:#{hex16}#{zone_id} # fe80::b%em1
1560+
)\z/x # :nodoc:
1561+
1562+
private_constant :IPV4_ADDRESS, :IPV6_ADDRESS
1563+
15421564
# Sets or returns the path to a CA certification file in PEM format.
15431565
attr_accessor :ca_file
15441566

@@ -1738,7 +1760,7 @@ def connect
17381760

17391761
# Server Name Indication (SNI) RFC 3546/6066
17401762
case @address
1741-
when Resolv::IPv4::Regex, Resolv::IPv6::Regex
1763+
when IPV4_ADDRESS, IPV6_ADDRESS
17421764
# don't set SNI, as IP addresses in SNI is not valid
17431765
# per RFC 6066, section 3.
17441766

‎test/net/http/test_http.rb‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,13 @@ def test_default_configuration
266266
Net::HTTP.default_configuration = nil
267267
end
268268

269+
def test_require_does_not_load_resolv
270+
assert_separately([], <<~'RUBY')
271+
require 'net/http'
272+
assert_nil defined?(Resolv)
273+
RUBY
274+
end
275+
269276
end
270277

271278
module TestNetHTTP_version_1_1_methods

0 commit comments

Comments
 (0)