From 8d546dacbc1b2fe82e67fee5d28b86fe8b428443 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Fri, 8 Nov 2013 18:47:05 +0100 Subject: 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 --- Makefile | 2 +- rebar.config | 2 +- src/cowboy_http.erl | 25 ------------------------- src/cowboy_req.erl | 6 +++--- 4 files changed, 5 insertions(+), 30 deletions(-) diff --git a/Makefile b/Makefile index 659697d..ef9de7c 100644 --- a/Makefile +++ b/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 diff --git a/rebar.config b/rebar.config index 1578c53..c596e79 100644 --- a/rebar.config +++ b/rebar.config @@ -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"}} ]}. 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. -- cgit v1.2.3