-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathsupport.cr
More file actions
76 lines (64 loc) · 2.24 KB
/
Copy pathsupport.cr
File metadata and controls
76 lines (64 loc) · 2.24 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
module ActionController::Support
def self.request_protocol(request)
return :https if request.headers["X-Forwarded-Proto"]? =~ /https/i
return :https if request.headers["Forwarded"]? =~ /https/i
:http
end
def self.redirect_to_https(context)
req = context.request
resp = context.response
resp.status_code = 302
resp.headers["Location"] = "https://#{req.headers["Host"]?.try(&.split(':')[0])}#{req.resource}"
end
def self.websocket_upgrade_request?(request)
return false unless upgrade = request.headers["Upgrade"]?
return false unless upgrade.compare("websocket", case_insensitive: true) == 0
request.headers.includes_word?("Connection", "Upgrade")
end
# Used in base.cr to build routes for the redirect_to helpers
def self.build_route(route, hash_parts : Hash((String | Symbol), (Nil | Bool | Int32 | Int64 | Float32 | Float64 | String | Symbol))? = nil, **tuple_parts)
keys = route.split("/:")[1..-1].map &.split("/")[0]
params = {} of String => String
if hash_parts
hash_parts.each do |key, value|
key = key.to_s
value = value.to_s
if keys.includes?(key)
route = route.sub(":#{key}", URI.encode_path(value))
keys.delete(key)
else
params[key] = value
end
end
end
# Tuple overwrites hash parts (so safe to use a user generated hash)
tuple_parts.each do |key, value|
key = key.to_s
value = value.to_s
if keys.includes?(key)
route = route.sub(":#{key}", URI.encode_path(value))
keys.delete(key)
else
params[key] = value
end
end
# Raise error if not all parts are substituted
raise ActionController::InvalidRoute.new("route parameters missing :#{keys.join(", :")} for #{route}") unless keys.empty?
# Add any remaining values as query params
if params.empty?
route
else
"#{route}?#{URI::Params.encode(params)}"
end
end
TYPE_SEPARATOR_REGEX = /;\s*/
# Extracts the mime type from the content type header
def self.content_type(headers)
ctype = headers["Content-Type"]?
if ctype && !ctype.empty?
ctype = ctype.split(TYPE_SEPARATOR_REGEX).first?
return ctype if ctype && !ctype.empty?
end
nil
end
end