| 1 |
= cowboy_stream_h(3) |
| 2 |
|
| 3 |
== Name |
| 4 |
|
| 5 |
cowboy_stream_h - Default stream handler |
| 6 |
|
| 7 |
== Description |
| 8 |
|
| 9 |
The module `cowboy_stream_h` is Cowboy's default stream |
| 10 |
handler and defines much of its behavior. It is responsible |
| 11 |
for managing the request process, sending it the request |
| 12 |
body and translating its messages into commands that |
| 13 |
Cowboy understands. |
| 14 |
|
| 15 |
== Options |
| 16 |
|
| 17 |
[source,erlang] |
| 18 |
---- |
| 19 |
opts() :: #{ |
| 20 |
env => cowboy_middleware:env(), |
| 21 |
middlewares => [module()], |
| 22 |
shutdown_timeout => timeout() |
| 23 |
} |
| 24 |
---- |
| 25 |
|
| 26 |
Configuration for the default stream handler. |
| 27 |
|
| 28 |
The default value is given next to the option name: |
| 29 |
|
| 30 |
env (#{}):: |
| 31 |
|
| 32 |
Middleware environment. |
| 33 |
|
| 34 |
middlewares ([cowboy_router, cowboy_handler]):: |
| 35 |
|
| 36 |
Middlewares to run for every request. |
| 37 |
|
| 38 |
shutdown_timeout (5000):: |
| 39 |
|
| 40 |
Time in ms Cowboy will wait for child processes to shut down before killing them. |
| 41 |
|
| 42 |
== Events |
| 43 |
|
| 44 |
The default stream handler spawns the request process |
| 45 |
and receives its exit signal when it terminates. It |
| 46 |
will stop the stream once its receives it. |
| 47 |
|
| 48 |
Because this stream handler converts events from the |
| 49 |
request process into commands, other stream handlers |
| 50 |
may not work properly if they are executed after the |
| 51 |
default stream handler. Always be mindful of in which |
| 52 |
order stream handlers will get executed. |
| 53 |
|
| 54 |
=== Request body |
| 55 |
|
| 56 |
The default stream handler implements the `read_body` |
| 57 |
mechanism. In addition to reading the body, the handler |
| 58 |
will automatically handle the `expect: 100-continue` |
| 59 |
header and send a 100 Continue response. |
| 60 |
|
| 61 |
Normally one would use |
| 62 |
link:man:cowboy_req:read_body(3)[cowboy_req:read_body(3)] |
| 63 |
to read the request body. The default stream handler |
| 64 |
will buffer data until the amount gets larger than the |
| 65 |
requested length before sending it. Alternatively, it |
| 66 |
will send whatever data it has when the period timeout |
| 67 |
triggers. Depending on the protocol, the flow control |
| 68 |
window is updated to allow receiving data for the |
| 69 |
requested length. |
| 70 |
|
| 71 |
The default stream handler also comes with an automatic |
| 72 |
mode for reading the request body. This can be used by |
| 73 |
sending the event message `{read_body, Pid, Ref, auto, infinity}` |
| 74 |
using link:man:cowboy_req:cast(3)[cowboy_req:cast(3)]. |
| 75 |
The default stream handler will then send data as soon |
| 76 |
as some becomes available using one of these two |
| 77 |
messages depending on whether body reading was completed: |
| 78 |
|
| 79 |
* `{request_body, Ref, nofin, Data}` |
| 80 |
* `{request_body, Ref, fin, BodyLen, Data}` |
| 81 |
|
| 82 |
Depending on the protocol, Cowboy will update the flow |
| 83 |
control window using the size of the data that was read. |
| 84 |
|
| 85 |
Auto mode automatically gets disabled after data has |
| 86 |
been sent to the handler. Therefore in order to continue |
| 87 |
reading data a `read_body` event message must be sent |
| 88 |
after each `request_body` message. |
| 89 |
|
| 90 |
=== Response |
| 91 |
|
| 92 |
In addition it returns a command for any event message |
| 93 |
looking like one of the following commands: `inform`, |
| 94 |
`response`, `headers`, `data`, `trailers`, `push`, |
| 95 |
`switch_protocol`. This is what allows the request |
| 96 |
process to send a response. |
| 97 |
|
| 98 |
== Changelog |
| 99 |
|
| 100 |
* *2.11*: Introduce body reading using auto mode. |
| 101 |
* *2.0*: Module introduced. |
| 102 |
|
| 103 |
== See also |
| 104 |
|
| 105 |
link:man:cowboy(7)[cowboy(7)], |
| 106 |
link:man:cowboy_stream(3)[cowboy_stream(3)], |
| 107 |
link:man:cowboy_compress_h(3)[cowboy_compress_h(3)], |
| 108 |
link:man:cowboy_decompress_h(3)[cowboy_decompress_h(3)], |
| 109 |
link:man:cowboy_metrics_h(3)[cowboy_metrics_h(3)], |
| 110 |
link:man:cowboy_tracer_h(3)[cowboy_tracer_h(3)], |
| 111 |
link:man:cowboy_req:cast(3)[cowboy_req:cast(3)] |