Module: As2::Base64Helper

Defined in:
lib/as2/base64_helper.rb

Class Method Summary collapse

Class Method Details

.ensure_base64(string) ⇒ Object

Will base64 encoded string, unless it already is base64 encoded



6
7
8
9
10
11
12
13
14
15
# File 'lib/as2/base64_helper.rb', line 6

def self.ensure_base64(string)
  begin
    # If string is not base64 encoded, this will raise an ArgumentError
    Base64.strict_decode64(string.gsub("\n",""))
    return string
  rescue ArgumentError
    # The string is not yet base64 encoded
    return Base64.encode64(string)
  end
end

.ensure_body_base64(multipart) ⇒ Object

If the multipart body is binary encoded, replace it with base64 encoded version



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/as2/base64_helper.rb', line 18

def self.ensure_body_base64(multipart)
  boundary = multipart.scan(/boundary="([^"]*)"/)[0][0]
  boundary_split = Regexp.escape("--#{boundary}")
  parts = multipart.split(/^#{boundary_split}-*\s*$/)
  signature = parts[2]
  transfer_encoding = signature.scan(/Content-Transfer-Encoding: (.*)/)[0][0].strip
  if transfer_encoding == 'binary'
    header, body = signature.split(/^\s*$/,2).map(&:lstrip)
    body_base64 = Base64.encode64(body)
    new_header = header.sub('Content-Transfer-Encoding: binary', 'Content-Transfer-Encoding: base64')
    parts[2] = new_header + "\r\n" + body_base64
    new_multipart = parts.join("--#{boundary}\r\n") + "--#{boundary}--\r\n"
    return new_multipart
  else
    return multipart
  end
end