Module: Mail::Parsers::ContentTypeParser

Extended by:
Mail::ParserTools
Defined in:
lib/mail/parsers/content_type_parser.rb

Defined Under Namespace

Classes: ContentTypeStruct

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Mail::ParserTools

chars

Class Attribute Details

._eof_actionsObject

Returns the value of attribute _eof_actions


651
652
653
# File 'lib/mail/parsers/content_type_parser.rb', line 651

def _eof_actions
  @_eof_actions
end

._index_offsetsObject

Returns the value of attribute _index_offsets


55
56
57
# File 'lib/mail/parsers/content_type_parser.rb', line 55

def _index_offsets
  @_index_offsets
end

._indiciesObject

Returns the value of attribute _indicies


69
70
71
# File 'lib/mail/parsers/content_type_parser.rb', line 69

def _indicies
  @_indicies
end

._key_spansObject

Returns the value of attribute _key_spans


41
42
43
# File 'lib/mail/parsers/content_type_parser.rb', line 41

def _key_spans
  @_key_spans
end

._trans_actionsObject

Returns the value of attribute _trans_actions


629
630
631
# File 'lib/mail/parsers/content_type_parser.rb', line 629

def _trans_actions
  @_trans_actions
end

._trans_keysObject

Returns the value of attribute _trans_keys


13
14
15
# File 'lib/mail/parsers/content_type_parser.rb', line 13

def _trans_keys
  @_trans_keys
end

._trans_targsObject

Returns the value of attribute _trans_targs


607
608
609
# File 'lib/mail/parsers/content_type_parser.rb', line 607

def _trans_targs
  @_trans_targs
end

.en_comment_tailObject

Returns the value of attribute en_comment_tail


678
679
680
# File 'lib/mail/parsers/content_type_parser.rb', line 678

def en_comment_tail
  @en_comment_tail
end

.en_mainObject

Returns the value of attribute en_main


682
683
684
# File 'lib/mail/parsers/content_type_parser.rb', line 682

def en_main
  @en_main
end

.errorObject

Returns the value of attribute error


673
674
675
# File 'lib/mail/parsers/content_type_parser.rb', line 673

def error
  @error
end

.first_finalObject

Returns the value of attribute first_final


669
670
671
# File 'lib/mail/parsers/content_type_parser.rb', line 669

def first_final
  @first_final
end

.startObject

Returns the value of attribute start


665
666
667
# File 'lib/mail/parsers/content_type_parser.rb', line 665

def start
  @start
end

Class Method Details

.parse(data) ⇒ Object


686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
# File 'lib/mail/parsers/content_type_parser.rb', line 686

