Browse Source

Replace some /binary to /bits in binary pattern matching

We don't need the extra check for multiple of 8 bits.
Loïc Hoguin 10 years ago
parent
commit
bee5ca852b
5 changed files with 28 additions and 28 deletions
  1. 2 2
      src/cowboy_protocol.erl
  2. 18 18
      src/cowboy_router.erl
  3. 2 2
      src/cowboy_spdy.erl
  4. 4 4
      src/cowboy_static.erl
  5. 2 2
      src/cowboy_websocket.erl

+ 2 - 2
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.
 -spec parse_request(binary(), #state{}, non_neg_integer()) -> ok.
 %% Empty lines must be using \r\n.
 %% Empty lines must be using \r\n.
-parse_request(<< $\n, _/binary >>, State, _) ->
+parse_request(<< $\n, _/bits >>, State, _) ->
 	error_terminate(400, State);
 	error_terminate(400, State);
 parse_request(<< $\s, _/bits >>, State, _) ->
 parse_request(<< $\s, _/bits >>, State, _) ->
 	error_terminate(400, State);
 	error_terminate(400, State);
@@ -150,7 +150,7 @@ parse_request(Buffer, State=#state{max_request_line_length=MaxLength,
 		1 when ReqEmpty =:= MaxEmpty ->
 		1 when ReqEmpty =:= MaxEmpty ->
 			error_terminate(400, State);
 			error_terminate(400, State);
 		1 ->
 		1 ->
-			<< _:16, Rest/binary >> = Buffer,
+			<< _:16, Rest/bits >> = Buffer,
 			parse_request(Rest, State, ReqEmpty + 1);
 			parse_request(Rest, State, ReqEmpty + 1);
 		_ ->
 		_ ->
 			parse_method(Buffer, State, <<>>)
 			parse_method(Buffer, State, <<>>)

+ 18 - 18
src/cowboy_router.erl

@@ -82,7 +82,7 @@ compile_paths([{PathMatch, Fields, Handler, Opts}|Tail], Acc)
 		Fields, Handler, Opts}|Tail], Acc);
 		Fields, Handler, Opts}|Tail], Acc);
 compile_paths([{'_', Fields, Handler, Opts}|Tail], Acc) ->
 compile_paths([{'_', Fields, Handler, Opts}|Tail], Acc) ->
 	compile_paths(Tail, [{'_', Fields, Handler, Opts}] ++ 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) ->
 		Acc) ->
 	PathRules = compile_rules(PathMatch, $/, [], [], <<>>),
 	PathRules = compile_rules(PathMatch, $/, [], [], <<>>),
 	Paths = [{lists:reverse(R), Fields, Handler, Opts} || R <- PathRules],
 	Paths = [{lists:reverse(R), Fields, Handler, Opts} || R <- PathRules],
