/
flow.rb
514 lines (450 loc) · 12.4 KB
/
flow.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
require 'time'
require 'digest/md5'
require 'base64'
require 'webmachine/constants'
require 'webmachine/decision/conneg'
require 'webmachine/decision/falsey'
require 'webmachine/translation'
require 'webmachine/etags'
module Webmachine
module Decision
# This module encapsulates all of the decisions in Webmachine's
# flow-chart. These invoke {Webmachine::Resource::Callbacks} methods to
# determine the appropriate response code, headers, and body for
# the response.
#
# This module is included into {FSM}, which drives the processing
# of the chart.
# @see https://raw.github.com/wiki/basho/webmachine/images/http-headers-status-v3.png
module Flow
include Base64
# Version of the flow diagram
VERSION = 3
# The first state in flow diagram
START = :b13
# Separate content-negotiation logic from flow diagram.
include Conneg
# Extract error strings into locale files
include Translation
# Handles standard decisions where halting is allowed
def decision_test(test, iftrue, iffalse)
case test
when Integer # Allows callbacks to "halt" with a given response code
test
when Falsey
iffalse
else
iftrue
end
end
# Service available?
def b13
decision_test(resource.service_available?, :b12, 503)
end
# Known method?
def b12
decision_test(resource.known_methods.include?(request.method), :b11, 501)
end
# URI too long?
def b11
decision_test(resource.uri_too_long?(request.uri), 414, :b10)
end
# Method allowed?
def b10
if resource.allowed_methods.include?(request.method)
:b9
else
response.headers['Allow'] = resource.allowed_methods.join(', ')
405
end
end
# Content-MD5 present?
def b9
request.content_md5 ? :b9a : :b9b
end
# Content-MD5 valid?
def b9a
case valid = resource.validate_content_checksum
when Integer
valid
when true
:b9b
when false
response.body = 'Content-MD5 header does not match request body.'
400
else # not_validated
if decode64(request.content_md5) == Digest::MD5.hexdigest(request.body)
:b9b
else
response.body = 'Content-MD5 header does not match request body.'
400
end
end
end
# Malformed?
def b9b
decision_test(resource.malformed_request?, 400, :b8)
end
# Authorized?
def b8
result = resource.is_authorized?(request.authorization)
case result
when true
:b7
when Integer
result
when String
response.headers['WWW-Authenticate'] = result
401
else
401
end
end
# Forbidden?
def b7
decision_test(resource.forbidden?, 403, :b6)
end
CONTENT = /content-/.freeze
# Okay Content-* Headers?
def b6
decision_test(resource.valid_content_headers?(request.headers.grep(CONTENT)), :b5, 501)
end
# Known Content-Type?
def b5
decision_test(resource.known_content_type?(request.content_type), :b4, 415)
end
# Req Entity Too Large?
def b4
decision_test(resource.valid_entity_length?(request.content_length), :b3, 413)
end
# OPTIONS?
def b3
if request.options?
response.headers.merge!(resource.options)
200
else
:c3
end
end
# Accept exists?
def c3
if !request.accept
metadata[CONTENT_TYPE] = MediaType.parse(resource.content_types_provided.first.first)
:d4
else
:c4
end
end
# Acceptable media type available?
def c4
types = resource.content_types_provided.map { |pair| pair.first }
chosen_type = choose_media_type(types, request.accept)
if !chosen_type
406
else
metadata[CONTENT_TYPE] = chosen_type
:d4
end
end
# Accept-Language exists?
def d4
if !request.accept_language
if language = choose_language(resource.languages_provided, STAR)
resource.language_chosen(language)
:e5
else
406
end
else
:d5
end
end
# Acceptable language available?
def d5
if language = choose_language(resource.languages_provided, request.accept_language)
resource.language_chosen(language)
:e5
else
406
end
end
# Accept-Charset exists?
def e5
if !request.accept_charset
choose_charset(resource.charsets_provided, STAR) ? :f6 : 406
else
:e6
end
end
# Acceptable Charset available?
def e6
choose_charset(resource.charsets_provided, request.accept_charset) ? :f6 : 406
end
# Accept-Encoding exists?
# (also, set content-type header here, now that charset is chosen)
def f6
chosen_type = metadata[CONTENT_TYPE]
if chosen_charset = metadata[CHARSET]
chosen_type.params['charset'] = chosen_charset
end
response.headers[CONTENT_TYPE] = chosen_type.to_s
if !request.accept_encoding
choose_encoding(resource.encodings_provided, 'identity;q=1.0,*;q=0.5') ? :g7 : 406
else
:f7
end
end
# Acceptable encoding available?
def f7
choose_encoding(resource.encodings_provided, request.accept_encoding) ? :g7 : 406
end
# Resource exists?
def g7
# This is the first place after all conneg, so set Vary here
response.headers['Vary'] = variances.join(', ') if variances.any?
decision_test(resource.resource_exists?, :g8, :h7)
end
# If-Match exists?
def g8
request.if_match ? :g9 : :h10
end
# If-Match: * exists?
def g9
(quote(request.if_match) == '"*"') ? :h10 : :g11
end
# ETag in If-Match
def g11
request_etags = request.if_match.split(SPLIT_COMMA).map { |etag| ETag.new(etag) }
request_etags.include?(ETag.new(resource.generate_etag)) ? :h10 : 412
end
# If-Match exists?
def h7
(request.if_match && unquote(request.if_match) == STAR) ? 412 : :i7
end
# If-Unmodified-Since exists?
def h10
request.if_unmodified_since ? :h11 : :i12
end
# If-Unmodified-Since is valid date?
def h11
date = Time.httpdate(request.if_unmodified_since)
metadata['If-Unmodified-Since'] = date
rescue ArgumentError
:i12
else
:h12
end
# Last-Modified > I-UM-S?
def h12
(resource.last_modified > metadata['If-Unmodified-Since']) ? 412 : :i12
end
# Moved permanently? (apply PUT to different URI)
def i4
case uri = resource.moved_permanently?
when String, URI
response.headers[LOCATION] = uri.to_s
301
when Integer
uri
else
:p3
end
end
# PUT?
def i7
request.put? ? :i4 : :k7
end
# If-none-match exists?
def i12
request.if_none_match ? :i13 : :l13
end
# If-none-match: * exists?
def i13
(quote(request.if_none_match) == '"*"') ? :j18 : :k13
end
# GET or HEAD?
def j18
(request.get? || request.head?) ? 304 : 412
end
# Moved permanently?
def k5
case uri = resource.moved_permanently?
when String, URI
response.headers[LOCATION] = uri.to_s
301
when Integer
uri
else
:l5
end
end
# Previously existed?
def k7
decision_test(resource.previously_existed?, :k5, :l7)
end
# Etag in if-none-match?
def k13
request_etags = request.if_none_match.split(SPLIT_COMMA).map { |etag| ETag.new(etag) }
resource_etag = resource.generate_etag
if resource_etag && request_etags.include?(ETag.new(resource_etag))
:j18
else
:l13
end
end
# Moved temporarily?
def l5
case uri = resource.moved_temporarily?
when String, URI
response.headers[LOCATION] = uri.to_s
307
when Integer
uri
else
:m5
end
end
# POST?
def l7
request.post? ? :m7 : 404
end
# If-Modified-Since exists?
def l13
request.if_modified_since ? :l14 : :m16
end
# IMS is valid date?
def l14
date = Time.httpdate(request.if_modified_since)
metadata['If-Modified-Since'] = date
rescue ArgumentError
:m16
else
:l15
end
# IMS > Now?
def l15
(metadata['If-Modified-Since'] > Time.now) ? :m16 : :l17
end
# Last-Modified > IMS?
def l17
(resource.last_modified.nil? || resource.last_modified > metadata['If-Modified-Since']) ? :m16 : 304
end
# POST?
def m5
request.post? ? :n5 : 410
end
# Server allows POST to missing resource?
def m7
decision_test(resource.allow_missing_post?, :n11, 404)
end
# DELETE?
def m16
request.delete? ? :m20 : :n16
end
# DELETE enacted immediately? (Also where DELETE is forced.)
def m20
decision_test(resource.delete_resource, :m20b, 500)
end
# Did the DELETE complete?
def m20b
decision_test(resource.delete_completed?, :o20, 202)
end
# Server allows POST to missing resource?
def n5
decision_test(resource.allow_missing_post?, :n11, 410)
end
# Redirect?
def n11
# Stage1
if resource.post_is_create?
case uri = resource.create_path
when nil
raise InvalidResource, t('create_path_nil', class: resource.class)
when URI, String
base_uri = resource.base_uri || request.base_uri
new_uri = URI.join(base_uri.to_s, uri)
request.disp_path = new_uri.path
response.headers[LOCATION] = new_uri.to_s
result = accept_helper
return result if Integer === result
end
else
case result = resource.process_post
when true
encode_body_if_set
when Integer
return result
else
raise InvalidResource, t('process_post_invalid', result: result.inspect)
end
end
if response.is_redirect?
if response.headers[LOCATION]
303
else
raise InvalidResource, t('do_redirect')
end
else
:p11
end
end
# POST?
def n16
request.post? ? :n11 : :o16
end
# Conflict?
def o14
if resource.is_conflict?
409
else
res = accept_helper
(Integer === res) ? res : :p11
end
end
# PUT?
def o16
request.put? ? :o14 : :o18
end
# Multiple representations?
# Also where body generation for GET and HEAD is done.
def o18
if request.get? || request.head?
add_caching_headers
content_type = metadata[CONTENT_TYPE]
handler = resource.content_types_provided.find { |ct, _| content_type.type_matches?(MediaType.parse(ct)) }.last
result = resource.send(handler)
if Integer === result
result
else
response.body = result
encode_body
:o18b
end
else
:o18b
end
end
# Multiple choices?
def o18b
decision_test(resource.multiple_choices?, 300, 200)
end
# Response includes an entity?
def o20
has_response_body? ? :o18 : 204
end
# Conflict?
def p3
if resource.is_conflict?
409
else
res = accept_helper
(Integer === res) ? res : :p11
end
end
# New resource?
def p11
(!response.headers[LOCATION]) ? :o20 : 201
end
end # module Flow
end # module Decision
end # module Webmachine