From da11b15aef87f392a807b4756bf285160e15a194 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20Dimitrov?= Date: Mon, 23 Oct 2017 12:02:16 +0200 Subject: stdlib: Update supported separators (query string) Update list of supported separators: - escaped_amp (default): "&" - amp: "&" - semicolon: ";" --- lib/stdlib/doc/src/uri_string.xml | 10 +++++----- lib/stdlib/src/uri_string.erl | 4 ++++ lib/stdlib/test/uri_string_SUITE.erl | 6 ++++-- 3 files changed, 13 insertions(+), 7 deletions(-) (limited to 'lib') diff --git a/lib/stdlib/doc/src/uri_string.xml b/lib/stdlib/doc/src/uri_string.xml index 496573ae2f..97b38ea93e 100644 --- a/lib/stdlib/doc/src/uri_string.xml +++ b/lib/stdlib/doc/src/uri_string.xml @@ -28,9 +28,10 @@ A uri_string - RFC 3986 compliant URI processing functions. + URI processing functions. -

This module contains functions for parsing and handling RFC 3986 compliant URIs.

+

This module contains functions for parsing and handling URIs (RFC 3986) and + form-urlencoded query strings (RFC 1866).

A URI is an identifier consisting of a sequence of characters matching the syntax rule named URI in RFC 3986.

The generic URI syntax consists of a hierarchical sequence of components referred @@ -109,7 +110,7 @@ QueryList, a list of unescaped key-value pairs. Media type application/x-www-form-urlencoded is defined in section 8.2.1 of RFC 1866 (HTML 2.0). Reserved and unsafe characters, as - defined by RFC 1738 (Uniform Resource Locators), are procent-encoded. + defined by RFC 1738 (Uniform Resource Locators), are percent-encoded.

Example:

@@ -125,8 +126,7 @@
       
         

Same as compose_query/1 but with an additional Options parameter, that controls the type of separator used - between key-value pairs. There are two supported separator types: amp () - and semicolon (;).

+ between key-value pairs. There are three supported separator types: amp (), escaped_amp () and semicolon (;). If the parameter Options is empty, separator takes the default value (escaped_amp).

Example:

 1> uri_string:compose_query([{"foo bar","1"},{"city","örebro"}],
diff --git a/lib/stdlib/src/uri_string.erl b/lib/stdlib/src/uri_string.erl
index 8723d3f183..a4fd9c66f4 100644
--- a/lib/stdlib/src/uri_string.erl
+++ b/lib/stdlib/src/uri_string.erl
@@ -1806,6 +1806,8 @@ get_separator(_, Acc) when length(Acc) =:= 0 ->
 get_separator([], _Acc) ->
     "&";
 get_separator([{separator, amp}], _Acc) ->
+    "&";
+get_separator([{separator, escaped_amp}], _Acc) ->
     "&";
 get_separator([{separator, semicolon}], _Acc) ->
     ";".
@@ -1901,6 +1903,8 @@ dissect_query_value([], Acc, Key, Value) ->
 
 dissect_query_separator_amp("&" ++ T, Acc, Key, Value) ->
     dissect_query_key(T, Acc, Key, Value);
+dissect_query_separator_amp("&" ++ T, Acc, Key, Value) ->
+    dissect_query_key(T, Acc, Key, Value);
 dissect_query_separator_amp(L, _, _, _) ->
     throw({error, invalid_separator, L}).
 
diff --git a/lib/stdlib/test/uri_string_SUITE.erl b/lib/stdlib/test/uri_string_SUITE.erl
index b70cb842de..fe832ac82c 100644
--- a/lib/stdlib/test/uri_string_SUITE.erl
+++ b/lib/stdlib/test/uri_string_SUITE.erl
@@ -827,7 +827,8 @@ transcode_negative(_Config) ->
 compose_query(_Config) ->
     [] = uri_string:compose_query([]),
     "foo=1&bar=2" = uri_string:compose_query([{<<"foo">>,"1"}, {"bar", "2"}]),
-    "foo=1&bar=2" = uri_string:compose_query([{"foo","1"}, {"bar", "2"}],[{separator,amp}]),
+    "foo=1&bar=2" = uri_string:compose_query([{"foo","1"}, {"bar", "2"}],[{separator,escaped_amp}]),
+    "foo=1&bar=2" = uri_string:compose_query([{"foo","1"}, {"bar", "2"}],[{separator,amp}]),
     "foo=1;bar=2" = uri_string:compose_query([{"foo","1"}, {"bar", "2"}],[{separator,semicolon}]),
     "foo+bar=1&%C3%B6=2" = uri_string:compose_query([{"foo bar","1"}, {"ö", "2"}]).
 
@@ -837,12 +838,13 @@ compose_query_negative(_Config) ->
 dissect_query(_Config) ->
     [] = uri_string:dissect_query(""),
     [{"foo","1"}, {"bar", "2"}] = uri_string:dissect_query("foo=1&bar=2"),
+    [{"foo","1"}, {"bar", "2"}] = uri_string:dissect_query("foo=1&bar=2"),
     [{"foo","1"}, {"bar", "2"}] = uri_string:dissect_query("foo=1;bar=2"),
     [{"foo","1"}, {"bar", "222"}] = uri_string:dissect_query([<<"foo=1;bar=2">>,"22"]),
     [{"foo","ö"}, {"bar", "2"}] = uri_string:dissect_query("foo=%C3%B6&bar=2").
 
 dissect_query_negative(_Config) ->
-    {error,invalid_separator,"≈bar=2"} =
+    {error,urldecode,";bar"} =
         uri_string:dissect_query("foo=1≈bar=2"),
     {error,urldecode,"&bar"} =
         uri_string:dissect_query("foo1&bar=2"),
-- 
cgit v1.2.3