def self.parse(data)
  data = data.dup.force_encoding(Encoding::ASCII_8BIT) if data.respond_to?(:force_encoding)

  return ContentTypeStruct.new("text", "plain", []) if Mail::Utilities.blank?(data)
  content_type = ContentTypeStruct.new(nil, nil, [])

  # Parser state
  main_type_s = sub_type_s = param_attr_s = param_attr = nil
  qstr_s = qstr = param_val_s = nil

  # 5.1 Variables Used by Ragel
  p = 0
  eof = pe = data.length
  stack = []

  begin
    p ||= 0
    pe ||= data.length
    cs = start
    top = 0
  end

  begin
    testEof = false
    _slen, _trans, _keys, _inds, _acts, _nacts = nil
    _goto_level = 0
    _resume = 10
    _eof_trans = 15
    _again = 20
    _test_eof = 30
    _out = 40
    while true
      if _goto_level <= 0
        if p == pe
          _goto_level = _test_eof
          next
        end
        if cs == 0
          _goto_level = _out
          next
        end
      end
      if _goto_level <= _resume
        _keys = cs << 1
        _inds = _index_offsets[cs]
        _slen = _key_spans[cs]
        _wide = data[p].ord
        _trans = if (_slen > 0 &&
                     _trans_keys[_keys] <= _wide &&
                     _wide <= _trans_keys[_keys + 1])
                   _indicies[_inds + _wide - _trans_keys[_keys]]
                 else
                   _indicies[_inds + _slen]
                 end
        cs = _trans_targs[_trans]
        if _trans_actions[_trans] != 0
          case _trans_actions[_trans]
          when 1
            begin
              main_type_s = p
            end
          when 2
            begin
              content_type.main_type = chars(data, main_type_s, p - 1).downcase
            end
          when 3
            begin
              sub_type_s = p
            end
          when 19
            begin
              content_type.sub_type = chars(data, sub_type_s, p - 1).downcase
            end
          when 4
            begin
              param_attr_s = p
            end
          when 6
            begin
              param_attr = chars(data, param_attr_s, p - 1)
            end
          when 9
            begin
              qstr_s = p
            end
          when 11
            begin
              qstr = chars(data, qstr_s, p - 1)
            end
          when 7
            begin
              param_val_s = p
            end
          when 21
            begin
              if param_attr.nil?
                raise Mail::Field::ParseError.new(Mail::ContentTypeElement, data, "no attribute for value")
              end

              # Use quoted s value if one exists, otherwise use parameter value
              value = qstr || chars(data, param_val_s, p - 1)

              content_type.parameters << {param_attr => value}
              param_attr = nil
              qstr = nil
            end
          when 12
            begin
            end
          when 15
            begin
            end
          when 5
            begin
              begin
                stack[top] = cs
                top += 1
                cs = 35
                _goto_level = _again
                next
              end
            end
          when 18
            begin
              begin
                top -= 1
                cs = stack[top]
                _goto_level = _again
                next
              end
            end
          when 20
            begin
              content_type.sub_type = chars(data, sub_type_s, p - 1).downcase
            end
            begin
              begin
                stack[top] = cs
                top += 1
                cs = 35
                _goto_level = _again
                next
              end
            end
          when 10
            begin
              qstr_s = p
            end
            begin
              qstr = chars(data, qstr_s, p - 1)
            end
          when 8
            begin
              param_val_s = p
            end
            begin
              begin
                stack[top] = cs
                top += 1
                cs = 35
                _goto_level = _again
                next
              end
            end
          when 25
            begin
              if param_attr.nil?
                raise Mail::Field::ParseError.new(Mail::ContentTypeElement, data, "no attribute for value")
              end

              # Use quoted s value if one exists, otherwise use parameter value
              value = qstr || chars(data, param_val_s, p - 1)

              content_type.parameters << {param_attr => value}
              param_attr = nil
              qstr = nil
            end
            begin
              begin
                stack[top] = cs
                top += 1
                cs = 35
                _goto_level = _again
                next
              end
            end
          when 13
            begin
            end
            begin
              param_attr_s = p
            end
          when 23
            begin
            end
            begin
              if param_attr.nil?
                raise Mail::Field::ParseError.new(Mail::ContentTypeElement, data, "no attribute for value")
              end

              # Use quoted s value if one exists, otherwise use parameter value
              value = qstr || chars(data, param_val_s, p - 1)

              content_type.parameters << {param_attr => value}
              param_attr = nil
              qstr = nil
            end
          when 14
            begin
            end
            begin
              begin
                stack[top] = cs
                top += 1
                cs = 35
                _goto_level = _again
                next
              end
            end
          when 16
            begin
            end
            begin
              begin
                stack[top] = cs
                top += 1
                cs = 35
                _goto_level = _again
                next
              end
            end
          when 17
            begin
            end
            begin
              begin
                top -= 1
                cs = stack[top]
                _goto_level = _again
                next
              end
            end
          when 22
            begin
              begin
                stack[top] = cs
                top += 1
                cs = 35
                _goto_level = _again
                next
              end
            end
            begin
              if param_attr.nil?
                raise Mail::Field::ParseError.new(Mail::ContentTypeElement, data, "no attribute for value")
              end

              # Use quoted s value if one exists, otherwise use parameter value
              value = qstr || chars(data, param_val_s, p - 1)

              content_type.parameters << {param_attr => value}
              param_attr = nil
              qstr = nil
            end
          when 24
            begin
            end
            begin
              begin
                stack[top] = cs
                top += 1
                cs = 35
                _goto_level = _again
                next
              end
            end
            begin
              if param_attr.nil?
                raise Mail::Field::ParseError.new(Mail::ContentTypeElement, data, "no attribute for value")
              end

              # Use quoted s value if one exists, otherwise use parameter value
              value = qstr || chars(data, param_val_s, p - 1)

              content_type.parameters << {param_attr => value}
              param_attr = nil
              qstr = nil
            end
          end
        end
      end
      if _goto_level <= _again
        if cs == 0
          _goto_level = _out
          next
        end
        p += 1
        if p != pe
          _goto_level = _resume
          next
        end
      end
      if _goto_level <= _test_eof
        if p == eof
          case _eof_actions[cs]
          when 19
            begin
              content_type.sub_type = chars(data, sub_type_s, p - 1).downcase
            end
          when 21
            begin
              if param_attr.nil?
                raise Mail::Field::ParseError.new(Mail::ContentTypeElement, data, "no attribute for value")
              end

              # Use quoted s value if one exists, otherwise use parameter value
              value = qstr || chars(data, param_val_s, p - 1)

              content_type.parameters << {param_attr => value}
              param_attr = nil
              qstr = nil
            end
          when 12
            begin
            end
          when 23
            begin
            end
            begin
              if param_attr.nil?
                raise Mail::Field::ParseError.new(Mail::ContentTypeElement, data, "no attribute for value")
              end

              # Use quoted s value if one exists, otherwise use parameter value
              value = qstr || chars(data, param_val_s, p - 1)

              content_type.parameters << {param_attr => value}
              param_attr = nil
              qstr = nil
            end
          end
        end
      end
      if _goto_level <= _out
        break
      end
    end
  end

  if p != eof || cs < 47
    raise Mail::Field::IncompleteParseError.new(Mail::ContentTypeElement, data, p)
  end

  content_type
end