Skip to content

Commit 42692c2

Browse files
author
José Valim
committed
Merge pull request #1669 from brunoro/ipv6-uri
URI.parse supports IPv6 addresses
2 parents bcceccd + 2b34ae1 commit 42692c2

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

lib/elixir/lib/uri.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,12 @@ defmodule URI do
192192
# Split an authority into its userinfo, host and port parts.
193193
defp split_authority(s) do
194194
s = s || ""
195-
components = Regex.run %r/(^(.*)@)?([^:]*)(:(\d*))?/, s
195+
components = Regex.run %r/(^(.*)@)?(\[[a-zA-Z0-9:.]*\]|[^:]*)(:(\d*))?/, s
196+
196197
destructure [_, _, userinfo, host, _, port], nillify(components)
197198
port = if port, do: binary_to_integer(port)
199+
host = if host, do: host |> String.lstrip(?[) |> String.rstrip(?])
200+
198201
{ userinfo, host, port }
199202
end
200203

lib/elixir/test/elixir/uri_test.exs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,37 @@ defmodule URITest do
132132
assert URI.parse("https")
133133
end
134134

135+
test :ipv6_addresses do
136+
addrs = [
137+
"::", # undefined
138+
"::1", # loopback
139+
"1080::8:800:200C:417A", # unicast
140+
"FF01::101", # multicast
141+
"2607:f3f0:2:0:216:3cff:fef0:174a", # abbreviated
142+
"2607:f3F0:2:0:216:3cFf:Fef0:174A", # mixed hex case
143+
"2051:0db8:2d5a:3521:8313:ffad:1242:8e2e", # complete
144+
"::00:192.168.10.184" # embedded IPv4
145+
]
146+
147+
Enum.each addrs, fn(addr) ->
148+
simple_uri = URI.parse("http://[#{addr}]/")
149+
assert simple_uri.host == addr
150+
151+
userinfo_uri = URI.parse("http://user:pass@[#{addr}]/")
152+
assert userinfo_uri.host == addr
153+
assert userinfo_uri.userinfo == "user:pass"
154+
155+
port_uri = URI.parse("http://[#{addr}]:2222/")
156+
assert port_uri.host == addr
157+
assert port_uri.port == 2222
158+
159+
userinfo_port_uri = URI.parse("http://user:pass@[#{addr}]:2222/")
160+
assert userinfo_port_uri.host == addr
161+
assert userinfo_port_uri.userinfo == "user:pass"
162+
assert userinfo_port_uri.port == 2222
163+
end
164+
end
165+
135166
test :downcase_scheme do
136167
assert URI.parse("hTtP://google.com").scheme == "http"
137168
end

0 commit comments

Comments
 (0)