aboutsummaryrefslogtreecommitdiffstats
path: root/doc/src/manual/cowboy_middleware.asciidoc
blob: 180fb0ae425e79dbf3f3a79dae4cb2eb38275763 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
= cowboy_middleware(3)

== Name

cowboy_middleware - Middlewares

== Description

The module `cowboy_middleware` defines a callback interface for
Cowboy middlewares.

Middlewares process the request sequentially in the order they
are configured.

== Callbacks

Middlewares implement the following interface:

[source,erlang]
----
execute(Req, Env)
    -> {ok, Req, Env}
     | {suspend, module(), atom(), [any()]}
     | {stop, Req}

Req :: cowboy_req:req()
Env :: cowboy_middleware:env()
----

The `execute/2` is the only callback that needs to be
implemented. It must execute the middleware and return
with instructions for Cowboy.

ok::

Cowboy should continue processing the request using the
returned Req object and environment.

suspend::

Cowboy will hibernate the process. When resuming, Cowboy
will apply the returned module, function and arguments.

stop::

Cowboy will stop middleware execution. No other middleware
will be executed. This effectively ends the processing of
the request.

// @todo No need to return the Req when stopping. Fix in 3.0.

== Types

=== env()

[source,erlang]
----
env() :: #{atom() => any()}
----

Middleware environment.

A new environment is created for every request. The initial
environment contained the user configured environment values
(like `dispatch` for example) plus the `listener` value which
contains the name of the listener for this connection.

Middlewares may modify the environment as necessary.

== Changelog

* *2.0*: The `env` type is now a map instead of a proplist.
* *1.0*: Behavior introduced.

== See also

link:man:cowboy(7)[cowboy(7)]