Module: Mail::Jdec::ContentTypeElementPatch

Defined in:
lib/mail/jdec/elements/content_type_element_patch.rb

Instance Method Summary collapse

Instance Method Details

#initialize(string) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mail/jdec/elements/content_type_element_patch.rb', line 6

def initialize(string)
  if Jdec.enabled?
    # Remove extra trailing semicolon
    string = string.gsub(/;+$/, '')
    # Remove unnecessary space
    string = string.gsub(/;\s*charset\s+=\s+/i, '; charset=')
    # Handles name=test
    string = string.gsub(/name\s*=\s*([^"]+?)\s*(;|$)/im) { %Q|name="#{$1}"#{$2}| }
    # Handles name=""test""
    string = string.gsub(/name\s*=\s*"+([^"]+?)"+\s*(;|$)/im) { %Q|name="#{$1}"#{$2}| }
    # Handles text; name=test
    string = string.gsub(/^\s*([^\/]+)\s*;\s*name\s*=\s*(.+)$/im) { "#{$1}/unknown; name=#{$2}" }
    # Handles ; name=test
    string = string.gsub(/^\s*;?\s*name\s*=\s*(.+)$/im) { "application/octet-stream; name=#{$1}" }
  end

  super
rescue Mail::Field::ParseError => e
  if Jdec.enabled?
    @main_type = 'application'
    @sub_type = 'octet-stream'
    @parameters = ['name' => Jdec::Decoder.force_utf8(string)]
  else
    raise e
  end
end