Skip to content

Commit 9155b59

Browse files
taketo11130x1eefsorah
committed
Fix handling of IPv6 literal hosts in Net::HTTPGenericRequest
Update uri dependency to version 0.11.0 or later to use `URI::HTTP#authority` and `URI#parse` without scheme Co-authored-by: 0x1eef <[email protected]> Co-authored-by: Sorah Fukumori <[email protected]>
1 parent bf081a2 commit 9155b59

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

lib/net/http/generic_request.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,13 @@ def initialize(m, reqbody, resbody, uri_or_path, initheader = nil) # :nodoc:
1919

2020
if URI === uri_or_path then
2121
raise ArgumentError, "not an HTTP URI" unless URI::HTTP === uri_or_path
22-
hostname = uri_or_path.hostname
22+
hostname = uri_or_path.host
2323
raise ArgumentError, "no host component for URI" unless (hostname && hostname.length > 0)
2424
@uri = uri_or_path.dup
25-
host = @uri.hostname.dup
26-
host << ":" << @uri.port.to_s if @uri.port != @uri.default_port
2725
@path = uri_or_path.request_uri
2826
raise ArgumentError, "no HTTP request path given" unless @path
2927
else
3028
@uri = nil
31-
host = nil
3229
raise ArgumentError, "no HTTP request path given" unless uri_or_path
3330
raise ArgumentError, "HTTP request path is empty" if uri_or_path.empty?
3431
@path = uri_or_path.dup
@@ -51,7 +48,7 @@ def initialize(m, reqbody, resbody, uri_or_path, initheader = nil) # :nodoc:
5148
initialize_http_header initheader
5249
self['Accept'] ||= '*/*'
5350
self['User-Agent'] ||= 'Ruby'
54-
self['Host'] ||= host if host
51+
self['Host'] ||= @uri.authority if @uri
5552
@body = nil
5653
@body_stream = nil
5754
@body_data = nil
@@ -245,7 +242,7 @@ def update_uri(addr, port, ssl) # :nodoc: internal use only
245242
end
246243

247244
if host = self['host']
248-
host.sub!(/:.*/m, '')
245+
host = URI.parse("//#{host}").host # Remove a port component from the existing Host header
249246
elsif host = @uri.host
250247
else
251248
host = addr

net-http.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@ Gem::Specification.new do |spec|
3535
spec.bindir = "exe"
3636
spec.require_paths = ["lib"]
3737

38-
spec.add_dependency "uri"
38+
spec.add_dependency "uri", "> 0.11.0"
3939
end

test/net/http/test_http_request.rb

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,18 @@ def test_initialize_GET_uri
7474
assert_equal "/foo", req.path
7575
assert_equal "example.com", req['Host']
7676

77+
req = Net::HTTP::Get.new(URI("https://203.0.113.1/foo"))
78+
assert_equal "/foo", req.path
79+
assert_equal "203.0.113.1", req['Host']
80+
81+
req = Net::HTTP::Get.new(URI("https://203.0.113.1:8000/foo"))
82+
assert_equal "/foo", req.path
83+
assert_equal "203.0.113.1:8000", req['Host']
84+
85+
req = Net::HTTP::Get.new(URI("https://[2001:db8::1]:8000/foo"))
86+
assert_equal "/foo", req.path
87+
assert_equal "[2001:db8::1]:8000", req['Host']
88+
7789
assert_raise(ArgumentError){ Net::HTTP::Get.new(URI("urn:ietf:rfc:7231")) }
7890
assert_raise(ArgumentError){ Net::HTTP::Get.new(URI("http://")) }
7991
end
@@ -89,5 +101,25 @@ def test_header_set
89101
'Bug #7831 - do not decode content if the user overrides'
90102
end if Net::HTTP::HAVE_ZLIB
91103

104+
def test_update_uri
105+
req = Net::HTTP::Get.new(URI.parse("http://203.0.113.1"))
106+
req.update_uri("test", 8080, false)
107+
assert_equal "203.0.113.1", req.uri.host
108+
assert_equal 8080, req.uri.port
109+
110+
req = Net::HTTP::Get.new(URI.parse("http://203.0.113.1:2020"))
111+
req.update_uri("test", 8080, false)
112+
assert_equal "203.0.113.1", req.uri.host
113+
assert_equal 8080, req.uri.port
114+
115+
req = Net::HTTP::Get.new(URI.parse("http://[2001:db8::1]"))
116+
req.update_uri("test", 8080, false)
117+
assert_equal "[2001:db8::1]", req.uri.host
118+
assert_equal 8080, req.uri.port
119+
120+
req = Net::HTTP::Get.new(URI.parse("http://[2001:db8::1]:2020"))
121+
req.update_uri("test", 8080, false)
122+
assert_equal "[2001:db8::1]", req.uri.host
123+
assert_equal 8080, req.uri.port
124+
end
92125
end
93-

0 commit comments

Comments
 (0)