Repository navigation
Expand file tree
/
Copy pathresponders.cr
More file actions
367 lines (322 loc) · 10.5 KB
/
Copy pathresponders.cr
File metadata and controls
367 lines (322 loc) · 10.5 KB
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
require "json"
require "yaml"
module ActionController::Responders
STATUS_CODES = {
# 1xx informational
continue: 100,
switching_protocols: 101,
processing: 102,
# 2xx success
ok: 200,
created: 201,
accepted: 202,
non_authoritative_information: 203,
no_content: 204,
reset_content: 205,
partial_content: 206,
multi_status: 207,
already_reported: 208,
im_used: 226,
# 4xx client error
bad_request: 400,
unauthorized: 401,
payment_required: 402,
forbidden: 403,
not_found: 404,
method_not_allowed: 405,
not_acceptable: 406,
proxy_authentication_required: 407,
request_timeout: 408,
conflict: 409,
gone: 410,
length_required: 411,
precondition_failed: 412,
payload_too_large: 413,
uri_too_long: 414,
unsupported_media_type: 415,
range_not_satisfiable: 416,
expectation_failed: 417,
misdirected_request: 421,
unprocessable_entity: 422,
locked: 423,
failed_dependency: 424,
upgrade_required: 426,
precondition_required: 428,
too_many_requests: 429,
request_header_fields_too_large: 431,
unavailable_for_legal_reasons: 451,
# 5xx server error
internal_server_error: 500,
not_implemented: 501,
bad_gateway: 502,
service_unavailable: 503,
gateway_timeout: 504,
http_version_not_supported: 505,
variant_also_negotiates: 506,
insufficient_storage: 507,
loop_detected: 508,
not_extended: 510,
network_authentication_required: 511,
}
REDIRECTION_CODES = {
# 3xx redirection
multiple_choices: 300,
moved_permanently: 301,
found: 302,
see_other: 303,
not_modified: 304,
use_proxy: 305,
temporary_redirect: 307,
permanent_redirect: 308,
}
MIME_TYPES = {
binary: "application/octet-stream",
json: "application/json",
xml: "application/xml",
text: "text/plain",
html: "text/html",
yaml: "text/yaml",
}
macro render(status = :ok, head = nil, json = nil, yaml = nil, xml = nil, html = nil, text = nil, binary = nil, template = nil, partial = nil, layout = nil)
{% if [head, json, xml, html, yaml, text, binary, template, partial].all?(&.is_a?(Path)) %}
{{ raise "Render must be called with one of json, xml, html, yaml, text, binary, template, partial" }}
{% end %}
%response = @context.response
%ret_val = {{ json || yaml || xml || html || text || binary || template || partial }}
{% if status.is_a?(SymbolLiteral) %}
%response.status_code = {{STATUS_CODES[status]}}
{% else %}
%response.status_code = ({{status}}).to_i
{% end %}
%ctype = %response.headers["Content-Type"]?
%session = @__session__
%session.encode(%response.cookies) if %session && %session.modified
{% if json %}
%response.content_type = {{MIME_TYPES[:json]}} unless %ctype
unless @__head_request__
%json = ({{json}})
if %json.is_a?(String)
%json.to_s(%response)
else
%json.to_json(%response)
end
end
{% end %}
{% if yaml %}
%response.content_type = {{MIME_TYPES[:yaml]}} unless %ctype
unless @__head_request__
%yaml = ({{yaml}})
if %yaml.is_a?(String)
%yaml.to_s(%response)
else
%yaml.to_yaml(%response)
end
end
{% end %}
{% if xml %}
%response.content_type = {{MIME_TYPES[:xml]}} unless %ctype
{{xml}}.to_s(%response) unless @__head_request__
{% end %}
{% if html %}
%response.content_type = {{MIME_TYPES[:html]}} unless %ctype
{{html}}.to_s(%response) unless @__head_request__
{% end %}
{% if text %}
%response.content_type = {{MIME_TYPES[:text]}} unless %ctype
{{text}}.to_s(%response) unless @__head_request__
{% end %}
{% if binary %}
%response.content_type = {{MIME_TYPES[:binary]}} unless %ctype
{{binary}}.to_s(%response) unless @__head_request__
{% end %}
{% if template %}
%response.content_type = {{MIME_TYPES[:html]}} unless %ctype
{% if layout %}
template({{template}}, layout: {{layout}}, io: %response) unless @__head_request__
{% else %}
template({{template}}, io: %response) unless @__head_request__
{% end %}
{% end %}
{% if partial %}
%response.content_type = {{MIME_TYPES[:html]}} unless %ctype
template(partial: {{partial}}, io: %response) unless @__head_request__
{% end %}
@render_called = true
return %ret_val
end
macro head(status)
render({{status}}, true)
end
macro redirect_to(path, status = :found)
%response = @context.response
%session = @__session__
%session.encode(%response.cookies) if %session && %session.modified
{% if status.is_a?(SymbolLiteral) %}
%response.status_code = {{REDIRECTION_CODES[status]}}
{% else %}
%response.status_code = ({{status}}).to_i
{% end %}
%response.headers["Location"] = {{path}}
@render_called = true
return
end
macro respond_with(status = :ok, &block)
%resp = ::ActionController::Responders::SelectResponse.new(response, accepts_formats, @__head_request__)
%resp.responses do
{{block.body}}
end
%response = @context.response
{% if status != :ok || status != 200 %}
{% if status.is_a?(SymbolLiteral) %}
%response.status_code = {{STATUS_CODES[status]}}
{% else %}
%response.status_code = ({{status}}).to_i
{% end %}
{% end %}
%session = @__session__
%session.encode(%response.cookies) if %session && %session.modified
%resp.build_response
@render_called = true
return
end
ACCEPT_SEPARATOR_REGEX = /,\s*/
# Extracts the mime types from the Accept header
def accepts_formats
accept = request.headers["Accept"]?
if accept && !accept.empty?
accepts = accept.split(";").first?.try(&.split(ACCEPT_SEPARATOR_REGEX))
return accepts if accepts && !accepts.empty?
end
[] of String
end
# Helper class for selecting the response to render / execute
class SelectResponse
def initialize(@response : HTTP::Server::Response, formats, @head_request : Bool)
@accepts = SelectResponse.accepts(formats)
@options = {} of Symbol => Proc(IO, Nil)
end
@accepts : Hash(Symbol, String)
getter options
# Build a list of possible responses to the request
def responses
with self yield
end
macro html(obj = nil, &block : IO -> Nil)
{% if block.is_a?(Nop) %}
options[:html] = ->(io : IO){ ({{obj}}).to_s(io) }
{% else %}
options[:html] = ->(io : IO){
({{ block.body }}).to_s(io)
}
{% end %}
end
macro xml(obj = nil, &block : IO -> Nil)
{% if block.is_a?(Nop) %}
options[:xml] = ->(io : IO){ ({{obj}}).to_s(io) }
{% else %}
options[:xml] = ->(io : IO){
({{ block.body }}).to_s(io)
}
{% end %}
end
macro json(obj = nil, &block : IO -> Nil)
{% if block.is_a?(Nop) %}
options[:json] = ->(io : IO){
output = {{obj}}
{% if obj.is_a?(String) %}
output.to_s(io)
{% else %}
output.to_json(io)
{% end %}
}
{% else %}
options[:json] = ->(io : IO){
({{ block.body }}).to_s(io)
}
{% end %}
end
macro yaml(obj = nil, &block : IO -> Nil)
{% if block.is_a?(Nop) %}
options[:yaml] = ->(io : IO){
output = {{obj}}
{% if obj.is_a?(String) %}
output.to_s(io)
{% else %}
output.to_yaml(io)
{% end %}
}
{% else %}
options[:yaml] = ->(io : IO){
({{ block.body }}).to_s(io)
}
{% end %}
end
macro text(obj = nil, &block : IO -> Nil)
{% if block.is_a?(Nop) %}
options[:text] = ->(io : IO){ ({{obj}}).to_s(io) }
{% else %}
options[:text] = ->(io : IO){
({{ block.body }}).to_s(io)
}
{% end %}
end
macro binary(obj = nil, &block : IO -> Nil)
{% if block.is_a?(Nop) %}
options[:binary] = ->(io : IO){ ({{obj}}).to_s(io) }
{% else %}
options[:binary] = ->(io : IO){
({{ block.body }}).to_s(io)
}
{% end %}
end
# Respond appropriately
def build_response
found = nil
# Search for the first acceptable format
if !@accepts.empty?
@accepts.each do |response_format, mime|
option = @options[response_format]?
if option
@response.content_type = mime
found = option
break
end
end
if found
found.call(@response) unless @head_request
else
@response.status_code = 406 # not acceptable
end
else
# If no format requested then default to the first format specified
opt = @options.first
format = opt[0]
@response.content_type = MIME_TYPES[format]
opt[1].call(@response) unless @head_request
end
end
ACCEPTED_FORMATS = {
"text/html": :html,
"application/xml": :xml,
"text/xml": :xml,
"application/json": :json,
"text/plain": :text,
"application/octet-stream": :binary,
"text/yaml": :yaml,
"text/x-yaml": :yaml,
"application/yaml": :yaml,
"application/x-yaml": :yaml,
}
# Creates an ordered list of supported formats with requested mime types
def self.accepts(accepts_formats)
formats = {} of Symbol => String
accepts_formats.each do |format|
data_type = ACCEPTED_FORMATS[format]?
if data_type && formats[data_type]?.nil?
formats[data_type] = format
end
end
formats
end
end
end