| 1 |
[[streams]] |
| 2 |
== Streams |
| 3 |
|
| 4 |
A stream is the set of messages that form an HTTP |
| 5 |
request/response pair. |
| 6 |
|
| 7 |
The term stream comes from HTTP/2. In Cowboy, it is |
| 8 |
also used when talking about HTTP/1.1 or HTTP/1.0. |
| 9 |
It should not be confused with streaming the request |
| 10 |
or response body. |
| 11 |
|
| 12 |
All versions of HTTP allow clients to initiate |
| 13 |
streams. HTTP/2 is the only one also allowing servers, |
| 14 |
through its server push feature. Both client and |
| 15 |
server-initiated streams go through the same process |
| 16 |
in Cowboy. |
| 17 |
|
| 18 |
=== Stream handlers |
| 19 |
|
| 20 |
link:man:cowboy_stream(3)[Stream handlers] |
| 21 |
must implement five different callbacks. |
| 22 |
Four of them are directly related; one is special. |
| 23 |
|
| 24 |
All callbacks receives the stream ID as first argument. |
| 25 |
|
| 26 |
Most of them can return a list of commands to be executed |
| 27 |
by Cowboy. When callbacks are chained, it is possible to |
| 28 |
intercept and modify these commands. This can be useful |
| 29 |
for modifying responses for example. |
| 30 |
|
| 31 |
The `init/3` callback is invoked when a new request |
| 32 |
comes in. It receives the Req object and the protocol options |
| 33 |
for this listener. |
| 34 |
|
| 35 |
The `data/4` callback is invoked when data from the request |
| 36 |
body is received. It receives both this data and a flag |
| 37 |
indicating whether more is to be expected. |
| 38 |
|
| 39 |
The `info/3` callback is invoked when an Erlang message is |
| 40 |
received for this stream. They will typically be messages |
| 41 |
sent by the request process. |
| 42 |
|
| 43 |
Finally the `terminate/3` callback is invoked with the |
| 44 |
terminate reason for the stream. The return value is ignored. |
| 45 |
Note that as with all terminate callbacks in Erlang, there |
| 46 |
is no strong guarantee that it will be called. |
| 47 |
|
| 48 |
The special callback `early_error/5` is called when an error |
| 49 |
occurs before the request headers were fully received and |
| 50 |
Cowboy is sending a response. It receives the partial Req |
| 51 |
object, the error reason, the protocol options and the response |
| 52 |
Cowboy will send. This response must be returned, possibly |
| 53 |
modified. |
| 54 |
|
| 55 |
=== Built-in handlers |
| 56 |
|
| 57 |
Cowboy comes with four handlers. |
| 58 |
|
| 59 |
link:man:cowboy_stream_h(3)[cowboy_stream_h] is the default |
| 60 |
stream handler. It is the core of much of the functionality |
| 61 |
of Cowboy. All chains of stream handlers should call it last. |
| 62 |
|
| 63 |
link:man:cowboy_compress_h(3)[cowboy_compress_h] will |
| 64 |
automatically compress responses when possible. It is not |
| 65 |
enabled by default. It is a good example for writing your |
| 66 |
own handlers that will modify responses. |
| 67 |
|
| 68 |
link:man:cowboy_decompress_h(3)[cowboy_decompress_h] will |
| 69 |
automatically decompress request bodies when possible. |
| 70 |
It is not enabled by default. It is a good example for |
| 71 |
writing your own handlers that will modify requests. |
| 72 |
|
| 73 |
link:man:cowboy_metrics_h(3)[cowboy_metrics_h] gathers |
| 74 |
metrics about a stream then passes them to a configurable |
| 75 |
function. It is not enabled by default. |
| 76 |
|
| 77 |
link:man:cowboy_tracer_h(3)[cowboy_tracer_h] can be used to |
| 78 |
conditionally trace streams based on the contents of the |
| 79 |
request or its origin. Trace events are passed to a |
| 80 |
configurable function. It is not enabled by default. |