aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2013-01-18 00:54:41 +0100
committerLoïc Hoguin <[email protected]>2013-01-18 00:54:41 +0100
commit2f0d68362245535f121f65a2b1b1723d8453a661 (patch)
tree34e24040790acf31c855c3cdc761843a27e78617
parent7f2409a4bb3ac15696fedcd0d53cbf3b17ce8c85 (diff)
downloadcowboy-2f0d68362245535f121f65a2b1b1723d8453a661.tar.gz
cowboy-2f0d68362245535f121f65a2b1b1723d8453a661.tar.bz2
cowboy-2f0d68362245535f121f65a2b1b1723d8453a661.zip
Add hooks documentation to the guide
-rw-r--r--guide/hooks.md69
1 files changed, 67 insertions, 2 deletions
diff --git a/guide/hooks.md b/guide/hooks.md
index ba48c4a..d4b520a 100644
--- a/guide/hooks.md
+++ b/guide/hooks.md
@@ -4,9 +4,74 @@ Hooks
On request
----------
-@todo Describe.
+The `onrequest` hook is called as soon as Cowboy finishes fetching
+the request headers. It occurs before any other processing, including
+routing. It can be used to perform any modification needed on the
+request object before continuing with the processing. If a reply is
+sent inside this hook, then Cowboy will move on to the next request,
+skipping any subsequent handling.
+
+This hook is a function that takes a request object as argument,
+and returns a request object. This function MUST NOT crash. Cowboy
+will not send any reply if a crash occurs in this function.
+
+You can specify the `onrequest` hook when creating the listener,
+inside the request options.
+
+``` erlang
+cowboy:start_http(my_http_listener, 100,
+ [{port, 8080}],
+ [
+ {env, [{dispatch, Dispatch}]},
+ {onrequest, fun ?MODULE:debug_hook/1}
+ ]
+).
+```
+
+The following hook function prints the request object everytime a
+request is received. This can be useful for debugging, for example.
+
+``` erlang
+debug_hook(Req) ->
+ erlang:display(Req),
+ Req.
+```
+
+Make sure to always return the last request object obtained.
On response
-----------
-@todo Describe.
+The `onresponse` hook is called right before sending the response
+to the socket. It can be used for the purposes of logging responses,
+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.
+
+You can specify the `onresponse` hook when creating the listener also.
+
+``` erlang
+cowboy:start_http(my_http_listener, 100,
+ [{port, 8080}],
+ [
+ {env, [{dispatch, Dispatch}]},
+ {onresponse, fun ?MODULE:custom_404_hook/4}
+ ]
+).
+```
+
+The following hook function will provide a custom body for 404 errors
+when it has not been provided before, and will let Cowboy proceed with
+the default response otherwise.
+
+``` erlang
+custom_404_hook(404, Headers, <<>>, Req) ->
+ {ok, Req2} = cowboy_req:reply(404, Headers, <<"404 Not Found.">>, Req),
+ Req2;
+custom_404_hook(_, _, _, Req) ->
+ Req.
+```
+
+Again, make sure to always return the last request object obtained.