Class: Mail::Parsers::ContentTypeParser

Inherits:
Object
  • Object
show all
Includes:
Utilities
Defined in:
lib/mail/parsers/content_type_parser.rb

Constant Summary

Constant Summary

Constants included from Constants

Constants::ASTERISK, Constants::ATOM_UNSAFE, Constants::B_VALUES, Constants::CAPITAL_M, Constants::COLON, Constants::CONTROL_CHAR, Constants::CR, Constants::CRLF, Constants::CR_ENCODED, Constants::EMPTY, Constants::ENCODED_VALUE, Constants::EQUAL_LF, Constants::FIELD_BODY, Constants::FIELD_LINE, Constants::FIELD_NAME, Constants::FIELD_PREFIX, Constants::FIELD_SPLIT, Constants::FWS, Constants::HEADER_LINE, Constants::HEADER_SPLIT, Constants::HYPHEN, Constants::LF, Constants::LF_ENCODED, Constants::NULL_SENDER, Constants::PHRASE_UNSAFE, Constants::QP_SAFE, Constants::QP_UNSAFE, Constants::Q_VALUES, Constants::SPACE, Constants::TEXT, Constants::TOKEN_UNSAFE, Constants::UNDERSCORE, Constants::WSP

Instance Method Summary collapse

Methods included from Utilities

#atom_safe?, #bracket, #capitalize_field, #constantize, #dasherize, #dquote, #escape_paren, #map_lines, #map_with_index, #match_to_s, #paren, #quote_atom, #quote_phrase, #quote_token, #token_safe?, #unbracket, #underscoreize, #unparen, #unquote, #uri_escape, #uri_parser, #uri_unescape

Instance Method Details

#parse(s) ⇒ Object



5
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/mail/parsers/content_type_parser.rb', line 5

def parse(s)
  actions, error = Ragel.parse(:content_type, s)
  if error
    raise Mail::Field::ParseError.new(Mail::ContentTypeElement, s, error)
  end

  content_type = ContentTypeStruct.new(nil,nil,[])

  content_type.parameters = []

  main_type_s = sub_type_s = param_attr_s = param_attr = nil
  qstr_s = qstr = param_val_s = nil
  actions.each_slice(2) do |action_id, p|
    action = Mail::Parsers::Ragel::ACTIONS[action_id]
    case action

    # Main Type
    when :main_type_s then main_type_s = p
    when :main_type_e then
      content_type.main_type = s[main_type_s..(p-1)]
      content_type.main_type.downcase!

    # Sub Type
    when :sub_type_s then sub_type_s = p
    when :sub_type_e
      content_type.sub_type = s[sub_type_s..(p-1)]
      content_type.sub_type.downcase!

    # Parameter Attribute
    when :param_attr_s then param_attr_s = p
    when :param_attr_e then param_attr = s[param_attr_s..(p-1)]

    # Quoted String.
    when :qstr_s then qstr_s = p
    when :qstr_e then qstr = s[qstr_s..(p-1)]

    # Parameter Value
    when :param_val_s then param_val_s = p
    when :param_val_e
      if param_attr.nil?
        raise Mail::Field::ParseError.new(Mail::ContentTypeElement, s, "no attribute for value")
      end

      # Use quoted s value if one exists, otherwise use parameter value
      if qstr
        value = qstr
      else
        value = s[param_val_s..(p-1)]
      end

      content_type.parameters <<  { param_attr => value }
      param_attr = nil
      qstr = nil

    else
      raise Mail::Field::ParseError.new(Mail::ContentTypeElement, s, "Failed to process unknown action: #{action}")
    end
  end
  content_type
end