Skip to content

Commit 53bf241

Browse files
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 2415133 commit 53bf241

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

lib/elixir/lib/uri.ex

+5-5
Original file line numberDiff line numberDiff line change
@@ -656,16 +656,16 @@ defmodule URI do
656656
scheme = String.downcase(scheme, :ascii)
657657

658658
case map do
659-
%{port: port} when port != :undefined ->
659+
%{port: port} when is_integer(port) ->
660660
%{uri | scheme: scheme}
661661

662662
%{} ->
663-
case default_port(scheme) do
664-
nil -> %{uri | scheme: scheme}
665-
port -> %{uri | scheme: scheme, port: port}
666-
end
663+
%{uri | scheme: scheme, port: default_port(scheme)}
667664
end
668665

666+
%{port: :undefined} ->
667+
%{uri | port: nil}
668+
669669
%{} ->
670670
uri
671671
end

lib/elixir/test/elixir/uri_test.exs

+26
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)