cowboy_stream.asciidoc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. = cowboy_stream(3)
  2. == Name
  3. cowboy_stream - Stream handlers
  4. == Description
  5. The module `cowboy_stream` defines a callback interface
  6. and a protocol for handling HTTP streams.
  7. An HTTP request and its associated response is called
  8. a stream. A connection may have many streams. In HTTP/1.1
  9. they are executed sequentially, while in HTTP/2 they are
  10. executed concurrently.
  11. Cowboy calls the stream handler for nearly all events
  12. related to a stream. Exceptions vary depending on the
  13. protocol.
  14. Extra care must be taken when implementing stream handlers
  15. to ensure compatibility. While some modification of the
  16. events and commands is allowed, it is generally not a good
  17. idea to completely discard them.
  18. == Callbacks
  19. Stream handlers must implement the following interface:
  20. [source,erlang]
  21. ----
  22. init(StreamID, Req, Opts) -> {Commands, State}
  23. data(StreamID, IsFin, Data, State) -> {Commands, State}
  24. info(StreamID, Info, State) -> {Commands, State}
  25. terminate(StreamID, Reason, State) -> any()
  26. early_error(StreamID, Reason, PartialReq, Resp, Opts) -> Resp
  27. StreamID :: cowboy_stream:streamid()
  28. Req :: cowboy_req:req()
  29. Opts :: cowboy:opts()
  30. Commands :: cowboy_stream:commands()
  31. State :: any()
  32. IsFin :: cowboy_stream:fin()
  33. Data :: binary()
  34. Info :: any()
  35. Reason :: cowboy_stream:reason()
  36. PartialReq - cowboy_req:req(), except all fields are optional
  37. Resp :: cowboy_stream:resp_command()
  38. ----
  39. HTTP/1.1 will initialize a stream only when the request-line
  40. and all headers have been received. When errors occur before
  41. that point Cowboy will call the callback `early_error/5`
  42. with a partial request, the error reason and the response
  43. Cowboy intends to send. All other events go throuh the
  44. stream handler using the normal callbacks.
  45. HTTP/2 will initialize the stream when the `HEADERS` block has
  46. been fully received and decoded. Any protocol error occuring
  47. before that will not result in a response being sent and
  48. will therefore not go through the stream handler. In addition
  49. Cowboy may terminate streams without sending an HTTP response
  50. back.
  51. The stream is initialized by calling `init/3`. All streams
  52. that are initialized will eventually be terminated by calling
  53. `terminate/3`.
  54. When Cowboy receives data for the stream it will call `data/4`.
  55. The data given is the request body after any transfer decoding
  56. has been applied.
  57. When Cowboy receives a message addressed to a stream, or when
  58. Cowboy needs to inform the stream handler that an internal
  59. event has occurred, it will call `info/3`.
  60. [[commands]]
  61. == Commands
  62. Stream handlers can return a list of commands to be executed
  63. from the `init/3`, `data/4` and `info/3` callbacks. In addition,
  64. the `early_error/5` callback must return a response command.
  65. // @todo The logger option and the {log, Level, Format, Args}
  66. // options need to be documented and tested.
  67. The following commands are defined:
  68. [[inform_command]]
  69. === inform
  70. Send an informational response to the client.
  71. [source,erlang]
  72. ----
  73. {inform, cowboy:http_status(), cowboy:http_headers()}
  74. ----
  75. Any number of informational responses may be sent,
  76. but only until the final response is sent.
  77. [[response_command]]
  78. === response
  79. Send a response to the client.
  80. [source,erlang]
  81. ----
  82. {response, cowboy:http_status(), cowboy:http_headers(),
  83. cowboy_req:resp_body()}
  84. ----
  85. No more data can be sent after this command.
  86. Note that in Cowboy it is the `cowboy_req` module that
  87. sets the date and server headers. When using the command
  88. directly those headers will not be added.
  89. [[headers_command]]
  90. === headers
  91. Initiate a response to the client.
  92. [source,erlang]
  93. ----
  94. {headers, cowboy:http_status(), cowboy:http_headers()}
  95. ----
  96. This initiates a response to the client. The stream
  97. will end when a data command with the `fin` flag or
  98. a trailer command is returned.
  99. Note that in Cowboy it is the `cowboy_req` module that
  100. sets the date and server headers. When using the command
  101. directly those headers will not be added.
  102. [[data_command]]
  103. === data
  104. Send data to the client.
  105. [source,erlang]
  106. ----
  107. {data, fin(), cowboy_req:resp_body()}
  108. ----
  109. [[trailers_command]]
  110. === trailers
  111. Send response trailers to the client.
  112. [source,erlang]
  113. ----
  114. {trailers, cowboy:http_headers()}
  115. ----
  116. [[push_command]]
  117. === push
  118. Push a resource to the client.
  119. [source,erlang]
  120. ----
  121. {push, Method, Scheme, Host, inet:port_number(),
  122. Path, Qs, cowboy:http_headers()}
  123. Method = Scheme = Host = Path = Qs = binary()
  124. ----
  125. The command will be ignored if the protocol does not provide
  126. any server push mechanism.
  127. === flow
  128. [source,erlang]
  129. ----
  130. {flow, pos_integer()}
  131. ----
  132. Request more data to be read from the request body. The
  133. exact behavior depends on the protocol.
  134. === spawn
  135. Inform Cowboy that a process was spawned and should be
  136. supervised.
  137. [source,erlang]
  138. ----
  139. {spawn, pid(), timeout()}
  140. ----
  141. === error_response
  142. Send an error response if no response was sent previously.
  143. [source,erlang]
  144. ----
  145. {error_response, cowboy:http_status(), cowboy:http_headers(), iodata()}
  146. ----
  147. [[switch_protocol_command]]
  148. === switch_protocol
  149. Switch to a different protocol.
  150. [source,erlang]
  151. ----
  152. {switch_protocol, cowboy:http_headers(), module(), state()}
  153. ----
  154. Contains the headers that will be sent in the 101 response,
  155. along with the module implementing the protocol we are
  156. switching to and its initial state.
  157. Note that the 101 informational response will not be sent
  158. after a final response.
  159. === stop
  160. Stop the stream.
  161. [source,erlang]
  162. ----
  163. stop
  164. ----
  165. While no more data can be sent after the `fin` flag was set,
  166. the stream is still tracked by Cowboy until it is stopped by
  167. the handler.
  168. The behavior when stopping a stream for which no response
  169. has been sent will vary depending on the protocol. The stream
  170. will end successfully as far as the client is concerned.
  171. To indicate that an error occurred, either use `error_response`
  172. before stopping, or use `internal_error`.
  173. === internal_error
  174. Stop the stream with an error.
  175. [source,erlang]
  176. ----
  177. {internal_error, Reason, HumanReadable}
  178. Reason = any()
  179. HumanReadable = atom()
  180. ----
  181. This command should be used when the stream cannot continue
  182. because of an internal error. An `error_response` command
  183. may be sent before that to advertise to the client why the
  184. stream is dropped.
  185. === log
  186. Log a message.
  187. [source,erlang]
  188. ----
  189. {log, logger:level(), io:format(), list()}
  190. ----
  191. This command can be used to log a message using the
  192. configured `logger` module.
  193. === set_options
  194. Set protocol options.
  195. [source,erlang]
  196. ----
  197. {set_options, map()}
  198. ----
  199. This can also be used to override stream handler
  200. options. For example this is supported by
  201. link:man:cowboy_compress_h(3)[cowboy_compress_h(3)].
  202. Not all options can be overriden. Please consult the
  203. relevant option's documentation for details.
  204. == Predefined events
  205. Cowboy will forward all messages sent to the stream to
  206. the `info/3` callback. To send a message to a stream,
  207. the function link:man:cowboy_req:cast(3)[cowboy_req:cast(3)]
  208. can be used.
  209. Cowboy will also forward the exit signals for the
  210. processes that the stream spawned.
  211. When Cowboy needs to send a response it will trigger
  212. an event that looks exactly like the corresponding
  213. command. This event must be returned to be processed
  214. by Cowboy (which is done automatically when using
  215. link:man:cowboy_stream_h(3)[cowboy_stream_h(3)]).
  216. Cowboy may trigger the following events on its own,
  217. regardless of the stream handlers configured:
  218. xref:inform_command[inform] (to send a 101
  219. informational response when upgrading to HTTP/2 or
  220. Websocket), xref:response_command[response],
  221. xref:headers_command[headers], xref:data_command[data]
  222. and xref:switch_protocol_command[switch_protocol].
  223. == Exports
  224. The following function should be called by modules implementing
  225. stream handlers to execute the next stream handler in the list:
  226. * link:man:cowboy_stream:init(3)[cowboy_stream:init(3)] - Initialize a stream
  227. * link:man:cowboy_stream:data(3)[cowboy_stream:data(3)] - Handle data for a stream
  228. * link:man:cowboy_stream:info(3)[cowboy_stream:info(3)] - Handle a message for a stream
  229. * link:man:cowboy_stream:terminate(3)[cowboy_stream:terminate(3)] - Terminate a stream
  230. * link:man:cowboy_stream:early_error(3)[cowboy_stream:early_error(3)] - Handle an early error for a stream
  231. == Types
  232. === commands()
  233. [source,erlang]
  234. ----
  235. commands() :: [Command]
  236. ----
  237. See the xref:commands[list of commands] for details.
  238. === fin()
  239. [source,erlang]
  240. ----
  241. fin() :: fin | nofin
  242. ----
  243. Used in commands and events to indicate that this is
  244. the end of the stream.
  245. === partial_req()
  246. [source,erlang]
  247. ----
  248. req() :: #{
  249. method => binary(), %% case sensitive
  250. version => cowboy:http_version() | atom(),
  251. scheme => binary(), %% lowercase; case insensitive
  252. host => binary(), %% lowercase; case insensitive
  253. port => inet:port_number(),
  254. path => binary(), %% case sensitive
  255. qs => binary(), %% case sensitive
  256. headers => cowboy:http_headers(),
  257. peer => {inet:ip_address(), inet:port_number()}
  258. }
  259. ----
  260. Partial request information received when an early error is
  261. detected.
  262. === reason()
  263. [source,erlang]
  264. ----
  265. reason() :: normal | switch_protocol
  266. | {internal_error, timeout | {error | exit | throw, any()}, HumanReadable}
  267. | {socket_error, closed | atom(), HumanReadable}
  268. | {stream_error, Error, HumanReadable}
  269. | {connection_error, Error, HumanReadable}
  270. | {stop, cow_http2:frame() | {exit, any()}, HumanReadable}
  271. Error = atom()
  272. HumanReadable = atom()
  273. ----
  274. Reason for the stream termination.
  275. === resp_command()
  276. [source,erlang]
  277. ----
  278. resp_command() :: {response, cowboy:http_status(),
  279. cowboy:http_headers(), cowboy_req:resp_body()}
  280. ----
  281. See the xref:response_command[response command] for details.
  282. === streamid()
  283. [source,erlang]
  284. ----
  285. streamid() :: any()
  286. ----
  287. The identifier for this stream.
  288. The identifier is unique over the connection process.
  289. It is possible to form a unique identifier node-wide and
  290. cluster-wide by wrapping it in a `{self(), StreamID}`
  291. tuple.
  292. == Changelog
  293. * *2.7*: The `log` and `set_options` commands were introduced.
  294. * *2.6*: The `data` command can now contain a sendfile tuple.
  295. * *2.6*: The `{stop, {exit, any()}, HumanReadable}` terminate reason was added.
  296. * *2.2*: The `trailers` command was introduced.
  297. * *2.0*: Module introduced.
  298. == See also
  299. link:man:cowboy(7)[cowboy(7)],
  300. link:man:cowboy_http(3)[cowboy_http(3)],
  301. link:man:cowboy_http2(3)[cowboy_http2(3)],
  302. link:man:cowboy_req:cast(3)[cowboy_req:cast(3)]