Module: Rack::Multipart

Defined in:
lib/rack/multipart.rb,
lib/rack/multipart/parser.rb,
lib/rack/multipart/generator.rb,
lib/rack/multipart/uploaded_file.rb

Overview

A multipart form data parser, adapted from IOWA.

Usually, Rack::Request#POST takes care of calling this.

Defined Under Namespace

Classes: EmptyContentError, Error, Generator, MultipartPartLimitError, Parser, UploadedFile

Constant Summary collapse

MULTIPART_BOUNDARY =
"AaB03x"
EOL =
"\r\n"
MULTIPART =
%r|\Amultipart/.*boundary=\"?([^\";,]+)\"?|ni
TOKEN =
/[^\s()<>,;:\\"\/\[\]?=]+/
CONDISP =
/Content-Disposition:\s*#{TOKEN}\s*/i
VALUE =
/"(?:\\"|[^"])*"|#{TOKEN}/
BROKEN =
/^#{CONDISP}.*;\s*filename=(#{VALUE})/i
MULTIPART_CONTENT_TYPE =
/Content-Type: (.*)#{EOL}/ni
MULTIPART_CONTENT_DISPOSITION =
/Content-Disposition:.*;\s*name=(#{VALUE})/ni
MULTIPART_CONTENT_ID =
/Content-ID:\s*([^#{EOL}]*)/ni
ATTRIBUTE_CHAR =

Updated definitions from RFC 2231

%r{[^ \t\v\n\r)(><@,;:\\"/\[\]?='*%]}
ATTRIBUTE =
/#{ATTRIBUTE_CHAR}+/
SECTION =
/\*[0-9]+/
REGULAR_PARAMETER_NAME =
/#{ATTRIBUTE}#{SECTION}?/
REGULAR_PARAMETER =
/(#{REGULAR_PARAMETER_NAME})=(#{VALUE})/
EXTENDED_OTHER_NAME =
/#{ATTRIBUTE}\*[1-9][0-9]*\*/
EXTENDED_OTHER_VALUE =
/%[0-9a-fA-F]{2}|#{ATTRIBUTE_CHAR}/
EXTENDED_OTHER_PARAMETER =
/(#{EXTENDED_OTHER_NAME})=(#{EXTENDED_OTHER_VALUE}*)/
EXTENDED_INITIAL_NAME =
/#{ATTRIBUTE}(?:\*0)?\*/
EXTENDED_INITIAL_VALUE =
/[a-zA-Z0-9\-]*'[a-zA-Z0-9\-]*'#{EXTENDED_OTHER_VALUE}*/
EXTENDED_INITIAL_PARAMETER =
/(#{EXTENDED_INITIAL_NAME})=(#{EXTENDED_INITIAL_VALUE})/
EXTENDED_PARAMETER =
/#{EXTENDED_INITIAL_PARAMETER}|#{EXTENDED_OTHER_PARAMETER}/
DISPPARM =
/;\s*(?:#{REGULAR_PARAMETER}|#{EXTENDED_PARAMETER})\s*/
RFC2183 =
/^#{CONDISP}(#{DISPPARM})+$/i

Class Method Summary collapse

Class Method Details

.build_multipart(params, first = true) ⇒ Object


39
40
41
# File 'lib/rack/multipart.rb', line 39

def build_multipart(params, first = true)
  Generator.new(params, first).dump
end

.extract_multipart(request, params = Rack::Utils.default_query_parser) ⇒ Object


35
36
37
# File 'lib/rack/multipart.rb', line 35

def extract_multipart(request, params = Rack::Utils.default_query_parser)
  parse_multipart(request.env)
end

.parse_multipart(env, params = Rack::Utils.default_query_parser) ⇒ Object


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rack/multipart.rb', line 17

def parse_multipart(env, params = Rack::Utils.default_query_parser)
  io = env[RACK_INPUT]

  if content_length = env['CONTENT_LENGTH']
    content_length = content_length.to_i
  end

  content_type = env['CONTENT_TYPE']

  tempfile = env[RACK_MULTIPART_TEMPFILE_FACTORY] || Parser::TEMPFILE_FACTORY
  bufsize = env[RACK_MULTIPART_BUFFER_SIZE] || Parser::BUFSIZE

  info = Parser.parse(io, content_length, content_type, tempfile, bufsize, params)
  env[RACK_TEMPFILES] = info.tmp_files

  return info.params
end