From 2fa1e5392802a271eb530f782e293d2a2a7895a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Mon, 6 Jun 2011 13:48:41 +0200 Subject: Compile the binary match pattern for websockets only once. --- src/cowboy_http_websocket.erl | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/cowboy_http_websocket.erl b/src/cowboy_http_websocket.erl index 8acc842..1b0691f 100644 --- a/src/cowboy_http_websocket.erl +++ b/src/cowboy_http_websocket.erl @@ -23,12 +23,14 @@ origin = undefined :: undefined | binary(), challenge = undefined :: undefined | binary(), timeout = infinity :: timeout(), - messages = undefined :: undefined | {atom(), atom(), atom()} + messages = undefined :: undefined | {atom(), atom(), atom()}, + eop :: tuple() }). -spec upgrade(module(), any(), #http_req{}) -> ok. upgrade(Handler, Opts, Req) -> - case catch websocket_upgrade(#state{handler=Handler, opts=Opts}, Req) of + EOP = binary:compile_pattern(<< 255 >>), + case catch websocket_upgrade(#state{handler=Handler, opts=Opts, eop=EOP}, Req) of {ok, State, Req2} -> handler_init(State, Req2); {'EXIT', _Reason} -> upgrade_error(Req) end. @@ -135,8 +137,8 @@ websocket_data(State, Req, HandlerState, Data) -> %% We do not support any frame type other than 0 yet. Just like the specs. -spec websocket_frame(#state{}, #http_req{}, any(), binary(), byte()) -> ok. -websocket_frame(State, Req, HandlerState, Data, 0) -> - case binary:match(Data, << 255 >>) of +websocket_frame(State=#state{eop=EOP}, Req, HandlerState, Data, 0) -> + case binary:match(Data, EOP) of {Pos, 1} -> Pos2 = Pos - 1, << 0, Frame:Pos2/binary, 255, Rest/bits >> = Data, -- cgit v1.2.3