diff options
author | Loïc Hoguin <[email protected]> | 2016-01-14 13:35:25 +0100 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2016-01-14 13:37:20 +0100 |
commit | 4023e7f4e429179fd9c2cce4487c33646c6bd327 (patch) | |
tree | 3c4e26d1b5592958e35297c82ad3069bdb642594 /doc/src/guide/hooks.asciidoc | |
parent | b7d666cfc746f55b0a72ef8d37f703885099daf7 (diff) | |
download | cowboy-4023e7f4e429179fd9c2cce4487c33646c6bd327.tar.gz cowboy-4023e7f4e429179fd9c2cce4487c33646c6bd327.tar.bz2 cowboy-4023e7f4e429179fd9c2cce4487c33646c6bd327.zip |
Convert the documentation to Asciidoc
A few small revisions were made, and Erlang.mk has been updated.
Diffstat (limited to 'doc/src/guide/hooks.asciidoc')
-rw-r--r-- | doc/src/guide/hooks.asciidoc | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/doc/src/guide/hooks.asciidoc b/doc/src/guide/hooks.asciidoc new file mode 100644 index 0000000..fc79f8a --- /dev/null +++ b/doc/src/guide/hooks.asciidoc @@ -0,0 +1,46 @@ +[[hooks]] +== Hooks + +Hooks allow the user to customize Cowboy's behavior during specific +operations. + +=== Onresponse + +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 this function MUST NOT crash. 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. + +[source,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. + +[source,erlang] +---- +custom_404_hook(404, Headers, <<>>, Req) -> + Body = <<"404 Not Found.">>, + Headers2 = lists:keyreplace(<<"content-length">>, 1, Headers, + {<<"content-length">>, integer_to_list(byte_size(Body))}), + cowboy_req:reply(404, Headers2, Body, Req); +custom_404_hook(_, _, _, Req) -> + Req. +---- + +Again, make sure to always return the last request object obtained. |