From bee5ca852b1a8e1506872aeea57f6c745c8add77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Fri, 3 Oct 2014 18:25:29 +0300 Subject: Replace some /binary to /bits in binary pattern matching We don't need the extra check for multiple of 8 bits. --- src/cowboy_protocol.erl | 4 ++-- src/cowboy_router.erl | 36 ++++++++++++++++++------------------ src/cowboy_spdy.erl | 4 ++-- src/cowboy_static.erl | 8 ++++---- src/cowboy_websocket.erl | 4 ++-- 5 files changed, 28 insertions(+), 28 deletions(-) (limited to 'src') diff --git a/src/cowboy_protocol.erl b/src/cowboy_protocol.erl index 00f8ba8..2da3a15 100644 --- a/src/cowboy_protocol.erl +++ b/src/cowboy_protocol.erl @@ -134,7 +134,7 @@ wait_request(Buffer, State=#state{socket=Socket, transport=Transport, -spec parse_request(binary(), #state{}, non_neg_integer()) -> ok. %% Empty lines must be using \r\n. -parse_request(<< $\n, _/binary >>, State, _) -> +parse_request(<< $\n, _/bits >>, State, _) -> error_terminate(400, State); parse_request(<< $\s, _/bits >>, State, _) -> error_terminate(400, State); @@ -150,7 +150,7 @@ parse_request(Buffer, State=#state{max_request_line_length=MaxLength, 1 when ReqEmpty =:= MaxEmpty -> error_terminate(400, State); 1 -> - << _:16, Rest/binary >> = Buffer, + << _:16, Rest/bits >> = Buffer, parse_request(Rest, State, ReqEmpty + 1); _ -> parse_method(Buffer, State, <<>>) diff --git a/src/cowboy_router.erl b/src/cowboy_router.erl index b09051e..2d1924d 100644 --- a/src/cowboy_router.erl +++ b/src/cowboy_router.erl @@ -82,7 +82,7 @@ compile_paths([{PathMatch, Fields, Handler, Opts}|Tail], Acc) Fields, Handler, Opts}|Tail], Acc); compile_paths([{'_', Fields, Handler, Opts}|Tail], Acc) -> compile_paths(Tail, [{'_', Fields, Handler, Opts}] ++ Acc); -compile_paths([{<< $/, PathMatch/binary >>, Fields, Handler, Opts}|Tail], +compile_paths([{<< $/, PathMatch/bits >>, Fields, Handler, Opts}|Tail], Acc) -> PathRules = compile_rules(PathMatch, $/, [], [], <<>>), Paths = [{lists:reverse(R), Fields, Handler, Opts} || R <- PathRules], @@ -95,32 +95,32 @@ compile_rules(<<>>, _, Segments, Rules, <<>>) -> [Segments|Rules]; compile_rules(<<>>, _, Segments, Rules, Acc) -> [[Acc|Segments]|Rules]; -compile_rules(<< S, Rest/binary >>, S, Segments, Rules, <<>>) -> +compile_rules(<< S, Rest/bits >>, S, Segments, Rules, <<>>) -> compile_rules(Rest, S, Segments, Rules, <<>>); -compile_rules(<< S, Rest/binary >>, S, Segments, Rules, Acc) -> +compile_rules(<< S, Rest/bits >>, S, Segments, Rules, Acc) -> compile_rules(Rest, S, [Acc|Segments], Rules, <<>>); -compile_rules(<< $:, Rest/binary >>, S, Segments, Rules, <<>>) -> +compile_rules(<< $:, Rest/bits >>, S, Segments, Rules, <<>>) -> {NameBin, Rest2} = compile_binding(Rest, S, <<>>), Name = binary_to_atom(NameBin, utf8), compile_rules(Rest2, S, Segments, Rules, Name); -compile_rules(<< $:, _/binary >>, _, _, _, _) -> +compile_rules(<< $:, _/bits >>, _, _, _, _) -> error(badarg); -compile_rules(<< $[, $., $., $., $], Rest/binary >>, S, Segments, Rules, Acc) +compile_rules(<< $[, $., $., $., $], Rest/bits >>, S, Segments, Rules, Acc) when Acc =:= <<>> -> compile_rules(Rest, S, ['...'|Segments], Rules, Acc); -compile_rules(<< $[, $., $., $., $], Rest/binary >>, S, Segments, Rules, Acc) -> +compile_rules(<< $[, $., $., $., $], Rest/bits >>, S, Segments, Rules, Acc) -> compile_rules(Rest, S, ['...', Acc|Segments], Rules, Acc); -compile_rules(<< $[, S, Rest/binary >>, S, Segments, Rules, Acc) -> +compile_rules(<< $[, S, Rest/bits >>, S, Segments, Rules, Acc) -> compile_brackets(Rest, S, [Acc|Segments], Rules); -compile_rules(<< $[, Rest/binary >>, S, Segments, Rules, <<>>) -> +compile_rules(<< $[, Rest/bits >>, S, Segments, Rules, <<>>) -> compile_brackets(Rest, S, Segments, Rules); %% Open bracket in the middle of a segment. -compile_rules(<< $[, _/binary >>, _, _, _, _) -> +compile_rules(<< $[, _/bits >>, _, _, _, _) -> error(badarg); %% Missing an open bracket. -compile_rules(<< $], _/binary >>, _, _, _, _) -> +compile_rules(<< $], _/bits >>, _, _, _, _) -> error(badarg); -compile_rules(<< C, Rest/binary >>, S, Segments, Rules, Acc) -> +compile_rules(<< C, Rest/bits >>, S, Segments, Rules, Acc) -> compile_rules(Rest, S, Segments, Rules, << Acc/binary, C >>). %% Everything past $: until the segment separator ($. for hosts, @@ -129,10 +129,10 @@ compile_binding(<<>>, _, <<>>) -> error(badarg); compile_binding(Rest = <<>>, _, Acc) -> {Acc, Rest}; -compile_binding(Rest = << C, _/binary >>, S, Acc) +compile_binding(Rest = << C, _/bits >>, S, Acc) when C =:= S; C =:= $[; C =:= $] -> {Acc, Rest}; -compile_binding(<< C, Rest/binary >>, S, Acc) -> +compile_binding(<< C, Rest/bits >>, S, Acc) -> compile_binding(Rest, S, << Acc/binary, C >>). compile_brackets(Rest, S, Segments, Rules) -> @@ -146,14 +146,14 @@ compile_brackets(Rest, S, Segments, Rules) -> compile_brackets_split(<<>>, _, _) -> error(badarg); %% Make sure we don't confuse the closing bracket we're looking for. -compile_brackets_split(<< C, Rest/binary >>, Acc, N) when C =:= $[ -> +compile_brackets_split(<< C, Rest/bits >>, Acc, N) when C =:= $[ -> compile_brackets_split(Rest, << Acc/binary, C >>, N + 1); -compile_brackets_split(<< C, Rest/binary >>, Acc, N) when C =:= $], N > 0 -> +compile_brackets_split(<< C, Rest/bits >>, Acc, N) when C =:= $], N > 0 -> compile_brackets_split(Rest, << Acc/binary, C >>, N - 1); %% That's the right one. -compile_brackets_split(<< $], Rest/binary >>, Acc, 0) -> +compile_brackets_split(<< $], Rest/bits >>, Acc, 0) -> {Acc, Rest}; -compile_brackets_split(<< C, Rest/binary >>, Acc, N) -> +compile_brackets_split(<< C, Rest/bits >>, Acc, N) -> compile_brackets_split(Rest, << Acc/binary, C >>, N). -spec execute(Req, Env) diff --git a/src/cowboy_spdy.erl b/src/cowboy_spdy.erl index 3057cca..4d83ff6 100644 --- a/src/cowboy_spdy.erl +++ b/src/cowboy_spdy.erl @@ -123,7 +123,7 @@ loop(State=#state{parent=Parent, socket=Socket, transport=Transport, FromPid ! {recv, FromSocket, {ok, InBuffer}}, loop(replace_child(Child#child{in_buffer= <<>>}, State)); byte_size(InBuffer) >= Length -> - << Data:Length/binary, Rest/binary >> = InBuffer, + << Data:Length/binary, Rest/bits >> = InBuffer, FromPid ! {recv, FromSocket, {ok, Data}}, loop(replace_child(Child#child{in_buffer=Rest}, State)); true -> @@ -293,7 +293,7 @@ handle_frame(State, {data, StreamID, IsFin, Data}) -> Child#child{input=IsFin2, in_buffer= <<>>, is_recv=false}; {passive, FromSocket, FromPid, Length, TRef} when byte_size(Data2) >= Length -> - << Data3:Length/binary, Rest/binary >> = Data2, + << Data3:Length/binary, Rest/bits >> = Data2, FromPid ! {recv, FromSocket, {ok, Data3}}, cancel_recv_timeout(StreamID, TRef), Child#child{input=IsFin2, in_buffer=Rest, is_recv=false}; diff --git a/src/cowboy_static.erl b/src/cowboy_static.erl index 99ad82e..b2a8302 100644 --- a/src/cowboy_static.erl +++ b/src/cowboy_static.erl @@ -134,7 +134,7 @@ good_path_check_test_() -> ], [{P, fun() -> case fullpath(P) of - << "/home/cowboy/", _/binary >> -> ok + << "/home/cowboy/", _/bits >> -> ok end end} || P <- Tests]. @@ -145,7 +145,7 @@ bad_path_check_test_() -> ], [{P, fun() -> error = case fullpath(P) of - << "/home/cowboy/", _/binary >> -> ok; + << "/home/cowboy/", _/bits >> -> ok; _ -> error end end} || P <- Tests]. @@ -167,7 +167,7 @@ good_path_win32_check_test_() -> end, [{P, fun() -> case fullpath(P) of - << "c:/home/cowboy/", _/binary >> -> ok + << "c:/home/cowboy/", _/bits >> -> ok end end} || P <- Tests]. @@ -185,7 +185,7 @@ bad_path_win32_check_test_() -> end, [{P, fun() -> error = case fullpath(P) of - << "c:/home/cowboy/", _/binary >> -> ok; + << "c:/home/cowboy/", _/bits >> -> ok; _ -> error end end} || P <- Tests]. diff --git a/src/cowboy_websocket.erl b/src/cowboy_websocket.erl index c700af9..cdd0365 100644 --- a/src/cowboy_websocket.erl +++ b/src/cowboy_websocket.erl @@ -269,7 +269,7 @@ websocket_data(State, Req, HandlerState, << Fin:1, Rsv:3/bits, Opcode:4, 1:1, websocket_data(State, Req, HandlerState, Opcode, Len, MaskKey, Rest, Rsv, Fin); %% When payload length is over 63 bits, the most significant bit MUST be 0. -websocket_data(State, Req, HandlerState, << _:8, 1:1, 127:7, 1:1, _:7, _/binary >>) -> +websocket_data(State, Req, HandlerState, << _:8, 1:1, 127:7, 1:1, _:7, _/bits >>) -> websocket_close(State, Req, HandlerState, {error, badframe}); %% All frames sent from the client to the server are masked. websocket_data(State, Req, HandlerState, << _:8, 0:1, _/bits >>) -> @@ -466,7 +466,7 @@ rotate_mask_key(MaskKey, UnmaskedLen) -> -spec is_utf8(binary()) -> false | binary(). is_utf8(Valid = <<>>) -> Valid; -is_utf8(<< _/utf8, Rest/binary >>) -> +is_utf8(<< _/utf8, Rest/bits >>) -> is_utf8(Rest); %% 2 bytes. Codepages C0 and C1 are invalid; fail early. is_utf8(<< 2#1100000:7, _/bits >>) -> -- cgit v1.2.3