File: content_disposition_parser.rb

package info (click to toggle)
ruby-mail 2.6.4%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 4,256 kB
  • ctags: 1,327
  • sloc: ruby: 44,678; makefile: 3
file content (68 lines) | stat: -rw-r--r-- 1,999 bytes parent folder | download
1
2
3
4
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
65
66
67
68
# frozen_string_literal: true
module Mail::Parsers
  class ContentDispositionParser
    include Mail::Utilities

    def parse(s)
      content_disposition = ContentDispositionStruct.new("", nil)
      if Mail::Utilities.blank?(s)
        return content_disposition
      end

      actions, error = Ragel.parse(:content_disposition, s)
      if error
        raise Mail::Field::ParseError.new(Mail::ContentDispositionElement, s, error)
      end

      content_disposition.parameters = []

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

        # Disposition Type
        when :disp_type_s then disp_type_s = p
        when :disp_type_e
          content_disposition.disposition_type = s[disp_type_s..(p-1)].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::ContentDispositionElement, s, "no attribute for value")
          end

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

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

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

      content_disposition
    end

    private
    def cleaned(string)
      string =~ /(.+);\s*$/ ? $1 : string
    end
  end
end