/
rack.rb
121 lines (110 loc) · 2.25 KB
/
rack.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
class Request
# Rack request
class Rack < self
include Concord.new(:rack_env)
SERVER_PORT = Key.new('SERVER_PORT')
REQUEST_METHOD = Key.new('REQUEST_METHOD')
RACK_URL_SCHEME = Key.new('rack.url_scheme')
IF_MODIFIED_SINCE = Key.new('HTTP_IF_MODIFIED_SINCE')
CONTENT_LENGTH = Key.new('CONTENT_LENGTH')
# Declare accessor
#
# @param [Symbol] name
# @param [Key] key
#
# @return [undefined]
#
# @api private
#
def self.accessor(name, key)
define_method(name) do
access(key)
end
end
# Return http port
#
# @return [Fixnum]
#
# @api private
#
def port
@rack_env.fetch(SERVER_PORT).to_i(10)
end
memoize :port
# Return request protocol
#
# @return [Protocol]
#
# @api private
#
def protocol
Protocol.get(access(RACK_URL_SCHEME))
end
memoize :protocol
# Return request method
#
# @return [Method]
#
# @api private
#
def request_method
Method.get(access(REQUEST_METHOD))
end
memoize :request_method
# Return content length
#
# @return [Fixnum]
#
# @api private
#
def content_length
access(CONTENT_LENGTH).to_i(10)
end
# Return query params
#
# @return [Array]
#
# @api private
#
def query_params
Addressable::URI.form_unencode(query_string)
end
memoize :query_params
# Return if modified since
#
# @return [Time]
# if present
#
# @return [nil]
# otherwise
#
# @api private
#
def if_modified_since
value = @rack_env.fetch(IF_MODIFIED_SINCE) { return }
begin
Time.httpdate(value)
rescue ArgumentError
nil
end
end
memoize :if_modified_since
accessor(:path_info, Key.new('PATH_INFO') )
accessor(:host, Key.new('SERVER_NAME') )
accessor(:query_string, Key.new('QUERY_STRING'))
accessor(:content_type, Key.new('CONTENT_TYPE'))
accessor(:body, Key.new('rack.input'))
private
# Return value for key
#
# @param [Key] key
#
# @return [Object]
#
# @api private
#
def access(key)
@rack_env.fetch(key)
end
end
end