Method: Nitro::Cgi.parse_multipart
- Defined in:
- lib/nitro/cgi.rb
.parse_multipart(context, boundary) ⇒ Object
Parse a multipart request. Adapted from Ruby’s cgi.rb – TODO: optimize and rationalize this. ++
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/nitro/cgi.rb', line 210 def self.parse_multipart(context, boundary) input = context.in content_length = context.content_length env_table = context.env params = Hash.new() boundary = "--" + boundary quoted_boundary = Regexp.quote(boundary, "n") buf = "" boundary_end="" # start multipart/form-data input.binmode if defined? input.binmode boundary_size = boundary.size + EOL.size content_length -= boundary_size status = input.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 if 10240 < content_length body = Tempfile.new("CGI") else begin require "stringio" body = StringIO.new rescue LoadError body = Tempfile.new("CGI") end end body.binmode if defined? body.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 ) body.print buf[0 ... (buf.size - (EOL + boundary + EOL).size)] buf[0 ... (buf.size - (EOL + boundary + EOL).size)] = "" end c = if Cgi.buffer_size < content_length input.read(Cgi.buffer_size) else input.read(content_length) end if c.nil? || c.empty? raise EOFError, "bad content body" end buf.concat(c) content_length -= c.size end buf = buf.sub(/\A((?:.|\n)*?)(?:[\r\n]{1,2})?#{quoted_boundary}([\r\n]{1,2}|--)/n) do body.print $1 if "--" == $2 content_length = -1 end boundary_end = $2.dup "" end body.rewind /Content-Disposition:.* filename="?([^\";]*)"?/ni.match(head) filename = ($1 or "") if /Mac/ni.match(env_table['HTTP_USER_AGENT']) and /Mozilla/ni.match(env_table['HTTP_USER_AGENT']) and (not /MSIE/ni.match(env_table['HTTP_USER_AGENT'])) filename = CGI::unescape(filename) end /Content-Type: (.*)/ni.match(head) content_type = ($1 or "") (class << body; self; end).class_eval do alias local_path path define_method(:original_filename) { filename.dup.taint } define_method(:content_type) { content_type.dup.taint } # gmosx: this hides the performance hit!! define_method(:to_s) { str = read; rewind; return str} end /Content-Disposition:.* name="?([^\";]*)"?/ni.match(head) name = $1.dup params = self.structure_param(params, name, body) break if buf.size == 0 break if content_length === -1 end raise EOFError, "bad boundary end of body" unless boundary_end =~ /--/ return params end |