Skip to content

Commit 43ffb28

Browse files
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 `:undefiend` to `nil`, in order to ensure we return a valid URI struct.
1 parent e3b6a91 commit 43ffb28

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

lib/elixir/lib/uri.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,9 @@ defmodule URI do
666666
end
667667
end
668668

669+
%{port: :undefined} ->
670+
%{uri | port: nil}
671+
669672
%{} ->
670673
uri
671674
end

lib/elixir/test/elixir/uri_test.exs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,20 @@ defmodule URITest do
9797
assert URI.new("") == {:ok, %URI{}}
9898
end
9999

100+
test "missing port part after host never resolves to :undefined" do
101+
assert URI.new("//https://www.example.com") ==
102+
{:ok,
103+
%URI{
104+
scheme: nil,
105+
userinfo: nil,
106+
host: "https",
107+
port: nil,
108+
path: "//www.example.com",
109+
query: nil,
110+
fragment: nil
111+
}}
112+
end
113+
100114
test "errors on bad URIs" do
101115
assert URI.new("/>") == {:error, ">"}
102116
assert URI.new(":https") == {:error, ":"}

0 commit comments

Comments
 (0)