cowboy_rest.ezdoc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. ::: cowboy_rest
  2. The `cowboy_rest` module implements REST semantics on top of
  3. the HTTP protocol.
  4. This module cannot be described as a behaviour due to most of
  5. the callbacks it defines being optional. It has the same
  6. semantics as a behaviour otherwise.
  7. The only mandatory callback is `init/3`, needed to perform
  8. the protocol upgrade.
  9. :: Types
  10. None.
  11. :: Meta values
  12. : charset
  13. Type: binary()
  14. Negotiated charset.
  15. This value may not be defined if no charset was negotiated.
  16. : language
  17. Type: binary()
  18. Negotiated language.
  19. This value may not be defined if no language was negotiated.
  20. : media_type
  21. Type: {binary(), binary(), '*' | [{binary(), binary()}]}
  22. Negotiated media-type.
  23. The media-type is the content-type, excluding the charset.
  24. This value is always defined after the call to
  25. `content_types_provided/2`.
  26. :: Callbacks
  27. : init({TransportName, ProtocolName}, Req, Opts)
  28. -> {upgrade, protocol, cowboy_rest}
  29. | {upgrade, protocol, cowboy_rest, Req, Opts}
  30. Types:
  31. * TransportName = tcp | ssl | atom()
  32. * ProtocolName = http | atom()
  33. * Req = cowboy_req:req()
  34. * Opts = any()
  35. Upgrade the protocol to `cowboy_rest`.
  36. This is the only mandatory callback.
  37. : rest_init(Req, Opts) -> {ok, Req, State}
  38. Types:
  39. * Req = cowboy_req:req()
  40. * Opts = any()
  41. * State = any()
  42. Initialize the state for this request.
  43. : rest_terminate(Req, State) -> ok
  44. Types:
  45. * Req = cowboy_req:req()
  46. * State = any()
  47. Perform any necessary cleanup of the state.
  48. This callback should release any resource currently in use,
  49. clear any active timer and reset the process to its original
  50. state, as it might be reused for future requests sent on the
  51. same connection.
  52. : Callback(Req, State) -> {Value, Req, State} | {halt, Req, State}
  53. Types:
  54. * Callback - one of the REST callbacks described below
  55. * Req = cowboy_req:req()
  56. * State = any()
  57. * Value - see the REST callbacks description below
  58. Please see the REST callbacks description below for details
  59. on the `Value` type, the default value if the callback is
  60. not defined, and more general information on when the
  61. callback is called and what its intended use is.
  62. The `halt` tuple can be returned to stop REST processing.
  63. It is up to the resource code to send a reply before that,
  64. otherwise a `204 No Content` will be sent.
  65. :: REST callbacks description
  66. : allowed_methods
  67. * Methods: all
  68. * Value type: [binary()]
  69. * Default value: [<<"GET">>, <<"HEAD">>, <<"OPTIONS">>]
  70. Return the list of allowed methods.
  71. Methods are case sensitive. Standard methods are always uppercase.
  72. : allow_missing_post
  73. * Methods: POST
  74. * Value type: boolean()
  75. * Default value: true
  76. Return whether POST is allowed when the resource doesn't exist.
  77. Returning `true` here means that a new resource will be
  78. created. The URL to the created resource should also be
  79. returned from the `AcceptResource` callback.
  80. : charsets_provided
  81. * Methods: GET, HEAD, POST, PUT, PATCH, DELETE
  82. * Value type: [binary()]
  83. * Skip to the next step if undefined
  84. Return the list of charsets the resource provides.
  85. The list must be ordered in order of preference.
  86. If the accept-charset header was not sent, the first charset
  87. in the list will be selected. Otherwise Cowboy will select
  88. the most appropriate charset from the list.
  89. The chosen charset will be set in the `Req` object as the meta
  90. value `charset`.
  91. While charsets are case insensitive, this callback is expected
  92. to return them as lowercase binary.
  93. : content_types_accepted
  94. * Methods: POST, PUT, PATCH
  95. * No default
  96. Types:
  97. * Value = [{binary() | {Type, SubType, Params}, AcceptResource}]
  98. * Type = SubType = binary()
  99. * Params = '*' | [{binary(), binary()}]
  100. * AcceptResource = atom()
  101. Return the list of content-types the resource accepts.
  102. The list must be ordered in order of preference.
  103. Each content-type can be given either as a binary string or as
  104. a tuple containing the type, subtype and parameters.
  105. Cowboy will select the most appropriate content-type from the list.
  106. If any parameter is acceptable, then the tuple form should be used
  107. with parameters set to `'*'`. If the parameters value is set to `[]`
  108. only content-type values with no parameters will be accepted. All
  109. parameter values are treated in a case sensitive manner except the
  110. `charset` parameter, if present, which is case insensitive.
  111. This function will be called for POST, PUT and PATCH requests.
  112. It is entirely possible to define different callbacks for different
  113. methods if the handling of the request differs. Simply verify
  114. what the method is with `cowboy_req:method/1` and return a
  115. different list for each methods.
  116. The `AcceptResource` value is the name of the callback that will
  117. be called if the content-type matches. It is defined as follow.
  118. * Value type: true | {true, URL} | false
  119. * No default
  120. Process the request body.
  121. This function should create or update the resource with the
  122. information contained in the request body. This information
  123. may be full or partial depending on the request method.
  124. If the request body was processed successfully, `true` or
  125. `{true, URL}` may be returned. If an URL is provided, the
  126. response will redirect the client to the location of the
  127. resource.
  128. If a response body must be sent, the appropriate media-type, charset
  129. and language can be retrieved using the `cowboy_req:meta/{2,3}`
  130. functions. The respective keys are `media_type`, `charset`
  131. and `language`. The body can be set using `cowboy_req:set_resp_body/2`.
  132. : content_types_provided
  133. * Methods: GET, HEAD, POST, PUT, PATCH, DELETE
  134. * Default value: [{{<<"text">>, <<"html">>, '*'}, to_html}]
  135. Types:
  136. * Value = [{binary() | {Type, SubType, Params}, ProvideResource}]
  137. * Type = SubType = binary()
  138. * Params = '*' | [{binary(), binary()}]
  139. * ProvideResource = atom()
  140. Return the list of content-types the resource provides.
  141. The list must be ordered in order of preference.
  142. Each content-type can be given either as a binary string or as
  143. a tuple containing the type, subtype and parameters.
  144. Cowboy will select the most appropriate content-type from the list.
  145. If any parameter is acceptable, then the tuple form should be used
  146. with parameters set to `'*'`. If the parameters value is set to `[]`
  147. only content-type values with no parameters will be accepted. All
  148. parameter values are treated in a case sensitive manner except the
  149. `charset` parameter, if present, which is case insensitive.
  150. The `ProvideResource` value is the name of the callback that will
  151. be called if the content-type matches. It will only be called when
  152. a representation of the resource needs to be returned. It is defined
  153. as follow.
  154. * Methods: GET, HEAD
  155. * Value type: iodata() | {stream, Fun} | {stream, Len, Fun} | {chunked, ChunkedFun}
  156. * No default
  157. Return the response body.
  158. The response body may be provided directly or through a fun.
  159. If a fun tuple is returned, the appropriate `set_resp_body_fun`
  160. function will be called. Please refer to the documentation for
  161. these functions for more information about the types.
  162. The call to this callback happens a good time after the call to
  163. `content_types_provided/2`, when it is time to start rendering
  164. the response body.
  165. : delete_completed
  166. * Methods: DELETE
  167. * Value type: boolean()
  168. * Default value: true
  169. Return whether the delete action has been completed.
  170. This function should return `false` if there is no guarantee
  171. that the resource gets deleted immediately from the system,
  172. including from any internal cache.
  173. When this function returns `false`, a `202 Accepted`
  174. response will be sent instead of a `200 OK` or `204 No Content`.
  175. : delete_resource
  176. * Methods: DELETE
  177. * Value type: boolean()
  178. * Default value: false
  179. Delete the resource.
  180. The value returned indicates if the action was successful,
  181. regardless of whether the resource is immediately deleted
  182. from the system.
  183. : expires
  184. * Methods: GET, HEAD
  185. * Value type: calendar:datetime() | binary() | undefined
  186. * Default value: undefined
  187. Return the date of expiration of the resource.
  188. This date will be sent as the value of the expires header.
  189. : forbidden
  190. * Methods: all
  191. * Value type: boolean()
  192. * Default value: false
  193. Return whether access to the resource is forbidden.
  194. A `403 Forbidden` response will be sent if this
  195. function returns `true`. This status code means that
  196. access is forbidden regardless of authentication,
  197. and that the request shouldn't be repeated.
  198. : generate_etag
  199. * Methods: GET, HEAD, POST, PUT, PATCH, DELETE
  200. * Value type: binary() | {weak | strong, binary()}
  201. * Default value: undefined
  202. Return the entity tag of the resource.
  203. This value will be sent as the value of the etag header.
  204. If a binary is returned, then the value will be parsed
  205. to the tuple form automatically. The value must be in
  206. the same format as the etag header, including quotes.
  207. : is_authorized
  208. * Methods: all
  209. * Value type: true | {false, AuthHeader}
  210. * Default value: true
  211. Return whether the user is authorized to perform the action.
  212. This function should be used to perform any necessary
  213. authentication of the user before attempting to perform
  214. any action on the resource.
  215. If the authentication fails, the value returned will be sent
  216. as the value for the www-authenticate header in the
  217. `401 Unauthorized` response.
  218. : is_conflict
  219. * Methods: PUT
  220. * Value type: boolean()
  221. * Default value: false
  222. Return whether the put action results in a conflict.
  223. A `409 Conflict` response will be sent if this function
  224. returns `true`.
  225. : known_content_type
  226. * Methods: all
  227. * Value type: boolean()
  228. * Default value: true
  229. Return whether the content-type is known.
  230. This function determines if the server understands the
  231. content-type, regardless of its use by the resource.
  232. : known_methods
  233. * Methods: all
  234. * Value type: [binary()]
  235. * Default value: [<<"GET">>, <<"HEAD">>, <<"POST">>, <<"PUT">>, <<"PATCH">>, <<"DELETE">>, <<"OPTIONS">>]
  236. Return the list of known methods.
  237. The full list of methods known by the server should be
  238. returned, regardless of their use in the resource.
  239. The default value lists the methods Cowboy knows and
  240. implement in `cowboy_rest`.
  241. Methods are case sensitive. Standard methods are always uppercase.
  242. : languages_provided
  243. * Methods: GET, HEAD, POST, PUT, PATCH, DELETE
  244. * Value type: [binary()]
  245. * Skip to the next step if undefined
  246. Return the list of languages the resource provides.
  247. The list must be ordered in order of preference.
  248. If the accept-language header was not sent, the first language
  249. in the list will be selected. Otherwise Cowboy will select
  250. the most appropriate language from the list.
  251. The chosen language will be set in the `Req` object as the meta
  252. value `language`.
  253. While languages are case insensitive, this callback is expected
  254. to return them as lowercase binary.
  255. : last_modified
  256. * Methods: GET, HEAD, POST, PUT, PATCH, DELETE
  257. * Value type: calendar:datetime()
  258. * Default value: undefined
  259. Return the date of last modification of the resource.
  260. This date will be used to test against the if-modified-since
  261. and if-unmodified-since headers, and sent as the last-modified
  262. header in the response of GET and HEAD requests.
  263. : malformed_request
  264. * Methods: all
  265. * Value type: boolean()
  266. * Default value: false
  267. Return whether the request is malformed.
  268. Cowboy has already performed all the necessary checks
  269. by the time this function is called, so few resources
  270. are expected to implement it.
  271. The check is to be done on the request itself, not on
  272. the request body, which is processed later.
  273. : moved_permanently
  274. * Methods: GET, HEAD, POST, PUT, PATCH, DELETE
  275. * Value type: {true, URL} | false
  276. * Default value: false
  277. Return whether the resource was permanently moved.
  278. If it was, its new URL is also returned and sent in the
  279. location header in the response.
  280. : moved_temporarily
  281. * Methods: GET, HEAD, POST, PATCH, DELETE
  282. * Value type: {true, URL} | false
  283. * Default value: false
  284. Return whether the resource was temporarily moved.
  285. If it was, its new URL is also returned and sent in the
  286. location header in the response.
  287. : multiple_choices
  288. * Methods: GET, HEAD, POST, PUT, PATCH, DELETE
  289. * Value type: boolean()
  290. * Default value: false
  291. Return whether there are multiple representations of the resource.
  292. This function should be used to inform the client if there
  293. are different representations of the resource, for example
  294. different content-type. If this function returns `true`,
  295. the response body should include information about these
  296. different representations using `cowboy_req:set_resp_body/2`.
  297. The content-type of the response should be the one previously
  298. negociated and that can be obtained by calling
  299. `cowboy_req:meta(media_type, Req)`.
  300. : options
  301. * Methods: OPTIONS
  302. * Value type: ok
  303. * Default value: ok
  304. Handle a request for information.
  305. The response should inform the client the communication
  306. options available for this resource.
  307. By default, Cowboy will send a `200 OK` response with the
  308. allow header set.
  309. : previously_existed
  310. * Methods: GET, HEAD, POST, PATCH, DELETE
  311. * Value type: boolean()
  312. * Default value: false
  313. Return whether the resource existed previously.
  314. : resource_exists
  315. * Methods: GET, HEAD, POST, PUT, PATCH, DELETE
  316. * Value type: boolean()
  317. * Default value: true
  318. Return whether the resource exists.
  319. If it exists, conditional headers will be tested before
  320. attempting to perform the action. Otherwise, Cowboy will
  321. check if the resource previously existed first.
  322. : service_available
  323. * Methods: all
  324. * Value type: boolean()
  325. * Default value: true
  326. Return whether the service is available.
  327. This function can be used to test that all relevant backend
  328. systems are up and able to handle requests.
  329. A `503 Service Unavailable` response will be sent if this
  330. function returns `false`.
  331. : uri_too_long
  332. * Methods: all
  333. * Value type: boolean()
  334. * Default value: false
  335. Return whether the requested URI is too long.
  336. Cowboy has already performed all the necessary checks
  337. by the time this function is called, so few resources
  338. are expected to implement it.
  339. A `414 Request-URI Too Long` response will be sent if this
  340. function returns `true`.
  341. : valid_content_headers
  342. * Methods: all
  343. * Value type: boolean()
  344. * Default value: true
  345. Return whether the content-* headers are valid.
  346. This also applies to the transfer-encoding header. This
  347. function must return `false` for any unknown content-*
  348. headers, or if the headers can't be understood. The
  349. function `cowboy_req:parse_header/2` can be used to
  350. quickly check the headers can be parsed.
  351. A `501 Not Implemented` response will be sent if this
  352. function returns `false`.
  353. : valid_entity_length
  354. * Methods: all
  355. * Value type: boolean()
  356. * Default value: true
  357. Return whether the request body length is within acceptable boundaries.
  358. A `413 Request Entity Too Large` response will be sent if this
  359. function returns `false`.
  360. : variances
  361. * Methods: GET, HEAD, POST, PUT, PATCH, DELETE
  362. * Value type: [binary()]
  363. * Default value: []
  364. Return the list of headers that affect the representation of the resource.
  365. These request headers return the same resource but with different
  366. parameters, like another language or a different content-type.
  367. Cowboy will automatically add the accept, accept-language and
  368. accept-charset headers to the list if the respective functions
  369. were defined in the resource.
  370. This operation is performed right before the `resource_exists/2`
  371. callback. All responses past that point will contain the vary
  372. header which holds this list.