rack 2.2.22 → 3.2.6
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 +588 -71
- 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 -3
- 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 +22 -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 +10 -4
- data/lib/rack/etag.rb +17 -23
- 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 +817 -648
- data/lib/rack/lock.rb +2 -5
- data/lib/rack/media_type.rb +6 -7
- data/lib/rack/method_override.rb +5 -1
- data/lib/rack/mime.rb +14 -5
- data/lib/rack/mock.rb +1 -300
- 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 +282 -101
- data/lib/rack/multipart/uploaded_file.rb +45 -4
- data/lib/rack/multipart.rb +53 -40
- data/lib/rack/null_logger.rb +9 -0
- data/lib/rack/query_parser.rb +116 -121
- data/lib/rack/recursive.rb +2 -0
- data/lib/rack/reloader.rb +0 -2
- data/lib/rack/request.rb +272 -144
- 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 +37 -32
- data/lib/rack/show_exceptions.rb +25 -6
- data/lib/rack/show_status.rb +17 -9
- data/lib/rack/static.rb +15 -11
- data/lib/rack/tempfile_reaper.rb +15 -4
- data/lib/rack/urlmap.rb +3 -1
- data/lib/rack/utils.rb +326 -244
- data/lib/rack/version.rb +3 -15
- data/lib/rack.rb +13 -90
- metadata +15 -41
- data/README.rdoc +0 -355
- 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 -53
- 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 -34
- 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 -90
- data/rack.gemspec +0 -46
data/lib/rack/utils.rb
CHANGED
|
@@ -6,32 +6,33 @@ require 'fileutils'
|
|
|
6
6
|
require 'set'
|
|
7
7
|
require 'tempfile'
|
|
8
8
|
require 'time'
|
|
9
|
+
require 'erb'
|
|
9
10
|
|
|
10
11
|
require_relative 'query_parser'
|
|
12
|
+
require_relative 'mime'
|
|
13
|
+
require_relative 'headers'
|
|
14
|
+
require_relative 'constants'
|
|
11
15
|
|
|
12
16
|
module Rack
|
|
13
17
|
# Rack::Utils contains a grab-bag of useful methods for writing web
|
|
14
18
|
# applications adopted from all kinds of Ruby libraries.
|
|
15
19
|
|
|
16
20
|
module Utils
|
|
17
|
-
(require_relative 'core_ext/regexp'; using ::Rack::RegexpExtensions) if RUBY_VERSION < '2.4'
|
|
18
|
-
|
|
19
21
|
ParameterTypeError = QueryParser::ParameterTypeError
|
|
20
22
|
InvalidParameterError = QueryParser::InvalidParameterError
|
|
23
|
+
ParamsTooDeepError = QueryParser::ParamsTooDeepError
|
|
21
24
|
DEFAULT_SEP = QueryParser::DEFAULT_SEP
|
|
22
25
|
COMMON_SEP = QueryParser::COMMON_SEP
|
|
23
26
|
KeySpaceConstrainedParams = QueryParser::Params
|
|
24
|
-
|
|
25
|
-
RFC2822_DAY_NAME = [ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ]
|
|
26
|
-
RFC2822_MONTH_NAME = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
|
|
27
|
-
RFC2396_PARSER = defined?(URI::RFC2396_PARSER) ? URI::RFC2396_PARSER : URI::RFC2396_Parser.new
|
|
27
|
+
URI_PARSER = defined?(::URI::RFC2396_PARSER) ? ::URI::RFC2396_PARSER : ::URI::DEFAULT_PARSER
|
|
28
28
|
|
|
29
29
|
class << self
|
|
30
30
|
attr_accessor :default_query_parser
|
|
31
31
|
end
|
|
32
|
-
# The default
|
|
33
|
-
# This helps prevent a rogue client from
|
|
34
|
-
|
|
32
|
+
# The default amount of nesting to allowed by hash parameters.
|
|
33
|
+
# This helps prevent a rogue client from triggering a possible stack overflow
|
|
34
|
+
# when parsing parameters.
|
|
35
|
+
self.default_query_parser = QueryParser.make_default(32)
|
|
35
36
|
|
|
36
37
|
module_function
|
|
37
38
|
|
|
@@ -43,13 +44,13 @@ module Rack
|
|
|
43
44
|
# Like URI escaping, but with %20 instead of +. Strictly speaking this is
|
|
44
45
|
# true URI escaping.
|
|
45
46
|
def escape_path(s)
|
|
46
|
-
|
|
47
|
+
URI_PARSER.escape s
|
|
47
48
|
end
|
|
48
49
|
|
|
49
50
|
# Unescapes the **path** component of a URI. See Rack::Utils.unescape for
|
|
50
51
|
# unescaping query parameters or form components.
|
|
51
52
|
def unescape_path(s)
|
|
52
|
-
|
|
53
|
+
URI_PARSER.unescape s
|
|
53
54
|
end
|
|
54
55
|
|
|
55
56
|
# Unescapes a URI escaped string with +encoding+. +encoding+ will be the
|
|
@@ -86,14 +87,6 @@ module Rack
|
|
|
86
87
|
self.default_query_parser = self.default_query_parser.new_depth_limit(v)
|
|
87
88
|
end
|
|
88
89
|
|
|
89
|
-
def self.key_space_limit
|
|
90
|
-
default_query_parser.key_space_limit
|
|
91
|
-
end
|
|
92
|
-
|
|
93
|
-
def self.key_space_limit=(v)
|
|
94
|
-
self.default_query_parser = self.default_query_parser.new_space_limit(v)
|
|
95
|
-
end
|
|
96
|
-
|
|
97
90
|
if defined?(Process::CLOCK_MONOTONIC)
|
|
98
91
|
def clock_time
|
|
99
92
|
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
@@ -132,13 +125,13 @@ module Rack
|
|
|
132
125
|
}.join("&")
|
|
133
126
|
when Hash
|
|
134
127
|
value.map { |k, v|
|
|
135
|
-
build_nested_query(v, prefix ? "#{prefix}[#{
|
|
128
|
+
build_nested_query(v, prefix ? "#{prefix}[#{k}]" : k)
|
|
136
129
|
}.delete_if(&:empty?).join('&')
|
|
137
130
|
when nil
|
|
138
|
-
prefix
|
|
131
|
+
escape(prefix)
|
|
139
132
|
else
|
|
140
133
|
raise ArgumentError, "value must be a Hash" if prefix.nil?
|
|
141
|
-
"#{prefix}=#{escape(value)}"
|
|
134
|
+
"#{escape(prefix)}=#{escape(value)}"
|
|
142
135
|
end
|
|
143
136
|
end
|
|
144
137
|
|
|
@@ -153,6 +146,80 @@ module Rack
|
|
|
153
146
|
end
|
|
154
147
|
end
|
|
155
148
|
|
|
149
|
+
ALLOWED_FORWARED_PARAMS = %w[by for host proto].to_h { |name| [name, name.to_sym] }.freeze
|
|
150
|
+
private_constant :ALLOWED_FORWARED_PARAMS
|
|
151
|
+
|
|
152
|
+
def forwarded_values(forwarded_header)
|
|
153
|
+
return unless forwarded_header
|
|
154
|
+
header = forwarded_header.to_s.tr("\n", ";")
|
|
155
|
+
header.sub!(/\A[\s;,]+/, '')
|
|
156
|
+
num_params = num_escapes = 0
|
|
157
|
+
max_params = max_escapes = 1024
|
|
158
|
+
params = {}
|
|
159
|
+
|
|
160
|
+
# Parse parameter list
|
|
161
|
+
while i = header.index('=')
|
|
162
|
+
# Only parse up to max parameters, to avoid potential denial of service
|
|
163
|
+
num_params += 1
|
|
164
|
+
return if num_params > max_params
|
|
165
|
+
|
|
166
|
+
# Found end of parameter name, ensure forward progress in loop
|
|
167
|
+
param = header.slice!(0, i+1)
|
|
168
|
+
|
|
169
|
+
# Remove ending equals and preceding whitespace from parameter name
|
|
170
|
+
param.chomp!('=')
|
|
171
|
+
param.strip!
|
|
172
|
+
param.downcase!
|
|
173
|
+
return unless param = ALLOWED_FORWARED_PARAMS[param]
|
|
174
|
+
|
|
175
|
+
if header[0] == '"'
|
|
176
|
+
# Parameter value is quoted, parse it, handling backslash escapes
|
|
177
|
+
header.slice!(0, 1)
|
|
178
|
+
value = String.new
|
|
179
|
+
|
|
180
|
+
while i = header.index(/(["\\])/)
|
|
181
|
+
c = $1
|
|
182
|
+
|
|
183
|
+
# Append all content until ending quote or escape
|
|
184
|
+
value << header.slice!(0, i)
|
|
185
|
+
|
|
186
|
+
# Remove either backslash or ending quote,
|
|
187
|
+
# ensures forward progress in loop
|
|
188
|
+
header.slice!(0, 1)
|
|
189
|
+
|
|
190
|
+
# stop parsing parameter value if found ending quote
|
|
191
|
+
break if c == '"'
|
|
192
|
+
|
|
193
|
+
# Only allow up to max escapes, to avoid potential denial of service
|
|
194
|
+
num_escapes += 1
|
|
195
|
+
return if num_escapes > max_escapes
|
|
196
|
+
escaped_char = header.slice!(0, 1)
|
|
197
|
+
value << escaped_char
|
|
198
|
+
end
|
|
199
|
+
else
|
|
200
|
+
if i = header.index(/[;,]/)
|
|
201
|
+
# Parameter value unquoted (which may be invalid), value ends at comma or semicolon
|
|
202
|
+
value = header.slice!(0, i)
|
|
203
|
+
value.sub!(/[\s;,]+\z/, '')
|
|
204
|
+
else
|
|
205
|
+
# If no ending semicolon, assume remainder of line is value and stop parsing
|
|
206
|
+
header.strip!
|
|
207
|
+
value = header
|
|
208
|
+
header = ''
|
|
209
|
+
end
|
|
210
|
+
value.lstrip!
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
(params[param] ||= []) << value
|
|
214
|
+
|
|
215
|
+
# skip trailing semicolons/commas/whitespace, to proceed to next parameter
|
|
216
|
+
header.sub!(/\A[\s;,]+/, '') unless header.empty?
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
params
|
|
220
|
+
end
|
|
221
|
+
module_function :forwarded_values
|
|
222
|
+
|
|
156
223
|
# Return best accept value to use, based on the algorithm
|
|
157
224
|
# in RFC 2616 Section 14. If there are multiple best
|
|
158
225
|
# matches (same specificity and quality), the value returned
|
|
@@ -167,36 +234,60 @@ module Rack
|
|
|
167
234
|
end.compact.sort_by do |match, quality|
|
|
168
235
|
(match.split('/', 2).count('*') * -10) + quality
|
|
169
236
|
end.last
|
|
170
|
-
matches
|
|
237
|
+
matches&.first
|
|
171
238
|
end
|
|
172
239
|
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
240
|
+
# Introduced in ERB 4.0. ERB::Escape is an alias for ERB::Utils which
|
|
241
|
+
# doesn't get monkey-patched by rails
|
|
242
|
+
if defined?(ERB::Escape) && ERB::Escape.instance_method(:html_escape)
|
|
243
|
+
define_method(:escape_html, ERB::Escape.instance_method(:html_escape))
|
|
244
|
+
# :nocov:
|
|
245
|
+
# Ruby 3.2/ERB 4.0 added ERB::Escape#html_escape, so the else
|
|
246
|
+
# branch cannot be hit on the current Ruby version.
|
|
247
|
+
else
|
|
248
|
+
require 'cgi/escape'
|
|
249
|
+
# Escape ampersands, brackets and quotes to their HTML/XML entities.
|
|
250
|
+
def escape_html(string)
|
|
251
|
+
CGI.escapeHTML(string.to_s)
|
|
252
|
+
end
|
|
253
|
+
# :nocov:
|
|
187
254
|
end
|
|
188
255
|
|
|
256
|
+
# Given an array of available encoding strings, and an array of
|
|
257
|
+
# acceptable encodings for a request, where each element of the
|
|
258
|
+
# acceptable encodings array is an array where the first element
|
|
259
|
+
# is an encoding name and the second element is the numeric
|
|
260
|
+
# priority for the encoding, return the available encoding with
|
|
261
|
+
# the highest priority.
|
|
262
|
+
#
|
|
263
|
+
# The accept_encoding argument is typically generated by calling
|
|
264
|
+
# Request#accept_encoding.
|
|
265
|
+
#
|
|
266
|
+
# Example:
|
|
267
|
+
#
|
|
268
|
+
# select_best_encoding(%w(compress gzip identity),
|
|
269
|
+
# [["compress", 0.5], ["gzip", 1.0]])
|
|
270
|
+
# # => "gzip"
|
|
271
|
+
#
|
|
272
|
+
# To reduce denial of service potential, only the first 16
|
|
273
|
+
# acceptable encodings are considered.
|
|
189
274
|
def select_best_encoding(available_encodings, accept_encoding)
|
|
190
275
|
# http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
|
|
191
276
|
|
|
277
|
+
# Only process the first 16 encodings
|
|
278
|
+
accept_encoding = accept_encoding[0...16]
|
|
192
279
|
expanded_accept_encoding = []
|
|
280
|
+
wildcard_seen = false
|
|
193
281
|
|
|
194
282
|
accept_encoding.each do |m, q|
|
|
195
283
|
preference = available_encodings.index(m) || available_encodings.size
|
|
196
284
|
|
|
197
285
|
if m == "*"
|
|
198
|
-
|
|
199
|
-
|
|
286
|
+
unless wildcard_seen
|
|
287
|
+
(available_encodings - accept_encoding.map(&:first)).each do |m2|
|
|
288
|
+
expanded_accept_encoding << [m2, q, preference]
|
|
289
|
+
end
|
|
290
|
+
wildcard_seen = true
|
|
200
291
|
end
|
|
201
292
|
else
|
|
202
293
|
expanded_accept_encoding << [m, q, preference]
|
|
@@ -204,7 +295,13 @@ module Rack
|
|
|
204
295
|
end
|
|
205
296
|
|
|
206
297
|
encoding_candidates = expanded_accept_encoding
|
|
207
|
-
.
|
|
298
|
+
.sort do |(_, q1, p1), (_, q2, p2)|
|
|
299
|
+
if r = (q1 <=> q2).nonzero?
|
|
300
|
+
-r
|
|
301
|
+
else
|
|
302
|
+
(p1 <=> p2).nonzero? || 0
|
|
303
|
+
end
|
|
304
|
+
end
|
|
208
305
|
.map!(&:first)
|
|
209
306
|
|
|
210
307
|
unless encoding_candidates.include?("identity")
|
|
@@ -218,24 +315,69 @@ module Rack
|
|
|
218
315
|
(encoding_candidates & available_encodings)[0]
|
|
219
316
|
end
|
|
220
317
|
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
318
|
+
# :call-seq:
|
|
319
|
+
# parse_cookies_header(value) -> hash
|
|
320
|
+
#
|
|
321
|
+
# Parse cookies from the provided header +value+ according to RFC6265. The
|
|
322
|
+
# syntax for cookie headers only supports semicolons. Returns a map of
|
|
323
|
+
# cookie +key+ to cookie +value+.
|
|
324
|
+
#
|
|
325
|
+
# parse_cookies_header('myname=myvalue; max-age=0')
|
|
326
|
+
# # => {"myname"=>"myvalue", "max-age"=>"0"}
|
|
327
|
+
#
|
|
328
|
+
def parse_cookies_header(value)
|
|
329
|
+
return {} unless value
|
|
224
330
|
|
|
225
|
-
|
|
226
|
-
# According to RFC 6265:
|
|
227
|
-
# The syntax for cookie headers only supports semicolons
|
|
228
|
-
# User Agent -> Server ==
|
|
229
|
-
# Cookie: SID=31d4d96e407aad42; lang=en-US
|
|
230
|
-
return {} unless header
|
|
231
|
-
header.split(/[;] */n).each_with_object({}) do |cookie, cookies|
|
|
331
|
+
value.split(/; */n).each_with_object({}) do |cookie, cookies|
|
|
232
332
|
next if cookie.empty?
|
|
233
333
|
key, value = cookie.split('=', 2)
|
|
234
334
|
cookies[key] = (unescape(value) rescue value) unless cookies.key?(key)
|
|
235
335
|
end
|
|
236
336
|
end
|
|
237
337
|
|
|
238
|
-
|
|
338
|
+
# :call-seq:
|
|
339
|
+
# parse_cookies(env) -> hash
|
|
340
|
+
#
|
|
341
|
+
# Parse cookies from the provided request environment using
|
|
342
|
+
# parse_cookies_header. Returns a map of cookie +key+ to cookie +value+.
|
|
343
|
+
#
|
|
344
|
+
# parse_cookies({'HTTP_COOKIE' => 'myname=myvalue'})
|
|
345
|
+
# # => {'myname' => 'myvalue'}
|
|
346
|
+
#
|
|
347
|
+
def parse_cookies(env)
|
|
348
|
+
parse_cookies_header env[HTTP_COOKIE]
|
|
349
|
+
end
|
|
350
|
+
|
|
351
|
+
# A valid cookie key according to RFC6265 and RFC2616.
|
|
352
|
+
# A <cookie-name> can be any US-ASCII characters, except control characters, spaces, or tabs. It also must not contain a separator character like the following: ( ) < > @ , ; : \ " / [ ] ? = { }.
|
|
353
|
+
VALID_COOKIE_KEY = /\A[!#$%&'*+\-\.\^_`|~0-9a-zA-Z]+\z/.freeze
|
|
354
|
+
private_constant :VALID_COOKIE_KEY
|
|
355
|
+
|
|
356
|
+
# :call-seq:
|
|
357
|
+
# set_cookie_header(key, value) -> encoded string
|
|
358
|
+
#
|
|
359
|
+
# Generate an encoded string using the provided +key+ and +value+ suitable
|
|
360
|
+
# for the +set-cookie+ header according to RFC6265. The +value+ may be an
|
|
361
|
+
# instance of either +String+ or +Hash+. If the cookie key is invalid (as
|
|
362
|
+
# defined by RFC6265), an +ArgumentError+ will be raised.
|
|
363
|
+
#
|
|
364
|
+
# If the cookie +value+ is an instance of +Hash+, it considers the following
|
|
365
|
+
# cookie attribute keys: +domain+, +max_age+, +expires+ (must be instance
|
|
366
|
+
# of +Time+), +secure+, +http_only+, +same_site+ and +value+. For more
|
|
367
|
+
# details about the interpretation of these fields, consult
|
|
368
|
+
# [RFC6265 Section 5.2](https://datatracker.ietf.org/doc/html/rfc6265#section-5.2).
|
|
369
|
+
#
|
|
370
|
+
# set_cookie_header("myname", "myvalue")
|
|
371
|
+
# # => "myname=myvalue"
|
|
372
|
+
#
|
|
373
|
+
# set_cookie_header("myname", {value: "myvalue", max_age: 10})
|
|
374
|
+
# # => "myname=myvalue; max-age=10"
|
|
375
|
+
#
|
|
376
|
+
def set_cookie_header(key, value)
|
|
377
|
+
unless key =~ VALID_COOKIE_KEY
|
|
378
|
+
raise ArgumentError, "invalid cookie key: #{key.inspect}"
|
|
379
|
+
end
|
|
380
|
+
|
|
239
381
|
case value
|
|
240
382
|
when Hash
|
|
241
383
|
domain = "; domain=#{value[:domain]}" if value[:domain]
|
|
@@ -243,123 +385,123 @@ module Rack
|
|
|
243
385
|
max_age = "; max-age=#{value[:max_age]}" if value[:max_age]
|
|
244
386
|
expires = "; expires=#{value[:expires].httpdate}" if value[:expires]
|
|
245
387
|
secure = "; secure" if value[:secure]
|
|
246
|
-
httponly = ";
|
|
388
|
+
httponly = "; httponly" if (value.key?(:httponly) ? value[:httponly] : value[:http_only])
|
|
247
389
|
same_site =
|
|
248
390
|
case value[:same_site]
|
|
249
391
|
when false, nil
|
|
250
392
|
nil
|
|
251
393
|
when :none, 'None', :None
|
|
252
|
-
';
|
|
394
|
+
'; samesite=none'
|
|
253
395
|
when :lax, 'Lax', :Lax
|
|
254
|
-
';
|
|
396
|
+
'; samesite=lax'
|
|
255
397
|
when true, :strict, 'Strict', :Strict
|
|
256
|
-
';
|
|
398
|
+
'; samesite=strict'
|
|
257
399
|
else
|
|
258
|
-
raise ArgumentError, "Invalid
|
|
400
|
+
raise ArgumentError, "Invalid :same_site value: #{value[:same_site].inspect}"
|
|
259
401
|
end
|
|
402
|
+
partitioned = "; partitioned" if value[:partitioned]
|
|
260
403
|
value = value[:value]
|
|
261
404
|
end
|
|
405
|
+
|
|
262
406
|
value = [value] unless Array === value
|
|
263
407
|
|
|
264
|
-
|
|
265
|
-
"#{path}#{max_age}#{expires}#{secure}#{httponly}#{same_site}"
|
|
408
|
+
return "#{key}=#{value.map { |v| escape v }.join('&')}#{domain}" \
|
|
409
|
+
"#{path}#{max_age}#{expires}#{secure}#{httponly}#{same_site}#{partitioned}"
|
|
410
|
+
end
|
|
266
411
|
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
412
|
+
# :call-seq:
|
|
413
|
+
# set_cookie_header!(headers, key, value) -> header value
|
|
414
|
+
#
|
|
415
|
+
# Append a cookie in the specified headers with the given cookie +key+ and
|
|
416
|
+
# +value+ using set_cookie_header.
|
|
417
|
+
#
|
|
418
|
+
# If the headers already contains a +set-cookie+ key, it will be converted
|
|
419
|
+
# to an +Array+ if not already, and appended to.
|
|
420
|
+
def set_cookie_header!(headers, key, value)
|
|
421
|
+
if header = headers[SET_COOKIE]
|
|
422
|
+
if header.is_a?(Array)
|
|
423
|
+
header << set_cookie_header(key, value)
|
|
424
|
+
else
|
|
425
|
+
headers[SET_COOKIE] = [header, set_cookie_header(key, value)]
|
|
426
|
+
end
|
|
274
427
|
else
|
|
275
|
-
|
|
428
|
+
headers[SET_COOKIE] = set_cookie_header(key, value)
|
|
276
429
|
end
|
|
277
430
|
end
|
|
278
431
|
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
432
|
+
# :call-seq:
|
|
433
|
+
# delete_set_cookie_header(key, value = {}) -> encoded string
|
|
434
|
+
#
|
|
435
|
+
# Generate an encoded string based on the given +key+ and +value+ using
|
|
436
|
+
# set_cookie_header for the purpose of causing the specified cookie to be
|
|
437
|
+
# deleted. The +value+ may be an instance of +Hash+ and can include
|
|
438
|
+
# attributes as outlined by set_cookie_header. The encoded cookie will have
|
|
439
|
+
# a +max_age+ of 0 seconds, an +expires+ date in the past and an empty
|
|
440
|
+
# +value+. When used with the +set-cookie+ header, it will cause the client
|
|
441
|
+
# to *remove* any matching cookie.
|
|
442
|
+
#
|
|
443
|
+
# delete_set_cookie_header("myname")
|
|
444
|
+
# # => "myname=; max-age=0; expires=Thu, 01 Jan 1970 00:00:00 GMT"
|
|
445
|
+
#
|
|
446
|
+
def delete_set_cookie_header(key, value = {})
|
|
447
|
+
set_cookie_header(key, value.merge(max_age: '0', expires: Time.at(0), value: ''))
|
|
282
448
|
end
|
|
283
449
|
|
|
284
|
-
def
|
|
285
|
-
|
|
286
|
-
when nil, ''
|
|
287
|
-
cookies = []
|
|
288
|
-
when String
|
|
289
|
-
cookies = header.split("\n")
|
|
290
|
-
when Array
|
|
291
|
-
cookies = header
|
|
292
|
-
end
|
|
293
|
-
|
|
294
|
-
key = escape(key)
|
|
295
|
-
domain = value[:domain]
|
|
296
|
-
path = value[:path]
|
|
297
|
-
regexp = if domain
|
|
298
|
-
if path
|
|
299
|
-
/\A#{key}=.*(?:domain=#{domain}(?:;|$).*path=#{path}(?:;|$)|path=#{path}(?:;|$).*domain=#{domain}(?:;|$))/
|
|
300
|
-
else
|
|
301
|
-
/\A#{key}=.*domain=#{domain}(?:;|$)/
|
|
302
|
-
end
|
|
303
|
-
elsif path
|
|
304
|
-
/\A#{key}=.*path=#{path}(?:;|$)/
|
|
305
|
-
else
|
|
306
|
-
/\A#{key}=/
|
|
307
|
-
end
|
|
450
|
+
def delete_cookie_header!(headers, key, value = {})
|
|
451
|
+
headers[SET_COOKIE] = delete_set_cookie_header!(headers[SET_COOKIE], key, value)
|
|
308
452
|
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
cookies.join("\n")
|
|
453
|
+
return nil
|
|
312
454
|
end
|
|
313
455
|
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
#
|
|
320
|
-
#
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
456
|
+
# :call-seq:
|
|
457
|
+
# delete_set_cookie_header!(header, key, value = {}) -> header value
|
|
458
|
+
#
|
|
459
|
+
# Set an expired cookie in the specified headers with the given cookie
|
|
460
|
+
# +key+ and +value+ using delete_set_cookie_header. This causes
|
|
461
|
+
# the client to immediately delete the specified cookie.
|
|
462
|
+
#
|
|
463
|
+
# delete_set_cookie_header!(nil, "mycookie")
|
|
464
|
+
# # => "mycookie=; max-age=0; expires=Thu, 01 Jan 1970 00:00:00 GMT"
|
|
465
|
+
#
|
|
466
|
+
# If the header is non-nil, it will be modified in place.
|
|
467
|
+
#
|
|
468
|
+
# header = []
|
|
469
|
+
# delete_set_cookie_header!(header, "mycookie")
|
|
470
|
+
# # => ["mycookie=; max-age=0; expires=Thu, 01 Jan 1970 00:00:00 GMT"]
|
|
471
|
+
# header
|
|
472
|
+
# # => ["mycookie=; max-age=0; expires=Thu, 01 Jan 1970 00:00:00 GMT"]
|
|
473
|
+
#
|
|
474
|
+
def delete_set_cookie_header!(header, key, value = {})
|
|
475
|
+
if header
|
|
476
|
+
header = Array(header)
|
|
477
|
+
header << delete_set_cookie_header(key, value)
|
|
478
|
+
else
|
|
479
|
+
header = delete_set_cookie_header(key, value)
|
|
480
|
+
end
|
|
328
481
|
|
|
482
|
+
return header
|
|
329
483
|
end
|
|
330
484
|
|
|
331
485
|
def rfc2822(time)
|
|
332
486
|
time.rfc2822
|
|
333
487
|
end
|
|
334
488
|
|
|
335
|
-
# Modified version of stdlib time.rb Time#rfc2822 to use '%d-%b-%Y' instead
|
|
336
|
-
# of '% %b %Y'.
|
|
337
|
-
# It assumes that the time is in GMT to comply to the RFC 2109.
|
|
338
|
-
#
|
|
339
|
-
# NOTE: I'm not sure the RFC says it requires GMT, but is ambiguous enough
|
|
340
|
-
# that I'm certain someone implemented only that option.
|
|
341
|
-
# Do not use %a and %b from Time.strptime, it would use localized names for
|
|
342
|
-
# weekday and month.
|
|
343
|
-
#
|
|
344
|
-
def rfc2109(time)
|
|
345
|
-
wday = RFC2822_DAY_NAME[time.wday]
|
|
346
|
-
mon = RFC2822_MONTH_NAME[time.mon - 1]
|
|
347
|
-
time.strftime("#{wday}, %d-#{mon}-%Y %H:%M:%S GMT")
|
|
348
|
-
end
|
|
349
|
-
|
|
350
489
|
# Parses the "Range:" header, if present, into an array of Range objects.
|
|
351
490
|
# Returns nil if the header is missing or syntactically invalid.
|
|
352
491
|
# Returns an empty array if none of the ranges are satisfiable.
|
|
353
|
-
def byte_ranges(env, size)
|
|
354
|
-
|
|
355
|
-
get_byte_ranges env['HTTP_RANGE'], size
|
|
492
|
+
def byte_ranges(env, size, max_ranges: 100)
|
|
493
|
+
get_byte_ranges env['HTTP_RANGE'], size, max_ranges: max_ranges
|
|
356
494
|
end
|
|
357
495
|
|
|
358
|
-
def get_byte_ranges(http_range, size)
|
|
496
|
+
def get_byte_ranges(http_range, size, max_ranges: 100)
|
|
359
497
|
# See <http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35>
|
|
498
|
+
# Ignore Range when file size is 0 to avoid a 416 error.
|
|
499
|
+
return nil if size.zero?
|
|
360
500
|
return nil unless http_range && http_range =~ /bytes=([^;]+)/
|
|
501
|
+
byte_range = $1
|
|
502
|
+
return nil if byte_range.count(',') >= max_ranges
|
|
361
503
|
ranges = []
|
|
362
|
-
|
|
504
|
+
byte_range.split(/,[ \t]*/).each do |range_spec|
|
|
363
505
|
return nil unless range_spec.include?('-')
|
|
364
506
|
range = range_spec.split('-')
|
|
365
507
|
r0, r1 = range[0], range[1]
|
|
@@ -382,25 +524,35 @@ module Rack
|
|
|
382
524
|
ranges << (r0..r1) if r0 <= r1
|
|
383
525
|
end
|
|
384
526
|
|
|
385
|
-
return [] if ranges.map(&:size).
|
|
527
|
+
return [] if ranges.map(&:size).sum > size
|
|
386
528
|
|
|
387
529
|
ranges
|
|
388
530
|
end
|
|
389
531
|
|
|
390
|
-
#
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
532
|
+
# :nocov:
|
|
533
|
+
if defined?(OpenSSL.fixed_length_secure_compare)
|
|
534
|
+
# Constant time string comparison.
|
|
535
|
+
#
|
|
536
|
+
# NOTE: the values compared should be of fixed length, such as strings
|
|
537
|
+
# that have already been processed by HMAC. This should not be used
|
|
538
|
+
# on variable length plaintext strings because it could leak length info
|
|
539
|
+
# via timing attacks.
|
|
540
|
+
def secure_compare(a, b)
|
|
541
|
+
return false unless a.bytesize == b.bytesize
|
|
542
|
+
|
|
543
|
+
OpenSSL.fixed_length_secure_compare(a, b)
|
|
544
|
+
end
|
|
545
|
+
# :nocov:
|
|
546
|
+
else
|
|
547
|
+
def secure_compare(a, b)
|
|
548
|
+
return false unless a.bytesize == b.bytesize
|
|
398
549
|
|
|
399
|
-
|
|
550
|
+
l = a.unpack("C*")
|
|
400
551
|
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
552
|
+
r, i = 0, -1
|
|
553
|
+
b.each_byte { |v| r |= v ^ l[i += 1] }
|
|
554
|
+
r == 0
|
|
555
|
+
end
|
|
404
556
|
end
|
|
405
557
|
|
|
406
558
|
# Context allows the use of a compatible middleware at different points
|
|
@@ -429,101 +581,12 @@ module Rack
|
|
|
429
581
|
end
|
|
430
582
|
end
|
|
431
583
|
|
|
432
|
-
# A case-insensitive Hash that preserves the original case of a
|
|
433
|
-
# header when set.
|
|
434
|
-
#
|
|
435
|
-
# @api private
|
|
436
|
-
class HeaderHash < Hash # :nodoc:
|
|
437
|
-
def self.[](headers)
|
|
438
|
-
if headers.is_a?(HeaderHash) && !headers.frozen?
|
|
439
|
-
return headers
|
|
440
|
-
else
|
|
441
|
-
return self.new(headers)
|
|
442
|
-
end
|
|
443
|
-
end
|
|
444
|
-
|
|
445
|
-
def initialize(hash = {})
|
|
446
|
-
super()
|
|
447
|
-
@names = {}
|
|
448
|
-
hash.each { |k, v| self[k] = v }
|
|
449
|
-
end
|
|
450
|
-
|
|
451
|
-
# on dup/clone, we need to duplicate @names hash
|
|
452
|
-
def initialize_copy(other)
|
|
453
|
-
super
|
|
454
|
-
@names = other.names.dup
|
|
455
|
-
end
|
|
456
|
-
|
|
457
|
-
# on clear, we need to clear @names hash
|
|
458
|
-
def clear
|
|
459
|
-
super
|
|
460
|
-
@names.clear
|
|
461
|
-
end
|
|
462
|
-
|
|
463
|
-
def each
|
|
464
|
-
super do |k, v|
|
|
465
|
-
yield(k, v.respond_to?(:to_ary) ? v.to_ary.join("\n") : v)
|
|
466
|
-
end
|
|
467
|
-
end
|
|
468
|
-
|
|
469
|
-
def to_hash
|
|
470
|
-
hash = {}
|
|
471
|
-
each { |k, v| hash[k] = v }
|
|
472
|
-
hash
|
|
473
|
-
end
|
|
474
|
-
|
|
475
|
-
def [](k)
|
|
476
|
-
super(k) || super(@names[k.downcase])
|
|
477
|
-
end
|
|
478
|
-
|
|
479
|
-
def []=(k, v)
|
|
480
|
-
canonical = k.downcase.freeze
|
|
481
|
-
delete k if @names[canonical] && @names[canonical] != k # .delete is expensive, don't invoke it unless necessary
|
|
482
|
-
@names[canonical] = k
|
|
483
|
-
super k, v
|
|
484
|
-
end
|
|
485
|
-
|
|
486
|
-
def delete(k)
|
|
487
|
-
canonical = k.downcase
|
|
488
|
-
result = super @names.delete(canonical)
|
|
489
|
-
result
|
|
490
|
-
end
|
|
491
|
-
|
|
492
|
-
def include?(k)
|
|
493
|
-
super || @names.include?(k.downcase)
|
|
494
|
-
end
|
|
495
|
-
|
|
496
|
-
alias_method :has_key?, :include?
|
|
497
|
-
alias_method :member?, :include?
|
|
498
|
-
alias_method :key?, :include?
|
|
499
|
-
|
|
500
|
-
def merge!(other)
|
|
501
|
-
other.each { |k, v| self[k] = v }
|
|
502
|
-
self
|
|
503
|
-
end
|
|
504
|
-
|
|
505
|
-
def merge(other)
|
|
506
|
-
hash = dup
|
|
507
|
-
hash.merge! other
|
|
508
|
-
end
|
|
509
|
-
|
|
510
|
-
def replace(other)
|
|
511
|
-
clear
|
|
512
|
-
other.each { |k, v| self[k] = v }
|
|
513
|
-
self
|
|
514
|
-
end
|
|
515
|
-
|
|
516
|
-
protected
|
|
517
|
-
def names
|
|
518
|
-
@names
|
|
519
|
-
end
|
|
520
|
-
end
|
|
521
|
-
|
|
522
584
|
# Every standard HTTP code mapped to the appropriate message.
|
|
523
585
|
# Generated with:
|
|
524
|
-
# curl -s https://www.iana.org/assignments/http-status-codes/http-status-codes-1.csv
|
|
525
|
-
# ruby -
|
|
526
|
-
#
|
|
586
|
+
# curl -s https://www.iana.org/assignments/http-status-codes/http-status-codes-1.csv \
|
|
587
|
+
# | ruby -rcsv -e "puts CSV.parse(STDIN, headers: true) \
|
|
588
|
+
# .reject {|v| v['Description'] == 'Unassigned' or v['Description'].include? '(' } \
|
|
589
|
+
# .map {|v| %Q/#{v['Value']} => '#{v['Description']}'/ }.join(','+?\n)"
|
|
527
590
|
HTTP_STATUS_CODES = {
|
|
528
591
|
100 => 'Continue',
|
|
529
592
|
101 => 'Switching Protocols',
|
|
@@ -545,7 +608,6 @@ module Rack
|
|
|
545
608
|
303 => 'See Other',
|
|
546
609
|
304 => 'Not Modified',
|
|
547
610
|
305 => 'Use Proxy',
|
|
548
|
-
306 => '(Unused)',
|
|
549
611
|
307 => 'Temporary Redirect',
|
|
550
612
|
308 => 'Permanent Redirect',
|
|
551
613
|
400 => 'Bad Request',
|
|
@@ -561,13 +623,13 @@ module Rack
|
|
|
561
623
|
410 => 'Gone',
|
|
562
624
|
411 => 'Length Required',
|
|
563
625
|
412 => 'Precondition Failed',
|
|
564
|
-
413 => '
|
|
626
|
+
413 => 'Content Too Large',
|
|
565
627
|
414 => 'URI Too Long',
|
|
566
628
|
415 => 'Unsupported Media Type',
|
|
567
629
|
416 => 'Range Not Satisfiable',
|
|
568
630
|
417 => 'Expectation Failed',
|
|
569
631
|
421 => 'Misdirected Request',
|
|
570
|
-
422 => 'Unprocessable
|
|
632
|
+
422 => 'Unprocessable Content',
|
|
571
633
|
423 => 'Locked',
|
|
572
634
|
424 => 'Failed Dependency',
|
|
573
635
|
425 => 'Too Early',
|
|
@@ -575,7 +637,7 @@ module Rack
|
|
|
575
637
|
428 => 'Precondition Required',
|
|
576
638
|
429 => 'Too Many Requests',
|
|
577
639
|
431 => 'Request Header Fields Too Large',
|
|
578
|
-
451 => 'Unavailable
|
|
640
|
+
451 => 'Unavailable For Legal Reasons',
|
|
579
641
|
500 => 'Internal Server Error',
|
|
580
642
|
501 => 'Not Implemented',
|
|
581
643
|
502 => 'Bad Gateway',
|
|
@@ -585,8 +647,6 @@ module Rack
|
|
|
585
647
|
506 => 'Variant Also Negotiates',
|
|
586
648
|
507 => 'Insufficient Storage',
|
|
587
649
|
508 => 'Loop Detected',
|
|
588
|
-
509 => 'Bandwidth Limit Exceeded',
|
|
589
|
-
510 => 'Not Extended',
|
|
590
650
|
511 => 'Network Authentication Required'
|
|
591
651
|
}
|
|
592
652
|
|
|
@@ -594,12 +654,34 @@ module Rack
|
|
|
594
654
|
STATUS_WITH_NO_ENTITY_BODY = Hash[((100..199).to_a << 204 << 304).product([true])]
|
|
595
655
|
|
|
596
656
|
SYMBOL_TO_STATUS_CODE = Hash[*HTTP_STATUS_CODES.map { |code, message|
|
|
597
|
-
[message.downcase.gsub(/\s
|
|
657
|
+
[message.downcase.gsub(/\s|-/, '_').to_sym, code]
|
|
598
658
|
}.flatten]
|
|
599
659
|
|
|
660
|
+
OBSOLETE_SYMBOLS_TO_STATUS_CODES = {
|
|
661
|
+
payload_too_large: 413,
|
|
662
|
+
unprocessable_entity: 422,
|
|
663
|
+
bandwidth_limit_exceeded: 509,
|
|
664
|
+
not_extended: 510
|
|
665
|
+
}.freeze
|
|
666
|
+
private_constant :OBSOLETE_SYMBOLS_TO_STATUS_CODES
|
|
667
|
+
|
|
668
|
+
OBSOLETE_SYMBOL_MAPPINGS = {
|
|
669
|
+
payload_too_large: :content_too_large,
|
|
670
|
+
unprocessable_entity: :unprocessable_content
|
|
671
|
+
}.freeze
|
|
672
|
+
private_constant :OBSOLETE_SYMBOL_MAPPINGS
|
|
673
|
+
|
|
600
674
|
def status_code(status)
|
|
601
675
|
if status.is_a?(Symbol)
|
|
602
|
-
SYMBOL_TO_STATUS_CODE.fetch(status)
|
|
676
|
+
SYMBOL_TO_STATUS_CODE.fetch(status) do
|
|
677
|
+
fallback_code = OBSOLETE_SYMBOLS_TO_STATUS_CODES.fetch(status) { raise ArgumentError, "Unrecognized status code #{status.inspect}" }
|
|
678
|
+
message = "Status code #{status.inspect} is deprecated and will be removed in a future version of Rack."
|
|
679
|
+
if canonical_symbol = OBSOLETE_SYMBOL_MAPPINGS[status]
|
|
680
|
+
message = "#{message} Please use #{canonical_symbol.inspect} instead."
|
|
681
|
+
end
|
|
682
|
+
warn message, uplevel: 3
|
|
683
|
+
fallback_code
|
|
684
|
+
end
|
|
603
685
|
else
|
|
604
686
|
status.to_i
|
|
605
687
|
end
|