module Fog::RiakCS::MultipartUtils

Public Instance Methods

extract_boundary(header_string) click to toggle source
# File lib/fog/riakcs.rb, line 41
def extract_boundary(header_string)
  $1 if header_string =~ /boundary=([A-Za-z0-9\'()+_,-.\/:=?]+)/
end
parse(data, boundary) click to toggle source
# File lib/fog/riakcs.rb, line 34
def parse(data, boundary)
  contents = data.match(end_boundary_regex(boundary)).pre_match rescue ""
  contents.split(inner_boundary_regex(boundary)).reject(&:empty?).map do |part|
    parse_multipart_section(part)
  end.compact
end

Private Instance Methods

end_boundary_regex(boundary) click to toggle source
# File lib/fog/riakcs.rb, line 46
def end_boundary_regex(boundary)
  /\r?\n--#{Regexp.escape(boundary)}--\r?\n?/
end
inner_boundary_regex(boundary) click to toggle source
# File lib/fog/riakcs.rb, line 50
def inner_boundary_regex(boundary)
  /\r?\n--#{Regexp.escape(boundary)}\r?\n/
end
parse_multipart_section(part) click to toggle source
# File lib/fog/riakcs.rb, line 54
def parse_multipart_section(part)
  headers = Headers.new
  if md = part.match(/\r?\n\r?\n/)
    body = md.post_match
    md.pre_match.split(/\r?\n/).each do |line|
      headers.parse(line)
    end

    if headers["content-type"] =~ /multipart\/mixed/
      boundary = extract_boundary(headers.to_hash["content-type"].first)
      parse(body, boundary)
    else
      {:headers => headers.to_hash, :body => body}
    end
  end
end