multipart.rb 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. module Rack
  2. # A multipart form data parser, adapted from IOWA.
  3. #
  4. # Usually, Rack::Request#POST takes care of calling this.
  5. module Multipart
  6. autoload :UploadedFile, 'rack/multipart/uploaded_file'
  7. autoload :Parser, 'rack/multipart/parser'
  8. autoload :Generator, 'rack/multipart/generator'
  9. EOL = "\r\n"
  10. MULTIPART_BOUNDARY = "AaB03x"
  11. MULTIPART = %r|\Amultipart/.*boundary=\"?([^\";,]+)\"?|n
  12. TOKEN = /[^\s()<>,;:\\"\/\[\]?=]+/
  13. CONDISP = /Content-Disposition:\s*#{TOKEN}\s*/i
  14. DISPPARM = /;\s*(#{TOKEN})=("(?:\\"|[^"])*"|#{TOKEN})*/
  15. RFC2183 = /^#{CONDISP}(#{DISPPARM})+$/i
  16. BROKEN_QUOTED = /^#{CONDISP}.*;\sfilename="(.*?)"(?:\s*$|\s*;\s*#{TOKEN}=)/i
  17. BROKEN_UNQUOTED = /^#{CONDISP}.*;\sfilename=(#{TOKEN})/i
  18. MULTIPART_CONTENT_TYPE = /Content-Type: (.*)#{EOL}/ni
  19. MULTIPART_CONTENT_DISPOSITION = /Content-Disposition:.*\s+name="?([^\";]*)"?/ni
  20. MULTIPART_CONTENT_ID = /Content-ID:\s*([^#{EOL}]*)/ni
  21. class << self
  22. def parse_multipart(env)
  23. Parser.new(env).parse
  24. end
  25. def build_multipart(params, first = true)
  26. Generator.new(params, first).dump
  27. end
  28. end
  29. end
  30. end