@@ -95,32 +95,32 @@ compile_rules(<<>>, _, Segments, Rules, <<>>) ->
 	[Segments|Rules];
 	[Segments|Rules];
 compile_rules(<<>>, _, Segments, Rules, Acc) ->
 compile_rules(<<>>, _, Segments, Rules, Acc) ->
 	[[Acc|Segments]|Rules];
 	[[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(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, S, [Acc|Segments], Rules, <<>>);
-compile_rules(<< $:, Rest/binary >>, S, Segments, Rules, <<>>) ->
+compile_rules(<< $:, Rest/bits >>, S, Segments, Rules, <<>>) ->
 	{NameBin, Rest2} = compile_binding(Rest, S, <<>>),
 	{NameBin, Rest2} = compile_binding(Rest, S, <<>>),
 	Name = binary_to_atom(NameBin, utf8),
 	Name = binary_to_atom(NameBin, utf8),
 	compile_rules(Rest2, S, Segments, Rules, Name);
 	compile_rules(Rest2, S, Segments, Rules, Name);
-compile_rules(<< $:, _/binary >>, _, _, _, _) ->
+compile_rules(<< $:, _/bits >>, _, _, _, _) ->
 	error(badarg);
 	error(badarg);
-compile_rules(<< $[, $., $., $., $], Rest/binary >>, S, Segments, Rules, Acc)
+compile_rules(<< $[, $., $., $., $], Rest/bits >>, S, Segments, Rules, Acc)
 		when Acc =:= <<>> ->
 		when Acc =:= <<>> ->
 	compile_rules(Rest, S, ['...'|Segments], Rules, 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(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_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);
 	compile_brackets(Rest, S, Segments, Rules);
 %% Open bracket in the middle of a segment.
 %% Open bracket in the middle of a segment.
-compile_rules(<< $[, _/binary >>, _, _, _, _) ->
+compile_rules(<< $[, _/bits >>, _, _, _, _) ->
 	error(badarg);
 	error(badarg);
 %% Missing an open bracket.
 %% Missing an open bracket.
-compile_rules(<< $], _/binary >>, _, _, _, _) ->
+compile_rules(<< $], _/bits >>, _, _, _, _) ->
 	error(badarg);
 	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 >>).
 	compile_rules(Rest, S, Segments, Rules, << Acc/binary, C >>).
 
 
 %% Everything past $: until the segment separator ($. for hosts,
 %% Everything past $: until the segment separator ($. for hosts,
@@ -129,10 +129,10 @@ compile_binding(<<>>, _, <<>>) ->
 	error(badarg);
 	error(badarg);
 compile_binding(Rest = <<>>, _, Acc) ->
 compile_binding(Rest = <<>>, _, Acc) ->
 	{Acc, Rest};
 	{Acc, Rest};
-compile_binding(Rest = << C, _/binary >>, S, Acc)
+compile_binding(Rest = << C, _/bits >>, S, Acc)
 		when C =:= S; C =:= $[; C =:= $] ->
 		when C =:= S; C =:= $[; C =:= $] ->
 	{Acc, Rest};
 	{Acc, Rest};
-compile_binding(<< C, Rest/binary >>, S, Acc) ->
+compile_binding(<< C, Rest/bits >>, S, Acc) ->
 	compile_binding(Rest, S, << Acc/binary, C >>).
 	compile_binding(Rest, S, << Acc/binary, C >>).
 
 
 compile_brackets(Rest, S, Segments, Rules) ->
 compile_brackets(Rest, S, Segments, Rules) ->
@@ -146,14 +146,14 @@ compile_brackets(Rest, S, Segments, Rules) ->
 compile_brackets_split(<<>>, _, _) ->
 compile_brackets_split(<<>>, _, _) ->
 	error(badarg);
 	error(badarg);
 %% Make sure we don't confuse the closing bracket we're looking for.
 %% 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(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);
 	compile_brackets_split(Rest, << Acc/binary, C >>, N - 1);
 %% That's the right one.
 %% That's the right one.
-compile_brackets_split(<< $], Rest/binary >>, Acc, 0) ->
+compile_brackets_split(<< $], Rest/bits >>, Acc, 0) ->
 	{Acc, Rest};
 	{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).
 	compile_brackets_split(Rest, << Acc/binary, C >>, N).
 
 
 -spec execute(Req, Env)
 -spec execute(Req, Env)

+ 2 - 2
src/cowboy_spdy.erl

@@ -123,7 +123,7 @@ loop(State=#state{parent=Parent, socket=Socket, transport=Transport,
 					FromPid ! {recv, FromSocket, {ok, InBuffer}},
 					FromPid ! {recv, FromSocket, {ok, InBuffer}},
 					loop(replace_child(Child#child{in_buffer= <<>>}, State));
 					loop(replace_child(Child#child{in_buffer= <<>>}, State));
 				byte_size(InBuffer) >= Length ->
 				byte_size(InBuffer) >= Length ->
-					<< Data:Length/binary, Rest/binary >> = InBuffer,
+					<< Data:Length/binary, Rest/bits >> = InBuffer,
 					FromPid ! {recv, FromSocket, {ok, Data}},
 					FromPid ! {recv, FromSocket, {ok, Data}},
 					loop(replace_child(Child#child{in_buffer=Rest}, State));
 					loop(replace_child(Child#child{in_buffer=Rest}, State));
 				true ->
 				true ->
@@ -293,7 +293,7 @@ handle_frame(State, {data, StreamID, IsFin, Data}) ->
 			Child#child{input=IsFin2, in_buffer= <<>>, is_recv=false};
 			Child#child{input=IsFin2, in_buffer= <<>>, is_recv=false};
 		{passive, FromSocket, FromPid, Length, TRef}
 		{passive, FromSocket, FromPid, Length, TRef}
 				when byte_size(Data2) >= Length ->
 				when byte_size(Data2) >= Length ->
-			<< Data3:Length/binary, Rest/binary >> = Data2,
+			<< Data3:Length/binary, Rest/bits >> = Data2,
 			FromPid ! {recv, FromSocket, {ok, Data3}},
 			FromPid ! {recv, FromSocket, {ok, Data3}},
 			cancel_recv_timeout(StreamID, TRef),
 			cancel_recv_timeout(StreamID, TRef),
 			Child#child{input=IsFin2, in_buffer=Rest, is_recv=false};
 			Child#child{input=IsFin2, in_buffer=Rest, is_recv=false};

+ 4 - 4
src/cowboy_static.erl

@@ -134,7 +134,7 @@ good_path_check_test_() ->
 	],
 	],
 	[{P, fun() ->
 	[{P, fun() ->
 		case fullpath(P) of
 		case fullpath(P) of
-			<< "/home/cowboy/", _/binary >> -> ok
+			<< "/home/cowboy/", _/bits >> -> ok
 		end
 		end
 	end} || P <- Tests].
 	end} || P <- Tests].
 
 
@@ -145,7 +145,7 @@ bad_path_check_test_() ->
 	],
 	],
 	[{P, fun() ->
 	[{P, fun() ->
 		error = case fullpath(P) of
 		error = case fullpath(P) of
-			<< "/home/cowboy/", _/binary >> -> ok;
+			<< "/home/cowboy/", _/bits >> -> ok;
 			_ -> error
 			_ -> error
 		end
 		end
 	end} || P <- Tests].
 	end} || P <- Tests].
@@ -167,7 +167,7 @@ good_path_win32_check_test_() ->
 	end,
 	end,
 	[{P, fun() ->
 	[{P, fun() ->
 		case fullpath(P) of
 		case fullpath(P) of
-			<< "c:/home/cowboy/", _/binary >> -> ok
+			<< "c:/home/cowboy/", _/bits >> -> ok
 		end
 		end
 	end} || P <- Tests].
 	end} || P <- Tests].
 
 
@@ -185,7 +185,7 @@ bad_path_win32_check_test_() ->
 	end,
 	end,
 	[{P, fun() ->
 	[{P, fun() ->
 		error = case fullpath(P) of
 		error = case fullpath(P) of
-			<< "c:/home/cowboy/", _/binary >> -> ok;
+			<< "c:/home/cowboy/", _/bits >> -> ok;
 			_ -> error
 			_ -> error
 		end
 		end
 	end} || P <- Tests].
 	end} || P <- Tests].

+ 2 - 2
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,
 	websocket_data(State, Req, HandlerState,
 		Opcode, Len, MaskKey, Rest, Rsv, Fin);
 		Opcode, Len, MaskKey, Rest, Rsv, Fin);
 %% When payload length is over 63 bits, the most significant bit MUST be 0.
 %% 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});
 	websocket_close(State, Req, HandlerState, {error, badframe});
 %% All frames sent from the client to the server are masked.
 %% All frames sent from the client to the server are masked.
 websocket_data(State, Req, HandlerState, << _:8, 0:1, _/bits >>) ->
 websocket_data(State, Req, HandlerState, << _:8, 0:1, _/bits >>) ->
@@ -466,7 +466,7 @@ rotate_mask_key(MaskKey, UnmaskedLen) ->
 -spec is_utf8(binary()) -> false | binary().
 -spec is_utf8(binary()) -> false | binary().
 is_utf8(Valid = <<>>) ->
 is_utf8(Valid = <<>>) ->
 	Valid;
 	Valid;
-is_utf8(<< _/utf8, Rest/binary >>) ->
+is_utf8(<< _/utf8, Rest/bits >>) ->
 	is_utf8(Rest);
 	is_utf8(Rest);
 %% 2 bytes. Codepages C0 and C1 are invalid; fail early.
 %% 2 bytes. Codepages C0 and C1 are invalid; fail early.
 is_utf8(<< 2#1100000:7, _/bits >>) ->
 is_utf8(<< 2#1100000:7, _/bits >>) ->