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 180
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
  @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 66
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

  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 59
def self.parse_boundary(content_type)
  return unless content_type
  data = content_type.match(MULTIPART)
  return unless data
  data[1]
end

Public Instance Methods

# File lib/rack/multipart/parser.rb, line 199
def on_read(content)
  handle_empty_content!(content)
  @sbuf.concat content
  run_parser
end
# File lib/rack/multipart/parser.rb, line 205
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