Skip to content

Commit

Permalink
Remove the onrequest hook
Browse files Browse the repository at this point in the history
It was redundant with middlewares. Allows us to save a few operations
for every incoming requests.
  • Loading branch information
essen committed Sep 24, 2014
1 parent 2525967 commit aa4d86b
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 258 deletions.
3 changes: 0 additions & 3 deletions ROADMAP.md
Expand Up @@ -45,9 +45,6 @@ the halt tuple. The error tuple will therefore be removed.

### Hooks

The `onrequest` hook will be removed. It can easily be replaced
by a middleware.

The interface of the `onresponse` hook will change. There has
been a number of issues and added complexity with the current
interface that warrant fixing. The main problem is that the
Expand Down
48 changes: 5 additions & 43 deletions doc/src/guide/hooks.ezdoc
@@ -1,45 +1,7 @@
::: Hooks

Cowboy provides two hooks. `onrequest` is called once the request
line and headers have been received. `onresponse` is called just
before sending the response.

:: Onrequest

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.
Hooks allow the user to customize Cowboy's behavior during specific
operations.

:: Onresponse

Expand All @@ -48,9 +10,9 @@ 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. If a reply
is sent, the hook MUST explicitly provide all headers that are needed.
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.

Expand Down
Binary file modified doc/src/guide/http_req_resp.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit aa4d86b

Please sign in to comment.