Skip to content

Commit 1385bde

Browse files
author
José Valim
committed
Merge pull request #1056 from meh/uri-downcase
Downcase host and scheme in URIs
2 parents dec997e + 06f8a12 commit 1385bde

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

lib/elixir/lib/uri.ex

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,16 @@ defmodule URI do
131131
destructure [_, _, scheme, _, authority, path, _, query, _, fragment], parts
132132
{ userinfo, host, port } = split_authority(authority)
133133

134+
if authority do
135+
authority = ""
136+
137+
if userinfo, do: authority = authority <> userinfo <> "@"
138+
if host, do: authority = authority <> host
139+
if port, do: authority = authority <> ":" <> integer_to_binary(port)
140+
end
141+
134142
info = URI.Info[
135-
scheme: scheme, path: path, query: query,
143+
scheme: scheme && String.downcase(scheme), path: path, query: query,
136144
fragment: fragment, authority: authority,
137145
userinfo: userinfo, host: host, port: port
138146
]
@@ -169,7 +177,7 @@ defmodule URI do
169177
components = Regex.run %r/(^(.*)@)?([^:]*)(:(\d*))?/, s
170178
destructure [_, _, userinfo, host, _, port], nillify(components)
171179
port = if port, do: binary_to_integer(port)
172-
{ userinfo, host, port }
180+
{ userinfo, host && String.downcase(host), port }
173181
end
174182

175183
# Regex.run returns empty strings sometimes. We want

lib/elixir/test/elixir/uri_test.exs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,10 @@ defmodule URITest do
123123
assert URI.parse(":https")
124124
assert URI.parse("https")
125125
end
126+
127+
test :downcase_properly do
128+
assert URI.parse("hTtP://google.com").scheme == "http"
129+
assert URI.parse("http://GoOgLe.CoM").host == "google.com"
130+
assert URI.parse("http://LOL:[email protected]").authority == "LOL:[email protected]"
131+
end
126132
end

0 commit comments

Comments
 (0)