mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-15 12:40:25 +00:00
Merge branch 'fix/loop_socket_passive' of git://github.com/keynslug/cowboy
This commit is contained in:
commit
5dec37bb9e
1 changed files with 21 additions and 2 deletions
|
@ -195,7 +195,7 @@ handler_loop(Req, State=#state{loop_buffer_size=NbBytes,
|
||||||
handler_before_loop(Req, State#state{resp_sent=true},
|
handler_before_loop(Req, State#state{resp_sent=true},
|
||||||
Handler, HandlerState);
|
Handler, HandlerState);
|
||||||
{timeout, TRef, ?MODULE} ->
|
{timeout, TRef, ?MODULE} ->
|
||||||
terminate_request(Req, State, Handler, HandlerState,
|
handler_after_loop(Req, State, Handler, HandlerState,
|
||||||
{normal, timeout});
|
{normal, timeout});
|
||||||
{timeout, OlderTRef, ?MODULE} when is_reference(OlderTRef) ->
|
{timeout, OlderTRef, ?MODULE} when is_reference(OlderTRef) ->
|
||||||
handler_before_loop(Req, State, Handler, HandlerState);
|
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) ->
|
handler_call(Req, State, Handler, HandlerState, Message) ->
|
||||||
try Handler:info(Message, Req, HandlerState) of
|
try Handler:info(Message, Req, HandlerState) of
|
||||||
{ok, Req2, HandlerState2} ->
|
{ok, Req2, HandlerState2} ->
|
||||||
terminate_request(Req2, State, Handler, HandlerState2,
|
handler_after_loop(Req2, State, Handler, HandlerState2,
|
||||||
{normal, shutdown});
|
{normal, shutdown});
|
||||||
{loop, Req2, HandlerState2} ->
|
{loop, Req2, HandlerState2} ->
|
||||||
handler_before_loop(Req2, State, Handler, HandlerState2);
|
handler_before_loop(Req2, State, Handler, HandlerState2);
|
||||||
|
@ -230,6 +230,25 @@ handler_call(Req, State, Handler, HandlerState, Message) ->
|
||||||
error_terminate(Req, State)
|
error_terminate(Req, State)
|
||||||
end.
|
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(),
|
-spec terminate_request(Req, #state{}, module(), any(),
|
||||||
{normal, timeout | shutdown} | {error, atom()}) ->
|
{normal, timeout | shutdown} | {error, atom()}) ->
|
||||||
{ok, Req, cowboy_middleware:env()} when Req::cowboy_req:req().
|
{ok, Req, cowboy_middleware:env()} when Req::cowboy_req:req().
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue