Skip to content

Commit

Permalink
Add a function to lock request responses instead of inlining
Browse files Browse the repository at this point in the history
Just more cleanup in order to have req() opaque.
  • Loading branch information
essen committed Sep 17, 2012
1 parent 8d5f8db commit f205d44
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/cowboy_protocol.erl
Expand Up @@ -383,7 +383,7 @@ handler_call(HandlerState, Req, State=#state{handler={Handler, Opts}},
-spec handler_terminate(any(), cowboy_req:req(), #state{}) -> ok.
handler_terminate(HandlerState, Req, #state{handler={Handler, Opts}}) ->
try
Handler:terminate(Req#http_req{resp_state=locked}, HandlerState)
Handler:terminate(cowboy_req:lock(Req), HandlerState)
catch Class:Reason ->
PLReq = cowboy_req:to_list(Req),
error_logger:error_msg(
Expand Down
7 changes: 7 additions & 0 deletions src/cowboy_req.erl
Expand Up @@ -103,6 +103,7 @@

%% Misc API.
-export([compact/1]).
-export([lock/1]).
-export([to_list/1]).
-export([transport/1]).

Expand Down Expand Up @@ -908,6 +909,12 @@ compact(Req) ->
bindings=undefined, headers=[],
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.
-spec to_list(req()) -> [{atom(), any()}].
to_list(Req) ->
Expand Down
2 changes: 1 addition & 1 deletion src/cowboy_rest.erl
Expand Up @@ -897,7 +897,7 @@ respond(Req, State, StatusCode) ->
terminate(Req, #state{handler=Handler, handler_state=HandlerState}) ->
case erlang:function_exported(Handler, rest_terminate, 2) of
true -> ok = Handler:rest_terminate(
Req#http_req{resp_state=locked}, HandlerState);
cowboy_req:lock(Req), HandlerState);
false -> ok
end,
{ok, Req}.

0 comments on commit f205d44

Please sign in to comment.