method
read_multipart
v2.1.0 -
Show latest stable
-
0 notes -
Class: ActionController::AbstractRequest
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3 (0)
- 2.1.0 (0)
- 2.2.1 (0)
- 2.3.8
- 3.0.0
- 3.0.9
- 3.1.0
- 3.2.1
- 3.2.8
- 3.2.13
- 4.0.2
- 4.1.8
- 4.2.1
- 4.2.7
- 4.2.9
- 5.0.0.1
- 5.1.7
- 5.2.3
- 6.0.0
- 6.1.3.1
- 6.1.7.7
- 7.0.0
- 7.1.3.2
- 7.1.3.4
- What's this?
read_multipart(body, boundary, body_size, env)
private
Hide source
# File actionpack/lib/action_controller/request.rb, line 523 def read_multipart(body, boundary, body_size, env) params = Hash.new([]) boundary = "--" + boundary quoted_boundary = Regexp.quote(boundary) buf = "" bufsize = 10 * 1024 boundary_end="" # start multipart/form-data body.binmode if defined? body.binmode case body when File body.set_encoding(Encoding::BINARY) if body.respond_to?(:set_encoding) when StringIO body.string.force_encoding(Encoding::BINARY) if body.string.respond_to?(:force_encoding) end boundary_size = boundary.size + EOL.size body_size -= boundary_size status = body.read(boundary_size) if nil == status raise EOFError, "no content body" elsif boundary + EOL != status raise EOFError, "bad content body" end loop do head = nil content = if 10240 < body_size UploadedTempfile.new("CGI") else UploadedStringIO.new end content.binmode if defined? content.binmode until head and /#{quoted_boundary}(?:#{EOL}|--)/n.match(buf) if (not head) and /#{EOL}#{EOL}/n.match(buf) buf = buf.sub(/\A((?:.|\n)*?#{EOL})#{EOL}/n) do head = $1.dup "" end next end if head and ( (EOL + boundary + EOL).size < buf.size ) content.print buf[0 ... (buf.size - (EOL + boundary + EOL).size)] buf[0 ... (buf.size - (EOL + boundary + EOL).size)] = "" end c = if bufsize < body_size body.read(bufsize) else body.read(body_size) end if c.nil? || c.empty? raise EOFError, "bad content body" end buf.concat(c) body_size -= c.size end buf = buf.sub(/\A((?:.|\n)*?)(?:[\r\n]{1,2})?#{quoted_boundary}([\r\n]{1,2}|--)/n) do content.print $1 if "--" == $2 body_size = -1 end boundary_end = $2.dup "" end content.rewind head =~ /Content-Disposition:.* filename=(?:"((?:\\.|[^\"])*)"|([^;]*))/ni if filename = $1 || $2 if /Mac/ni.match(env['HTTP_USER_AGENT']) and /Mozilla/ni.match(env['HTTP_USER_AGENT']) and (not /MSIE/ni.match(env['HTTP_USER_AGENT'])) filename = CGI.unescape(filename) end content.original_path = filename.dup end head =~ /Content-Type: ([^\r]*)/ni content.content_type = $1.dup if $1 head =~ /Content-Disposition:.* name="?([^\";]*)"?/ni name = $1.dup if $1 if params.has_key?(name) params[name].push(content) else params[name] = [content] end break if body_size == -1 end raise EOFError, "bad boundary end of body part" unless boundary_end=~/--/ begin body.rewind if body.respond_to?(:rewind) rescue Errno::ESPIPE # Handles exceptions raised by input streams that cannot be rewound # such as when using plain CGI under Apache end params end