class Rack::Multipart::Parser
Constants
- BOUNDARY_REGEX
- BUFSIZE
- CHARSET
- EMPTY
- MultipartInfo
- TEMPFILE_FACTORY
- TEXT_PLAIN
Attributes
Public Class Methods
# File lib/rack/multipart/parser.rb, line 283 def initialize(boundary, tempfile, bufsize, query_parser) @query_parser = query_parser @params = query_parser.make_params @boundary = "--#{boundary}" @bufsize = bufsize @full_boundary = @boundary @end_boundary = @boundary + '--' @state = :FAST_FORWARD @mime_index = 0 @body_retained = nil @retained_size = 0 @total_bytes_read = (0 if PARSER_BYTESIZE_LIMIT) @collector = Collector.new tempfile @sbuf = StringScanner.new("".dup) @body_regex = /(?:#{EOL})?#{Regexp.quote(@boundary)}(?:#{EOL}|--)/m @end_boundary_size = boundary.bytesize + 6 # (-- at start, -- at finish, EOL at end) @rx_max_size = EOL.size + @boundary.bytesize + [EOL.size, '--'.size].max @head_regex = /(.*?#{EOL})#{EOL}/m end
# File lib/rack/multipart/parser.rb, line 165 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 EOFError, "multipart Content-Length #{content_length} exceeds limit of #{PARSER_BYTESIZE_LIMIT} bytes" end io = BoundedIO.new(io, content_length) if content_length outbuf = String.new parser = new(boundary, tmpfile, bufsize, qp) parser.on_read io.read(bufsize, outbuf) loop do break if parser.state == :DONE parser.on_read io.read(bufsize, outbuf) end io.rewind parser.result end
# File lib/rack/multipart/parser.rb, line 102 def self.parse_boundary(content_type) return unless content_type && /\Amultipart\//ni.match?(content_type) 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 EOFError, "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") == 0 # 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 && /\Aboundary\*/ni.match?(name) unless scanner.skip(/=/n) if is_boundary && scanner.match?(/\s+=/n) raise EOFError, "whitespace between boundary parameter name and equal sign" end raise EOFError, "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 EOFError, "invalid parameter value in multipart content type" end next unless is_boundary || is_rfc2231_boundary if boundary_seen raise EOFError, "multiple boundary parameters found in multipart content type" end boundary_seen = true next unless is_boundary if value.empty? raise EOFError, "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 EOFError if the parameters are malformed, the boundary is empty, or there is more than one boundary parameter.
Public Instance Methods
Source
# File lib/rack/multipart/parser.rb, line 305 def on_read(content) handle_empty_content!(content) if @total_bytes_read @total_bytes_read += content.bytesize if @total_bytes_read > PARSER_BYTESIZE_LIMIT raise EOFError, "multipart upload exceeds limit of #{PARSER_BYTESIZE_LIMIT} bytes" end end @sbuf.concat content run_parser end
Source
# File lib/rack/multipart/parser.rb, line 317 def result @collector.each do |part| part.get_data do |data| tag_multipart_encoding(part.filename, part.content_type, part.name, data) @query_parser.normalize_params(@params, part.name, data, @query_parser.param_depth_limit) end end MultipartInfo.new @params.to_params_hash, @collector.find_all(&:file?).map(&:body) end