diff options
| author | Taketo Takashima <t.taketo1113@gmail.com> | 2025-11-05 11:47:27 +0900 |
|---|---|---|
| committer | git <svn-admin@ruby-lang.org> | 2025-11-13 14:33:22 +0000 |
| commit | 2ab21f56f9c96485def7c0b40bebb1ec2a4113c4 (patch) | |
| tree | cf08bae36409ec46b2c1e00c2bc034149a3145e0 | |
| parent | 61500c6f48135ef018f5e496ff292a86b0043c65 (diff) | |
[ruby/net-http] 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
https://github.com/ruby/net-http/commit/3d4f06bd7f
Co-authored-by: 0x1eef <0x1eef@users.noreply.github.com>
Co-authored-by: Sorah Fukumori <sora134@gmail.com>
| -rw-r--r-- | lib/net/http/generic_request.rb | 9 | ||||
| -rw-r--r-- | lib/net/http/net-http.gemspec | 2 | ||||
| -rw-r--r-- | test/net/http/test_http_request.rb | 34 |
3 files changed, 37 insertions, 8 deletions
diff --git a/lib/net/http/generic_request.rb b/lib/net/http/generic_request.rb index d9a4c4fef0..5b01ea4abd 100644 --- a/lib/net/http/generic_request.rb +++ b/lib/net/http/generic_request.rb @@ -19,16 +19,13 @@ class Net::HTTPGenericRequest if URI === uri_or_path then raise ArgumentError, "not an HTTP URI" unless URI::HTTP === uri_or_path - hostname = uri_or_path.hostname + hostname = uri_or_path.host raise ArgumentError, "no host component for URI" unless (hostname && hostname.length > 0) @uri = uri_or_path.dup - host = @uri.hostname.dup - host << ":" << @uri.port.to_s if @uri.port != @uri.default_port @path = uri_or_path.request_uri raise ArgumentError, "no HTTP request path given" unless @path else @uri = nil - host = nil raise ArgumentError, "no HTTP request path given" unless uri_or_path raise ArgumentError, "HTTP request path is empty" if uri_or_path.empty? @path = uri_or_path.dup @@ -51,7 +48,7 @@ class Net::HTTPGenericRequest initialize_http_header initheader self['Accept'] ||= '*/*' self['User-Agent'] ||= 'Ruby' - self['Host'] ||= host if host + self['Host'] ||= @uri.authority if @uri @body = nil @body_stream = nil @body_data = nil @@ -245,7 +242,7 @@ class Net::HTTPGenericRequest end if host = self['host'] - host.sub!(/:.*/m, '') + host = URI.parse("//#{host}").host # Remove a port component from the existing Host header elsif host = @uri.host else host = addr diff --git a/lib/net/http/net-http.gemspec b/lib/net/http/net-http.gemspec index e4d26c9b3a..55f25e485e 100644 --- a/lib/net/http/net-http.gemspec +++ b/lib/net/http/net-http.gemspec @@ -35,5 +35,5 @@ Gem::Specification.new do |spec| spec.bindir = "exe" spec.require_paths = ["lib"] - spec.add_dependency "uri" + spec.add_dependency "uri", ">= 0.11.1" end diff --git a/test/net/http/test_http_request.rb b/test/net/http/test_http_request.rb index 7fd82b0353..9f5cf4f8f5 100644 --- a/test/net/http/test_http_request.rb +++ b/test/net/http/test_http_request.rb @@ -74,6 +74,18 @@ class HTTPRequestTest < Test::Unit::TestCase assert_equal "/foo", req.path assert_equal "example.com", req['Host'] + req = Net::HTTP::Get.new(URI("https://203.0.113.1/foo")) + assert_equal "/foo", req.path + assert_equal "203.0.113.1", req['Host'] + + req = Net::HTTP::Get.new(URI("https://203.0.113.1:8000/foo")) + assert_equal "/foo", req.path + assert_equal "203.0.113.1:8000", req['Host'] + + req = Net::HTTP::Get.new(URI("https://[2001:db8::1]:8000/foo")) + assert_equal "/foo", req.path + assert_equal "[2001:db8::1]:8000", req['Host'] + assert_raise(ArgumentError){ Net::HTTP::Get.new(URI("urn:ietf:rfc:7231")) } assert_raise(ArgumentError){ Net::HTTP::Get.new(URI("http://")) } end @@ -89,5 +101,25 @@ class HTTPRequestTest < Test::Unit::TestCase 'Bug #7831 - do not decode content if the user overrides' end if Net::HTTP::HAVE_ZLIB + def test_update_uri + req = Net::HTTP::Get.new(URI.parse("http://203.0.113.1")) + req.update_uri("test", 8080, false) + assert_equal "203.0.113.1", req.uri.host + assert_equal 8080, req.uri.port + + req = Net::HTTP::Get.new(URI.parse("http://203.0.113.1:2020")) + req.update_uri("test", 8080, false) + assert_equal "203.0.113.1", req.uri.host + assert_equal 8080, req.uri.port + + req = Net::HTTP::Get.new(URI.parse("http://[2001:db8::1]")) + req.update_uri("test", 8080, false) + assert_equal "[2001:db8::1]", req.uri.host + assert_equal 8080, req.uri.port + + req = Net::HTTP::Get.new(URI.parse("http://[2001:db8::1]:2020")) + req.update_uri("test", 8080, false) + assert_equal "[2001:db8::1]", req.uri.host + assert_equal 8080, req.uri.port + end end - |
