Skip to content

Commit 14722ba

Browse files
committed
Make URI.encode/1 to conform to RFC 3986
1 parent 7f4437e commit 14722ba

File tree

2 files changed

+14
-22
lines changed

2 files changed

+14
-22
lines changed

lib/elixir/lib/uri.ex

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,15 @@ defmodule URI do
156156
"""
157157
def encode(s), do: for(<<c <- s>>, into: "", do: percent(c))
158158

159-
defp percent(32), do: <<?+>>
159+
defp percent(?~), do: <<?~>>
160160
defp percent(?-), do: <<?->>
161161
defp percent(?_), do: <<?_>>
162162
defp percent(?.), do: <<?.>>
163163

164164
defp percent(c)
165-
when c >= ?0 and c <= ?9
166-
when c >= ?a and c <= ?z
167-
when c >= ?A and c <= ?Z do
165+
when c in ?0..?9
166+
when c in ?a..?z
167+
when c in ?A..?Z do
168168
<<c>>
169169
end
170170

@@ -187,11 +187,11 @@ defmodule URI do
187187
end
188188

189189
def decode(<<?%, hex1, hex2, tail :: binary >>, uri) do
190-
<< bsl(hex_to_dec(hex1, uri), 4) + hex_to_dec(hex2, uri) >> <> decode(tail, uri)
190+
<<bsl(hex_to_dec(hex1, uri), 4) + hex_to_dec(hex2, uri)>> <> decode(tail, uri)
191191
end
192192

193193
def decode(<<head, tail :: binary >>, uri) do
194-
<<check_plus(head)>> <> decode(tail, uri)
194+
<<head>> <> decode(tail, uri)
195195
end
196196

197197
def decode(<<>>, _uri), do: <<>>
@@ -203,9 +203,6 @@ defmodule URI do
203203
raise ArgumentError, "malformed URI #{inspect uri}"
204204
end
205205

206-
defp check_plus(?+), do: 32
207-
defp check_plus(c), do: c
208-
209206
@doc """
210207
Parses a URI into components.
211208

lib/elixir/test/elixir/uri_test.exs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ Code.require_file "test_helper.exs", __DIR__
33
defmodule URITest do
44
use ExUnit.Case, async: true
55

6-
test :encode_with_binary do
7-
raw = <<13, 10, 38, 60, 62, 34, 32, 227, 130, 134, 227, 130, 147, 227, 130, 134, 227, 130, 147>>
8-
expected = "%0D%0A%26%3C%3E%22+%E3%82%86%E3%82%93%E3%82%86%E3%82%93"
9-
assert URI.encode(raw) == expected
6+
test :encode do
7+
assert URI.encode("4_test.is-s~") == "4_test.is-s~"
8+
assert URI.encode("\r\n&<%>\" ゆ") == "%0D%0A%26%3C%25%3E%22%20%E3%82%86"
109
end
1110

1211
test :encode_query do
@@ -44,11 +43,12 @@ defmodule URITest do
4443
end
4544

4645
test :decode do
47-
data_to_be_decoded = "%26%3C%3E%22+%E3%82%86%E3%82%93%E3%82%86%E3%82%93"
48-
assert URI.decode(data_to_be_decoded) == "&<>\" ゆんゆん"
46+
assert URI.decode("%0D%0A%26%3C%25%3E%22%20%E3%82%86") == "\r\n&<%>\" ゆ"
47+
assert URI.decode("%2f%41%4a%55") == "/AJU"
48+
assert URI.decode("4_t+st.is-s~") == "4_t+st.is-s~"
4949

50-
assert_raise ArgumentError, ~r/malformed URI/, fn ->
51-
assert URI.decode("% invalid")
50+
assert_raise ArgumentError, ~R/malformed URI/, fn ->
51+
URI.decode("% invalid")
5252
end
5353
end
5454

@@ -180,9 +180,4 @@ defmodule URITest do
180180
assert to_string(URI.parse("http://google.com?q=lol")) == "http://google.com?q=lol"
181181
assert to_string(URI.parse("http://google.com?q=lol#omg")) == "http://google.com?q=lol#omg"
182182
end
183-
184-
test :escape do
185-
assert URI.decode("%2f%41%4a%55") == "/AJU"
186-
assert URI.decode("%2F%41%4A%55") == "/AJU"
187-
end
188183
end

0 commit comments

Comments
 (0)