File tree Expand file tree Collapse file tree 2 files changed +27
-3
lines changed Expand file tree Collapse file tree 2 files changed +27
-3
lines changed Original file line number Diff line number Diff line change @@ -142,7 +142,8 @@ defmodule URI do
142
142
end
143
143
144
144
defp pair ( { k , v } ) do
145
- encode ( to_string ( k ) ) <> "=" <> encode ( to_string ( v ) )
145
+ encode_www_form ( to_string ( k ) ) <>
146
+ "=" <> encode_www_form ( to_string ( v ) )
146
147
end
147
148
148
149
@ doc """
@@ -154,7 +155,6 @@ defmodule URI do
154
155
c in ':/?#[]@!$&\' ()*+,;='
155
156
end
156
157
157
- #
158
158
@ doc """
159
159
Checks if the character is a "unreserved" character in a URI.
160
160
@@ -191,6 +191,24 @@ defmodule URI do
191
191
for << c <- str >> , into: "" , do: percent ( c , predicate )
192
192
end
193
193
194
+ @ doc """
195
+ Encode a string as "x-www-urlencoded".
196
+
197
+ ## Example
198
+
199
+ iex> URI.encode_www_form("put: it+й")
200
+ "put%3A+it%2B%D0%B9"
201
+
202
+ """
203
+ def encode_www_form ( str ) do
204
+ for << c <- str >> , into: "" do
205
+ case percent ( c , & char_unreserved? / 1 ) do
206
+ "%20" -> "+"
207
+ pct -> pct
208
+ end
209
+ end
210
+ end
211
+
194
212
defp percent ( c , predicate ) do
195
213
if predicate . ( c ) do
196
214
<< c >>
Original file line number Diff line number Diff line change @@ -9,10 +9,16 @@ defmodule URITest do
9
9
"%0D%0A%26%3C%25%3E%22%20%E3%82%86"
10
10
end
11
11
12
+ test :encode_www_form do
13
+ assert URI . encode_www_form ( "4test ~1.x" ) == "4test+~1.x"
14
+ assert URI . encode_www_form ( "poll:146%" ) == "poll%3A146%25"
15
+ assert URI . encode_www_form ( "/\n +/ゆ" ) == "%2F%0A%2B%2F%E3%82%86"
16
+ end
17
+
12
18
test :encode_query do
13
19
assert URI . encode_query ( [ { :foo , :bar } , { :baz , :quux } ] ) == "foo=bar&baz=quux"
14
20
assert URI . encode_query ( [ { "foo" , "bar" } , { "baz" , "quux" } ] ) == "foo=bar&baz=quux"
15
- assert URI . encode_query ( [ { "foo" , :bar } ] ) == "foo=bar"
21
+ assert URI . encode_query ( [ { "foo z " , :bar } ] ) == "foo+z =bar"
16
22
17
23
assert_raise ArgumentError , fn ->
18
24
URI . encode_query ( [ { "foo" , 'bar' } ] )
You can’t perform that action at this time.
0 commit comments