Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/net/http/generic_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ def initialize(m, reqbody, resbody, uri_or_path, initheader = nil) # :nodoc:

if URI === uri_or_path then
raise ArgumentError, "not an HTTP URI" unless URI::HTTP === uri_or_path
hostname = uri_or_path.hostname
raise ArgumentError, "no host component for URI" unless (hostname && hostname.length > 0)
host = uri_or_path.host
raise ArgumentError, "no host component for URI" unless (host && host.length > 0)
@uri = uri_or_path.dup
host = @uri.hostname.dup
host = host.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
Expand Down Expand Up @@ -220,7 +220,7 @@ def update_uri(addr, port, ssl) # :nodoc: internal use only
end

if host = self['host']
host.sub!(/:.*/m, '')
host.sub!(/:\d+\Z/, '')
elsif host = @uri.host
else
host = addr
Expand Down
22 changes: 22 additions & 0 deletions test/net/http/test_http_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,27 @@ def test_header_set
'Bug #7831 - do not decode content if the user overrides'
end if Net::HTTP::HAVE_ZLIB

def test_ipv6_address_as_host_header
req = Net::HTTP::Get.new(URI.parse("http://[::1]"))
assert_equal "[::1]", req["host"]
end

def test_ipv6_address_as_host_header_with_port
req = Net::HTTP::Get.new(URI.parse("http://[::1]:2020"))
assert_equal "[::1]:2020", req["host"]
end

def test_ipv6_address_on_update_uri
req = Net::HTTP::Get.new(URI.parse("http://[::1]"))
req.send(:update_uri, "[::1]", 80, false)
assert_equal "[::1]", req.instance_variable_get(:@uri).host
end

def test_ipv6_address_with_port_on_update_uri
req = Net::HTTP::Get.new(URI.parse("http://[::1]:7777"))
req.send(:update_uri, "[::1]", 2020, false)
assert_equal "[::1]", req.instance_variable_get(:@uri).host
assert_equal 2020, req.instance_variable_get(:@uri).port
end
end