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: Generator, MultipartPartLimitError, Parser, UploadedFile

Constant Summary collapse

EOL =
"\r\n"
MULTIPART_BOUNDARY =
"AaB03x"
MULTIPART =
%r|\Amultipart/.*boundary=\"?([^\";,]+)\"?|ni
TOKEN =
/[^\s()<>,;:\\"\/\[\]?=]+/
CONDISP =
/Content-Disposition:\s*#{TOKEN}\s*/i
VALUE =
/"(?:\\"|[^"])*"|#{TOKEN}/
BROKEN_QUOTED =
/^#{CONDISP}.*;\sfilename="(.*?)"(?:\s*$|\s*;\s*#{TOKEN}=)/i
BROKEN_UNQUOTED =
/^#{CONDISP}.*;\sfilename=(#{TOKEN})/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



57
58
59
# File 'lib/rack/multipart.rb', line 57

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

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



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rack/multipart.rb', line 43

def extract_multipart(req, params = Rack::Utils.default_query_parser)
  io = req.get_header(RACK_INPUT)
  io.rewind
  content_length = req.content_length
  content_length = content_length.to_i if content_length

  tempfile = req.get_header(RACK_MULTIPART_TEMPFILE_FACTORY) || Parser::TEMPFILE_FACTORY
  bufsize = req.get_header(RACK_MULTIPART_BUFFER_SIZE) || Parser::BUFSIZE

  info = Parser.parse io, content_length, req.get_header('CONTENT_TYPE'), tempfile, bufsize, params
  req.set_header(RACK_TEMPFILES, info.tmp_files)
  info.params
end

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



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

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