Debian Codegraph @

doc/src/guide/rest_flowcharts.asciidoc

raw
1 [[rest_flowcharts]]
2 == REST flowcharts
3
4 This chapter will explain the REST handler state machine through
5 a number of different diagrams.
6
7 There are four main paths that requests may follow. One for the
8 method OPTIONS; one for the methods GET and HEAD; one for the
9 methods PUT, POST and PATCH; and one for the method DELETE.
10
11 All paths start with the "Start" diagram, and all paths excluding
12 the OPTIONS path go through the "Content negotiation" diagram
13 and optionally the "Conditional requests" diagram if the resource
14 exists.
15
16 The red squares refer to another diagram. The light green squares
17 indicate a response. Other squares may be either a callback or a
18 question answered by Cowboy itself. Green arrows tend to indicate
19 the default behavior if the callback is undefined. The star next
20 to values indicate that the value is descriptive rather than exact.
21
22 === Start
23
24 All requests start from here.
25
26 image::rest_start.png[REST starting flowchart]
27
28 A series of callbacks are called in succession to perform
29 a general checkup of the service, the request line and
30 request headers.
31
32 The request body, if any, is not expected to have been
33 received for any of these steps. It is only processed
34 at the end of the "PUT, POST and PATCH methods" diagram,
35 when all conditions have been met.
36
37 The `known_methods` and `allowed_methods` callbacks
38 return a list of methods. Cowboy then checks if the request
39 method is in the list, and stops otherwise.
40
41 The `is_authorized` callback may be used to check that
42 access to the resource is authorized. Authentication
43 may also be performed as needed. When authorization is
44 denied, the return value from the callback must include
45 a challenge applicable to the requested resource, which
46 will be sent back to the client in the www-authenticate
47 header.
48
49 This diagram is immediately followed by either the
50 "OPTIONS method" diagram when the request method is
51 OPTIONS, or the "Content negotiation" diagram otherwise.
52
53 === OPTIONS method
54
55 This diagram only applies to OPTIONS requests.
56
57 image::rest_options.png[REST OPTIONS method flowchart]
58
59 The `options` callback may be used to add information
60 about the resource, such as media types or languages
61 provided; allowed methods; any extra information. A
62 response body may also be set, although clients should
63 not be expected to read it.
64
65 If the `options` callback is not defined, Cowboy will
66 send a response containing the list of allowed methods
67 by default.
68
69 === Content negotiation
70
71 This diagram applies to all request methods other than
72 OPTIONS. It is executed right after the "Start" diagram
73 is completed.
74
75 image::rest_conneg.png[REST content negotiation flowchart]
76
77 The purpose of these steps is to determine an appropriate
78 representation to be sent back to the client.
79
80 The request may contain any of the accept header; the
81 accept-language header; or the accept-charset header.
82 When present, Cowboy will parse the headers and then
83 call the corresponding callback to obtain the list
84 of provided content-type, language or charset for this
85 resource. It then automatically select the best match
86 based on the request.
87
88 If a callback is not defined, Cowboy will select the
89 content-type, language or charset that the client
90 prefers.
91
92 The `content_types_provided` also returns the name of
93 a callback for every content-type it accepts. This
94 callback will only be called at the end of the
95 "GET and HEAD methods" diagram, when all conditions
96 have been met.
97
98 Optionally, the `ranges_provided` also returns the
99 name of a callback for every range unit it accepts. This
100 will be called at the end of the "GET and HEAD methods"
101 diagram in the case of ranged requests.
102
103 The selected content-type, language and charset are
104 saved as meta values in the Req object. You *should*
105 use the appropriate representation if you set a
106 response body manually (alongside an error code,
107 for example).
108
109 This diagram is immediately followed by
110 the "GET and HEAD methods" diagram,
111 the "PUT, POST and PATCH methods" diagram,
112 or the "DELETE method" diagram, depending on the
113 method.
114
115 === GET and HEAD methods
116
117 This diagram only applies to GET and HEAD requests.
118
119 For a description of the `cond` step, please see
120 the "Conditional requests" diagram.
121
122 image::rest_get_head.png[REST GET/HEAD methods flowchart]
123
124 When the resource exists, and the conditional steps
125 succeed, the resource can be retrieved.
126
127 Cowboy prepares the response by first retrieving
128 metadata about the representation, then by calling
129 the `ProvideCallback` callback. This is the callback
130 you defined for each content-types you returned from
131 `content_types_provided`. This callback returns the body
132 that will be sent back to the client.
133
134 For ranged requests, but only when the `ranges_provided`
135 callback was defined earlier, Cowboy will add the selected
136 `range` information to the Req object and call the
137 `range_satisfiable` callback. After confirming that the
138 range can be provided, Cowboy calls the `RangeResource`
139 callback and produces a ranged response using the
140 ranged data from the callback.
141
142 When the resource does not exist, Cowboy will figure out
143 whether the resource existed previously, and if so whether
144 it was moved elsewhere in order to redirect the client to
145 the new URI.
146
147 The `moved_permanently` and `moved_temporarily` callbacks
148 must return the new location of the resource if it was in
149 fact moved.
150
151 === PUT, POST and PATCH methods
152
153 This diagram only applies to PUT, POST and PATCH requests.
154
155 For a description of the `cond` step, please see
156 the "Conditional requests" diagram.
157
158 image::rest_put_post_patch.png[REST PUT/POST/PATCH methods flowchart]
159
160 When the resource exists, first the conditional steps
161 are executed. When that succeeds, and the method is PUT,
162 Cowboy will call the `is_conflict` callback. This function
163 can be used to prevent potential race conditions, by locking
164 the resource for example.
165
166 Then all three methods reach the `content_types_accepted`
167 step that we will describe in a few paragraphs.
168
169 When the resource does not exist, and the method is PUT,
170 Cowboy will check for conflicts and then move on to the
171 `content_types_accepted` step. For other methods, Cowboy
172 will figure out whether the resource existed previously,
173 and if so whether it was moved elsewhere. If the resource
174 is truly non-existent, the method is POST and the call
175 for `allow_missing_post` returns `true`, then Cowboy will
176 move on to the `content_types_accepted` step. Otherwise
177 the request processing ends there.
178
179 The `moved_permanently` and `moved_temporarily` callbacks
180 must return the new location of the resource if it was in
181 fact moved.
182
183 The `content_types_accepted` returns a list of
184 content-types it accepts, but also the name of a callback
185 for each of them. Cowboy will select the appropriate
186 callback for processing the request body and call it.
187
188 This callback may return one of three different return
189 values.
190
191 If an error occurred while processing the request body,
192 it must return `false` and Cowboy will send an
193 appropriate error response.
194
195 If the method is POST, then you may return `true` with
196 an URI of where the resource has been created. This is
197 especially useful for writing handlers for collections.
198
199 Otherwise, return `true` to indicate success. Cowboy
200 will select the appropriate response to be sent depending
201 on whether a resource has been created, rather than
202 modified, and on the availability of a location header
203 or a body in the response.
204
205 === DELETE method
206
207 This diagram only applies to DELETE requests.
208
209 For a description of the `cond` step, please see
210 the "Conditional requests" diagram.
211
212 image::rest_delete.png[REST DELETE method flowchart]
213
214 When the resource exists, and the conditional steps
215 succeed, the resource can be deleted.
216
217 Deleting the resource is a two steps process. First
218 the callback `delete_resource` is executed. Use this
219 callback to delete the resource.
220
221 Because the resource may be cached, you must also
222 delete all cached representations of this resource
223 in the system. This operation may take a while though,
224 so you may return before it finished.
225
226 Cowboy will then call the `delete_completed` callback.
227 If you know that the resource has been completely
228 deleted from your system, including from caches, then
229 you can return `true`. If any doubts persist, return
230 `false`. Cowboy will assume `true` by default.
231
232 To finish, Cowboy checks if you set a response body,
233 and depending on that, sends the appropriate response.
234
235 When the resource does not exist, Cowboy will figure out
236 whether the resource existed previously, and if so whether
237 it was moved elsewhere in order to redirect the client to
238 the new URI.
239
240 The `moved_permanently` and `moved_temporarily` callbacks
241 must return the new location of the resource if it was in
242 fact moved.
243
244 === Conditional requests
245
246 This diagram applies to all request methods other than
247 OPTIONS. It is executed right after the `resource_exists`
248 callback, when the resource exists.
249
250 image::rest_cond.png[REST conditional requests flowchart]
251
252 A request becomes conditional when it includes either of
253 the if-match header; the if-unmodified-since header; the
254 if-none-match header; or the if-modified-since header.
255
256 If the condition fails, the request ends immediately
257 without any retrieval or modification of the resource.
258
259 The `generate_etag` and `last_modified` are called as
260 needed. Cowboy will only call them once and then cache
261 the results for subsequent use.