Class: Rack::Multipart::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/multipart/parser.rb

Defined Under Namespace

Classes: BoundedIO, Collector, MultipartInfo

Constant Summary collapse

BUFSIZE =
1_048_576
TEXT_PLAIN =
"text/plain"
TEMPFILE_FACTORY =
lambda { |filename, content_type|
  Tempfile.new(["RackMultipart", ::File.extname(filename.gsub("\0", '%00'))])
}
BOUNDARY_REGEX =
/\A([^\n]*(?:\n|\Z))/
EMPTY =
MultipartInfo.new(nil, [])

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(boundary, tempfile, bufsize, query_parser) ⇒ Parser

Returns a new instance of Parser.


161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/rack/multipart/parser.rb', line 161

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
  @rx_max_size = EOL.size + @boundary.bytesize + [EOL.size, '--'.size].max
  @head_regex = /(.*?#{EOL})#{EOL}/m
end

Instance Attribute Details

#stateObject (readonly)

Returns the value of attribute state


159
160
161
# File 'lib/rack/multipart/parser.rb', line 159

def state
  @state
end

Class Method Details

.parse(io, content_length, content_type, tmpfile, bufsize, qp) ⇒ Object


63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rack/multipart/parser.rb', line 63

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

  parser = new(boundary, tmpfile, bufsize, qp)
  parser.parse(io)

  io.rewind
  parser.result
end

.parse_boundary(content_type) ⇒ Object


56
57
58
59
60
61
# File 'lib/rack/multipart/parser.rb', line 56

def self.parse_boundary(content_type)
  return unless content_type
  data = content_type.match(MULTIPART)
  return unless data
  data[1]
end

Instance Method Details

#parse(io) ⇒ Object


179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/rack/multipart/parser.rb', line 179

def parse(io)
  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
      when :DONE
        return
      end

    read_data(io, outbuf) if status == :want_read
  end
end

#resultObject


202
203
204
205
206
207
208
209
210
# File 'lib/rack/multipart/parser.rb', line 202

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