mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 12:20:24 +00:00
Optimize query string parsing
* Parsing code was moved to cowlib: cowboy_qs:parse_qs/1 * A function was added to build query strings: cowboy_qs:qs/1 * Also added cowboy_qs:urlencode/1 and cowboy_qsurldecode/1
This commit is contained in:
parent
6672ea0415
commit
8d546dacbc
4 changed files with 5 additions and 30 deletions
2
Makefile
2
Makefile
|
@ -11,7 +11,7 @@ PLT_APPS = crypto public_key ssl
|
|||
# Dependencies.
|
||||
|
||||
DEPS = cowlib ranch
|
||||
dep_cowlib = pkg://cowlib 0.3.0
|
||||
dep_cowlib = pkg://cowlib 0.4.0
|
||||
dep_ranch = pkg://ranch 0.8.5
|
||||
|
||||
TEST_DEPS = ct_helper gun
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
{deps, [
|
||||
{cowlib, ".*", {git, "git://github.com/extend/cowlib.git", "0.3.0"}},
|
||||
{cowlib, ".*", {git, "git://github.com/extend/cowlib.git", "0.4.0"}},
|
||||
{ranch, ".*", {git, "git://github.com/extend/ranch.git", "0.8.5"}}
|
||||
]}.
|
||||
|
|
|
@ -49,7 +49,6 @@
|
|||
-export([urldecode/2]).
|
||||
-export([urlencode/1]).
|
||||
-export([urlencode/2]).
|
||||
-export([x_www_form_urlencoded/1]).
|
||||
|
||||
%% Parsing.
|
||||
|
||||
|
@ -1037,16 +1036,6 @@ 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()) -> list({binary(), binary() | true}).
|
||||
x_www_form_urlencoded(<<>>) ->
|
||||
[];
|
||||
x_www_form_urlencoded(Qs) ->
|
||||
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.
|
||||
|
||||
-ifdef(TEST).
|
||||
|
@ -1227,20 +1216,6 @@ 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">>}]}
|
||||
],
|
||||
[{Qs, fun() -> R = x_www_form_urlencoded(Qs) end} || {Qs, R} <- Tests].
|
||||
|
||||
urldecode_test_() ->
|
||||
F = fun(Qs, O) ->
|
||||
try urldecode(Qs, O) of
|
||||
|
|
|
@ -279,7 +279,7 @@ qs_val(Name, Req) when is_binary(Name) ->
|
|||
-> {binary() | true | Default, Req} when Req::req(), Default::any().
|
||||
qs_val(Name, Req=#http_req{qs=RawQs, qs_vals=undefined}, Default)
|
||||
when is_binary(Name) ->
|
||||
QsVals = cowboy_http:x_www_form_urlencoded(RawQs),
|
||||
QsVals = cow_qs:parse_qs(RawQs),
|
||||
qs_val(Name, Req#http_req{qs_vals=QsVals}, Default);
|
||||
qs_val(Name, Req, Default) ->
|
||||
case lists:keyfind(Name, 1, Req#http_req.qs_vals) of
|
||||
|
@ -290,7 +290,7 @@ qs_val(Name, Req, Default) ->
|
|||
%% @doc Return the full list of query string values.
|
||||
-spec qs_vals(Req) -> {list({binary(), binary() | true}), Req} when Req::req().
|
||||
qs_vals(Req=#http_req{qs=RawQs, qs_vals=undefined}) ->
|
||||
QsVals = cowboy_http:x_www_form_urlencoded(RawQs),
|
||||
QsVals = cow_qs:parse_qs(RawQs),
|
||||
qs_vals(Req#http_req{qs_vals=QsVals});
|
||||
qs_vals(Req=#http_req{qs_vals=QsVals}) ->
|
||||
{QsVals, Req}.
|
||||
|
@ -776,7 +776,7 @@ body_qs(Req) ->
|
|||
body_qs(MaxBodyLength, Req) ->
|
||||
case body(MaxBodyLength, Req) of
|
||||
{ok, Body, Req2} ->
|
||||
{ok, cowboy_http:x_www_form_urlencoded(Body), Req2};
|
||||
{ok, cow_qs:parse_qs(Body), Req2};
|
||||
{error, Reason} ->
|
||||
{error, Reason}
|
||||
end.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue