Skip to content

Commit 0c4cc6c

Browse files
tfiedlerdejanzejosevalim
authored andcommitted
Translate :undefined URI port to nil (#13464)
Resolves #13462 When a url string is schema less and the host is followed by a colon without setting the actual port, `:uri_string.parse/1` returns the port to be `:undefined`. As long as the url string is parseable, we should translate `:undefined` to `nil`, in order to ensure we return a valid URI struct.
1 parent a5b730a commit 0c4cc6c

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

lib/elixir/lib/uri.ex

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -648,16 +648,16 @@ defmodule URI do
648648
scheme = String.downcase(scheme, :ascii)
649649

650650
case map do
651-
%{port: port} when port != :undefined ->
651+
%{port: port} when is_integer(port) ->
652652
%{uri | scheme: scheme}
653653

654654
%{} ->
655-
case default_port(scheme) do
656-
nil -> %{uri | scheme: scheme}
657-
port -> %{uri | scheme: scheme, port: port}
658-
end
655+
%{uri | scheme: scheme, port: default_port(scheme)}
659656
end
660657

658+
%{port: :undefined} ->
659+
%{uri | port: nil}
660+
661661
%{} ->
662662
uri
663663
end

lib/elixir/test/elixir/uri_test.exs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,32 @@ defmodule URITest do
277277
test "preserves an empty query" do
278278
assert URI.new!("http://foo.com/?").query == ""
279279
end
280+
281+
test "without scheme, undefined port after host translates to nil" do
282+
assert URI.new!("//https://www.example.com") ==
283+
%URI{
284+
scheme: nil,
285+
userinfo: nil,
286+
host: "https",
287+
port: nil,
288+
path: "//www.example.com",
289+
query: nil,
290+
fragment: nil
291+
}
292+
end
293+
294+
test "with scheme, undefined port after host translates to nil" do
295+
assert URI.new!("myscheme://myhost:/path/info") ==
296+
%URI{
297+
scheme: "myscheme",
298+
userinfo: nil,
299+
host: "myhost",
300+
port: nil,
301+
path: "/path/info",
302+
query: nil,
303+
fragment: nil
304+
}
305+
end
280306
end
281307

282308
test "http://http://http://@http://http://?http://#http://" do

0 commit comments

Comments
 (0)