class Rack::Multipart::Parser
Rack::Multipart::Parser handles parsing of multipart/form-data requests.
File Parameter Contents
When processing file uploads, the parser returns a hash containing information about uploaded files. For file parameters, the hash includes:
-
:filename- The original filename, already URL decoded by the parser -
:type- The content type of the uploaded file -
:name- The parameter name from the form -
:tempfile- ATempfileobject containing the uploaded data -
:head- The raw header content for this part
Constants
- BUFSIZE
- CHARSET
- CONTENT_DISPOSITION_MAX_BYTES
- CONTENT_DISPOSITION_MAX_PARAMS
- EMPTY
- MultipartInfo
- REENCODE_DUMMY_ENCODINGS
- TEMPFILE_FACTORY
- TEXT_PLAIN
Attributes
Public Class Methods
Source
# File lib/rack/multipart/parser.rb, line 209 def self.default_bufsize BUFSIZE end
Source
# File lib/rack/multipart/parser.rb, line 213 def self.default_query_parser Rack::Utils.default_query_parser end
Source
# File lib/rack/multipart/parser.rb, line 205 def self.default_tempfile_factory TEMPFILE_FACTORY end
# File lib/rack/multipart/parser.rb, line 360 def initialize(boundary, tempfile, bufsize, query_parser) @query_parser = query_parser @params = query_parser.make_params @bufsize = bufsize @state = :FAST_FORWARD @mime_index = 0 @body_retained = nil @retained_size = 0 @total_bytes_read = (0 if PARSER_BYTESIZE_LIMIT) @content_disposition_quoted_escapes = 0 @collector = Collector.new tempfile @sbuf = StringScanner.new("".b) @body_regex = /(?:#{EOL}|\A)--#{Regexp.quote(boundary)}(?:#{EOL}|--)/m @body_regex_at_end = /#{@body_regex}\z/m @end_boundary_size = boundary.bytesize + 4 # (-- at start, -- at finish) @rx_max_size = boundary.bytesize + 6 # (\r\n-- at start, either \r\n or -- at finish) @head_regex = /(.*?#{EOL})#{EOL}/m end
# File lib/rack/multipart/parser.rb, line 237 def self.parse(io, content_length, content_type, tmpfile, bufsize, qp) return EMPTY if 0 == content_length boundary = parse_boundary content_type return EMPTY unless boundary if PARSER_BYTESIZE_LIMIT && content_length && content_length > PARSER_BYTESIZE_LIMIT raise Error, "multipart Content-Length #{content_length} exceeds limit of #{PARSER_BYTESIZE_LIMIT} bytes" end if boundary.length > 70 # RFC 1521 Section 7.2.1 imposes a 70 character maximum for the boundary. # Most clients use no more than 55 characters. raise BoundaryTooLongError, "multipart boundary size too large (#{boundary.length} characters)" end io = BoundedIO.new(io, content_length) if content_length parser = new(boundary, tmpfile, bufsize, qp) parser.parse(io) parser.result end
# File lib/rack/multipart/parser.rb, line 142 def self.parse_boundary(content_type) return unless content_type && content_type.match?(/\Amultipart\//ni) scanner = StringScanner.new(content_type.b) return unless scanner.skip(/multipart\/[!#$%&'*+\-.^_`|~0-9A-Za-z]+/ni) boundary = nil boundary_seen = false ows = /[ \t]*/n # From RFC 9110 Section 5.6.2 token = /[!#$%&'*+\-.^_`|~0-9A-Za-z]+/n loop do scanner.skip(ows) break if scanner.eos? unless scanner.skip(/;/n) raise Error, "invalid parameters in multipart content type" end scanner.skip(ows) # Empty parameter, such as in "a=b;;c=d" or a trailing ";". next unless name = scanner.scan(token) is_boundary = name.casecmp?("boundary") # RFC 2231 forms such as boundary*, boundary*0, or boundary*0*. Rack # does not use them, but some parsers (for example Go's # mime.ParseMediaType) prefer them over a plain boundary parameter, # so treat them as boundary parameters when checking for duplicates. is_rfc2231_boundary = !is_boundary && name.match?(/\Aboundary\*/ni) unless scanner.skip(/=/n) if is_boundary && scanner.match?(/\s+=/n) raise Error, "whitespace between boundary parameter name and equal sign" end raise Error, "invalid parameter in multipart content type" end if scanner.scan(/"((?:[\t \x21\x23-\x5B\x5D-\x7E\x80-\xFF]|\\[\t \x21-\x7E\x80-\xFF])*)"/n) value = scanner[1] value.gsub!(/\\(.)/mn, '\1') elsif !(value = scanner.scan(token)) raise Error, "invalid parameter value in multipart content type" end next unless is_boundary || is_rfc2231_boundary if boundary_seen raise BoundaryTooLongError, "multiple boundary parameters found in multipart content type" end boundary_seen = true next unless is_boundary if value.empty? raise Error, "empty boundary in multipart content type" end boundary = value end boundary end
Parse the boundary parameter from a multipart content type, following the media type grammar in RFC 9110 Section 8.3.1:
media-type = type "/" subtype parameters parameters = *( OWS ";" OWS [ parameter ] ) parameter = parameter-name "=" parameter-value parameter-value = ( token / quoted-string )
Returns nil if the content type is not multipart or has no boundary parameter. Raises Error if the parameters are malformed, the boundary is empty, or there is more than one boundary parameter.
# File lib/rack/multipart/parser.rb, line 217 def self.parse_from_env(env, query_parser: default_query_parser) unless io = env[RACK_INPUT] raise MissingInputError, "Missing input stream!" end if content_length = env['CONTENT_LENGTH'] content_length = content_length.to_i end content_type = env['CONTENT_TYPE'] tempfile = env[RACK_MULTIPART_TEMPFILE_FACTORY] || default_tempfile_factory bufsize = env[RACK_MULTIPART_BUFFER_SIZE] || default_bufsize info = parse(io, content_length, content_type, tempfile, bufsize, query_parser) env[RACK_TEMPFILES] = info.tmp_files return info.params end
Public Instance Methods
Source
# File lib/rack/multipart/parser.rb, line 381 def parse(io) @total_bytes_read &&= nil if io.is_a?(BoundedIO) outbuf = String.new read_data(io, outbuf) loop do status = case @state when :FAST_FORWARD handle_fast_forward when :CONSUME_TOKEN handle_consume_token when :MIME_HEAD handle_mime_head when :MIME_BODY handle_mime_body else # when :DONE return end read_data(io, outbuf) if status == :want_read end end
Source
# File lib/rack/multipart/parser.rb, line 405 def result @collector.each do |part| part.get_data do |data| tag_multipart_encoding(part.filename, part.content_type, part.name, data) name, data = handle_dummy_encoding(part.name, data) @query_parser.normalize_params(@params, name, data) end end MultipartInfo.new @params.to_params_hash, @collector.find_all(&:file?).map(&:body) end