generated from athena-framework/component-template
/
request_context.cr
124 lines (98 loc) · 3.02 KB
/
request_context.cr
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
# Represents data from a request in an agnostic manner, primarily used to augment URL matching and generation with additional context.
class Athena::Routing::RequestContext
# Represents the path of the URL _before_ `#path`.
# E.g. a path that should be prefixed to all other `#path`s.
getter base_url : String
getter method : String
getter path : String
getter host : String
getter scheme : String
getter http_port : Int32
getter https_port : Int32
# Returns the query string of the current request.
getter query_string : String
# Returns the global parameters that should be used as part of the URL generation logic.
getter parameters : Hash(String, String?) = Hash(String, String?).new
# Creates a new instance of self from the provided *uri*.
# The *host*, *scheme*, *http_port*, and *https_port* optionally act as fallbacks if they are not contained within the *uri*.
def self.from_uri(uri : String, host : String = "localhost", scheme : String = "http", http_port : Int32 = 80, https_port : Int32 = 443) : self
self.from_uri URI.parse(uri), host, scheme, http_port, https_port
end
# :ditto:
def self.from_uri(uri : URI, host : String = "localhost", scheme : String = "http", http_port : Int32 = 80, https_port : Int32 = 443) : self
scheme = uri.scheme || scheme
if port = uri.port
if "http" == scheme
http_port = port
elsif "https" == scheme
https_port = port
end
end
new(
uri.path,
"GET",
uri.hostname || host,
scheme,
http_port,
https_port
)
end
def initialize(
@base_url : String = "",
@method : String = "GET",
@host : String = "localhost",
@scheme : String = "http",
@http_port : Int32 = 80,
@https_port : Int32 = 443,
@path : String = "/",
@query_string : String = ""
)
self.method = @method
self.host = @host
self.scheme = @scheme
end
# Updates the properties within `self` based on the provided *request*.
def apply(request : ART::Request) : self
self.method = request.method
self.host = if (h = request.hostname) && (h != "localhost")
h
elsif h = @host
h
else
"localhost"
end
self.query_string = request.query || ""
# TODO: Support this once it's exposed.
# self.scheme = request.scheme
self
end
def base_url=(@base_url : String) : self
self
end
def path=(@path : String) : self
self
end
def method=(method : String) : self
@method = method.upcase
self
end
def host=(host : String) : self
@host = host.downcase
self
end
def scheme=(scheme : String) : self
@scheme = scheme.downcase
self
end
def query_string=(query_string : String?) : self
@query_string = query_string.to_s
self
end
def set_parameter(name : String, value : String?) : self
@parameters[name] = value
self
end
def has_parameter?(name : String) : Bool
@parameters.has_key name
end
end