diff options
author | Loïc Hoguin <[email protected]> | 2013-11-08 18:47:05 +0100 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2013-11-08 18:47:05 +0100 |
commit | 8d546dacbc1b2fe82e67fee5d28b86fe8b428443 (patch) | |
tree | 87c34a5056c17f62e28fd4497da214c25679c2eb /src | |
parent | 6672ea04155a075e60381413bf9b65b3974b7d57 (diff) | |
download | cowboy-8d546dacbc1b2fe82e67fee5d28b86fe8b428443.tar.gz cowboy-8d546dacbc1b2fe82e67fee5d28b86fe8b428443.tar.bz2 cowboy-8d546dacbc1b2fe82e67fee5d28b86fe8b428443.zip |
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
Diffstat (limited to 'src')
-rw-r--r-- | src/cowboy_http.erl | 25 | ||||
-rw-r--r-- | src/cowboy_req.erl | 6 |
2 files changed, 3 insertions, 28 deletions
diff --git a/src/cowboy_http.erl b/src/cowboy_http.erl index 9f06522..ac4a30f 100644 --- a/src/cowboy_http.erl +++ b/src/cowboy_http.erl @@ -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 diff --git a/src/cowboy_req.erl b/src/cowboy_req.erl index 34302c4..452d390 100644 --- a/src/cowboy_req.erl +++ b/src/cowboy_req.erl @@ -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. |