From 88414e36b4199f10ed5b29d5fffda037c5d81eca Mon Sep 17 00:00:00 2001
From: Adam Cammack <acammack42@gmail.com>
Date: Fri, 1 Mar 2013 18:02:33 -0600
Subject: Add an example of onresponse hooks

Also fix the guide entry on hooks.
---
 guide/hooks.md | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

(limited to 'guide')

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.
-- 
cgit v1.2.3