aboutsummaryrefslogtreecommitdiffstats
path: root/src/cowboy_http.erl
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2014-03-10 10:27:07 +0100
committerLoïc Hoguin <[email protected]>2014-03-10 10:27:07 +0100
commit67e5f597ec5d80c003ffa94b53f28befcc79b98c (patch)
tree38071da27d5e17d79a1f1b88f1e683d399c3e252 /src/cowboy_http.erl
parent8993249e421fbfff603cedec70e90be64d3694e6 (diff)
downloadcowboy-67e5f597ec5d80c003ffa94b53f28befcc79b98c.tar.gz
cowboy-67e5f597ec5d80c003ffa94b53f28befcc79b98c.tar.bz2
cowboy-67e5f597ec5d80c003ffa94b53f28befcc79b98c.zip
Remove cowboy_http:urldecode/1 and urlencode/1
Use cow_qs:urldecode/1 and cow_qs:urlencode/1 instead
Diffstat (limited to 'src/cowboy_http.erl')
-rw-r--r--src/cowboy_http.erl127
1 files changed, 0 insertions, 127 deletions
diff --git a/src/cowboy_http.erl b/src/cowboy_http.erl
index f3a198d..9c268ea 100644
--- a/src/cowboy_http.erl
+++ b/src/cowboy_http.erl
@@ -44,12 +44,6 @@
-export([te_identity/2]).
-export([ce_identity/1]).
-%% Interpretation.
--export([urldecode/1]).
--export([urldecode/2]).
--export([urlencode/1]).
--export([urlencode/2]).
-
%% Parsing.
%% @doc Parse a non-empty list of the given type.
@@ -950,92 +944,6 @@ te_identity(Data, {Streamed, Total}) ->
ce_identity(Data) ->
{ok, Data}.
-%% Interpretation.
-
-%% @doc Decode a URL encoded binary.
-%% @equiv urldecode(Bin, crash)
--spec urldecode(binary()) -> binary().
-urldecode(Bin) when is_binary(Bin) ->
- urldecode(Bin, <<>>, crash).
-
-%% @doc Decode a URL encoded binary.
-%% The second argument specifies how to handle percent characters that are not
-%% followed by two valid hex characters. Use `skip' to ignore such errors,
-%% if `crash' is used the function will fail with the reason `badarg'.
--spec urldecode(binary(), crash | skip) -> binary().
-urldecode(Bin, OnError) when is_binary(Bin) ->
- urldecode(Bin, <<>>, OnError).
-
--spec urldecode(binary(), binary(), crash | skip) -> binary().
-urldecode(<<$%, H, L, Rest/binary>>, Acc, OnError) ->
- G = unhex(H),
- M = unhex(L),
- if G =:= error; M =:= error ->
- case OnError of skip -> ok; crash -> erlang:error(badarg) end,
- urldecode(<<H, L, Rest/binary>>, <<Acc/binary, $%>>, OnError);
- true ->
- urldecode(Rest, <<Acc/binary, (G bsl 4 bor M)>>, OnError)
- end;
-urldecode(<<$%, Rest/binary>>, Acc, OnError) ->
- case OnError of skip -> ok; crash -> erlang:error(badarg) end,
- urldecode(Rest, <<Acc/binary, $%>>, OnError);
-urldecode(<<$+, Rest/binary>>, Acc, OnError) ->
- urldecode(Rest, <<Acc/binary, $ >>, OnError);
-urldecode(<<C, Rest/binary>>, Acc, OnError) ->
- urldecode(Rest, <<Acc/binary, C>>, OnError);
-urldecode(<<>>, Acc, _OnError) ->
- Acc.
-
--spec unhex(byte()) -> byte() | error.
-unhex(C) when C >= $0, C =< $9 -> C - $0;
-unhex(C) when C >= $A, C =< $F -> C - $A + 10;
-unhex(C) when C >= $a, C =< $f -> C - $a + 10;
-unhex(_) -> error.
-
-
-%% @doc URL encode a string binary.
-%% @equiv urlencode(Bin, [])
--spec urlencode(binary()) -> binary().
-urlencode(Bin) ->
- urlencode(Bin, []).
-
-%% @doc URL encode a string binary.
-%% The `noplus' option disables the default behaviour of quoting space
-%% characters, `\s', as `+'. The `upper' option overrides the default behaviour
-%% of writing hex numbers using lowecase letters to using uppercase letters
-%% instead.
--spec urlencode(binary(), [noplus|upper]) -> binary().
-urlencode(Bin, Opts) ->
- Plus = not lists:member(noplus, Opts),
- Upper = lists:member(upper, Opts),
- urlencode(Bin, <<>>, Plus, Upper).
-
--spec urlencode(binary(), binary(), boolean(), boolean()) -> binary().
-urlencode(<<C, Rest/binary>>, Acc, P=Plus, U=Upper) ->
- if C >= $0, C =< $9 -> urlencode(Rest, <<Acc/binary, C>>, P, U);
- C >= $A, C =< $Z -> urlencode(Rest, <<Acc/binary, C>>, P, U);
- C >= $a, C =< $z -> urlencode(Rest, <<Acc/binary, C>>, P, U);
- C =:= $.; C =:= $-; C =:= $~; C =:= $_ ->
- urlencode(Rest, <<Acc/binary, C>>, P, U);
- C =:= $ , Plus ->
- urlencode(Rest, <<Acc/binary, $+>>, P, U);
- true ->
- H = C band 16#F0 bsr 4, L = C band 16#0F,
- H1 = if Upper -> tohexu(H); true -> tohexl(H) end,
- L1 = if Upper -> tohexu(L); true -> tohexl(L) end,
- urlencode(Rest, <<Acc/binary, $%, H1, L1>>, P, U)
- end;
-urlencode(<<>>, Acc, _Plus, _Upper) ->
- Acc.
-
--spec tohexu(byte()) -> byte().
-tohexu(C) when C < 10 -> $0 + C;
-tohexu(C) when C < 16 -> $A + C - 10.
-
--spec tohexl(byte()) -> byte().
-tohexl(C) when C < 10 -> $0 + C;
-tohexl(C) when C < 16 -> $a + C - 10.
-
%% Tests.
-ifdef(TEST).
@@ -1216,41 +1124,6 @@ digits_test_() ->
],
[{V, fun() -> R = digits(V) end} || {V, R} <- Tests].
-urldecode_test_() ->
- F = fun(Qs, O) ->
- try urldecode(Qs, O) of
- R ->
- {ok, R}
- catch _:E ->
- {error, E}
- end
- end,
- Tests = [
- {<<"%20">>, crash, {ok, <<" ">>}},
- {<<"+">>, crash, {ok, <<" ">>}},
- {<<"%00">>, crash, {ok, <<0>>}},
- {<<"%fF">>, crash, {ok, <<255>>}},
- {<<"123">>, crash, {ok, <<"123">>}},
- {<<"%i5">>, skip, {ok, <<"%i5">>}},
- {<<"%5">>, skip, {ok, <<"%5">>}},
- {<<"%i5">>, crash, {error, badarg}},
- {<<"%5">>, crash, {error, badarg}}
- ],
- [{Qs, fun() -> R = F(Qs,O) end} || {Qs, O, R} <- Tests].
-
-urlencode_test_() ->
- Tests = [
- {<<255,0>>, [], <<"%ff%00">>},
- {<<255,0>>, [upper], <<"%FF%00">>},
- {<<" ">>, [], <<"+">>},
- {<<" ">>, [noplus], <<"%20">>},
- {<<"aBc">>, [], <<"aBc">>},
- {<<".-~_">>, [], <<".-~_">>}
- ],
- Tests2 = [{<<255, " ">>,<<"%ff+">>}],
- [{V, fun() -> R = urlencode(V, O) end} || {V, O, R} <- Tests] ++
- [{V, fun() -> R = urlencode(V) end} || {V, R} <- Tests2].
-
http_authorization_test_() ->
Tests = [
{<<"basic">>, <<"QWxsYWRpbjpvcGVuIHNlc2FtZQ==">>,