cowboy_handler.asciidoc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. = cowboy_handler(3)
  2. == Name
  3. cowboy_handler - handler middleware and behaviour
  4. == Description
  5. The `cowboy_handler` middleware executes the handler passed
  6. through the environment values `handler` and `handler_opts`,
  7. and adds the result of this execution to the environment as
  8. the value `result`, indicating that the request has been
  9. handled and received a response.
  10. Environment input:
  11. handler = module():: Handler to be executed.
  12. handler_opts = any():: Options to be passed to the handler.
  13. Environment output:
  14. result = ok:: Result of the request.
  15. This module also defines the `cowboy_handler` behaviour that
  16. defines the basic interface for handlers. All Cowboy handlers
  17. implement at least the `init/2` callback, and may implement
  18. the `terminate/3` callback optionally.
  19. == Terminate reasons
  20. The following values may be received as the terminate reason
  21. in the optional `terminate/3` callback. Different handler types
  22. may define additional terminate reasons.
  23. normal::
  24. The connection was closed normally.
  25. {crash, Class, Reason}::
  26. A crash occurred in the handler. `Class` and `Reason` can be
  27. used to obtain more information about the crash. The function
  28. `erlang:get_stacktrace/0` can also be called to obtain the
  29. stacktrace of the process when the crash occurred.
  30. == Callbacks
  31. === init(Req, Opts) -> {ok, Req, State} | {Module, Req, State} | {Module, Req, State, hibernate | Timeout} | {Module, Req, State, Timeout, hibernate}
  32. Req = cowboy_req:req():: The Req object.
  33. Opts = any():: Handler options.
  34. State = any():: Handler state.
  35. Module = module():: Module of the sub-protocol to use.
  36. Timeout = timeout():: Timeout passed to the sub-protocol, when applicable.
  37. Process the request.
  38. This function can be used to switch to an alternate handler
  39. type by returning the name of the module to be used, along
  40. with a few options.
  41. For basic handlers this is the function where the response
  42. should be sent. If no response is sent, Cowboy will ensure
  43. that a `204 No Content` response is sent.
  44. A crash in this callback will result in `terminate/3` being
  45. called if it is defined, with the `State` argument set to
  46. the value of `Opts` originally given to the `init/2` callback.
  47. === terminate(Reason, Req, State) -> ok
  48. Reason = any():: Reason for termination.
  49. Req = cowboy_req:req():: The Req object.
  50. State = any():: Handler state.
  51. Perform any necessary cleanup of the state.
  52. This callback should release any resource currently in use,
  53. clear any active timer and reset the process to its original
  54. state, as it might be reused for future requests sent on the
  55. same connection. Typical plain HTTP handlers rarely need to
  56. use it.
  57. A crash in this callback or an invalid return value will
  58. result in the closing of the connection and the termination
  59. of the process.
  60. == Exports
  61. === terminate(Reason, Req, State, Handler) -> ok
  62. Reason = any():: Reason for termination.
  63. Req = cowboy_req:req():: The Req object.
  64. State = any():: Handler state.
  65. Handler = module():: Handler module.
  66. Call the optional `terminate/3` callback if it exists.
  67. This function should always be called at the end of the execution
  68. of a handler, to give it a chance to clean up or perform
  69. miscellaneous operations.