Skip to content

Commit 3fc07ef

Browse files
author
José Valim
committed
Merge pull request #2636 from voltone/uri-recomposition
Improved RFC compliance for URI to_string
2 parents f8647da + aa244c3 commit 3fc07ef

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

lib/elixir/lib/uri.ex

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,12 +349,20 @@ defimpl String.Chars, for: URI do
349349
if uri.port == port, do: uri = %{uri | port: nil}
350350
end
351351

352+
# Based on http://tools.ietf.org/html/rfc3986#section-5.3
353+
354+
if uri.host do
355+
authority = uri.host
356+
if uri.userinfo, do: authority = uri.userinfo <> "@" <> authority
357+
if uri.port, do: authority = authority <> ":" <> Integer.to_string(uri.port)
358+
else
359+
authority = uri.authority
360+
end
361+
352362
result = ""
353363

354-
if uri.scheme, do: result = result <> uri.scheme <> "://"
355-
if uri.userinfo, do: result = result <> uri.userinfo <> "@"
356-
if uri.host, do: result = result <> uri.host
357-
if uri.port, do: result = result <> ":" <> Integer.to_string(uri.port)
364+
if uri.scheme, do: result = result <> uri.scheme <> ":"
365+
if authority, do: result = result <> "//" <> authority
358366
if uri.path, do: result = result <> uri.path
359367
if uri.query, do: result = result <> "?" <> uri.query
360368
if uri.fragment, do: result = result <> "#" <> uri.fragment

lib/elixir/test/elixir/uri_test.exs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,5 +198,8 @@ defmodule URITest do
198198
assert to_string(URI.parse("http://google.com/elixir")) == "http://google.com/elixir"
199199
assert to_string(URI.parse("http://google.com?q=lol")) == "http://google.com?q=lol"
200200
assert to_string(URI.parse("http://google.com?q=lol#omg")) == "http://google.com?q=lol#omg"
201+
assert to_string(URI.parse("//google.com/elixir")) == "//google.com/elixir"
202+
assert to_string(URI.parse("//google.com:8080/elixir")) == "//google.com:8080/elixir"
203+
assert to_string(URI.parse("//user:[email protected]/")) == "//user:[email protected]/"
201204
end
202205
end

0 commit comments

Comments
 (0)