0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-15 12:40:25 +00:00

Use binary:match/2 instead of binary:split/2 in cowboy_dispatcher

Didn't replace everything, just the ones that didn't use the
global option. Also removed a couple now useless code paths.
This commit is contained in:
Loïc Hoguin 2012-09-24 00:54:05 +02:00
parent 9d4f4ec9c7
commit 793d058125

View file

@ -41,13 +41,12 @@
%% @doc Split a hostname into a list of tokens. %% @doc Split a hostname into a list of tokens.
-spec split_host(binary()) -spec split_host(binary())
-> {tokens(), binary(), undefined | inet:port_number()}. -> {tokens(), binary(), undefined | inet:port_number()}.
split_host(<<>>) ->
{[], <<>>, undefined};
split_host(Host) -> split_host(Host) ->
case binary:split(Host, <<":">>) of case binary:match(Host, <<":">>) of
[Host] -> nomatch ->
{binary:split(Host, <<".">>, [global, trim]), Host, undefined}; {binary:split(Host, <<".">>, [global, trim]), Host, undefined};
[Host2, Port] -> {Pos, _} ->
<< Host2:Pos/binary, _:8, Port/bits >> = Host,
{binary:split(Host2, <<".">>, [global, trim]), Host2, {binary:split(Host2, <<".">>, [global, trim]), Host2,
list_to_integer(binary_to_list(Port))} list_to_integer(binary_to_list(Port))}
end. end.
@ -60,15 +59,17 @@ split_host(Host) ->
-spec split_path(binary(), fun((binary()) -> binary())) -> -spec split_path(binary(), fun((binary()) -> binary())) ->
{tokens(), binary(), binary()}. {tokens(), binary(), binary()}.
split_path(Path, URLDec) -> split_path(Path, URLDec) ->
case binary:split(Path, <<"?">>) of case binary:match(Path, <<"?">>) of
[Path] -> {do_split_path(Path, <<"/">>, URLDec), Path, <<>>}; nomatch ->
[<<>>, Qs] -> {[], <<>>, Qs}; {do_split_path(Path, URLDec), Path, <<>>};
[Path2, Qs] -> {do_split_path(Path2, <<"/">>, URLDec), Path2, Qs} {Pos, _} ->
<< Path2:Pos/binary, _:8, Qs/bits >> = Path,
{do_split_path(Path2, URLDec), Path2, Qs}
end. end.
-spec do_split_path(binary(), <<_:8>>, fun((binary()) -> binary())) -> tokens(). -spec do_split_path(binary(), fun((binary()) -> binary())) -> tokens().
do_split_path(RawPath, Separator, URLDec) -> do_split_path(RawPath, URLDec) ->
EncodedPath = case binary:split(RawPath, Separator, [global, trim]) of EncodedPath = case binary:split(RawPath, <<"/">>, [global, trim]) of
[<<>>|Path] -> Path; [<<>>|Path] -> Path;
Path -> Path Path -> Path
end, end,
@ -219,8 +220,8 @@ split_host_fail_test_() ->
split_path_test_() -> split_path_test_() ->
%% {Path, Result, QueryString} %% {Path, Result, QueryString}
Tests = [ Tests = [
{<<"?">>, [], <<"">>, <<"">>}, {<<"/?">>, [], <<"/">>, <<"">>},
{<<"???">>, [], <<"">>, <<"??">>}, {<<"/???">>, [], <<"/">>, <<"??">>},
{<<"/">>, [], <<"/">>, <<"">>}, {<<"/">>, [], <<"/">>, <<"">>},
{<<"/extend//cowboy">>, [<<"extend">>, <<>>, <<"cowboy">>], {<<"/extend//cowboy">>, [<<"extend">>, <<>>, <<"cowboy">>],
<<"/extend//cowboy">>, <<>>}, <<"/extend//cowboy">>, <<>>},