Class: Rack::Multipart::Parser
- Inherits:
-
Object
- Object
- Rack::Multipart::Parser
- Defined in:
- lib/rack/multipart/parser.rb
Overview
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- A Tempfile object containing the uploaded data:head- The raw header content for this part
Defined Under Namespace
Classes: BoundedIO, Collector, MultipartInfo
Constant Summary collapse
- BUFSIZE =
1_048_576- TEXT_PLAIN =
"text/plain"- TEMPFILE_FACTORY =
lambda { |filename, content_type| extension = ::File.extname(filename.gsub("\0", '%00'))[0, 129] Tempfile.new(["RackMultipart", extension]) }
- EMPTY =
MultipartInfo.new(nil, [])
Instance Attribute Summary collapse
-
#state ⇒ Object
readonly
Returns the value of attribute state.
Class Method Summary collapse
- .parse(io, content_length, content_type, tmpfile, bufsize, qp) ⇒ Object
-
.parse_boundary(content_type) ⇒ Object
Parse the boundary parameter from a multipart content type, following the media type grammar in RFC 9110 Section 8.3.1:.
Instance Method Summary collapse
-
#initialize(boundary, tempfile, bufsize, query_parser) ⇒ Parser
constructor
A new instance of Parser.
- #parse(io) ⇒ Object
- #result ⇒ Object
Constructor Details
#initialize(boundary, tempfile, bufsize, query_parser) ⇒ Parser
Returns a new instance of Parser.
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/rack/multipart/parser.rb', line 314 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("".dup) @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 |
Instance Attribute Details
#state ⇒ Object (readonly)
Returns the value of attribute state.
312 313 314 |
# File 'lib/rack/multipart/parser.rb', line 312 def state @state end |
Class Method Details
.parse(io, content_length, content_type, tmpfile, bufsize, qp) ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/rack/multipart/parser.rb', line 197 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 |
.parse_boundary(content_type) ⇒ Object
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.
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/rack/multipart/parser.rb', line 134 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 |
Instance Method Details
#parse(io) ⇒ Object
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
# File 'lib/rack/multipart/parser.rb', line 335 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 |
#result ⇒ Object
359 360 361 362 363 364 365 366 367 368 |
# File 'lib/rack/multipart/parser.rb', line 359 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 |