rack 2.2.3 → 3.2.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +774 -69
- data/CONTRIBUTING.md +63 -55
- data/MIT-LICENSE +1 -1
- data/README.md +384 -0
- data/SPEC.rdoc +243 -277
- data/lib/rack/auth/abstract/handler.rb +3 -1
- data/lib/rack/auth/abstract/request.rb +5 -1
- data/lib/rack/auth/basic.rb +1 -4
- data/lib/rack/bad_request.rb +8 -0
- data/lib/rack/body_proxy.rb +21 -3
- data/lib/rack/builder.rb +108 -69
- data/lib/rack/cascade.rb +2 -3
- data/lib/rack/common_logger.rb +26 -17
- data/lib/rack/conditional_get.rb +20 -16
- data/lib/rack/constants.rb +68 -0
- data/lib/rack/content_length.rb +12 -16
- data/lib/rack/content_type.rb +8 -5
- data/lib/rack/deflater.rb +40 -26
- data/lib/rack/directory.rb +15 -6
- data/lib/rack/etag.rb +17 -21
- data/lib/rack/events.rb +25 -6
- data/lib/rack/files.rb +16 -18
- data/lib/rack/head.rb +8 -8
- data/lib/rack/headers.rb +238 -0
- data/lib/rack/lint.rb +863 -705
- data/lib/rack/lock.rb +2 -5
- data/lib/rack/media_type.rb +18 -9
- data/lib/rack/method_override.rb +6 -2
- data/lib/rack/mime.rb +14 -5
- data/lib/rack/mock.rb +1 -271
- data/lib/rack/mock_request.rb +161 -0
- data/lib/rack/mock_response.rb +156 -0
- data/lib/rack/multipart/generator.rb +7 -5
- data/lib/rack/multipart/parser.rb +349 -92
- data/lib/rack/multipart/uploaded_file.rb +45 -4
- data/lib/rack/multipart.rb +53 -41
- data/lib/rack/null_logger.rb +9 -0
- data/lib/rack/query_parser.rb +150 -106
- data/lib/rack/recursive.rb +2 -0
- data/lib/rack/reloader.rb +0 -2
- data/lib/rack/request.rb +272 -141
- data/lib/rack/response.rb +151 -66
- data/lib/rack/rewindable_input.rb +27 -5
- data/lib/rack/runtime.rb +7 -6
- data/lib/rack/sendfile.rb +70 -35
- data/lib/rack/show_exceptions.rb +25 -6
- data/lib/rack/show_status.rb +17 -9
- data/lib/rack/static.rb +17 -12
- data/lib/rack/tempfile_reaper.rb +15 -4
- data/lib/rack/urlmap.rb +4 -2
- data/lib/rack/utils.rb +351 -250
- data/lib/rack/version.rb +3 -15
- data/lib/rack.rb +13 -90
- metadata +16 -45
- data/README.rdoc +0 -306
- data/Rakefile +0 -130
- data/bin/rackup +0 -5
- data/contrib/rack.png +0 -0
- data/contrib/rack.svg +0 -150
- data/contrib/rack_logo.svg +0 -164
- data/contrib/rdoc.css +0 -412
- data/example/lobster.ru +0 -6
- data/example/protectedlobster.rb +0 -16
- data/example/protectedlobster.ru +0 -10
- data/lib/rack/auth/digest/md5.rb +0 -131
- data/lib/rack/auth/digest/nonce.rb +0 -54
- data/lib/rack/auth/digest/params.rb +0 -54
- data/lib/rack/auth/digest/request.rb +0 -43
- data/lib/rack/chunked.rb +0 -117
- data/lib/rack/core_ext/regexp.rb +0 -14
- data/lib/rack/file.rb +0 -7
- data/lib/rack/handler/cgi.rb +0 -59
- data/lib/rack/handler/fastcgi.rb +0 -100
- data/lib/rack/handler/lsws.rb +0 -61
- data/lib/rack/handler/scgi.rb +0 -71
- data/lib/rack/handler/thin.rb +0 -36
- data/lib/rack/handler/webrick.rb +0 -129
- data/lib/rack/handler.rb +0 -104
- data/lib/rack/lobster.rb +0 -70
- data/lib/rack/logger.rb +0 -20
- data/lib/rack/server.rb +0 -466
- data/lib/rack/session/abstract/id.rb +0 -523
- data/lib/rack/session/cookie.rb +0 -203
- data/lib/rack/session/memcache.rb +0 -10
- data/lib/rack/session/pool.rb +0 -85
- data/rack.gemspec +0 -46
data/lib/rack/lint.rb
CHANGED
|
@@ -2,805 +2,963 @@
|
|
|
2
2
|
|
|
3
3
|
require 'forwardable'
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
# responses according to the Rack spec.
|
|
5
|
+
require_relative 'constants'
|
|
6
|
+
require_relative 'utils'
|
|
8
7
|
|
|
8
|
+
module Rack
|
|
9
|
+
# Validates your application and the requests and responses according to the Rack spec. See SPEC.rdoc for details.
|
|
9
10
|
class Lint
|
|
11
|
+
# Represents a failure to meet the Rack specification.
|
|
12
|
+
class LintError < RuntimeError; end
|
|
13
|
+
|
|
14
|
+
# Invoke the application, validating the request and response according to the Rack spec.
|
|
15
|
+
def call(env = nil)
|
|
16
|
+
Wrapper.new(@app, env).response
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# :stopdoc:
|
|
20
|
+
|
|
21
|
+
ALLOWED_SCHEMES = %w(https http wss ws).freeze
|
|
22
|
+
|
|
23
|
+
REQUEST_PATH_ORIGIN_FORM = /\A\/[^#]*\z/
|
|
24
|
+
REQUEST_PATH_ABSOLUTE_FORM = /\A#{Utils::URI_PARSER.make_regexp}\z/
|
|
25
|
+
REQUEST_PATH_AUTHORITY_FORM = /\A[^\/:]+:\d+\z/
|
|
26
|
+
REQUEST_PATH_ASTERISK_FORM = '*'
|
|
27
|
+
|
|
28
|
+
# Match a host name, according to RFC3986. Copied from `URI::RFC3986_Parser::HOST` because older Ruby versions (< 3.3) don't expose it.
|
|
29
|
+
HOST_PATTERN = /
|
|
30
|
+
(?<IP-literal>\[(?:
|
|
31
|
+
(?<IPv6address>
|
|
32
|
+
(?:\h{1,4}:){6}
|
|
33
|
+
(?<ls32>\h{1,4}:\h{1,4}
|
|
34
|
+
| (?<IPv4address>(?<dec-octet>[1-9]\d|1\d{2}|2[0-4]\d|25[0-5]|\d)
|
|
35
|
+
\.\g<dec-octet>\.\g<dec-octet>\.\g<dec-octet>)
|
|
36
|
+
)
|
|
37
|
+
| ::(?:\h{1,4}:){5}\g<ls32>
|
|
38
|
+
| \h{1,4}?::(?:\h{1,4}:){4}\g<ls32>
|
|
39
|
+
| (?:(?:\h{1,4}:)?\h{1,4})?::(?:\h{1,4}:){3}\g<ls32>
|
|
40
|
+
| (?:(?:\h{1,4}:){,2}\h{1,4})?::(?:\h{1,4}:){2}\g<ls32>
|
|
41
|
+
| (?:(?:\h{1,4}:){,3}\h{1,4})?::\h{1,4}:\g<ls32>
|
|
42
|
+
| (?:(?:\h{1,4}:){,4}\h{1,4})?::\g<ls32>
|
|
43
|
+
| (?:(?:\h{1,4}:){,5}\h{1,4})?::\h{1,4}
|
|
44
|
+
| (?:(?:\h{1,4}:){,6}\h{1,4})?::
|
|
45
|
+
)
|
|
46
|
+
| (?<IPvFuture>v\h++\.[!$&-.0-9:;=A-Z_a-z~]++)
|
|
47
|
+
)\])
|
|
48
|
+
| \g<IPv4address>
|
|
49
|
+
| (?<reg-name>(?:%\h\h|[!$&-.0-9;=A-Z_a-z~])*+)
|
|
50
|
+
/x.freeze
|
|
51
|
+
SERVER_NAME_PATTERN = /\A#{HOST_PATTERN}\z/.freeze
|
|
52
|
+
HTTP_HOST_PATTERN = /\A#{HOST_PATTERN}(:\d*+)?\z/.freeze
|
|
53
|
+
|
|
54
|
+
private_constant :HOST_PATTERN, :SERVER_NAME_PATTERN, :HTTP_HOST_PATTERN
|
|
55
|
+
|
|
56
|
+
# N.B. The empty `##` comments creates paragraphs in the output. A trailing "\" is used to escape the newline character, which combines the comments into a single paragraph.
|
|
57
|
+
#
|
|
58
|
+
## = Rack Specification
|
|
59
|
+
##
|
|
60
|
+
## This specification aims to formalize the Rack protocol. You can (and should) use +Rack::Lint+ to enforce it. When you develop middleware, be sure to test with +Rack::Lint+ to catch possible violations of this specification.
|
|
61
|
+
##
|
|
62
|
+
## == The Application
|
|
63
|
+
##
|
|
64
|
+
## A Rack application is a Ruby object that responds to +call+. \
|
|
10
65
|
def initialize(app)
|
|
66
|
+
raise LintError, "app must respond to call" unless app.respond_to?(:call)
|
|
67
|
+
|
|
11
68
|
@app = app
|
|
12
|
-
@content_length = nil
|
|
13
69
|
end
|
|
14
70
|
|
|
15
|
-
|
|
71
|
+
class Wrapper
|
|
72
|
+
def initialize(app, env)
|
|
73
|
+
@app = app
|
|
74
|
+
@env = env
|
|
75
|
+
@response = nil
|
|
76
|
+
@head_request = false
|
|
77
|
+
|
|
78
|
+
@status = nil
|
|
79
|
+
@headers = nil
|
|
80
|
+
@body = nil
|
|
81
|
+
@consumed = nil
|
|
82
|
+
@content_length = nil
|
|
83
|
+
@closed = false
|
|
84
|
+
@size = 0
|
|
85
|
+
end
|
|
16
86
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
87
|
+
def response
|
|
88
|
+
## It takes exactly one argument, the +environment+ (representing an HTTP request) \
|
|
89
|
+
raise LintError, "No env given" unless @env
|
|
90
|
+
check_environment(@env)
|
|
91
|
+
|
|
92
|
+
## and returns a non-frozen +Array+ of exactly three elements: \
|
|
93
|
+
@response = @app.call(@env)
|
|
94
|
+
|
|
95
|
+
raise LintError, "response is not an Array, but #{@response.class}" unless @response.kind_of? Array
|
|
96
|
+
raise LintError, "response is frozen" if @response.frozen?
|
|
97
|
+
raise LintError, "response array has #{@response.size} elements instead of 3" unless @response.size == 3
|
|
98
|
+
|
|
99
|
+
@status, @headers, @body = @response
|
|
100
|
+
## the +status+, \
|
|
101
|
+
check_status(@status)
|
|
102
|
+
|
|
103
|
+
## the +headers+, \
|
|
104
|
+
check_headers(@headers)
|
|
105
|
+
|
|
106
|
+
hijack_proc = check_hijack_response(@headers, @env)
|
|
107
|
+
if hijack_proc
|
|
108
|
+
@headers[RACK_HIJACK] = hijack_proc
|
|
22
109
|
end
|
|
23
|
-
end
|
|
24
|
-
end
|
|
25
|
-
include Assertion
|
|
26
110
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
111
|
+
## and the +body+ (representing an HTTP response).
|
|
112
|
+
check_content_type_header(@status, @headers)
|
|
113
|
+
check_content_length_header(@status, @headers)
|
|
114
|
+
check_rack_protocol_header(@status, @headers)
|
|
115
|
+
@head_request = @env[REQUEST_METHOD] == HEAD
|
|
32
116
|
|
|
33
|
-
|
|
117
|
+
@lint = (@env['rack.lint'] ||= []) << self
|
|
34
118
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
dup._call(env)
|
|
39
|
-
end
|
|
119
|
+
if (@env['rack.lint.body_iteration'] ||= 0) > 0
|
|
120
|
+
raise LintError, "Middleware must not call #each directly"
|
|
121
|
+
end
|
|
40
122
|
|
|
41
|
-
|
|
42
|
-
## It takes exactly one argument, the *environment*
|
|
43
|
-
assert("No env given") { env }
|
|
44
|
-
check_env env
|
|
45
|
-
|
|
46
|
-
env[RACK_INPUT] = InputWrapper.new(env[RACK_INPUT])
|
|
47
|
-
env[RACK_ERRORS] = ErrorWrapper.new(env[RACK_ERRORS])
|
|
48
|
-
|
|
49
|
-
## and returns an Array of exactly three values:
|
|
50
|
-
ary = @app.call(env)
|
|
51
|
-
assert("response #{ary.inspect} is not an Array , but #{ary.class}") {
|
|
52
|
-
ary.kind_of? Array
|
|
53
|
-
}
|
|
54
|
-
assert("response array #{ary.inspect} has #{ary.size} elements instead of 3") {
|
|
55
|
-
ary.size == 3
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
status, headers, @body = ary
|
|
59
|
-
## The *status*,
|
|
60
|
-
check_status status
|
|
61
|
-
## the *headers*,
|
|
62
|
-
check_headers headers
|
|
63
|
-
|
|
64
|
-
hijack_proc = check_hijack_response headers, env
|
|
65
|
-
if hijack_proc && headers.is_a?(Hash)
|
|
66
|
-
headers[RACK_HIJACK] = hijack_proc
|
|
123
|
+
return [@status, @headers, self]
|
|
67
124
|
end
|
|
68
125
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
check_content_length status, headers
|
|
72
|
-
@head_request = env[REQUEST_METHOD] == HEAD
|
|
73
|
-
[status, headers, self]
|
|
74
|
-
end
|
|
126
|
+
private def assert_required(key)
|
|
127
|
+
raise LintError, "env missing required key #{key}" unless @env.include?(key)
|
|
75
128
|
|
|
76
|
-
|
|
77
|
-
def check_env(env)
|
|
78
|
-
## The environment must be an unfrozen instance of Hash that includes
|
|
79
|
-
## CGI-like headers. The application is free to modify the
|
|
80
|
-
## environment.
|
|
81
|
-
assert("env #{env.inspect} is not a Hash, but #{env.class}") {
|
|
82
|
-
env.kind_of? Hash
|
|
83
|
-
}
|
|
84
|
-
assert("env should not be frozen, but is") {
|
|
85
|
-
!env.frozen?
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
##
|
|
89
|
-
## The environment is required to include these variables
|
|
90
|
-
## (adopted from PEP333), except when they'd be empty, but see
|
|
91
|
-
## below.
|
|
92
|
-
|
|
93
|
-
## <tt>REQUEST_METHOD</tt>:: The HTTP request method, such as
|
|
94
|
-
## "GET" or "POST". This cannot ever
|
|
95
|
-
## be an empty string, and so is
|
|
96
|
-
## always required.
|
|
97
|
-
|
|
98
|
-
## <tt>SCRIPT_NAME</tt>:: The initial portion of the request
|
|
99
|
-
## URL's "path" that corresponds to the
|
|
100
|
-
## application object, so that the
|
|
101
|
-
## application knows its virtual
|
|
102
|
-
## "location". This may be an empty
|
|
103
|
-
## string, if the application corresponds
|
|
104
|
-
## to the "root" of the server.
|
|
105
|
-
|
|
106
|
-
## <tt>PATH_INFO</tt>:: The remainder of the request URL's
|
|
107
|
-
## "path", designating the virtual
|
|
108
|
-
## "location" of the request's target
|
|
109
|
-
## within the application. This may be an
|
|
110
|
-
## empty string, if the request URL targets
|
|
111
|
-
## the application root and does not have a
|
|
112
|
-
## trailing slash. This value may be
|
|
113
|
-
## percent-encoded when originating from
|
|
114
|
-
## a URL.
|
|
115
|
-
|
|
116
|
-
## <tt>QUERY_STRING</tt>:: The portion of the request URL that
|
|
117
|
-
## follows the <tt>?</tt>, if any. May be
|
|
118
|
-
## empty, but is always required!
|
|
119
|
-
|
|
120
|
-
## <tt>SERVER_NAME</tt>:: When combined with <tt>SCRIPT_NAME</tt> and
|
|
121
|
-
## <tt>PATH_INFO</tt>, these variables can be
|
|
122
|
-
## used to complete the URL. Note, however,
|
|
123
|
-
## that <tt>HTTP_HOST</tt>, if present,
|
|
124
|
-
## should be used in preference to
|
|
125
|
-
## <tt>SERVER_NAME</tt> for reconstructing
|
|
126
|
-
## the request URL.
|
|
127
|
-
## <tt>SERVER_NAME</tt> can never be an empty
|
|
128
|
-
## string, and so is always required.
|
|
129
|
-
|
|
130
|
-
## <tt>SERVER_PORT</tt>:: An optional +Integer+ which is the port the
|
|
131
|
-
## server is running on. Should be specified if
|
|
132
|
-
## the server is running on a non-standard port.
|
|
133
|
-
|
|
134
|
-
## <tt>HTTP_</tt> Variables:: Variables corresponding to the
|
|
135
|
-
## client-supplied HTTP request
|
|
136
|
-
## headers (i.e., variables whose
|
|
137
|
-
## names begin with <tt>HTTP_</tt>). The
|
|
138
|
-
## presence or absence of these
|
|
139
|
-
## variables should correspond with
|
|
140
|
-
## the presence or absence of the
|
|
141
|
-
## appropriate HTTP header in the
|
|
142
|
-
## request. See
|
|
143
|
-
## {RFC3875 section 4.1.18}[https://tools.ietf.org/html/rfc3875#section-4.1.18]
|
|
144
|
-
## for specific behavior.
|
|
145
|
-
|
|
146
|
-
## In addition to this, the Rack environment must include these
|
|
147
|
-
## Rack-specific variables:
|
|
148
|
-
|
|
149
|
-
## <tt>rack.version</tt>:: The Array representing this version of Rack
|
|
150
|
-
## See Rack::VERSION, that corresponds to
|
|
151
|
-
## the version of this SPEC.
|
|
152
|
-
|
|
153
|
-
## <tt>rack.url_scheme</tt>:: +http+ or +https+, depending on the
|
|
154
|
-
## request URL.
|
|
155
|
-
|
|
156
|
-
## <tt>rack.input</tt>:: See below, the input stream.
|
|
157
|
-
|
|
158
|
-
## <tt>rack.errors</tt>:: See below, the error stream.
|
|
159
|
-
|
|
160
|
-
## <tt>rack.multithread</tt>:: true if the application object may be
|
|
161
|
-
## simultaneously invoked by another thread
|
|
162
|
-
## in the same process, false otherwise.
|
|
163
|
-
|
|
164
|
-
## <tt>rack.multiprocess</tt>:: true if an equivalent application object
|
|
165
|
-
## may be simultaneously invoked by another
|
|
166
|
-
## process, false otherwise.
|
|
167
|
-
|
|
168
|
-
## <tt>rack.run_once</tt>:: true if the server expects
|
|
169
|
-
## (but does not guarantee!) that the
|
|
170
|
-
## application will only be invoked this one
|
|
171
|
-
## time during the life of its containing
|
|
172
|
-
## process. Normally, this will only be true
|
|
173
|
-
## for a server based on CGI
|
|
174
|
-
## (or something similar).
|
|
175
|
-
|
|
176
|
-
## <tt>rack.hijack?</tt>:: present and true if the server supports
|
|
177
|
-
## connection hijacking. See below, hijacking.
|
|
178
|
-
|
|
179
|
-
## <tt>rack.hijack</tt>:: an object responding to #call that must be
|
|
180
|
-
## called at least once before using
|
|
181
|
-
## rack.hijack_io.
|
|
182
|
-
## It is recommended #call return rack.hijack_io
|
|
183
|
-
## as well as setting it in env if necessary.
|
|
184
|
-
|
|
185
|
-
## <tt>rack.hijack_io</tt>:: if rack.hijack? is true, and rack.hijack
|
|
186
|
-
## has received #call, this will contain
|
|
187
|
-
## an object resembling an IO. See hijacking.
|
|
188
|
-
|
|
189
|
-
## Additional environment specifications have approved to
|
|
190
|
-
## standardized middleware APIs. None of these are required to
|
|
191
|
-
## be implemented by the server.
|
|
192
|
-
|
|
193
|
-
## <tt>rack.session</tt>:: A hash like interface for storing
|
|
194
|
-
## request session data.
|
|
195
|
-
## The store must implement:
|
|
196
|
-
if session = env[RACK_SESSION]
|
|
197
|
-
## store(key, value) (aliased as []=);
|
|
198
|
-
assert("session #{session.inspect} must respond to store and []=") {
|
|
199
|
-
session.respond_to?(:store) && session.respond_to?(:[]=)
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
## fetch(key, default = nil) (aliased as []);
|
|
203
|
-
assert("session #{session.inspect} must respond to fetch and []") {
|
|
204
|
-
session.respond_to?(:fetch) && session.respond_to?(:[])
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
## delete(key);
|
|
208
|
-
assert("session #{session.inspect} must respond to delete") {
|
|
209
|
-
session.respond_to?(:delete)
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
## clear;
|
|
213
|
-
assert("session #{session.inspect} must respond to clear") {
|
|
214
|
-
session.respond_to?(:clear)
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
## to_hash (returning unfrozen Hash instance);
|
|
218
|
-
assert("session #{session.inspect} must respond to to_hash and return unfrozen Hash instance") {
|
|
219
|
-
session.respond_to?(:to_hash) && session.to_hash.kind_of?(Hash) && !session.to_hash.frozen?
|
|
220
|
-
}
|
|
129
|
+
return @env[key]
|
|
221
130
|
end
|
|
222
131
|
|
|
223
|
-
##
|
|
224
|
-
##
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
logger.respond_to?(:warn)
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
## error(message, &block)
|
|
242
|
-
assert("logger #{logger.inspect} must respond to error") {
|
|
243
|
-
logger.respond_to?(:error)
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
## fatal(message, &block)
|
|
247
|
-
assert("logger #{logger.inspect} must respond to fatal") {
|
|
248
|
-
logger.respond_to?(:fatal)
|
|
249
|
-
}
|
|
250
|
-
end
|
|
132
|
+
##
|
|
133
|
+
## == The Request Environment
|
|
134
|
+
##
|
|
135
|
+
## Incoming HTTP requests are represented using an environment. \
|
|
136
|
+
def check_environment(env)
|
|
137
|
+
## The environment must be an unfrozen +Hash+. The Rack application is free to modify the environment, but the modified environment should also comply with this specification. \
|
|
138
|
+
raise LintError, "env #{env.inspect} is not a Hash, but #{env.class}" unless env.kind_of? Hash
|
|
139
|
+
raise LintError, "env should not be frozen, but is" if env.frozen?
|
|
140
|
+
|
|
141
|
+
## All environment keys must be strings.
|
|
142
|
+
keys = env.keys
|
|
143
|
+
keys.reject!{|key| String === key}
|
|
144
|
+
unless keys.empty?
|
|
145
|
+
raise LintError, "env contains non-string keys: #{keys.inspect}"
|
|
146
|
+
end
|
|
251
147
|
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
148
|
+
##
|
|
149
|
+
## === CGI Variables
|
|
150
|
+
##
|
|
151
|
+
## The environment is required to include these variables, adopted from {The Common Gateway Interface}[https://datatracker.ietf.org/doc/html/rfc3875] (CGI), except when they'd be empty, but see below.
|
|
152
|
+
|
|
153
|
+
##
|
|
154
|
+
## The CGI keys (named without a period) must have +String+ values and are reserved for the Rack specification. If the values for CGI keys contain non-ASCII characters, they should use <tt>ASCII-8BIT</tt> encoding.
|
|
155
|
+
env.each do |key, value|
|
|
156
|
+
next if key.include?(".") # Skip extensions
|
|
157
|
+
|
|
158
|
+
unless value.kind_of? String
|
|
159
|
+
raise LintError, "env variable #{key} has non-string value #{value.inspect}"
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
next if value.encoding == Encoding::ASCII_8BIT
|
|
163
|
+
|
|
164
|
+
unless value.b !~ /[\x80-\xff]/n
|
|
165
|
+
raise LintError, "env variable #{key} has value containing non-ASCII characters and has non-ASCII-8BIT encoding #{value.inspect} encoding: #{value.encoding}"
|
|
166
|
+
end
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
##
|
|
170
|
+
## The server and application can store their own data in the environment, too. The keys must contain at least one dot, and should be prefixed uniquely. The prefix <tt>rack.</tt> is reserved for use with the Rack specification and the classes that ship with Rack.
|
|
258
171
|
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
172
|
+
##
|
|
173
|
+
## ==== <tt>REQUEST_METHOD</tt>
|
|
174
|
+
##
|
|
175
|
+
## The HTTP request method, such as "GET" or "POST". This cannot ever be an empty string, and so is always required.
|
|
176
|
+
request_method = assert_required(REQUEST_METHOD)
|
|
177
|
+
unless request_method =~ /\A[0-9A-Za-z!\#$%&'*+.^_`|~-]+\z/
|
|
178
|
+
raise LintError, "REQUEST_METHOD unknown: #{request_method.inspect}"
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
##
|
|
182
|
+
## ==== <tt>SCRIPT_NAME</tt>
|
|
183
|
+
##
|
|
184
|
+
## The initial portion of the request URL's path that corresponds to the application object, so that the application knows its virtual location. This may be an empty string, if the application corresponds to the root of the server. If non-empty, the string must start with <tt>/</tt>, but should not end with <tt>/</tt>.
|
|
185
|
+
if script_name = env[SCRIPT_NAME]
|
|
186
|
+
if script_name != "" && script_name !~ /\A\//
|
|
187
|
+
raise LintError, "SCRIPT_NAME must start with /"
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
##
|
|
191
|
+
## In addition, <tt>SCRIPT_NAME</tt> MUST not be <tt>/</tt>, but instead be empty, \
|
|
192
|
+
if script_name == "/"
|
|
193
|
+
raise LintError, "SCRIPT_NAME cannot be '/', make it '' and PATH_INFO '/'"
|
|
194
|
+
end
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
## and one of <tt>SCRIPT_NAME</tt> or <tt>PATH_INFO</tt> must be set, e.g. <tt>PATH_INFO</tt> can be <tt>/</tt> if <tt>SCRIPT_NAME</tt> is empty.
|
|
198
|
+
path_info = env[PATH_INFO]
|
|
199
|
+
if (script_name.nil? || script_name.empty?) && (path_info.nil? || path_info.empty?)
|
|
200
|
+
raise LintError, "One of SCRIPT_NAME or PATH_INFO must be set (make PATH_INFO '/' if SCRIPT_NAME is empty)"
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
##
|
|
204
|
+
## ==== <tt>PATH_INFO</tt>
|
|
205
|
+
##
|
|
206
|
+
## The remainder of the request URL's "path", designating the virtual "location" of the request's target within the application. This may be an empty string, if the request URL targets the application root and does not have a trailing slash. This value may be percent-encoded when originating from a URL.
|
|
207
|
+
##
|
|
208
|
+
## The <tt>PATH_INFO</tt>, if provided, must be a valid request target or an empty string, as defined by {RFC9110}[https://datatracker.ietf.org/doc/html/rfc9110#target.resource].
|
|
209
|
+
case path_info
|
|
210
|
+
when REQUEST_PATH_ASTERISK_FORM
|
|
211
|
+
## * Only <tt>OPTIONS</tt> requests may have <tt>PATH_INFO</tt> set to <tt>*</tt> (asterisk-form).
|
|
212
|
+
unless request_method == OPTIONS
|
|
213
|
+
raise LintError, "Only OPTIONS requests may have PATH_INFO set to '*' (asterisk-form)"
|
|
214
|
+
end
|
|
215
|
+
when REQUEST_PATH_AUTHORITY_FORM
|
|
216
|
+
## * Only <tt>CONNECT</tt> requests may have <tt>PATH_INFO</tt> set to an authority (authority-form). Note that in HTTP/2+, the authority-form is not a valid request target.
|
|
217
|
+
unless request_method == CONNECT
|
|
218
|
+
raise LintError, "Only CONNECT requests may have PATH_INFO set to an authority (authority-form)"
|
|
219
|
+
end
|
|
220
|
+
when REQUEST_PATH_ABSOLUTE_FORM
|
|
221
|
+
## * <tt>CONNECT</tt> and <tt>OPTIONS</tt> requests must not have <tt>PATH_INFO</tt> set to a URI (absolute-form).
|
|
222
|
+
if request_method == CONNECT || request_method == OPTIONS
|
|
223
|
+
raise LintError, "CONNECT and OPTIONS requests must not have PATH_INFO set to a URI (absolute-form)"
|
|
224
|
+
end
|
|
225
|
+
when REQUEST_PATH_ORIGIN_FORM
|
|
226
|
+
## * Otherwise, <tt>PATH_INFO</tt> must start with a <tt>/</tt> and must not include a fragment part starting with <tt>#</tt> (origin-form).
|
|
227
|
+
when "", nil
|
|
228
|
+
# Empty string or nil is okay.
|
|
229
|
+
else
|
|
230
|
+
raise LintError, "PATH_INFO must start with a '/' and must not include a fragment part starting with '#' (origin-form)"
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
##
|
|
234
|
+
## ==== <tt>QUERY_STRING</tt>
|
|
235
|
+
##
|
|
236
|
+
## The portion of the request URL that follows the <tt>?</tt>, if any. May be empty, but is always required!
|
|
237
|
+
assert_required(QUERY_STRING)
|
|
238
|
+
|
|
239
|
+
##
|
|
240
|
+
## ==== <tt>SERVER_NAME</tt>
|
|
241
|
+
##
|
|
242
|
+
## Must be a valid host, as defined by {RFC3986}[https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2].
|
|
243
|
+
##
|
|
244
|
+
## When combined with <tt>SCRIPT_NAME</tt>, <tt>PATH_INFO</tt>, and <tt>QUERY_STRING</tt>, these variables can be used to reconstruct the original the request URL. Note, however, that <tt>HTTP_HOST</tt>, if present, should be used in preference to <tt>SERVER_NAME</tt> for reconstructing the request URL.
|
|
245
|
+
server_name = assert_required(SERVER_NAME)
|
|
246
|
+
unless server_name.match?(SERVER_NAME_PATTERN)
|
|
247
|
+
raise LintError, "env[SERVER_NAME] must be a valid host"
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
##
|
|
251
|
+
## ==== <tt>SERVER_PROTOCOL</tt>
|
|
252
|
+
##
|
|
253
|
+
## The HTTP version used for the request. It must match the regular expression <tt>HTTP\/\d(\.\d)?</tt>.
|
|
254
|
+
server_protocol = assert_required(SERVER_PROTOCOL)
|
|
255
|
+
unless %r{HTTP/\d(\.\d)?}.match?(server_protocol)
|
|
256
|
+
raise LintError, "env[SERVER_PROTOCOL] does not match HTTP/\\d(\\.\\d)?"
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
##
|
|
260
|
+
## ==== <tt>SERVER_PORT</tt>
|
|
261
|
+
##
|
|
262
|
+
## The port the server is running on, if the server is running on a non-standard port. It must consist of digits only.
|
|
263
|
+
##
|
|
264
|
+
## The standard ports are:
|
|
265
|
+
## * 80 for HTTP
|
|
266
|
+
## * 443 for HTTPS
|
|
267
|
+
if server_port = env[SERVER_PORT]
|
|
268
|
+
unless server_port =~ /\A\d+\z/
|
|
269
|
+
raise LintError, "env[SERVER_PORT] is not an Integer"
|
|
270
|
+
end
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
##
|
|
274
|
+
## ==== <tt>CONTENT_TYPE</tt>
|
|
275
|
+
##
|
|
276
|
+
## The optional MIME type of the request body, if any.
|
|
277
|
+
# N.B. We do not validate this field as it is considered user-provided data.
|
|
278
|
+
|
|
279
|
+
##
|
|
280
|
+
## ==== <tt>CONTENT_LENGTH</tt>
|
|
281
|
+
##
|
|
282
|
+
## The length of the request body, if any. It must consist of digits only.
|
|
283
|
+
if content_length = env["CONTENT_LENGTH"]
|
|
284
|
+
if content_length !~ /\A\d+\z/
|
|
285
|
+
raise LintError, "Invalid CONTENT_LENGTH: #{content_length.inspect}"
|
|
286
|
+
end
|
|
287
|
+
end
|
|
288
|
+
|
|
289
|
+
##
|
|
290
|
+
## ==== <tt>HTTP_HOST</tt>
|
|
291
|
+
##
|
|
292
|
+
## An optional HTTP authority, as defined by {RFC9110}[https://datatracker.ietf.org/doc/html/rfc9110#name-host-and-authority].
|
|
293
|
+
if http_host = env[HTTP_HOST]
|
|
294
|
+
unless http_host.match?(HTTP_HOST_PATTERN)
|
|
295
|
+
raise LintError, "env[HTTP_HOST] must be a valid authority"
|
|
296
|
+
end
|
|
297
|
+
end
|
|
298
|
+
|
|
299
|
+
##
|
|
300
|
+
## ==== <tt>HTTP_</tt> Headers
|
|
301
|
+
##
|
|
302
|
+
## Unless specified above, the environment can contain any number of additional headers, each starting with <tt>HTTP_</tt>. The presence or absence of these variables should correspond with the presence or absence of the appropriate HTTP header in the request, and those headers have no specific interpretation or validation by the Rack specification. However, there are many standard HTTP headers that have a specific meaning in the context of a request; see {RFC3875 section 4.1.18}[https://tools.ietf.org/html/rfc3875#section-4.1.18] for more details.
|
|
303
|
+
##
|
|
304
|
+
## For compatibility with the CGI specifiction, the environment must not contain the keys <tt>HTTP_CONTENT_TYPE</tt> or <tt>HTTP_CONTENT_LENGTH</tt>. Instead, the keys <tt>CONTENT_TYPE</tt> and <tt>CONTENT_LENGTH</tt> must be used.
|
|
305
|
+
%w[HTTP_CONTENT_TYPE HTTP_CONTENT_LENGTH].each do |header|
|
|
306
|
+
if env.include?(header)
|
|
307
|
+
raise LintError, "env contains #{header}, must use #{header[5..-1]}"
|
|
308
|
+
end
|
|
309
|
+
end
|
|
310
|
+
|
|
311
|
+
##
|
|
312
|
+
## === Rack-Specific Variables
|
|
313
|
+
##
|
|
314
|
+
## In addition to CGI variables, the Rack environment includes Rack-specific variables. These variables are prefixed with <tt>rack.</tt> and are reserved for use by the Rack specification, or by the classes that ship with Rack.
|
|
315
|
+
##
|
|
316
|
+
## ==== <tt>rack.url_scheme</tt>
|
|
317
|
+
##
|
|
318
|
+
## The URL scheme, which must be one of <tt>http</tt>, <tt>https</tt>, <tt>ws</tt> or <tt>wss</tt>. This can never be an empty string, and so is always required. The scheme should be set according to the last hop. For example, if a client makes a request to a reverse proxy over HTTPS, but the connection between the reverse proxy and the server is over plain HTTP, the reverse proxy should set <tt>rack.url_scheme</tt> to <tt>http</tt>.
|
|
319
|
+
rack_url_scheme = assert_required(RACK_URL_SCHEME)
|
|
320
|
+
unless ALLOWED_SCHEMES.include?(rack_url_scheme)
|
|
321
|
+
raise LintError, "rack.url_scheme unknown: #{rack_url_scheme.inspect}"
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
##
|
|
325
|
+
## ==== <tt>rack.protocol</tt>
|
|
326
|
+
##
|
|
327
|
+
## An optional +Array+ of +String+ values, containing the protocols advertised by the client in the <tt>upgrade</tt> header (HTTP/1) or the <tt>:protocol</tt> pseudo-header (HTTP/2+).
|
|
328
|
+
if protocols = env[RACK_PROTOCOL]
|
|
329
|
+
unless protocols.is_a?(Array) && protocols.all?{|protocol| protocol.is_a?(String)}
|
|
330
|
+
raise LintError, "rack.protocol must be an Array of Strings"
|
|
331
|
+
end
|
|
332
|
+
end
|
|
333
|
+
|
|
334
|
+
##
|
|
335
|
+
## ==== <tt>rack.session</tt>
|
|
336
|
+
##
|
|
337
|
+
## An optional +Hash+-like interface for storing request session data. The store must implement:
|
|
338
|
+
if session = env[RACK_SESSION]
|
|
339
|
+
## * <tt>store(key, value)</tt> (aliased as <tt>[]=</tt>) to set a value for a key,
|
|
340
|
+
unless session.respond_to?(:store) && session.respond_to?(:[]=)
|
|
341
|
+
raise LintError, "session #{session.inspect} must respond to store and []="
|
|
342
|
+
end
|
|
343
|
+
|
|
344
|
+
## * <tt>fetch(key, default = nil)</tt> (aliased as <tt>[]</tt>) to retrieve a value for a key,
|
|
345
|
+
unless session.respond_to?(:fetch) && session.respond_to?(:[])
|
|
346
|
+
raise LintError, "session #{session.inspect} must respond to fetch and []"
|
|
347
|
+
end
|
|
348
|
+
|
|
349
|
+
## * <tt>delete(key)</tt> to delete a key,
|
|
350
|
+
unless session.respond_to?(:delete)
|
|
351
|
+
raise LintError, "session #{session.inspect} must respond to delete"
|
|
352
|
+
end
|
|
353
|
+
|
|
354
|
+
## * <tt>clear</tt> to clear the session,
|
|
355
|
+
unless session.respond_to?(:clear)
|
|
356
|
+
raise LintError, "session #{session.inspect} must respond to clear"
|
|
357
|
+
end
|
|
358
|
+
|
|
359
|
+
## * <tt>to_hash</tt> (optional) to retrieve the session as a Hash.
|
|
360
|
+
unless session.respond_to?(:to_hash) && session.to_hash.kind_of?(Hash) && !session.to_hash.frozen?
|
|
361
|
+
raise LintError, "session #{session.inspect} must respond to to_hash and return unfrozen Hash instance"
|
|
362
|
+
end
|
|
363
|
+
end
|
|
364
|
+
|
|
365
|
+
##
|
|
366
|
+
## ==== <tt>rack.logger</tt>
|
|
367
|
+
##
|
|
368
|
+
## An optional +Logger+-like interface for logging messages. The logger must implement:
|
|
369
|
+
if logger = env[RACK_LOGGER]
|
|
370
|
+
## * <tt>info(message, &block)</tt>,
|
|
371
|
+
unless logger.respond_to?(:info)
|
|
372
|
+
raise LintError, "logger #{logger.inspect} must respond to info"
|
|
373
|
+
end
|
|
374
|
+
|
|
375
|
+
## * <tt>debug(message, &block)</tt>,
|
|
376
|
+
unless logger.respond_to?(:debug)
|
|
377
|
+
raise LintError, "logger #{logger.inspect} must respond to debug"
|
|
378
|
+
end
|
|
379
|
+
|
|
380
|
+
## * <tt>warn(message, &block)</tt>,
|
|
381
|
+
unless logger.respond_to?(:warn)
|
|
382
|
+
raise LintError, "logger #{logger.inspect} must respond to warn"
|
|
383
|
+
end
|
|
384
|
+
|
|
385
|
+
## * <tt>error(message, &block)</tt>,
|
|
386
|
+
unless logger.respond_to?(:error)
|
|
387
|
+
raise LintError, "logger #{logger.inspect} must respond to error"
|
|
388
|
+
end
|
|
389
|
+
|
|
390
|
+
## * <tt>fatal(message, &block)</tt>.
|
|
391
|
+
unless logger.respond_to?(:fatal)
|
|
392
|
+
raise LintError, "logger #{logger.inspect} must respond to fatal"
|
|
393
|
+
end
|
|
394
|
+
end
|
|
395
|
+
|
|
396
|
+
##
|
|
397
|
+
## ==== <tt>rack.multipart.buffer_size</tt>
|
|
398
|
+
##
|
|
399
|
+
## An optional +Integer+ hint to the multipart parser as to what chunk size to use for reads and writes.
|
|
400
|
+
if rack_multipart_buffer_size = env[RACK_MULTIPART_BUFFER_SIZE]
|
|
401
|
+
unless rack_multipart_buffer_size.is_a?(Integer) && rack_multipart_buffer_size > 0
|
|
402
|
+
raise LintError, "rack.multipart.buffer_size must be an Integer > 0 if specified"
|
|
403
|
+
end
|
|
404
|
+
end
|
|
405
|
+
|
|
406
|
+
##
|
|
407
|
+
## ==== <tt>rack.multipart.tempfile_factory</tt>
|
|
408
|
+
##
|
|
409
|
+
## An optional object for constructing temporary files for multipart form data. The factory must implement:
|
|
410
|
+
if rack_multipart_tempfile_factory = env[RACK_MULTIPART_TEMPFILE_FACTORY]
|
|
411
|
+
## * <tt>call(filename, content_type)</tt> to create a temporary file for a multipart form field.
|
|
412
|
+
unless rack_multipart_tempfile_factory.respond_to?(:call)
|
|
413
|
+
raise LintError, "rack.multipart.tempfile_factory must respond to #call"
|
|
414
|
+
end
|
|
415
|
+
|
|
416
|
+
## The factory must return an +IO+-like object that responds to <tt><<</tt> and optionally <tt>rewind</tt>.
|
|
417
|
+
env[RACK_MULTIPART_TEMPFILE_FACTORY] = lambda do |filename, content_type|
|
|
418
|
+
io = rack_multipart_tempfile_factory.call(filename, content_type)
|
|
419
|
+
unless io.respond_to?(:<<)
|
|
420
|
+
raise LintError, "rack.multipart.tempfile_factory return value must respond to #<<"
|
|
421
|
+
end
|
|
422
|
+
io
|
|
423
|
+
end
|
|
424
|
+
end
|
|
425
|
+
|
|
426
|
+
##
|
|
427
|
+
## ==== <tt>rack.hijack?</tt>
|
|
428
|
+
##
|
|
429
|
+
## If present and truthy, indicates that the server supports partial hijacking. See the section below on hijacking for more information.
|
|
430
|
+
#
|
|
431
|
+
# N.B. There is no specific validation here. If the user provides a partial hijack response, we will confirm this value is truthy in `check_hijack_response`.
|
|
432
|
+
|
|
433
|
+
##
|
|
434
|
+
## ==== <tt>rack.hijack</tt>
|
|
435
|
+
##
|
|
436
|
+
## If present, an object responding to +call+ that is used to perform a full hijack. See the section below on hijacking for more information.
|
|
437
|
+
check_hijack(env)
|
|
438
|
+
|
|
439
|
+
##
|
|
440
|
+
## ==== <tt>rack.early_hints</tt>
|
|
441
|
+
##
|
|
442
|
+
## If present, an object responding to +call+ that is used to send early hints. See the section below on early hints for more information.
|
|
443
|
+
check_early_hints env
|
|
444
|
+
|
|
445
|
+
##
|
|
446
|
+
## ==== <tt>rack.input</tt>
|
|
447
|
+
##
|
|
448
|
+
## If present, the input stream. See the section below on the input stream for more information.
|
|
449
|
+
if rack_input = env[RACK_INPUT]
|
|
450
|
+
check_input_stream(rack_input)
|
|
451
|
+
@env[RACK_INPUT] = InputWrapper.new(rack_input)
|
|
452
|
+
end
|
|
453
|
+
|
|
454
|
+
##
|
|
455
|
+
## ==== <tt>rack.errors</tt>
|
|
456
|
+
##
|
|
457
|
+
## The error stream. See the section below on the error stream for more information.
|
|
458
|
+
rack_errors = assert_required(RACK_ERRORS)
|
|
459
|
+
check_error_stream(rack_errors)
|
|
460
|
+
@env[RACK_ERRORS] = ErrorWrapper.new(rack_errors)
|
|
461
|
+
|
|
462
|
+
##
|
|
463
|
+
## ==== <tt>rack.response_finished</tt>
|
|
464
|
+
##
|
|
465
|
+
## If present, an array of callables that will be run by the server after the response has been processed. The callables are called with <tt>environment, status, headers, error</tt> arguments and should not raise any exceptions. The callables would typically be called after sending the response to the client, but it could also be called if an error occurs while generating the response or sending the response (in that case, the +error+ argument will be a kind of +Exception+). The callables will be called in reverse order.
|
|
466
|
+
if rack_response_finished = env[RACK_RESPONSE_FINISHED]
|
|
467
|
+
raise LintError, "rack.response_finished must be an array of callable objects" unless rack_response_finished.is_a?(Array)
|
|
468
|
+
rack_response_finished.each do |callable|
|
|
469
|
+
raise LintError, "rack.response_finished values must respond to call(env, status, headers, error)" unless callable.respond_to?(:call)
|
|
470
|
+
end
|
|
266
471
|
end
|
|
267
472
|
end
|
|
268
473
|
|
|
269
|
-
## The server or the application can store their own data in the
|
|
270
|
-
## environment, too. The keys must contain at least one dot,
|
|
271
|
-
## and should be prefixed uniquely. The prefix <tt>rack.</tt>
|
|
272
|
-
## is reserved for use with the Rack core distribution and other
|
|
273
|
-
## accepted specifications and must not be used otherwise.
|
|
274
474
|
##
|
|
475
|
+
## === The Input Stream
|
|
476
|
+
##
|
|
477
|
+
## The input stream is an +IO+-like object which contains the raw HTTP request data. \
|
|
478
|
+
def check_input_stream(input)
|
|
479
|
+
## When applicable, its external encoding must be <tt>ASCII-8BIT</tt> and it must be opened in binary mode. \
|
|
480
|
+
if input.respond_to?(:external_encoding) && input.external_encoding != Encoding::ASCII_8BIT
|
|
481
|
+
raise LintError, "rack.input #{input} does not have ASCII-8BIT as its external encoding"
|
|
482
|
+
end
|
|
483
|
+
if input.respond_to?(:binmode?) && !input.binmode?
|
|
484
|
+
raise LintError, "rack.input #{input} is not opened in binary mode"
|
|
485
|
+
end
|
|
275
486
|
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
## The <tt>SERVER_PORT</tt> must be an Integer if set.
|
|
283
|
-
assert("env[SERVER_PORT] is not an Integer") do
|
|
284
|
-
server_port = env["SERVER_PORT"]
|
|
285
|
-
server_port.nil? || (Integer(server_port) rescue false)
|
|
487
|
+
## The input stream must respond to +gets+, +each+, and +read+:
|
|
488
|
+
[:gets, :each, :read].each do |method|
|
|
489
|
+
unless input.respond_to? method
|
|
490
|
+
raise LintError, "rack.input #{input} does not respond to ##{method}"
|
|
491
|
+
end
|
|
492
|
+
end
|
|
286
493
|
end
|
|
287
494
|
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
495
|
+
class InputWrapper
|
|
496
|
+
def initialize(input)
|
|
497
|
+
@input = input
|
|
498
|
+
end
|
|
499
|
+
|
|
500
|
+
## * +gets+ must be called without arguments and return a +String+, or +nil+ on EOF (end-of-file).
|
|
501
|
+
def gets(*args)
|
|
502
|
+
raise LintError, "rack.input#gets called with arguments" unless args.size == 0
|
|
503
|
+
|
|
504
|
+
chunk = @input.gets
|
|
505
|
+
|
|
506
|
+
unless chunk.nil? or chunk.kind_of? String
|
|
507
|
+
raise LintError, "rack.input#gets didn't return a String"
|
|
508
|
+
end
|
|
509
|
+
|
|
510
|
+
chunk
|
|
511
|
+
end
|
|
512
|
+
|
|
513
|
+
## * +read+ behaves like <tt>IO#read</tt>. Its signature is <tt>read([length, [buffer]])</tt>.
|
|
514
|
+
## * If given, +length+ must be a non-negative Integer (>= 0) or +nil+, and +buffer+ must be a +String+ and may not be +nil+.
|
|
515
|
+
## * If +length+ is given and not +nil+, then this method reads at most +length+ bytes from the input stream.
|
|
516
|
+
## * If +length+ is not given or +nil+, then this method reads all data until EOF.
|
|
517
|
+
## * When EOF is reached, this method returns +nil+ if +length+ is given and not +nil+, or +""+ if +length+ is not given or is +nil+.
|
|
518
|
+
## * If +buffer+ is given, then the read data will be placed into +buffer+ instead of a newly created +String+.
|
|
519
|
+
def read(*args)
|
|
520
|
+
unless args.size <= 2
|
|
521
|
+
raise LintError, "rack.input#read called with too many arguments"
|
|
522
|
+
end
|
|
523
|
+
if args.size >= 1
|
|
524
|
+
unless args.first.kind_of?(Integer) || args.first.nil?
|
|
525
|
+
raise LintError, "rack.input#read called with non-integer and non-nil length"
|
|
526
|
+
end
|
|
527
|
+
unless args.first.nil? || args.first >= 0
|
|
528
|
+
raise LintError, "rack.input#read called with a negative length"
|
|
529
|
+
end
|
|
530
|
+
end
|
|
531
|
+
if args.size >= 2
|
|
532
|
+
unless args[1].kind_of?(String)
|
|
533
|
+
raise LintError, "rack.input#read called with non-String buffer"
|
|
534
|
+
end
|
|
535
|
+
end
|
|
536
|
+
|
|
537
|
+
chunk = @input.read(*args)
|
|
538
|
+
|
|
539
|
+
unless chunk.nil? or chunk.kind_of? String
|
|
540
|
+
raise LintError, "rack.input#read didn't return nil or a String"
|
|
541
|
+
end
|
|
542
|
+
if args[0].nil?
|
|
543
|
+
unless !chunk.nil?
|
|
544
|
+
raise LintError, "rack.input#read(nil) returned nil on EOF"
|
|
545
|
+
end
|
|
546
|
+
end
|
|
547
|
+
|
|
548
|
+
chunk
|
|
549
|
+
end
|
|
550
|
+
|
|
551
|
+
## * +each+ must be called without arguments and only yield +String+ values.
|
|
552
|
+
def each(*args)
|
|
553
|
+
raise LintError, "rack.input#each called with arguments" unless args.size == 0
|
|
554
|
+
@input.each do |line|
|
|
555
|
+
unless line.kind_of? String
|
|
556
|
+
raise LintError, "rack.input#each didn't yield a String"
|
|
557
|
+
end
|
|
558
|
+
yield line
|
|
559
|
+
end
|
|
560
|
+
end
|
|
561
|
+
|
|
562
|
+
## * +close+ can be called on the input stream to indicate that any remaining input is not needed.
|
|
563
|
+
def close(*args)
|
|
564
|
+
@input.close(*args)
|
|
565
|
+
end
|
|
291
566
|
end
|
|
292
567
|
|
|
293
|
-
##
|
|
294
|
-
|
|
295
|
-
|
|
568
|
+
##
|
|
569
|
+
## === The Error Stream
|
|
570
|
+
##
|
|
571
|
+
def check_error_stream(error)
|
|
572
|
+
## The error stream must respond to +puts+, +write+ and +flush+:
|
|
573
|
+
[:puts, :write, :flush].each do |method|
|
|
574
|
+
unless error.respond_to? method
|
|
575
|
+
raise LintError, "rack.error #{error} does not respond to ##{method}"
|
|
576
|
+
end
|
|
577
|
+
end
|
|
296
578
|
end
|
|
297
579
|
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
assert("env contains #{header}, must use #{header[5, -1]}") {
|
|
303
|
-
not env.include? header
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
## The CGI keys (named without a period) must have String values.
|
|
308
|
-
## If the string values for CGI keys contain non-ASCII characters,
|
|
309
|
-
## they should use ASCII-8BIT encoding.
|
|
310
|
-
env.each { |key, value|
|
|
311
|
-
next if key.include? "." # Skip extensions
|
|
312
|
-
assert("env variable #{key} has non-string value #{value.inspect}") {
|
|
313
|
-
value.kind_of? String
|
|
314
|
-
}
|
|
315
|
-
next if value.encoding == Encoding::ASCII_8BIT
|
|
316
|
-
assert("env variable #{key} has value containing non-ASCII characters and has non-ASCII-8BIT encoding #{value.inspect} encoding: #{value.encoding}") {
|
|
317
|
-
value.b !~ /[\x80-\xff]/n
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
## There are the following restrictions:
|
|
322
|
-
|
|
323
|
-
## * <tt>rack.version</tt> must be an array of Integers.
|
|
324
|
-
assert("rack.version must be an Array, was #{env[RACK_VERSION].class}") {
|
|
325
|
-
env[RACK_VERSION].kind_of? Array
|
|
326
|
-
}
|
|
327
|
-
## * <tt>rack.url_scheme</tt> must either be +http+ or +https+.
|
|
328
|
-
assert("rack.url_scheme unknown: #{env[RACK_URL_SCHEME].inspect}") {
|
|
329
|
-
%w[http https].include?(env[RACK_URL_SCHEME])
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
## * There must be a valid input stream in <tt>rack.input</tt>.
|
|
333
|
-
check_input env[RACK_INPUT]
|
|
334
|
-
## * There must be a valid error stream in <tt>rack.errors</tt>.
|
|
335
|
-
check_error env[RACK_ERRORS]
|
|
336
|
-
## * There may be a valid hijack stream in <tt>rack.hijack_io</tt>
|
|
337
|
-
check_hijack env
|
|
338
|
-
|
|
339
|
-
## * The <tt>REQUEST_METHOD</tt> must be a valid token.
|
|
340
|
-
assert("REQUEST_METHOD unknown: #{env[REQUEST_METHOD]}") {
|
|
341
|
-
env[REQUEST_METHOD] =~ /\A[0-9A-Za-z!\#$%&'*+.^_`|~-]+\z/
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
## * The <tt>SCRIPT_NAME</tt>, if non-empty, must start with <tt>/</tt>
|
|
345
|
-
assert("SCRIPT_NAME must start with /") {
|
|
346
|
-
!env.include?(SCRIPT_NAME) ||
|
|
347
|
-
env[SCRIPT_NAME] == "" ||
|
|
348
|
-
env[SCRIPT_NAME] =~ /\A\//
|
|
349
|
-
}
|
|
350
|
-
## * The <tt>PATH_INFO</tt>, if non-empty, must start with <tt>/</tt>
|
|
351
|
-
assert("PATH_INFO must start with /") {
|
|
352
|
-
!env.include?(PATH_INFO) ||
|
|
353
|
-
env[PATH_INFO] == "" ||
|
|
354
|
-
env[PATH_INFO] =~ /\A\//
|
|
355
|
-
}
|
|
356
|
-
## * The <tt>CONTENT_LENGTH</tt>, if given, must consist of digits only.
|
|
357
|
-
assert("Invalid CONTENT_LENGTH: #{env["CONTENT_LENGTH"]}") {
|
|
358
|
-
!env.include?("CONTENT_LENGTH") || env["CONTENT_LENGTH"] =~ /\A\d+\z/
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
## * One of <tt>SCRIPT_NAME</tt> or <tt>PATH_INFO</tt> must be
|
|
362
|
-
## set. <tt>PATH_INFO</tt> should be <tt>/</tt> if
|
|
363
|
-
## <tt>SCRIPT_NAME</tt> is empty.
|
|
364
|
-
assert("One of SCRIPT_NAME or PATH_INFO must be set (make PATH_INFO '/' if SCRIPT_NAME is empty)") {
|
|
365
|
-
env[SCRIPT_NAME] || env[PATH_INFO]
|
|
366
|
-
}
|
|
367
|
-
## <tt>SCRIPT_NAME</tt> never should be <tt>/</tt>, but instead be empty.
|
|
368
|
-
assert("SCRIPT_NAME cannot be '/', make it '' and PATH_INFO '/'") {
|
|
369
|
-
env[SCRIPT_NAME] != "/"
|
|
370
|
-
}
|
|
371
|
-
end
|
|
580
|
+
class ErrorWrapper
|
|
581
|
+
def initialize(error)
|
|
582
|
+
@error = error
|
|
583
|
+
end
|
|
372
584
|
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
def check_input(input)
|
|
378
|
-
## When applicable, its external encoding must be "ASCII-8BIT" and it
|
|
379
|
-
## must be opened in binary mode, for Ruby 1.9 compatibility.
|
|
380
|
-
assert("rack.input #{input} does not have ASCII-8BIT as its external encoding") {
|
|
381
|
-
input.external_encoding == Encoding::ASCII_8BIT
|
|
382
|
-
} if input.respond_to?(:external_encoding)
|
|
383
|
-
assert("rack.input #{input} is not opened in binary mode") {
|
|
384
|
-
input.binmode?
|
|
385
|
-
} if input.respond_to?(:binmode?)
|
|
386
|
-
|
|
387
|
-
## The input stream must respond to +gets+, +each+, +read+ and +rewind+.
|
|
388
|
-
[:gets, :each, :read, :rewind].each { |method|
|
|
389
|
-
assert("rack.input #{input} does not respond to ##{method}") {
|
|
390
|
-
input.respond_to? method
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
end
|
|
585
|
+
## * +puts+ must be called with a single argument that responds to +to_s+.
|
|
586
|
+
def puts(str)
|
|
587
|
+
@error.puts str
|
|
588
|
+
end
|
|
394
589
|
|
|
395
|
-
|
|
396
|
-
|
|
590
|
+
## * +write+ must be called with a single argument that is a +String+.
|
|
591
|
+
def write(str)
|
|
592
|
+
raise LintError, "rack.errors#write not called with a String" unless str.kind_of? String
|
|
593
|
+
@error.write str
|
|
594
|
+
end
|
|
397
595
|
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
596
|
+
## * +flush+ must be called without arguments and must be called in order to make the error appear for sure.
|
|
597
|
+
def flush
|
|
598
|
+
@error.flush
|
|
599
|
+
end
|
|
401
600
|
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
v = @input.gets
|
|
407
|
-
assert("rack.input#gets didn't return a String") {
|
|
408
|
-
v.nil? or v.kind_of? String
|
|
409
|
-
}
|
|
410
|
-
v
|
|
601
|
+
## * +close+ must never be called on the error stream.
|
|
602
|
+
def close(*args)
|
|
603
|
+
raise LintError, "rack.errors#close must not be called"
|
|
604
|
+
end
|
|
411
605
|
end
|
|
412
606
|
|
|
413
|
-
## * +read+ behaves like IO#read.
|
|
414
|
-
## Its signature is <tt>read([length, [buffer]])</tt>.
|
|
415
607
|
##
|
|
416
|
-
##
|
|
417
|
-
## and +buffer+ must be a String and may not be nil.
|
|
608
|
+
## === Hijacking
|
|
418
609
|
##
|
|
419
|
-
##
|
|
420
|
-
## +length+ bytes from the input stream.
|
|
610
|
+
## The hijacking interfaces provides a means for an application to take control of the HTTP connection. There are two distinct hijack interfaces: full hijacking where the application takes over the raw connection, and partial hijacking where the application takes over just the response body stream. In both cases, the application is responsible for closing the hijacked stream.
|
|
421
611
|
##
|
|
422
|
-
##
|
|
423
|
-
## all data until EOF.
|
|
612
|
+
## Full hijacking only works with HTTP/1. Partial hijacking is functionally equivalent to streaming bodies, and is still optionally supported for backwards compatibility with older Rack versions.
|
|
424
613
|
##
|
|
425
|
-
##
|
|
426
|
-
## and not nil, or "" if +length+ is not given or is nil.
|
|
614
|
+
## ==== Full Hijack
|
|
427
615
|
##
|
|
428
|
-
##
|
|
429
|
-
##
|
|
430
|
-
def
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
if args.size >= 1
|
|
435
|
-
assert("rack.input#read called with non-integer and non-nil length") {
|
|
436
|
-
args.first.kind_of?(Integer) || args.first.nil?
|
|
437
|
-
}
|
|
438
|
-
assert("rack.input#read called with a negative length") {
|
|
439
|
-
args.first.nil? || args.first >= 0
|
|
440
|
-
}
|
|
441
|
-
end
|
|
442
|
-
if args.size >= 2
|
|
443
|
-
assert("rack.input#read called with non-String buffer") {
|
|
444
|
-
args[1].kind_of?(String)
|
|
445
|
-
}
|
|
446
|
-
end
|
|
616
|
+
## Full hijack is used to completely take over an HTTP/1 connection. It occurs before any headers are written and causes the server to ignore any response generated by the application. It is intended to be used when applications need access to the raw HTTP/1 connection.
|
|
617
|
+
##
|
|
618
|
+
def check_hijack(env)
|
|
619
|
+
## If <tt>rack.hijack</tt> is present in +env+, it must respond to +call+ \
|
|
620
|
+
if original_hijack = env[RACK_HIJACK]
|
|
621
|
+
raise LintError, "rack.hijack must respond to call" unless original_hijack.respond_to?(:call)
|
|
447
622
|
|
|
448
|
-
|
|
623
|
+
env[RACK_HIJACK] = proc do
|
|
624
|
+
io = original_hijack.call
|
|
449
625
|
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
}
|
|
453
|
-
if args[0].nil?
|
|
454
|
-
assert("rack.input#read(nil) returned nil on EOF") {
|
|
455
|
-
!v.nil?
|
|
456
|
-
}
|
|
457
|
-
end
|
|
626
|
+
## and return an +IO+ object which can be used to read and write to the underlying connection using HTTP/1 semantics and formatting.
|
|
627
|
+
raise LintError, "rack.hijack must return an IO instance" unless io.is_a?(IO)
|
|
458
628
|
|
|
459
|
-
|
|
629
|
+
io
|
|
630
|
+
end
|
|
631
|
+
end
|
|
460
632
|
end
|
|
461
633
|
|
|
462
|
-
##
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
634
|
+
##
|
|
635
|
+
## ==== Partial Hijack
|
|
636
|
+
##
|
|
637
|
+
## Partial hijack is used for bi-directional streaming of the request and response body. It occurs after the status and headers are written by the server and causes the server to ignore the Body of the response. It is intended to be used when applications need bi-directional streaming.
|
|
638
|
+
##
|
|
639
|
+
def check_hijack_response(headers, env)
|
|
640
|
+
## If <tt>rack.hijack?</tt> is present in +env+ and truthy, \
|
|
641
|
+
if env[RACK_IS_HIJACK]
|
|
642
|
+
## an application may set the special response header <tt>rack.hijack</tt> \
|
|
643
|
+
if original_hijack = headers[RACK_HIJACK]
|
|
644
|
+
## to an object that responds to +call+, \
|
|
645
|
+
unless original_hijack.respond_to?(:call)
|
|
646
|
+
raise LintError, 'rack.hijack header must respond to #call'
|
|
647
|
+
end
|
|
648
|
+
## accepting a +stream+ argument.
|
|
649
|
+
return proc do |io|
|
|
650
|
+
original_hijack.call StreamWrapper.new(io)
|
|
651
|
+
end
|
|
652
|
+
end
|
|
653
|
+
##
|
|
654
|
+
## After the response status and headers have been sent, this hijack callback will be called with a +stream+ argument which follows the same interface as outlined in "Streaming Body". Servers must ignore the +body+ part of the response tuple when the <tt>rack.hijack</tt> response header is present. Using an empty +Array+ is recommended.
|
|
655
|
+
else
|
|
656
|
+
##
|
|
657
|
+
## If <tt>rack.hijack?</tt> is not present and truthy, the special response header <tt>rack.hijack</tt> must not be present in the response headers.
|
|
658
|
+
if headers.key?(RACK_HIJACK)
|
|
659
|
+
raise LintError, 'rack.hijack header must not be present if server does not support hijacking'
|
|
660
|
+
end
|
|
661
|
+
end
|
|
662
|
+
|
|
663
|
+
nil
|
|
471
664
|
end
|
|
472
665
|
|
|
473
|
-
##
|
|
474
|
-
##
|
|
475
|
-
##
|
|
476
|
-
##
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
666
|
+
##
|
|
667
|
+
## === Early Hints
|
|
668
|
+
##
|
|
669
|
+
## The application or any middleware may call the <tt>rack.early_hints</tt> with an object which would be valid as the headers of a Rack response.
|
|
670
|
+
def check_early_hints(env)
|
|
671
|
+
if env[RACK_EARLY_HINTS]
|
|
672
|
+
##
|
|
673
|
+
## If <tt>rack.early_hints</tt> is present, it must respond to +call+.
|
|
674
|
+
unless env[RACK_EARLY_HINTS].respond_to?(:call)
|
|
675
|
+
raise LintError, "rack.early_hints must respond to call"
|
|
676
|
+
end
|
|
677
|
+
|
|
678
|
+
original_callback = env[RACK_EARLY_HINTS]
|
|
679
|
+
env[RACK_EARLY_HINTS] = lambda do |headers|
|
|
680
|
+
## If <tt>rack.early_hints</tt> is called, it must be called with valid Rack response headers.
|
|
681
|
+
check_headers(headers)
|
|
682
|
+
original_callback.call(headers)
|
|
683
|
+
end
|
|
684
|
+
end
|
|
488
685
|
end
|
|
489
686
|
|
|
490
|
-
##
|
|
491
|
-
|
|
492
|
-
|
|
687
|
+
##
|
|
688
|
+
## == The Response
|
|
689
|
+
##
|
|
690
|
+
## Outgoing HTTP responses are generated from the response tuple generated by the application. The response tuple is an +Array+ of three elements, which are: the HTTP status, the headers, and the response body. The Rack application is responsible for ensuring that the response tuple is well-formed and should follow the rules set out in this specification.
|
|
691
|
+
##
|
|
692
|
+
## === The Status
|
|
693
|
+
##
|
|
694
|
+
def check_status(status)
|
|
695
|
+
## This is an HTTP status. It must be an Integer greater than or equal to 100.
|
|
696
|
+
unless status.is_a?(Integer) && status >= 100
|
|
697
|
+
raise LintError, "Status must be an Integer >=100"
|
|
698
|
+
end
|
|
493
699
|
end
|
|
494
|
-
end
|
|
495
700
|
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
##
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
701
|
+
##
|
|
702
|
+
## === The Headers
|
|
703
|
+
##
|
|
704
|
+
def check_headers(headers)
|
|
705
|
+
## The headers must be an unfrozen +Hash+. \
|
|
706
|
+
unless headers.kind_of?(Hash)
|
|
707
|
+
raise LintError, "headers object should be a hash, but isn't (got #{headers.class} as headers)"
|
|
708
|
+
end
|
|
709
|
+
|
|
710
|
+
if headers.frozen?
|
|
711
|
+
raise LintError, "headers object should not be frozen, but is"
|
|
712
|
+
end
|
|
713
|
+
|
|
714
|
+
headers.each do |key, value|
|
|
715
|
+
## The header keys must be +String+ values. \
|
|
716
|
+
unless key.kind_of? String
|
|
717
|
+
raise LintError, "header key must be a string, was #{key.class}"
|
|
718
|
+
end
|
|
505
719
|
|
|
506
|
-
|
|
507
|
-
|
|
720
|
+
## Special headers starting <tt>rack.</tt> are for communicating with the server, and must not be sent back to the client.
|
|
721
|
+
next if key.start_with?("rack.")
|
|
508
722
|
|
|
509
|
-
|
|
510
|
-
|
|
723
|
+
##
|
|
724
|
+
## * The headers must not contain a <tt>"status"</tt> key.
|
|
725
|
+
raise LintError, "headers must not contain status" if key == "status"
|
|
726
|
+
## * Header keys must conform to {RFC7230}[https://tools.ietf.org/html/rfc7230] token specification, i.e. cannot contain non-printable ASCII, <tt>DQUOTE</tt> or <tt>(),/:;<=>?@[\]{}</tt>.
|
|
727
|
+
raise LintError, "invalid header name: #{key}" if key =~ /[\(\),\/:;<=>\?@\[\\\]{}[:cntrl:]]/
|
|
728
|
+
## * Header keys must not contain uppercase ASCII characters (A-Z).
|
|
729
|
+
raise LintError, "uppercase character in header name: #{key}" if key =~ /[A-Z]/
|
|
730
|
+
|
|
731
|
+
## * Header values must be either a +String+, \
|
|
732
|
+
if value.kind_of?(String)
|
|
733
|
+
check_header_value(key, value)
|
|
734
|
+
elsif value.kind_of?(Array)
|
|
735
|
+
## or an +Array+ of +String+ values, \
|
|
736
|
+
value.each{|value| check_header_value(key, value)}
|
|
737
|
+
else
|
|
738
|
+
raise LintError, "a header value must be a String or Array of Strings, but the value of '#{key}' is a #{value.class}"
|
|
739
|
+
end
|
|
740
|
+
end
|
|
511
741
|
end
|
|
512
742
|
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
743
|
+
def check_header_value(key, value)
|
|
744
|
+
## such that each +String+ must not contain <tt>NUL</tt> (<tt>\0</tt>), <tt>CR</tt> (<tt>\r</tt>), or <tt>LF</tt> (<tt>\n</tt>).
|
|
745
|
+
if value.match?(/[\x00\x0A\x0D]/)
|
|
746
|
+
raise LintError, "invalid header value #{key}: #{value.inspect}"
|
|
747
|
+
end
|
|
516
748
|
end
|
|
517
749
|
|
|
518
|
-
##
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
750
|
+
##
|
|
751
|
+
## ==== The <tt>content-type</tt> Header
|
|
752
|
+
##
|
|
753
|
+
def check_content_type_header(status, headers)
|
|
754
|
+
headers.each do |key, value|
|
|
755
|
+
## There must not be a <tt>content-type</tt> header key when the status is <tt>1xx</tt>, <tt>204</tt>, or <tt>304</tt>.
|
|
756
|
+
if key == "content-type"
|
|
757
|
+
if Rack::Utils::STATUS_WITH_NO_ENTITY_BODY.key? status.to_i
|
|
758
|
+
raise LintError, "content-type header found in #{status} response, not allowed"
|
|
759
|
+
end
|
|
760
|
+
return
|
|
761
|
+
end
|
|
762
|
+
end
|
|
522
763
|
end
|
|
523
764
|
|
|
524
|
-
##
|
|
525
|
-
##
|
|
526
|
-
|
|
527
|
-
|
|
765
|
+
##
|
|
766
|
+
## ==== The <tt>content-length</tt> Header
|
|
767
|
+
##
|
|
768
|
+
def check_content_length_header(status, headers)
|
|
769
|
+
headers.each do |key, value|
|
|
770
|
+
if key == 'content-length'
|
|
771
|
+
## There must not be a <tt>content-length</tt> header key when the status is <tt>1xx</tt>, <tt>204</tt>, or <tt>304</tt>.
|
|
772
|
+
if Rack::Utils::STATUS_WITH_NO_ENTITY_BODY.key? status.to_i
|
|
773
|
+
raise LintError, "content-length header found in #{status} response, not allowed"
|
|
774
|
+
end
|
|
775
|
+
@content_length = value
|
|
776
|
+
end
|
|
777
|
+
end
|
|
528
778
|
end
|
|
529
779
|
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
780
|
+
def verify_content_length(size)
|
|
781
|
+
if @head_request
|
|
782
|
+
unless size == 0
|
|
783
|
+
raise LintError, "Response body was given for HEAD request, but should be empty"
|
|
784
|
+
end
|
|
785
|
+
elsif @content_length
|
|
786
|
+
unless @content_length == size.to_s
|
|
787
|
+
raise LintError, "content-length header was #{@content_length}, but should be #{size}"
|
|
788
|
+
end
|
|
789
|
+
end
|
|
533
790
|
end
|
|
534
|
-
end
|
|
535
791
|
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
792
|
+
##
|
|
793
|
+
## ==== The <tt>rack.protocol</tt> Header
|
|
794
|
+
##
|
|
795
|
+
def check_rack_protocol_header(status, headers)
|
|
796
|
+
## If the <tt>rack.protocol</tt> header is present, it must be a +String+, and must be one of the values from the <tt>rack.protocol</tt> array from the environment.
|
|
797
|
+
protocol = headers['rack.protocol']
|
|
539
798
|
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
799
|
+
if protocol
|
|
800
|
+
request_protocols = @env['rack.protocol']
|
|
801
|
+
|
|
802
|
+
if request_protocols.nil?
|
|
803
|
+
raise LintError, "rack.protocol header is #{protocol.inspect}, but rack.protocol was not set in request!"
|
|
804
|
+
elsif !request_protocols.include?(protocol)
|
|
805
|
+
raise LintError, "rack.protocol header is #{protocol.inspect}, but should be one of #{request_protocols.inspect} from the request!"
|
|
806
|
+
end
|
|
807
|
+
end
|
|
808
|
+
end
|
|
809
|
+
##
|
|
810
|
+
## Setting this value informs the server that it should perform a connection upgrade. In HTTP/1, this is done using the +upgrade+ header. In HTTP/2+, this is done by accepting the request.
|
|
811
|
+
##
|
|
812
|
+
## === The Body
|
|
813
|
+
##
|
|
814
|
+
## The Body is typically an +Array+ of +String+ values, an enumerable that yields +String+ values, a +Proc+, or an +IO+-like object.
|
|
815
|
+
##
|
|
816
|
+
## The Body must respond to +each+ or +call+. It may optionally respond to +to_path+ or +to_ary+. A Body that responds to +each+ is considered to be an Enumerable Body. A Body that responds to +call+ is considered to be a Streaming Body.
|
|
817
|
+
##
|
|
818
|
+
## A Body that responds to both +each+ and +call+ must be treated as an Enumerable Body, not a Streaming Body. If it responds to +each+, you must call +each+ and not +call+. If the Body doesn't respond to +each+, then you can assume it responds to +call+.
|
|
819
|
+
##
|
|
820
|
+
## The Body must either be consumed or returned. The Body is consumed by optionally calling either +each+ or +call+. Then, if the Body responds to +close+, it must be called to release any resources associated with the generation of the body. In other words, +close+ must always be called at least once; typically after the web server has sent the response to the client, but also in cases where the Rack application makes internal/virtual requests and discards the response.
|
|
821
|
+
def close
|
|
822
|
+
##
|
|
823
|
+
## After calling +close+, the Body is considered closed and should not be consumed again. \
|
|
824
|
+
@closed = true
|
|
544
825
|
|
|
545
|
-
|
|
826
|
+
## If the original Body is replaced by a new Body, the new Body must also consume the original Body by calling +close+ if possible.
|
|
827
|
+
@body.close if @body.respond_to?(:close)
|
|
546
828
|
|
|
547
|
-
|
|
548
|
-
@
|
|
549
|
-
|
|
550
|
-
assert("rack.hijack_io must respond to #{meth}") { io.respond_to? meth }
|
|
829
|
+
index = @lint.index(self)
|
|
830
|
+
unless @env['rack.lint'][0..index].all? {|lint| lint.instance_variable_get(:@closed)}
|
|
831
|
+
raise LintError, "Body has not been closed"
|
|
551
832
|
end
|
|
552
833
|
end
|
|
553
|
-
end
|
|
554
834
|
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
env[RACK_HIJACK] = proc do
|
|
568
|
-
## rack.hijack must return the io that will also be assigned (or is
|
|
569
|
-
## already present, in rack.hijack_io.
|
|
570
|
-
io = original_hijack.call
|
|
571
|
-
HijackWrapper.new(io)
|
|
572
|
-
##
|
|
573
|
-
## rack.hijack_io must respond to:
|
|
574
|
-
## <tt>read, write, read_nonblock, write_nonblock, flush, close,
|
|
575
|
-
## close_read, close_write, closed?</tt>
|
|
576
|
-
##
|
|
577
|
-
## The semantics of these IO methods must be a best effort match to
|
|
578
|
-
## those of a normal ruby IO or Socket object, using standard
|
|
579
|
-
## arguments and raising standard exceptions. Servers are encouraged
|
|
580
|
-
## to simply pass on real IO objects, although it is recognized that
|
|
581
|
-
## this approach is not directly compatible with SPDY and HTTP 2.0.
|
|
582
|
-
##
|
|
583
|
-
## IO provided in rack.hijack_io should preference the
|
|
584
|
-
## IO::WaitReadable and IO::WaitWritable APIs wherever supported.
|
|
585
|
-
##
|
|
586
|
-
## There is a deliberate lack of full specification around
|
|
587
|
-
## rack.hijack_io, as semantics will change from server to server.
|
|
588
|
-
## Users are encouraged to utilize this API with a knowledge of their
|
|
589
|
-
## server choice, and servers may extend the functionality of
|
|
590
|
-
## hijack_io to provide additional features to users. The purpose of
|
|
591
|
-
## rack.hijack is for Rack to "get out of the way", as such, Rack only
|
|
592
|
-
## provides the minimum of specification and support.
|
|
593
|
-
env[RACK_HIJACK_IO] = HijackWrapper.new(env[RACK_HIJACK_IO])
|
|
594
|
-
io
|
|
595
|
-
end
|
|
596
|
-
else
|
|
597
|
-
##
|
|
598
|
-
## If rack.hijack? is false, then rack.hijack should not be set.
|
|
599
|
-
assert("rack.hijack? is false, but rack.hijack is present") { env[RACK_HIJACK].nil? }
|
|
600
|
-
##
|
|
601
|
-
## If rack.hijack? is false, then rack.hijack_io should not be set.
|
|
602
|
-
assert("rack.hijack? is false, but rack.hijack_io is present") { env[RACK_HIJACK_IO].nil? }
|
|
835
|
+
def verify_to_path
|
|
836
|
+
##
|
|
837
|
+
## If the Body responds to +to_path+, it must return either +nil+ or a +String+. If a +String+ is returned, it must be a path for the local file system whose contents are identical to that produced by calling +each+; this may be used by the server as an alternative, possibly more efficient way to transport the response. The +to_path+ method does not consume the body.
|
|
838
|
+
if @body.respond_to?(:to_path)
|
|
839
|
+
optional_path = @body.to_path
|
|
840
|
+
|
|
841
|
+
if optional_path != nil
|
|
842
|
+
unless optional_path.is_a?(String) && ::File.exist?(optional_path)
|
|
843
|
+
raise LintError, "body.to_path must be nil or a path to an existing file"
|
|
844
|
+
end
|
|
845
|
+
end
|
|
846
|
+
end
|
|
603
847
|
end
|
|
604
|
-
end
|
|
605
848
|
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
# headers respond to #each
|
|
613
|
-
headers = Rack::Utils::HeaderHash[headers]
|
|
614
|
-
|
|
615
|
-
## In order to do this, an application may set the special header
|
|
616
|
-
## <tt>rack.hijack</tt> to an object that responds to <tt>call</tt>
|
|
617
|
-
## accepting an argument that conforms to the <tt>rack.hijack_io</tt>
|
|
618
|
-
## protocol.
|
|
619
|
-
##
|
|
620
|
-
## After the headers have been sent, and this hijack callback has been
|
|
621
|
-
## called, the application is now responsible for the remaining lifecycle
|
|
622
|
-
## of the IO. The application is also responsible for maintaining HTTP
|
|
623
|
-
## semantics. Of specific note, in almost all cases in the current SPEC,
|
|
624
|
-
## applications will have wanted to specify the header Connection:close in
|
|
625
|
-
## HTTP/1.1, and not Connection:keep-alive, as there is no protocol for
|
|
626
|
-
## returning hijacked sockets to the web server. For that purpose, use the
|
|
627
|
-
## body streaming API instead (progressively yielding strings via each).
|
|
628
|
-
##
|
|
629
|
-
## Servers must ignore the <tt>body</tt> part of the response tuple when
|
|
630
|
-
## the <tt>rack.hijack</tt> response API is in use.
|
|
631
|
-
|
|
632
|
-
if env[RACK_IS_HIJACK] && headers[RACK_HIJACK]
|
|
633
|
-
assert('rack.hijack header must respond to #call') {
|
|
634
|
-
headers[RACK_HIJACK].respond_to? :call
|
|
635
|
-
}
|
|
636
|
-
original_hijack = headers[RACK_HIJACK]
|
|
637
|
-
proc do |io|
|
|
638
|
-
original_hijack.call HijackWrapper.new(io)
|
|
639
|
-
end
|
|
640
|
-
else
|
|
641
|
-
##
|
|
642
|
-
## The special response header <tt>rack.hijack</tt> must only be set
|
|
643
|
-
## if the request env has <tt>rack.hijack?</tt> <tt>true</tt>.
|
|
644
|
-
assert('rack.hijack header must not be present if server does not support hijacking') {
|
|
645
|
-
headers[RACK_HIJACK].nil?
|
|
646
|
-
}
|
|
849
|
+
##
|
|
850
|
+
## ==== Enumerable Body
|
|
851
|
+
##
|
|
852
|
+
def each
|
|
853
|
+
## The Enumerable Body must respond to +each+, \
|
|
854
|
+
raise LintError, "Enumerable Body must respond to each" unless @body.respond_to?(:each)
|
|
647
855
|
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
end
|
|
651
|
-
## ==== Conventions
|
|
652
|
-
## * Middleware should not use hijack unless it is handling the whole
|
|
653
|
-
## response.
|
|
654
|
-
## * Middleware may wrap the IO object for the response pattern.
|
|
655
|
-
## * Middleware should not wrap the IO object for the request pattern. The
|
|
656
|
-
## request pattern is intended to provide the hijacker with "raw tcp".
|
|
657
|
-
|
|
658
|
-
## == The Response
|
|
659
|
-
|
|
660
|
-
## === The Status
|
|
661
|
-
def check_status(status)
|
|
662
|
-
## This is an HTTP status. When parsed as integer (+to_i+), it must be
|
|
663
|
-
## greater than or equal to 100.
|
|
664
|
-
assert("Status must be >=100 seen as integer") { status.to_i >= 100 }
|
|
665
|
-
end
|
|
856
|
+
## which must only be called once, \
|
|
857
|
+
raise LintError, "Response body must only be called once (#{@consumed})" unless @consumed.nil?
|
|
666
858
|
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
## The header must respond to +each+, and yield values of key and value.
|
|
670
|
-
assert("headers object should respond to #each, but doesn't (got #{header.class} as headers)") {
|
|
671
|
-
header.respond_to? :each
|
|
672
|
-
}
|
|
673
|
-
|
|
674
|
-
header.each { |key, value|
|
|
675
|
-
## The header keys must be Strings.
|
|
676
|
-
assert("header key must be a string, was #{key.class}") {
|
|
677
|
-
key.kind_of? String
|
|
678
|
-
}
|
|
679
|
-
|
|
680
|
-
## Special headers starting "rack." are for communicating with the
|
|
681
|
-
## server, and must not be sent back to the client.
|
|
682
|
-
next if key =~ /^rack\..+$/
|
|
683
|
-
|
|
684
|
-
## The header must not contain a +Status+ key.
|
|
685
|
-
assert("header must not contain Status") { key.downcase != "status" }
|
|
686
|
-
## The header must conform to RFC7230 token specification, i.e. cannot
|
|
687
|
-
## contain non-printable ASCII, DQUOTE or "(),/:;<=>?@[\]{}".
|
|
688
|
-
assert("invalid header name: #{key}") { key !~ /[\(\),\/:;<=>\?@\[\\\]{}[:cntrl:]]/ }
|
|
689
|
-
|
|
690
|
-
## The values of the header must be Strings,
|
|
691
|
-
assert("a header value must be a String, but the value of " +
|
|
692
|
-
"'#{key}' is a #{value.class}") { value.kind_of? String }
|
|
693
|
-
## consisting of lines (for multiple header values, e.g. multiple
|
|
694
|
-
## <tt>Set-Cookie</tt> values) separated by "\\n".
|
|
695
|
-
value.split("\n").each { |item|
|
|
696
|
-
## The lines must not contain characters below 037.
|
|
697
|
-
assert("invalid header value #{key}: #{item.inspect}") {
|
|
698
|
-
item !~ /[\000-\037]/
|
|
699
|
-
}
|
|
700
|
-
}
|
|
701
|
-
}
|
|
702
|
-
end
|
|
859
|
+
## must not be called after being closed, \
|
|
860
|
+
raise LintError, "Response body is already closed" if @closed
|
|
703
861
|
|
|
704
|
-
|
|
705
|
-
def check_content_type(status, headers)
|
|
706
|
-
headers.each { |key, value|
|
|
707
|
-
## There must not be a <tt>Content-Type</tt>, when the +Status+ is 1xx,
|
|
708
|
-
## 204 or 304.
|
|
709
|
-
if key.downcase == "content-type"
|
|
710
|
-
assert("Content-Type header found in #{status} response, not allowed") {
|
|
711
|
-
not Rack::Utils::STATUS_WITH_NO_ENTITY_BODY.key? status.to_i
|
|
712
|
-
}
|
|
713
|
-
return
|
|
714
|
-
end
|
|
715
|
-
}
|
|
716
|
-
end
|
|
862
|
+
@consumed = :each
|
|
717
863
|
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
864
|
+
@body.each do |chunk|
|
|
865
|
+
## and must only yield +String+ values.
|
|
866
|
+
unless chunk.kind_of? String
|
|
867
|
+
raise LintError, "Body yielded non-string value #{chunk.inspect}"
|
|
868
|
+
end
|
|
869
|
+
|
|
870
|
+
##
|
|
871
|
+
## Middleware must not call +each+ directly on the Body. Instead, middleware can return a new Body that calls +each+ on the original Body, yielding at least once per iteration.
|
|
872
|
+
if @lint[0] == self
|
|
873
|
+
@env['rack.lint.body_iteration'] += 1
|
|
874
|
+
else
|
|
875
|
+
if (@env['rack.lint.body_iteration'] -= 1) > 0
|
|
876
|
+
raise LintError, "New body must yield at least once per iteration of old body"
|
|
877
|
+
end
|
|
878
|
+
end
|
|
879
|
+
|
|
880
|
+
@size += chunk.bytesize
|
|
881
|
+
yield chunk
|
|
882
|
+
end
|
|
731
883
|
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
bytes == 0
|
|
736
|
-
}
|
|
737
|
-
elsif @content_length
|
|
738
|
-
assert("Content-Length header was #{@content_length}, but should be #{bytes}") {
|
|
739
|
-
@content_length == bytes.to_s
|
|
740
|
-
}
|
|
884
|
+
verify_content_length(@size)
|
|
885
|
+
|
|
886
|
+
verify_to_path
|
|
741
887
|
end
|
|
742
|
-
end
|
|
743
888
|
|
|
744
|
-
|
|
745
|
-
def each
|
|
746
|
-
@closed = false
|
|
747
|
-
bytes = 0
|
|
889
|
+
BODY_METHODS = {to_ary: true, each: true, call: true, to_path: true}
|
|
748
890
|
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
891
|
+
def to_path
|
|
892
|
+
@body.to_path
|
|
893
|
+
end
|
|
894
|
+
|
|
895
|
+
def respond_to?(name, *)
|
|
896
|
+
if BODY_METHODS.key?(name)
|
|
897
|
+
@body.respond_to?(name)
|
|
898
|
+
else
|
|
899
|
+
super
|
|
900
|
+
end
|
|
752
901
|
end
|
|
753
902
|
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
903
|
+
##
|
|
904
|
+
## If the Body responds to +to_ary+, it must return an +Array+ whose contents are identical to that produced by calling +each+. Middleware may call +to_ary+ directly on the Body and return a new Body in its place. In other words, middleware can only process the Body directly if it responds to +to_ary+. If the Body responds to both +to_ary+ and +close+, its implementation of +to_ary+ must call +close+.
|
|
905
|
+
def to_ary
|
|
906
|
+
@body.to_ary.tap do |content|
|
|
907
|
+
unless content == @body.enum_for.to_a
|
|
908
|
+
raise LintError, "#to_ary not identical to contents produced by calling #each"
|
|
909
|
+
end
|
|
910
|
+
end
|
|
911
|
+
ensure
|
|
912
|
+
close
|
|
913
|
+
end
|
|
763
914
|
|
|
764
915
|
##
|
|
765
|
-
##
|
|
766
|
-
## break in Ruby 1.9.
|
|
916
|
+
## ==== Streaming Body
|
|
767
917
|
##
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
# XXX howto: assert("Body has not been closed") { @closed }
|
|
918
|
+
def call(stream)
|
|
919
|
+
## The Streaming Body must respond to +call+, \
|
|
920
|
+
raise LintError, "Streaming Body must respond to call" unless @body.respond_to?(:call)
|
|
772
921
|
|
|
922
|
+
## which must only be called once, \
|
|
923
|
+
raise LintError, "Response body must only be called once (#{@consumed})" unless @consumed.nil?
|
|
773
924
|
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
## identifying the location of a file whose contents are identical
|
|
777
|
-
## to that produced by calling +each+; this may be used by the
|
|
778
|
-
## server as an alternative, possibly more efficient way to
|
|
779
|
-
## transport the response.
|
|
925
|
+
## must not be called after being closed, \
|
|
926
|
+
raise LintError, "Response body is already closed" if @closed
|
|
780
927
|
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
928
|
+
@consumed = :call
|
|
929
|
+
|
|
930
|
+
## and accept a +stream+ argument.
|
|
931
|
+
##
|
|
932
|
+
## The +stream+ argument must respond to: +read+, +write+, <tt><<</tt>, +flush+, +close+, +close_read+, +close_write+, and +closed?+. \
|
|
933
|
+
@body.call(StreamWrapper.new(stream))
|
|
785
934
|
end
|
|
786
935
|
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
## instance itself, or a File-like object.
|
|
790
|
-
end
|
|
936
|
+
class StreamWrapper
|
|
937
|
+
extend Forwardable
|
|
791
938
|
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
939
|
+
## The semantics of these +IO+ methods must be a best effort match to those of a normal Ruby +IO+ or +Socket+ object, using standard arguments and raising standard exceptions. Servers may simply pass on real +IO+ objects to the Streaming Body. In some cases (e.g. when using <tt>transfer-encoding</tt> or HTTP/2+), the server may need to provide a wrapper that implements the required methods, in order to provide the correct semantics.
|
|
940
|
+
REQUIRED_METHODS = [
|
|
941
|
+
:read, :write, :<<, :flush, :close,
|
|
942
|
+
:close_read, :close_write, :closed?
|
|
943
|
+
]
|
|
796
944
|
|
|
797
|
-
|
|
945
|
+
def_delegators :@stream, *REQUIRED_METHODS
|
|
798
946
|
|
|
947
|
+
def initialize(stream)
|
|
948
|
+
@stream = stream
|
|
949
|
+
|
|
950
|
+
REQUIRED_METHODS.each do |method_name|
|
|
951
|
+
raise LintError, "Stream must respond to #{method_name}" unless stream.respond_to?(method_name)
|
|
952
|
+
end
|
|
953
|
+
end
|
|
954
|
+
end
|
|
955
|
+
end
|
|
799
956
|
end
|
|
800
957
|
end
|
|
801
958
|
|
|
959
|
+
##
|
|
802
960
|
## == Thanks
|
|
803
|
-
##
|
|
804
|
-
##
|
|
805
|
-
##
|
|
806
|
-
## everyone involved in that effort.
|
|
961
|
+
##
|
|
962
|
+
## We'd like to thank everyone who has contributed to the Rack project over the years. Your work has made this specification possible. That includes everyone who has contributed code, documentation, bug reports, and feedback. We'd also like to thank the authors of the various web servers, frameworks, and libraries that have implemented the Rack specification. Your work has helped to make the web a better place.
|
|
963
|
+
##
|
|
964
|
+
## Some parts of this specification are adapted from {PEP 333 – Python Web Server Gateway Interface v1.0}[https://peps.python.org/pep-0333/]. We'd like to thank everyone involved in that effort.
|