Skip to content
Open
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
15 changes: 9 additions & 6 deletions lib/net/http/generic_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@ 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
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
@host = @uri.host.dup
@port = @uri.port 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
@host = nil
@port = 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
Expand All @@ -51,7 +52,9 @@ def initialize(m, reqbody, resbody, uri_or_path, initheader = nil) # :nodoc:
initialize_http_header initheader
self['Accept'] ||= '*/*'
self['User-Agent'] ||= 'Ruby'
self['Host'] ||= host if host
host = @host.dup
host << ":" << @port.to_s if @port
self['Host'] ||= "#{host}" if host
@body = nil
@body_stream = nil
@body_data = nil
Expand Down Expand Up @@ -245,7 +248,7 @@ def update_uri(addr, port, ssl) # :nodoc: internal use only
end

if host = self['host']
host.sub!(/:.*/m, '')
host = URI.parse("#{scheme}://#{host}").host
elsif host = @uri.host
else
host = addr
Expand Down
34 changes: 33 additions & 1 deletion test/net/http/test_http_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ def test_initialize_GET_uri
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
Expand All @@ -89,5 +101,25 @@ def test_header_set
'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