rack 2.2.26 → 3.0.0.beta1
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 +134 -219
- data/CONTRIBUTING.md +53 -47
- data/MIT-LICENSE +1 -1
- data/README.md +287 -0
- data/Rakefile +40 -7
- data/SPEC.rdoc +166 -125
- data/contrib/LICENSE.md +7 -0
- data/contrib/logo.webp +0 -0
- data/lib/rack/auth/abstract/handler.rb +3 -1
- data/lib/rack/auth/abstract/request.rb +3 -1
- data/lib/rack/auth/basic.rb +2 -1
- data/lib/rack/auth/digest/md5.rb +1 -131
- data/lib/rack/auth/digest/nonce.rb +1 -53
- data/lib/rack/auth/digest/params.rb +1 -54
- data/lib/rack/auth/digest/request.rb +1 -43
- data/lib/rack/auth/digest.rb +256 -0
- data/lib/rack/body_proxy.rb +3 -1
- data/lib/rack/builder.rb +60 -42
- data/lib/rack/cascade.rb +2 -0
- data/lib/rack/chunked.rb +16 -13
- data/lib/rack/common_logger.rb +24 -20
- data/lib/rack/conditional_get.rb +18 -15
- data/lib/rack/constants.rb +62 -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 +13 -11
- data/lib/rack/etag.rb +14 -23
- data/lib/rack/events.rb +4 -0
- data/lib/rack/file.rb +2 -0
- data/lib/rack/files.rb +16 -18
- data/lib/rack/head.rb +9 -8
- data/lib/rack/headers.rb +154 -0
- data/lib/rack/lint.rb +740 -649
- data/lib/rack/lock.rb +2 -5
- data/lib/rack/logger.rb +2 -0
- data/lib/rack/media_type.rb +7 -17
- data/lib/rack/method_override.rb +5 -1
- data/lib/rack/mime.rb +8 -0
- data/lib/rack/mock.rb +1 -300
- data/lib/rack/mock_request.rb +166 -0
- data/lib/rack/mock_response.rb +124 -0
- data/lib/rack/multipart/generator.rb +7 -5
- data/lib/rack/multipart/parser.rb +125 -251
- data/lib/rack/multipart/uploaded_file.rb +4 -0
- data/lib/rack/multipart.rb +20 -42
- data/lib/rack/null_logger.rb +9 -0
- data/lib/rack/query_parser.rb +78 -122
- data/lib/rack/recursive.rb +2 -0
- data/lib/rack/reloader.rb +0 -2
- data/lib/rack/request.rb +190 -95
- data/lib/rack/response.rb +131 -61
- data/lib/rack/rewindable_input.rb +24 -5
- data/lib/rack/runtime.rb +7 -6
- data/lib/rack/sendfile.rb +40 -65
- data/lib/rack/show_exceptions.rb +15 -2
- data/lib/rack/show_status.rb +17 -7
- data/lib/rack/static.rb +12 -17
- data/lib/rack/tempfile_reaper.rb +15 -4
- data/lib/rack/urlmap.rb +4 -2
- data/lib/rack/utils.rb +219 -240
- data/lib/rack/version.rb +9 -4
- data/lib/rack.rb +5 -76
- data/rack.gemspec +6 -6
- metadata +22 -31
- data/README.rdoc +0 -355
- 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/lib/rack/core_ext/regexp.rb +0 -14
- 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/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
|
@@ -2,56 +2,52 @@
|
|
|
2
2
|
|
|
3
3
|
require 'strscan'
|
|
4
4
|
|
|
5
|
+
require_relative '../utils'
|
|
6
|
+
|
|
5
7
|
module Rack
|
|
6
8
|
module Multipart
|
|
7
9
|
class MultipartPartLimitError < Errno::EMFILE; end
|
|
8
|
-
class MultipartTotalPartLimitError < StandardError; end
|
|
9
10
|
|
|
10
|
-
class
|
|
11
|
-
|
|
11
|
+
# Use specific error class when parsing multipart request
|
|
12
|
+
# that ends early.
|
|
13
|
+
class EmptyContentError < ::EOFError; end
|
|
14
|
+
|
|
15
|
+
# Base class for multipart exceptions that do not subclass from
|
|
16
|
+
# other exception classes for backwards compatibility.
|
|
17
|
+
class Error < StandardError; end
|
|
18
|
+
|
|
19
|
+
EOL = "\r\n"
|
|
20
|
+
MULTIPART = %r|\Amultipart/.*boundary=\"?([^\";,]+)\"?|ni
|
|
21
|
+
TOKEN = /[^\s()<>,;:\\"\/\[\]?=]+/
|
|
22
|
+
CONDISP = /Content-Disposition:\s*#{TOKEN}\s*/i
|
|
23
|
+
VALUE = /"(?:\\"|[^"])*"|#{TOKEN}/
|
|
24
|
+
BROKEN = /^#{CONDISP}.*;\s*filename=(#{VALUE})/i
|
|
25
|
+
MULTIPART_CONTENT_TYPE = /Content-Type: (.*)#{EOL}/ni
|
|
26
|
+
MULTIPART_CONTENT_DISPOSITION = /Content-Disposition:.*;\s*name=(#{VALUE})/ni
|
|
27
|
+
MULTIPART_CONTENT_ID = /Content-ID:\s*([^#{EOL}]*)/ni
|
|
28
|
+
# Updated definitions from RFC 2231
|
|
29
|
+
ATTRIBUTE_CHAR = %r{[^ \t\v\n\r)(><@,;:\\"/\[\]?='*%]}
|
|
30
|
+
ATTRIBUTE = /#{ATTRIBUTE_CHAR}+/
|
|
31
|
+
SECTION = /\*[0-9]+/
|
|
32
|
+
REGULAR_PARAMETER_NAME = /#{ATTRIBUTE}#{SECTION}?/
|
|
33
|
+
REGULAR_PARAMETER = /(#{REGULAR_PARAMETER_NAME})=(#{VALUE})/
|
|
34
|
+
EXTENDED_OTHER_NAME = /#{ATTRIBUTE}\*[1-9][0-9]*\*/
|
|
35
|
+
EXTENDED_OTHER_VALUE = /%[0-9a-fA-F]{2}|#{ATTRIBUTE_CHAR}/
|
|
36
|
+
EXTENDED_OTHER_PARAMETER = /(#{EXTENDED_OTHER_NAME})=(#{EXTENDED_OTHER_VALUE}*)/
|
|
37
|
+
EXTENDED_INITIAL_NAME = /#{ATTRIBUTE}(?:\*0)?\*/
|
|
38
|
+
EXTENDED_INITIAL_VALUE = /[a-zA-Z0-9\-]*'[a-zA-Z0-9\-]*'#{EXTENDED_OTHER_VALUE}*/
|
|
39
|
+
EXTENDED_INITIAL_PARAMETER = /(#{EXTENDED_INITIAL_NAME})=(#{EXTENDED_INITIAL_VALUE})/
|
|
40
|
+
EXTENDED_PARAMETER = /#{EXTENDED_INITIAL_PARAMETER}|#{EXTENDED_OTHER_PARAMETER}/
|
|
41
|
+
DISPPARM = /;\s*(?:#{REGULAR_PARAMETER}|#{EXTENDED_PARAMETER})\s*/
|
|
42
|
+
RFC2183 = /^#{CONDISP}(#{DISPPARM})+$/i
|
|
12
43
|
|
|
44
|
+
class Parser
|
|
13
45
|
BUFSIZE = 1_048_576
|
|
14
46
|
TEXT_PLAIN = "text/plain"
|
|
15
47
|
TEMPFILE_FACTORY = lambda { |filename, content_type|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
Tempfile.new(["RackMultipart", extension])
|
|
48
|
+
Tempfile.new(["RackMultipart", ::File.extname(filename.gsub("\0", '%00'))])
|
|
19
49
|
}
|
|
20
50
|
|
|
21
|
-
BOUNDARY_REGEX = /\A([^\n]*(?:\n|\Z))/
|
|
22
|
-
|
|
23
|
-
LINE_END = /\n/
|
|
24
|
-
private_constant :LINE_END
|
|
25
|
-
|
|
26
|
-
BOUNDARY_START_LIMIT = 16 * 1024
|
|
27
|
-
private_constant :BOUNDARY_START_LIMIT
|
|
28
|
-
|
|
29
|
-
MIME_HEADER_BYTESIZE_LIMIT = 64 * 1024
|
|
30
|
-
|
|
31
|
-
OBS_UNFOLD = /\r\n([ \t])/
|
|
32
|
-
|
|
33
|
-
private_constant :OBS_UNFOLD
|
|
34
|
-
private_constant :MIME_HEADER_BYTESIZE_LIMIT
|
|
35
|
-
|
|
36
|
-
env_int = lambda do |key, val|
|
|
37
|
-
if str_val = ENV[key]
|
|
38
|
-
begin
|
|
39
|
-
val = Integer(str_val, 10)
|
|
40
|
-
rescue ArgumentError
|
|
41
|
-
raise ArgumentError, "non-integer value provided for environment variable #{key}"
|
|
42
|
-
end
|
|
43
|
-
end
|
|
44
|
-
|
|
45
|
-
val
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
BUFFERED_UPLOAD_BYTESIZE_LIMIT = env_int.call("RACK_MULTIPART_BUFFERED_UPLOAD_BYTESIZE_LIMIT", 16 * 1024 * 1024)
|
|
49
|
-
private_constant :BUFFERED_UPLOAD_BYTESIZE_LIMIT
|
|
50
|
-
|
|
51
|
-
bytesize_limit = env_int.call("RACK_MULTIPART_PARSER_BYTESIZE_LIMIT", 10 * 1024 * 1024 * 1024)
|
|
52
|
-
PARSER_BYTESIZE_LIMIT = bytesize_limit > 0 ? bytesize_limit : nil
|
|
53
|
-
private_constant :PARSER_BYTESIZE_LIMIT
|
|
54
|
-
|
|
55
51
|
class BoundedIO # :nodoc:
|
|
56
52
|
def initialize(io, content_length)
|
|
57
53
|
@io = io
|
|
@@ -73,93 +69,22 @@ module Rack
|
|
|
73
69
|
if str
|
|
74
70
|
@cursor += str.bytesize
|
|
75
71
|
else
|
|
76
|
-
# Raise an error for mismatching
|
|
72
|
+
# Raise an error for mismatching content-length and actual contents
|
|
77
73
|
raise EOFError, "bad content body"
|
|
78
74
|
end
|
|
79
75
|
|
|
80
76
|
str
|
|
81
77
|
end
|
|
82
|
-
|
|
83
|
-
def rewind
|
|
84
|
-
@io.rewind
|
|
85
|
-
end
|
|
86
78
|
end
|
|
87
79
|
|
|
88
80
|
MultipartInfo = Struct.new :params, :tmp_files
|
|
89
81
|
EMPTY = MultipartInfo.new(nil, [])
|
|
90
82
|
|
|
91
|
-
# Parse the boundary parameter from a multipart content type, following
|
|
92
|
-
# the media type grammar in RFC 9110 Section 8.3.1:
|
|
93
|
-
#
|
|
94
|
-
# media-type = type "/" subtype parameters
|
|
95
|
-
# parameters = *( OWS ";" OWS [ parameter ] )
|
|
96
|
-
# parameter = parameter-name "=" parameter-value
|
|
97
|
-
# parameter-value = ( token / quoted-string )
|
|
98
|
-
#
|
|
99
|
-
# Returns nil if the content type is not multipart or has no boundary
|
|
100
|
-
# parameter. Raises EOFError if the parameters are malformed, the boundary
|
|
101
|
-
# is empty, or there is more than one boundary parameter.
|
|
102
83
|
def self.parse_boundary(content_type)
|
|
103
|
-
return unless content_type
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
boundary = nil
|
|
109
|
-
boundary_seen = false
|
|
110
|
-
ows = /[ \t]*/n
|
|
111
|
-
# From RFC 9110 Section 5.6.2
|
|
112
|
-
token = /[!#$%&'*+\-.^_`|~0-9A-Za-z]+/n
|
|
113
|
-
|
|
114
|
-
loop do
|
|
115
|
-
scanner.skip(ows)
|
|
116
|
-
break if scanner.eos?
|
|
117
|
-
|
|
118
|
-
unless scanner.skip(/;/n)
|
|
119
|
-
raise EOFError, "invalid parameters in multipart content type"
|
|
120
|
-
end
|
|
121
|
-
|
|
122
|
-
scanner.skip(ows)
|
|
123
|
-
# Empty parameter, such as in "a=b;;c=d" or a trailing ";".
|
|
124
|
-
next unless name = scanner.scan(token)
|
|
125
|
-
is_boundary = name.casecmp("boundary") == 0
|
|
126
|
-
# RFC 2231 forms such as boundary*, boundary*0, or boundary*0*. Rack
|
|
127
|
-
# does not use them, but some parsers (for example Go's
|
|
128
|
-
# mime.ParseMediaType) prefer them over a plain boundary parameter,
|
|
129
|
-
# so treat them as boundary parameters when checking for duplicates.
|
|
130
|
-
is_rfc2231_boundary = !is_boundary && /\Aboundary\*/ni.match?(name)
|
|
131
|
-
|
|
132
|
-
unless scanner.skip(/=/n)
|
|
133
|
-
if is_boundary && scanner.match?(/\s+=/n)
|
|
134
|
-
raise EOFError, "whitespace between boundary parameter name and equal sign"
|
|
135
|
-
end
|
|
136
|
-
raise EOFError, "invalid parameter in multipart content type"
|
|
137
|
-
end
|
|
138
|
-
|
|
139
|
-
if scanner.scan(/"((?:[\t \x21\x23-\x5B\x5D-\x7E\x80-\xFF]|\\[\t \x21-\x7E\x80-\xFF])*)"/n)
|
|
140
|
-
value = scanner[1]
|
|
141
|
-
value.gsub!(/\\(.)/mn, '\1')
|
|
142
|
-
elsif !(value = scanner.scan(token))
|
|
143
|
-
raise EOFError, "invalid parameter value in multipart content type"
|
|
144
|
-
end
|
|
145
|
-
|
|
146
|
-
next unless is_boundary || is_rfc2231_boundary
|
|
147
|
-
|
|
148
|
-
if boundary_seen
|
|
149
|
-
raise EOFError, "multiple boundary parameters found in multipart content type"
|
|
150
|
-
end
|
|
151
|
-
boundary_seen = true
|
|
152
|
-
|
|
153
|
-
next unless is_boundary
|
|
154
|
-
|
|
155
|
-
if value.empty?
|
|
156
|
-
raise EOFError, "empty boundary in multipart content type"
|
|
157
|
-
end
|
|
158
|
-
|
|
159
|
-
boundary = value
|
|
160
|
-
end
|
|
161
|
-
|
|
162
|
-
boundary
|
|
84
|
+
return unless content_type
|
|
85
|
+
data = content_type.match(MULTIPART)
|
|
86
|
+
return unless data
|
|
87
|
+
data[1]
|
|
163
88
|
end
|
|
164
89
|
|
|
165
90
|
def self.parse(io, content_length, content_type, tmpfile, bufsize, qp)
|
|
@@ -168,22 +93,17 @@ module Rack
|
|
|
168
93
|
boundary = parse_boundary content_type
|
|
169
94
|
return EMPTY unless boundary
|
|
170
95
|
|
|
171
|
-
if
|
|
172
|
-
|
|
96
|
+
if boundary.length > 70
|
|
97
|
+
# RFC 1521 Section 7.2.1 imposes a 70 character maximum for the boundary.
|
|
98
|
+
# Most clients use no more than 55 characters.
|
|
99
|
+
raise Error, "multipart boundary size too large (#{boundary.length} characters)"
|
|
173
100
|
end
|
|
174
101
|
|
|
175
102
|
io = BoundedIO.new(io, content_length) if content_length
|
|
176
|
-
outbuf = String.new
|
|
177
103
|
|
|
178
104
|
parser = new(boundary, tmpfile, bufsize, qp)
|
|
179
|
-
parser.
|
|
180
|
-
|
|
181
|
-
loop do
|
|
182
|
-
break if parser.state == :DONE
|
|
183
|
-
parser.on_read io.read(bufsize, outbuf)
|
|
184
|
-
end
|
|
105
|
+
parser.parse(io)
|
|
185
106
|
|
|
186
|
-
io.rewind
|
|
187
107
|
parser.result
|
|
188
108
|
end
|
|
189
109
|
|
|
@@ -246,7 +166,7 @@ module Rack
|
|
|
246
166
|
|
|
247
167
|
@mime_parts[mime_index] = klass.new(body, head, filename, content_type, name)
|
|
248
168
|
|
|
249
|
-
|
|
169
|
+
check_open_files
|
|
250
170
|
end
|
|
251
171
|
|
|
252
172
|
def on_mime_body(mime_index, content)
|
|
@@ -258,23 +178,13 @@ module Rack
|
|
|
258
178
|
|
|
259
179
|
private
|
|
260
180
|
|
|
261
|
-
def
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
if file_limit && file_limit > 0
|
|
266
|
-
if @open_files >= file_limit
|
|
181
|
+
def check_open_files
|
|
182
|
+
if Utils.multipart_part_limit > 0
|
|
183
|
+
if @open_files >= Utils.multipart_part_limit
|
|
267
184
|
@mime_parts.each(&:close)
|
|
268
185
|
raise MultipartPartLimitError, 'Maximum file multiparts in content reached'
|
|
269
186
|
end
|
|
270
187
|
end
|
|
271
|
-
|
|
272
|
-
if part_limit && part_limit > 0
|
|
273
|
-
if @mime_parts.size >= part_limit
|
|
274
|
-
@mime_parts.each(&:close)
|
|
275
|
-
raise MultipartTotalPartLimitError, 'Maximum total multiparts in content reached'
|
|
276
|
-
end
|
|
277
|
-
end
|
|
278
188
|
end
|
|
279
189
|
end
|
|
280
190
|
|
|
@@ -283,42 +193,46 @@ module Rack
|
|
|
283
193
|
def initialize(boundary, tempfile, bufsize, query_parser)
|
|
284
194
|
@query_parser = query_parser
|
|
285
195
|
@params = query_parser.make_params
|
|
286
|
-
@boundary = "--#{boundary}"
|
|
287
196
|
@bufsize = bufsize
|
|
288
197
|
|
|
289
|
-
@full_boundary = @boundary
|
|
290
|
-
@end_boundary = @boundary + '--'
|
|
291
198
|
@state = :FAST_FORWARD
|
|
292
199
|
@mime_index = 0
|
|
293
|
-
@body_retained = nil
|
|
294
|
-
@retained_size = 0
|
|
295
|
-
@total_bytes_read = (0 if PARSER_BYTESIZE_LIMIT)
|
|
296
200
|
@collector = Collector.new tempfile
|
|
297
201
|
|
|
298
202
|
@sbuf = StringScanner.new("".dup)
|
|
299
|
-
@body_regex = /(?:#{EOL})
|
|
300
|
-
@
|
|
301
|
-
@rx_max_size = EOL.size + @boundary.bytesize + [EOL.size, '--'.size].max
|
|
203
|
+
@body_regex = /(?:#{EOL}|\A)--#{Regexp.quote(boundary)}(?:#{EOL}|--)/m
|
|
204
|
+
@rx_max_size = boundary.bytesize + 6 # (\r\n-- at start, either \r\n or -- at finish)
|
|
302
205
|
@head_regex = /(.*?#{EOL})#{EOL}/m
|
|
303
206
|
end
|
|
304
207
|
|
|
305
|
-
def
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
208
|
+
def parse(io)
|
|
209
|
+
outbuf = String.new
|
|
210
|
+
read_data(io, outbuf)
|
|
211
|
+
|
|
212
|
+
loop do
|
|
213
|
+
status =
|
|
214
|
+
case @state
|
|
215
|
+
when :FAST_FORWARD
|
|
216
|
+
handle_fast_forward
|
|
217
|
+
when :CONSUME_TOKEN
|
|
218
|
+
handle_consume_token
|
|
219
|
+
when :MIME_HEAD
|
|
220
|
+
handle_mime_head
|
|
221
|
+
when :MIME_BODY
|
|
222
|
+
handle_mime_body
|
|
223
|
+
else # when :DONE
|
|
224
|
+
return
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
read_data(io, outbuf) if status == :want_read
|
|
312
228
|
end
|
|
313
|
-
@sbuf.concat content
|
|
314
|
-
run_parser
|
|
315
229
|
end
|
|
316
230
|
|
|
317
231
|
def result
|
|
318
232
|
@collector.each do |part|
|
|
319
233
|
part.get_data do |data|
|
|
320
234
|
tag_multipart_encoding(part.filename, part.content_type, part.name, data)
|
|
321
|
-
@query_parser.normalize_params(@params, part.name, data
|
|
235
|
+
@query_parser.normalize_params(@params, part.name, data)
|
|
322
236
|
end
|
|
323
237
|
end
|
|
324
238
|
MultipartInfo.new @params.to_params_hash, @collector.find_all(&:file?).map(&:body)
|
|
@@ -326,40 +240,38 @@ module Rack
|
|
|
326
240
|
|
|
327
241
|
private
|
|
328
242
|
|
|
329
|
-
def
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
break if handle_fast_forward == :want_read
|
|
334
|
-
when :CONSUME_TOKEN
|
|
335
|
-
break if handle_consume_token == :want_read
|
|
336
|
-
when :MIME_HEAD
|
|
337
|
-
break if handle_mime_head == :want_read
|
|
338
|
-
when :MIME_BODY
|
|
339
|
-
break if handle_mime_body == :want_read
|
|
340
|
-
when :DONE
|
|
341
|
-
break
|
|
342
|
-
end
|
|
343
|
-
end
|
|
243
|
+
def dequote(str) # From WEBrick::HTTPUtils
|
|
244
|
+
ret = (/\A"(.*)"\Z/ =~ str) ? $1 : str.dup
|
|
245
|
+
ret.gsub!(/\\(.)/, "\\1")
|
|
246
|
+
ret
|
|
344
247
|
end
|
|
345
248
|
|
|
346
|
-
def
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
@state = :DONE
|
|
352
|
-
elsif tok
|
|
353
|
-
@state = :MIME_HEAD
|
|
354
|
-
else
|
|
355
|
-
raise EOFError, "bad content body" if @sbuf.rest_size >= @bufsize
|
|
356
|
-
|
|
357
|
-
# We raise if we don't find the multipart boundary, to avoid unbounded memory
|
|
358
|
-
# buffering. Note that the actual limit is the higher of 16KB and the buffer size (1MB by default)
|
|
359
|
-
raise EOFError, "multipart boundary not found within limit" if @sbuf.string.bytesize > BOUNDARY_START_LIMIT
|
|
249
|
+
def read_data(io, outbuf)
|
|
250
|
+
content = io.read(@bufsize, outbuf)
|
|
251
|
+
handle_empty_content!(content)
|
|
252
|
+
@sbuf.concat(content)
|
|
253
|
+
end
|
|
360
254
|
|
|
361
|
-
|
|
362
|
-
|
|
255
|
+
# This handles the initial parser state. We read until we find the starting
|
|
256
|
+
# boundary, then we can transition to the next state. If we find the ending
|
|
257
|
+
# boundary, this is an invalid multipart upload, but keep scanning for opening
|
|
258
|
+
# boundary in that case. If no boundary found, we need to keep reading data
|
|
259
|
+
# and retry. It's highly unlikely the initial read will not consume the
|
|
260
|
+
# boundary. The client would have to deliberately craft a response
|
|
261
|
+
# with the opening boundary beyond the buffer size for that to happen.
|
|
262
|
+
def handle_fast_forward
|
|
263
|
+
while true
|
|
264
|
+
case consume_boundary
|
|
265
|
+
when :BOUNDARY
|
|
266
|
+
# found opening boundary, transition to next state
|
|
267
|
+
@state = :MIME_HEAD
|
|
268
|
+
return
|
|
269
|
+
when :END_BOUNDARY
|
|
270
|
+
# invalid multipart upload, but retry for opening boundary
|
|
271
|
+
else
|
|
272
|
+
# no boundary found, keep reading data
|
|
273
|
+
return :want_read
|
|
274
|
+
end
|
|
363
275
|
end
|
|
364
276
|
end
|
|
365
277
|
|
|
@@ -376,47 +288,29 @@ module Rack
|
|
|
376
288
|
def handle_mime_head
|
|
377
289
|
if @sbuf.scan_until(@head_regex)
|
|
378
290
|
head = @sbuf[1]
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
content_type = unfolded_head[MULTIPART_CONTENT_TYPE, 1]
|
|
383
|
-
if name = unfolded_head[MULTIPART_CONTENT_DISPOSITION, 1]
|
|
384
|
-
name = Rack::Auth::Digest::Params::dequote(name)
|
|
291
|
+
content_type = head[MULTIPART_CONTENT_TYPE, 1]
|
|
292
|
+
if name = head[MULTIPART_CONTENT_DISPOSITION, 1]
|
|
293
|
+
name = dequote(name)
|
|
385
294
|
else
|
|
386
|
-
name =
|
|
295
|
+
name = head[MULTIPART_CONTENT_ID, 1]
|
|
387
296
|
end
|
|
388
297
|
|
|
389
|
-
filename = get_filename(
|
|
298
|
+
filename = get_filename(head)
|
|
390
299
|
|
|
391
300
|
if name.nil? || name.empty?
|
|
392
301
|
name = filename || "#{content_type || TEXT_PLAIN}[]".dup
|
|
393
302
|
end
|
|
394
303
|
|
|
395
|
-
# Mime part head data is retained for both TempfilePart and BufferPart
|
|
396
|
-
# for the entireity of the parse, even though it isn't used for BufferPart.
|
|
397
|
-
update_retained_size(head.bytesize)
|
|
398
|
-
|
|
399
|
-
# If a filename is given, a TempfilePart will be used, so the body will
|
|
400
|
-
# not be buffered in memory. However, if a filename is not given, a BufferPart
|
|
401
|
-
# will be used, and the body will be buffered in memory.
|
|
402
|
-
@body_retained = !filename
|
|
403
|
-
|
|
404
304
|
@collector.on_mime_head @mime_index, head, filename, content_type, name
|
|
405
305
|
@state = :MIME_BODY
|
|
406
306
|
else
|
|
407
|
-
|
|
408
|
-
# buffering. Note that the actual limit is the higher of 64KB and the buffer size (1MB by default)
|
|
409
|
-
raise EOFError, "multipart mime part header too large" if @sbuf.rest.bytesize > MIME_HEADER_BYTESIZE_LIMIT
|
|
410
|
-
|
|
411
|
-
@sbuf.string = @sbuf.rest
|
|
412
|
-
return :want_read
|
|
307
|
+
:want_read
|
|
413
308
|
end
|
|
414
309
|
end
|
|
415
310
|
|
|
416
311
|
def handle_mime_body
|
|
417
312
|
if (body_with_boundary = @sbuf.check_until(@body_regex)) # check but do not advance the pointer yet
|
|
418
313
|
body = body_with_boundary.sub(/#{@body_regex}\z/m, '') # remove the boundary from the string
|
|
419
|
-
update_retained_size(body.bytesize) if @body_retained
|
|
420
314
|
@collector.on_mime_body @mime_index, body
|
|
421
315
|
@sbuf.pos += body.length + 2 # skip \r\n after the content
|
|
422
316
|
@state = :CONSUME_TOKEN
|
|
@@ -425,51 +319,24 @@ module Rack
|
|
|
425
319
|
# Save what we have so far
|
|
426
320
|
if @rx_max_size < @sbuf.rest_size
|
|
427
321
|
delta = @sbuf.rest_size - @rx_max_size
|
|
428
|
-
|
|
429
|
-
update_retained_size(body.bytesize) if @body_retained
|
|
430
|
-
@collector.on_mime_body @mime_index, body
|
|
322
|
+
@collector.on_mime_body @mime_index, @sbuf.peek(delta)
|
|
431
323
|
@sbuf.pos += delta
|
|
324
|
+
@sbuf.string = @sbuf.rest
|
|
432
325
|
end
|
|
433
|
-
@sbuf.string = @sbuf.rest
|
|
434
326
|
:want_read
|
|
435
327
|
end
|
|
436
328
|
end
|
|
437
329
|
|
|
438
|
-
def full_boundary; @full_boundary; end
|
|
439
|
-
|
|
440
|
-
def update_retained_size(size)
|
|
441
|
-
@retained_size += size
|
|
442
|
-
if @retained_size > BUFFERED_UPLOAD_BYTESIZE_LIMIT
|
|
443
|
-
raise EOFError, "multipart data over retained size limit"
|
|
444
|
-
end
|
|
445
|
-
end
|
|
446
|
-
|
|
447
330
|
# Scan until the we find the start or end of the boundary.
|
|
448
331
|
# If we find it, return the appropriate symbol for the start or
|
|
449
332
|
# end of the boundary. If we don't find the start or end of the
|
|
450
333
|
# boundary, clear the buffer and return nil.
|
|
451
334
|
def consume_boundary
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
when full_boundary then return :BOUNDARY
|
|
455
|
-
when @end_boundary then return :END_BOUNDARY
|
|
456
|
-
end
|
|
457
|
-
return if @sbuf.eos?
|
|
458
|
-
end
|
|
459
|
-
end
|
|
460
|
-
|
|
461
|
-
# Return the next line of the buffer including its trailing "\n", or the
|
|
462
|
-
# rest of the buffer if it contains no "\n", and advance past it. This is
|
|
463
|
-
# equivalent to `@sbuf.scan_until(BOUNDARY_REGEX)`, but searching for the
|
|
464
|
-
# "\n" directly avoids the regexp engine's backtracking stack, which grows
|
|
465
|
-
# by one entry per byte matched by `[^\n]*` (tens of MB for a 1 MB line).
|
|
466
|
-
def scan_line
|
|
467
|
-
if line = @sbuf.scan_until(LINE_END)
|
|
468
|
-
line
|
|
335
|
+
if read_buffer = @sbuf.scan_until(@body_regex)
|
|
336
|
+
read_buffer.end_with?(EOL) ? :BOUNDARY : :END_BOUNDARY
|
|
469
337
|
else
|
|
470
|
-
line = @sbuf.rest
|
|
471
338
|
@sbuf.terminate
|
|
472
|
-
|
|
339
|
+
nil
|
|
473
340
|
end
|
|
474
341
|
end
|
|
475
342
|
|
|
@@ -479,10 +346,10 @@ module Rack
|
|
|
479
346
|
when RFC2183
|
|
480
347
|
params = Hash[*head.scan(DISPPARM).flat_map(&:compact)]
|
|
481
348
|
|
|
482
|
-
if filename = params['filename']
|
|
483
|
-
filename = $1 if filename =~ /^"(.*)"$/
|
|
484
|
-
elsif filename = params['filename*']
|
|
349
|
+
if filename = params['filename*']
|
|
485
350
|
encoding, _, filename = filename.split("'", 3)
|
|
351
|
+
elsif filename = params['filename']
|
|
352
|
+
filename = $1 if filename =~ /^"(.*)"$/
|
|
486
353
|
end
|
|
487
354
|
when BROKEN
|
|
488
355
|
filename = $1
|
|
@@ -509,6 +376,7 @@ module Rack
|
|
|
509
376
|
end
|
|
510
377
|
|
|
511
378
|
CHARSET = "charset"
|
|
379
|
+
deprecate_constant :CHARSET
|
|
512
380
|
|
|
513
381
|
def tag_multipart_encoding(filename, content_type, name, body)
|
|
514
382
|
name = name.to_s
|
|
@@ -529,7 +397,13 @@ module Rack
|
|
|
529
397
|
k.strip!
|
|
530
398
|
v.strip!
|
|
531
399
|
v = v[1..-2] if v.start_with?('"') && v.end_with?('"')
|
|
532
|
-
|
|
400
|
+
if k == "charset"
|
|
401
|
+
encoding = begin
|
|
402
|
+
Encoding.find v
|
|
403
|
+
rescue ArgumentError
|
|
404
|
+
Encoding::BINARY
|
|
405
|
+
end
|
|
406
|
+
end
|
|
533
407
|
end
|
|
534
408
|
end
|
|
535
409
|
end
|
|
@@ -540,7 +414,7 @@ module Rack
|
|
|
540
414
|
|
|
541
415
|
def handle_empty_content!(content)
|
|
542
416
|
if content.nil? || content.empty?
|
|
543
|
-
raise
|
|
417
|
+
raise EmptyContentError
|
|
544
418
|
end
|
|
545
419
|
end
|
|
546
420
|
end
|
data/lib/rack/multipart.rb
CHANGED
|
@@ -1,66 +1,44 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require_relative 'constants'
|
|
4
|
+
require_relative 'utils'
|
|
5
|
+
|
|
3
6
|
require_relative 'multipart/parser'
|
|
7
|
+
require_relative 'multipart/generator'
|
|
4
8
|
|
|
5
9
|
module Rack
|
|
6
10
|
# A multipart form data parser, adapted from IOWA.
|
|
7
11
|
#
|
|
8
12
|
# Usually, Rack::Request#POST takes care of calling this.
|
|
9
13
|
module Multipart
|
|
10
|
-
autoload :UploadedFile, 'rack/multipart/uploaded_file'
|
|
11
|
-
autoload :Generator, 'rack/multipart/generator'
|
|
12
|
-
|
|
13
|
-
EOL = "\r\n"
|
|
14
14
|
MULTIPART_BOUNDARY = "AaB03x"
|
|
15
|
-
MULTIPART = %r|\Amultipart/.*?boundary(\s*)=\"?([^\";,]+)\"?|ni
|
|
16
|
-
TOKEN = /[^\s()<>,;:\\"\/\[\]?=]+/
|
|
17
|
-
CONDISP = /Content-Disposition:\s*#{TOKEN}\s*/i
|
|
18
|
-
# Quoted strings may not contain a raw CR or LF. Folded header lines are
|
|
19
|
-
# unfolded before matching, so any remaining line break is a header terminator.
|
|
20
|
-
VALUE = /"(?:\\"|[^"\r\n])*"|#{TOKEN}/
|
|
21
|
-
BROKEN = /^#{CONDISP}.*;\s*filename=(#{VALUE})/i
|
|
22
|
-
MULTIPART_CONTENT_TYPE = /Content-Type: ([^\r\n]*)/ni
|
|
23
|
-
MULTIPART_CONTENT_DISPOSITION = /Content-Disposition:[^:]*;\s*name=(#{VALUE})/ni
|
|
24
|
-
MULTIPART_CONTENT_ID = /Content-ID:\s*([^#{EOL}]*)/ni
|
|
25
|
-
# Updated definitions from RFC 2231
|
|
26
|
-
ATTRIBUTE_CHAR = %r{[^ \x00-\x1f\x7f)(><@,;:\\"/\[\]?='*%]}
|
|
27
|
-
ATTRIBUTE = /#{ATTRIBUTE_CHAR}+/
|
|
28
|
-
SECTION = /\*[0-9]+/
|
|
29
|
-
REGULAR_PARAMETER_NAME = /#{ATTRIBUTE}#{SECTION}?/
|
|
30
|
-
REGULAR_PARAMETER = /(#{REGULAR_PARAMETER_NAME})=(#{VALUE})/
|
|
31
|
-
EXTENDED_OTHER_NAME = /#{ATTRIBUTE}\*[1-9][0-9]*\*/
|
|
32
|
-
EXTENDED_OTHER_VALUE = /%[0-9a-fA-F]{2}|#{ATTRIBUTE_CHAR}/
|
|
33
|
-
EXTENDED_OTHER_PARAMETER = /(#{EXTENDED_OTHER_NAME})=(#{EXTENDED_OTHER_VALUE}*)/
|
|
34
|
-
EXTENDED_INITIAL_NAME = /#{ATTRIBUTE}(?:\*0)?\*/
|
|
35
|
-
EXTENDED_INITIAL_VALUE = /[a-zA-Z0-9\-]*'[a-zA-Z0-9\-]*'#{EXTENDED_OTHER_VALUE}*/
|
|
36
|
-
EXTENDED_INITIAL_PARAMETER = /(#{EXTENDED_INITIAL_NAME})=(#{EXTENDED_INITIAL_VALUE})/
|
|
37
|
-
EXTENDED_PARAMETER = /#{EXTENDED_INITIAL_PARAMETER}|#{EXTENDED_OTHER_PARAMETER}/
|
|
38
|
-
DISPPARM = /;\s*(?:#{REGULAR_PARAMETER}|#{EXTENDED_PARAMETER})\s*/
|
|
39
|
-
RFC2183 = /^#{CONDISP}(#{DISPPARM})+$/i
|
|
40
15
|
|
|
41
16
|
class << self
|
|
42
17
|
def parse_multipart(env, params = Rack::Utils.default_query_parser)
|
|
43
|
-
|
|
44
|
-
|
|
18
|
+
io = env[RACK_INPUT]
|
|
19
|
+
|
|
20
|
+
if content_length = env['CONTENT_LENGTH']
|
|
21
|
+
content_length = content_length.to_i
|
|
22
|
+
end
|
|
45
23
|
|
|
46
|
-
|
|
47
|
-
io = req.get_header(RACK_INPUT)
|
|
48
|
-
io.rewind
|
|
49
|
-
content_length = req.content_length
|
|
50
|
-
content_length = content_length.to_i if content_length
|
|
24
|
+
content_type = env['CONTENT_TYPE']
|
|
51
25
|
|
|
52
|
-
tempfile =
|
|
53
|
-
bufsize =
|
|
26
|
+
tempfile = env[RACK_MULTIPART_TEMPFILE_FACTORY] || Parser::TEMPFILE_FACTORY
|
|
27
|
+
bufsize = env[RACK_MULTIPART_BUFFER_SIZE] || Parser::BUFSIZE
|
|
54
28
|
|
|
55
|
-
info = Parser.parse
|
|
56
|
-
|
|
57
|
-
|
|
29
|
+
info = Parser.parse(io, content_length, content_type, tempfile, bufsize, params)
|
|
30
|
+
env[RACK_TEMPFILES] = info.tmp_files
|
|
31
|
+
|
|
32
|
+
return info.params
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def extract_multipart(request, params = Rack::Utils.default_query_parser)
|
|
36
|
+
parse_multipart(request.env)
|
|
58
37
|
end
|
|
59
38
|
|
|
60
39
|
def build_multipart(params, first = true)
|
|
61
40
|
Generator.new(params, first).dump
|
|
62
41
|
end
|
|
63
42
|
end
|
|
64
|
-
|
|
65
43
|
end
|
|
66
44
|
end
|
data/lib/rack/null_logger.rb
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require_relative 'constants'
|
|
4
|
+
|
|
3
5
|
module Rack
|
|
4
6
|
class NullLogger
|
|
5
7
|
def initialize(app)
|
|
@@ -22,6 +24,11 @@ module Rack
|
|
|
22
24
|
def warn? ; end
|
|
23
25
|
def error? ; end
|
|
24
26
|
def fatal? ; end
|
|
27
|
+
def debug! ; end
|
|
28
|
+
def error! ; end
|
|
29
|
+
def fatal! ; end
|
|
30
|
+
def info! ; end
|
|
31
|
+
def warn! ; end
|
|
25
32
|
def level ; end
|
|
26
33
|
def progname ; end
|
|
27
34
|
def datetime_format ; end
|
|
@@ -34,6 +41,8 @@ module Rack
|
|
|
34
41
|
def sev_threshold=(sev_threshold); end
|
|
35
42
|
def close ; end
|
|
36
43
|
def add(severity, message = nil, progname = nil, &block); end
|
|
44
|
+
def log(severity, message = nil, progname = nil, &block); end
|
|
37
45
|
def <<(msg); end
|
|
46
|
+
def reopen(logdev = nil); end
|
|
38
47
|
end
|
|
39
48
|
end
|