aboutsummaryrefslogtreecommitdiffstats
path: root/src/cowboy_http.erl
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2012-02-23 20:56:11 +0100
committerLoïc Hoguin <[email protected]>2012-02-23 20:56:11 +0100
commita7264a1af3170fa9215f8044907f6034f3aefdd5 (patch)
tree3862c9d3a13aa2bbf950499d35dffe2f75cd3147 /src/cowboy_http.erl
parentdb382d4d392930c9b4e9789605afda51fa710d42 (diff)
downloadcowboy-a7264a1af3170fa9215f8044907f6034f3aefdd5.tar.gz
cowboy-a7264a1af3170fa9215f8044907f6034f3aefdd5.tar.bz2
cowboy-a7264a1af3170fa9215f8044907f6034f3aefdd5.zip
Add cowboy_http:x_www_form_urlencoded/2
This was initially an internal function, it has been made public due to popular demand as it can sometimes be needed.
Diffstat (limited to 'src/cowboy_http.erl')
-rw-r--r--src/cowboy_http.erl28
1 files changed, 27 insertions, 1 deletions
diff --git a/src/cowboy_http.erl b/src/cowboy_http.erl
index 6c5a6ea..93b83e5 100644
--- a/src/cowboy_http.erl
+++ b/src/cowboy_http.erl
@@ -24,7 +24,7 @@
%% Interpretation.
-export([connection_to_atom/1, urldecode/1, urldecode/2, urlencode/1,
- urlencode/2]).
+ urlencode/2, x_www_form_urlencoded/2]).
-type method() :: 'OPTIONS' | 'GET' | 'HEAD'
| 'POST' | 'PUT' | 'DELETE' | 'TRACE' | binary().
@@ -800,6 +800,16 @@ tohexu(C) when C < 17 -> $A + C - 10.
tohexl(C) when C < 10 -> $0 + C;
tohexl(C) when C < 17 -> $a + C - 10.
+-spec x_www_form_urlencoded(binary(), fun((binary()) -> binary())) ->
+ list({binary(), binary() | true}).
+x_www_form_urlencoded(<<>>, _URLDecode) ->
+ [];
+x_www_form_urlencoded(Qs, URLDecode) ->
+ Tokens = binary:split(Qs, <<"&">>, [global, trim]),
+ [case binary:split(Token, <<"=">>) of
+ [Token] -> {URLDecode(Token), true};
+ [Name, Value] -> {URLDecode(Name), URLDecode(Value)}
+ end || Token <- Tokens].
%% Tests.
@@ -967,6 +977,22 @@ digits_test_() ->
],
[{V, fun() -> R = digits(V) end} || {V, R} <- Tests].
+x_www_form_urlencoded_test_() ->
+ %% {Qs, Result}
+ Tests = [
+ {<<"">>, []},
+ {<<"a=b">>, [{<<"a">>, <<"b">>}]},
+ {<<"aaa=bbb">>, [{<<"aaa">>, <<"bbb">>}]},
+ {<<"a&b">>, [{<<"a">>, true}, {<<"b">>, true}]},
+ {<<"a=b&c&d=e">>, [{<<"a">>, <<"b">>},
+ {<<"c">>, true}, {<<"d">>, <<"e">>}]},
+ {<<"a=b=c=d=e&f=g">>, [{<<"a">>, <<"b=c=d=e">>}, {<<"f">>, <<"g">>}]},
+ {<<"a+b=c+d">>, [{<<"a b">>, <<"c d">>}]}
+ ],
+ URLDecode = fun urldecode/1,
+ [{Qs, fun() -> R = x_www_form_urlencoded(
+ Qs, URLDecode) end} || {Qs, R} <- Tests].
+
urldecode_test_() ->
U = fun urldecode/2,
[?_assertEqual(<<" ">>, U(<<"%20">>, crash)),