mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 20:30:23 +00:00
Add a function to lock request responses instead of inlining
Just more cleanup in order to have req() opaque.
This commit is contained in:
parent
8d5f8db90a
commit
f205d44518
3 changed files with 9 additions and 2 deletions
|
@ -383,7 +383,7 @@ handler_call(HandlerState, Req, State=#state{handler={Handler, Opts}},
|
||||||
-spec handler_terminate(any(), cowboy_req:req(), #state{}) -> ok.
|
-spec handler_terminate(any(), cowboy_req:req(), #state{}) -> ok.
|
||||||
handler_terminate(HandlerState, Req, #state{handler={Handler, Opts}}) ->
|
handler_terminate(HandlerState, Req, #state{handler={Handler, Opts}}) ->
|
||||||
try
|
try
|
||||||
Handler:terminate(Req#http_req{resp_state=locked}, HandlerState)
|
Handler:terminate(cowboy_req:lock(Req), HandlerState)
|
||||||
catch Class:Reason ->
|
catch Class:Reason ->
|
||||||
PLReq = cowboy_req:to_list(Req),
|
PLReq = cowboy_req:to_list(Req),
|
||||||
error_logger:error_msg(
|
error_logger:error_msg(
|
||||||
|
|
|
@ -103,6 +103,7 @@
|
||||||
|
|
||||||
%% Misc API.
|
%% Misc API.
|
||||||
-export([compact/1]).
|
-export([compact/1]).
|
||||||
|
-export([lock/1]).
|
||||||
-export([to_list/1]).
|
-export([to_list/1]).
|
||||||
-export([transport/1]).
|
-export([transport/1]).
|
||||||
|
|
||||||
|
@ -908,6 +909,12 @@ compact(Req) ->
|
||||||
bindings=undefined, headers=[],
|
bindings=undefined, headers=[],
|
||||||
p_headers=[], cookies=[]}.
|
p_headers=[], cookies=[]}.
|
||||||
|
|
||||||
|
%% @doc Prevent any further responses.
|
||||||
|
%% @private
|
||||||
|
-spec lock(Req) -> Req when Req::req().
|
||||||
|
lock(Req) ->
|
||||||
|
Req#http_req{resp_state=locked}.
|
||||||
|
|
||||||
%% @doc Convert the Req object to a list of key/values.
|
%% @doc Convert the Req object to a list of key/values.
|
||||||
-spec to_list(req()) -> [{atom(), any()}].
|
-spec to_list(req()) -> [{atom(), any()}].
|
||||||
to_list(Req) ->
|
to_list(Req) ->
|
||||||
|
|
|
@ -897,7 +897,7 @@ respond(Req, State, StatusCode) ->
|
||||||
terminate(Req, #state{handler=Handler, handler_state=HandlerState}) ->
|
terminate(Req, #state{handler=Handler, handler_state=HandlerState}) ->
|
||||||
case erlang:function_exported(Handler, rest_terminate, 2) of
|
case erlang:function_exported(Handler, rest_terminate, 2) of
|
||||||
true -> ok = Handler:rest_terminate(
|
true -> ok = Handler:rest_terminate(
|
||||||
Req#http_req{resp_state=locked}, HandlerState);
|
cowboy_req:lock(Req), HandlerState);
|
||||||
false -> ok
|
false -> ok
|
||||||
end,
|
end,
|
||||||
{ok, Req}.
|
{ok, Req}.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue