diff options
author | Adam Cammack <[email protected]> | 2013-03-01 18:02:33 -0600 |
---|---|---|
committer | Adam Cammack <[email protected]> | 2013-03-01 19:54:30 -0600 |
commit | 88414e36b4199f10ed5b29d5fffda037c5d81eca (patch) | |
tree | b8c1aff6602bee191f252ed74c959b0b077b6811 /guide/hooks.md | |
parent | 23b3b038e930646af7f27a439e9dca1cf0c0e453 (diff) | |
download | cowboy-88414e36b4199f10ed5b29d5fffda037c5d81eca.tar.gz cowboy-88414e36b4199f10ed5b29d5fffda037c5d81eca.tar.bz2 cowboy-88414e36b4199f10ed5b29d5fffda037c5d81eca.zip |
Add an example of onresponse hooks
Also fix the guide entry on hooks.
Diffstat (limited to 'guide/hooks.md')
-rw-r--r-- | guide/hooks.md | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/guide/hooks.md b/guide/hooks.md index d4b520a..d7e6c72 100644 --- a/guide/hooks.md +++ b/guide/hooks.md @@ -48,7 +48,8 @@ or for modifying the response headers or body. The best example is providing custom error pages. Note that like the `onrequest` hook, this function MUST NOT crash. -Cowboy may or may not send a reply if this function crashes. +Cowboy may or may not send a reply if this function crashes. If a reply +is sent, the hook MUST explicitly provide all headers that are needed. You can specify the `onresponse` hook when creating the listener also. @@ -68,7 +69,10 @@ the default response otherwise. ``` erlang custom_404_hook(404, Headers, <<>>, Req) -> - {ok, Req2} = cowboy_req:reply(404, Headers, <<"404 Not Found.">>, Req), + Body = <<"404 Not Found.">>, + Headers2 = lists:keyreplace(<<"content-length">>, 1, Headers, + {<<"content-length">>, integer_to_list(byte_size(Body))}), + {ok, Req2} = cowboy_req:reply(404, Headers2, Body, Req), Req2; custom_404_hook(_, _, _, Req) -> Req. |