Skip to content

Commit

Permalink
Optimize URI#hostname and URI#hostname=
Browse files Browse the repository at this point in the history
  • Loading branch information
lzap authored and jeremyevans committed Mar 4, 2021
1 parent 3bd2bcc commit 3b7ccfd
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/uri/generic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ def host=(v)
#
def hostname
v = self.host
/\A\[(.*)\]\z/ =~ v ? $1 : v
v&.start_with?('[') && v.end_with?(']') ? v[1..-2] : v
end

# Sets the host part of the URI as the argument with brackets for IPv6 addresses.
Expand All @@ -659,7 +659,7 @@ def hostname
# it is wrapped with brackets.
#
def hostname=(v)
v = "[#{v}]" if /\A\[.*\]\z/ !~ v && /:/ =~ v
v = "[#{v}]" if !(v&.start_with?('[') && v&.end_with?(']')) && v&.index(':')
self.host = v
end

Expand Down
4 changes: 4 additions & 0 deletions test/uri/test_generic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -799,8 +799,12 @@ def test_ipv6

u = URI("http://foo/bar")
assert_equal("http://foo/bar", u.to_s)
u.hostname = "[::1]"
assert_equal("http://[::1]/bar", u.to_s)
u.hostname = "::1"
assert_equal("http://[::1]/bar", u.to_s)
u.hostname = ""
assert_equal("http:///bar", u.to_s)
end

def test_build
Expand Down

0 comments on commit 3b7ccfd

Please sign in to comment.