mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 12:20:24 +00:00
Remove cowboy_http:urldecode/1 and urlencode/1
Use cow_qs:urldecode/1 and cow_qs:urlencode/1 instead
This commit is contained in:
parent
8993249e42
commit
67e5f597ec
2 changed files with 2 additions and 129 deletions
|
@ -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==">>,
|
||||
|
|
|
@ -332,9 +332,9 @@ split_path(Path, Acc) ->
|
|||
try
|
||||
case binary:match(Path, <<"/">>) of
|
||||
nomatch when Path =:= <<>> ->
|
||||
lists:reverse([cowboy_http:urldecode(S) || S <- Acc]);
|
||||
lists:reverse([cow_qs:urldecode(S) || S <- Acc]);
|
||||
nomatch ->
|
||||
lists:reverse([cowboy_http:urldecode(S) || S <- [Path|Acc]]);
|
||||
lists:reverse([cow_qs:urldecode(S) || S <- [Path|Acc]]);
|
||||
{Pos, _} ->
|
||||
<< Segment:Pos/binary, _:8, Rest/bits >> = Path,
|
||||
split_path(Rest, [Segment|Acc])
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue