Return-Path: X-Original-To: apmail-couchdb-commits-archive@www.apache.org Delivered-To: apmail-couchdb-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 6363617C8F for ; Mon, 29 Jun 2015 21:46:11 +0000 (UTC) Received: (qmail 13970 invoked by uid 500); 29 Jun 2015 21:46:08 -0000 Delivered-To: apmail-couchdb-commits-archive@couchdb.apache.org Received: (qmail 13815 invoked by uid 500); 29 Jun 2015 21:46:08 -0000 Mailing-List: contact commits-help@couchdb.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@couchdb.apache.org Delivered-To: mailing list commits@couchdb.apache.org Received: (qmail 12835 invoked by uid 99); 29 Jun 2015 21:46:08 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 29 Jun 2015 21:46:08 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id EFB65E360E; Mon, 29 Jun 2015 21:46:07 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: robertkowalski@apache.org To: commits@couchdb.apache.org Date: Mon, 29 Jun 2015 21:46:30 -0000 Message-Id: <649386a2a3f84f4784d19a085b3a6132@git.apache.org> In-Reply-To: <41909276fb954bfdb4649bc815a971dd@git.apache.org> References: <41909276fb954bfdb4649bc815a971dd@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [24/50] couch commit: updated refs/heads/COUCHDB-2734-header-date to f3e022c refactor of request/5 function Project: http://git-wip-us.apache.org/repos/asf/couchdb-couch/repo Commit: http://git-wip-us.apache.org/repos/asf/couchdb-couch/commit/3961edcb Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch/tree/3961edcb Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch/diff/3961edcb Branch: refs/heads/COUCHDB-2734-header-date Commit: 3961edcb51caed422abb6c6ce42c00bca52ab785 Parents: de79c0a Author: Łukasz Lalik Authored: Wed Dec 25 13:13:46 2013 +0100 Committer: Łukasz Lalik Committed: Wed Dec 25 13:13:46 2013 +0100 ---------------------------------------------------------------------- examples/websocket/websocket.erl | 4 +-- src/mochiweb_websocket.erl | 66 ++++++++++++++++------------------- 2 files changed, 33 insertions(+), 37 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/3961edcb/examples/websocket/websocket.erl ---------------------------------------------------------------------- diff --git a/examples/websocket/websocket.erl b/examples/websocket/websocket.erl index 8d4eeb0..b70a0de 100644 --- a/examples/websocket/websocket.erl +++ b/examples/websocket/websocket.erl @@ -26,8 +26,8 @@ start_link() -> Loop = fun (Req) -> - ?MODULE:loop(Req) - end, + ?MODULE:loop(Req) + end, mochiweb_http:start_link([ {name, client_access}, http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/3961edcb/src/mochiweb_websocket.erl ---------------------------------------------------------------------- diff --git a/src/mochiweb_websocket.erl b/src/mochiweb_websocket.erl index 677091b..3ef5c5e 100644 --- a/src/mochiweb_websocket.erl +++ b/src/mochiweb_websocket.erl @@ -46,33 +46,18 @@ request(Socket, Body, State, WsVersion, ReplyChannel) -> {tcp, _, WsFrames} -> {M, F} = Body, - case WsVersion of - hybi -> - Reply = fun(close) -> - mochiweb_socket:close(Socket), - exit(normal); - (Payload) -> - NewState = M:F(Payload, State, ReplyChannel), - loop(Socket, Body, NewState, WsVersion, ReplyChannel) - end, - - try parse_hybi_frames(Socket, WsFrames, []) of - Parsed -> process_frames(Parsed, Reply, []) - catch - _:_ -> Reply(close) - end; - - hixie -> - try parse_hixie_frames(WsFrames, []) of - Payload -> - NewState = M:F(Payload, State), - loop(Socket, Body, NewState, WsVersion, ReplyChannel) - catch - _:_ -> - mochiweb_socket:close(Socket), - exit(normal) - end - + case parse_frames(WsVersion, WsFrames, Socket) of + close -> + mochiweb_socket:close(Socket), + exit(normal); + + error -> + mochiweb_socket:close(Socket), + exit(normal); + + Payload -> + NewState = M:F(Payload, State, ReplyChannel), + loop(Socket, Body, NewState, WsVersion, ReplyChannel) end; _ -> @@ -155,20 +140,31 @@ hixie_handshake(Host, Path, Key1, Key2, Body, Origin) -> Challenge}, {hixie, Response}. +parse_frames(hybi, Frames, Socket) -> + try parse_hybi_frames(Socket, Frames, []) of + Parsed -> process_frames(Parsed, []) + catch + _:_ -> error + end; + +parse_frames(hixie, Frames, Socket) -> + try parse_hixie_frames(Frames, []) of + Payload -> Payload + catch + _:_ -> error + end. + %% %% Websockets internal functions for RFC6455 and hybi draft %% -process_frames([], Reply, Acc) -> - Reply(lists:reverse(Acc)); +process_frames([], Acc) -> + lists:reverse(Acc); -process_frames([{Opcode, Payload} | Rest], Reply, Acc) -> +process_frames([{Opcode, Payload} | Rest], Acc) -> case Opcode of - 8 -> - Reply(lists:reverse(Acc)), - Reply(close); - + 8 -> close; _ -> - process_frames(Rest, Reply, [Payload | Acc]) + process_frames(Rest, [Payload | Acc]) end. parse_hybi_frames(_, <<>>, Acc) ->