Skip to content
Browse files

The onresponse hook now receives 4 arguments, including the body

We do not always provide the body however. It is not available
when using chunked replies, or when using set_resp_body_fun.
  • Loading branch information...
1 parent 7367f07 commit 76cd1e40c933e60ed507474cceea4e25e10db225 @essen essen committed
Showing with 5 additions and 5 deletions.
  1. +2 −2 src/cowboy_protocol.erl
  2. +1 −1 src/cowboy_req.erl
  3. +2 −2 test/http_SUITE.erl
View
4 src/cowboy_protocol.erl
@@ -34,7 +34,7 @@
%% any dispatching is done. Host info, path info and bindings are thus
%% not available at this point.</dd>
%% <dt>onresponse</dt><dd>Optional fun that allows replacing a response
-%% sent by the application based on its status code or headers.</dd>
+%% sent by the application.</dd>
%% <dt>timeout</dt><dd>Time in milliseconds before an idle
%% connection is closed. Defaults to 5000 milliseconds.</dd>
%% </dl>
@@ -56,7 +56,7 @@
-type onrequest_fun() :: fun((Req) -> Req).
-type onresponse_fun() ::
- fun((cowboy_http:status(), cowboy_http:headers(), Req) -> Req).
+ fun((cowboy_http:status(), cowboy_http:headers(), iodata(), Req) -> Req).
-export_type([onrequest_fun/0]).
-export_type([onresponse_fun/0]).
View
2 src/cowboy_req.erl
@@ -1108,7 +1108,7 @@ response(Status, Headers, RespHeaders, DefaultHeaders, Body, Req=#http_req{
FullHeaders = response_merge_headers(Headers, RespHeaders, DefaultHeaders),
Req2 = case OnResponse of
undefined -> Req;
- OnResponse -> OnResponse(Status, FullHeaders,
+ OnResponse -> OnResponse(Status, FullHeaders, Body,
%% Don't call 'onresponse' from the hook itself.
Req#http_req{resp_headers=[], resp_body= <<>>,
onresponse=undefined})
View
4 test/http_SUITE.erl
@@ -188,7 +188,7 @@ init_per_group(onresponse, Config) ->
{ok, _} = cowboy:start_http(onresponse, 100, [{port, Port}], [
{dispatch, init_dispatch(Config)},
{max_keepalive, 50},
- {onresponse, fun onresponse_hook/3},
+ {onresponse, fun onresponse_hook/4},
{timeout, 500}
]),
{ok, Client} = cowboy_client:init([]),
@@ -623,7 +623,7 @@ onresponse_reply(Config) ->
{error, closed} = cowboy_client:response_body(Client3).
%% Hook for the above onresponse tests.
-onresponse_hook(_, Headers, Req) ->
+onresponse_hook(_, Headers, _, Req) ->
{ok, Req2} = cowboy_req:reply(
<<"777 Lucky">>, [{<<"x-hook">>, <<"onresponse">>}|Headers], Req),
Req2.

0 comments on commit 76cd1e4

Please sign in to comment.
Something went wrong with that request. Please try again.