diff options
author | Loïc Hoguin <[email protected]> | 2013-02-16 15:37:15 +0100 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2013-02-16 15:37:15 +0100 |
commit | 5dec37bb9e38312dcd03cf353d7564a3d9298bbe (patch) | |
tree | 55d90523986658ae2d5618c6f6af450318829cac /src | |
parent | d305527e8871bf41c6362930848d93e42d523b29 (diff) | |
parent | 3ea855137c7913caae30c5900b0c792e538c779e (diff) | |
download | cowboy-5dec37bb9e38312dcd03cf353d7564a3d9298bbe.tar.gz cowboy-5dec37bb9e38312dcd03cf353d7564a3d9298bbe.tar.bz2 cowboy-5dec37bb9e38312dcd03cf353d7564a3d9298bbe.zip |
Merge branch 'fix/loop_socket_passive' of git://github.com/keynslug/cowboy
Diffstat (limited to 'src')
-rw-r--r-- | src/cowboy_handler.erl | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/src/cowboy_handler.erl b/src/cowboy_handler.erl index 65e22b7..3b0af6f 100644 --- a/src/cowboy_handler.erl +++ b/src/cowboy_handler.erl @@ -195,7 +195,7 @@ handler_loop(Req, State=#state{loop_buffer_size=NbBytes, handler_before_loop(Req, State#state{resp_sent=true}, Handler, HandlerState); {timeout, TRef, ?MODULE} -> - terminate_request(Req, State, Handler, HandlerState, + handler_after_loop(Req, State, Handler, HandlerState, {normal, timeout}); {timeout, OlderTRef, ?MODULE} when is_reference(OlderTRef) -> handler_before_loop(Req, State, Handler, HandlerState); @@ -210,7 +210,7 @@ handler_loop(Req, State=#state{loop_buffer_size=NbBytes, handler_call(Req, State, Handler, HandlerState, Message) -> try Handler:info(Message, Req, HandlerState) of {ok, Req2, HandlerState2} -> - terminate_request(Req2, State, Handler, HandlerState2, + handler_after_loop(Req2, State, Handler, HandlerState2, {normal, shutdown}); {loop, Req2, HandlerState2} -> handler_before_loop(Req2, State, Handler, HandlerState2); @@ -230,6 +230,25 @@ handler_call(Req, State, Handler, HandlerState, Message) -> error_terminate(Req, State) end. +%% It is sometimes important to make a socket passive as it was initially +%% and as it is expected to be by cowboy_protocol, right after we're done +%% with loop handling. The browser may freely pipeline a bunch of requests +%% if previous one was, say, a JSONP long-polling request. +-spec handler_after_loop(Req, #state{}, module(), any(), + {normal, timeout | shutdown} | {error, atom()}) -> + {ok, Req, cowboy_middleware:env()} when Req::cowboy_req:req(). +handler_after_loop(Req, State, Handler, HandlerState, Reason) -> + [Socket, Transport] = cowboy_req:get([socket, transport], Req), + Transport:setopts(Socket, [{active, false}]), + {OK, _Closed, _Error} = Transport:messages(), + Req2 = receive + {OK, Socket, Data} -> + cowboy_req:append_buffer(Data, Req) + after 0 -> + Req + end, + terminate_request(Req2, State, Handler, HandlerState, Reason). + -spec terminate_request(Req, #state{}, module(), any(), {normal, timeout | shutdown} | {error, atom()}) -> {ok, Req, cowboy_middleware:env()} when Req::cowboy_req:req(). |