policy.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. """This will be the home for the policy that hooks in the new
  2. code that adds all the email6 features.
  3. """
  4. from __future__ import unicode_literals
  5. from __future__ import division
  6. from __future__ import absolute_import
  7. from future.builtins import super
  8. from future.standard_library.email._policybase import (Policy, Compat32,
  9. compat32, _extend_docstrings)
  10. from future.standard_library.email.utils import _has_surrogates
  11. from future.standard_library.email.headerregistry import HeaderRegistry as HeaderRegistry
  12. __all__ = [
  13. 'Compat32',
  14. 'compat32',
  15. 'Policy',
  16. 'EmailPolicy',
  17. 'default',
  18. 'strict',
  19. 'SMTP',
  20. 'HTTP',
  21. ]
  22. @_extend_docstrings
  23. class EmailPolicy(Policy):
  24. """+
  25. PROVISIONAL
  26. The API extensions enabled by this policy are currently provisional.
  27. Refer to the documentation for details.
  28. This policy adds new header parsing and folding algorithms. Instead of
  29. simple strings, headers are custom objects with custom attributes
  30. depending on the type of the field. The folding algorithm fully
  31. implements RFCs 2047 and 5322.
  32. In addition to the settable attributes listed above that apply to
  33. all Policies, this policy adds the following additional attributes:
  34. refold_source -- if the value for a header in the Message object
  35. came from the parsing of some source, this attribute
  36. indicates whether or not a generator should refold
  37. that value when transforming the message back into
  38. stream form. The possible values are:
  39. none -- all source values use original folding
  40. long -- source values that have any line that is
  41. longer than max_line_length will be
  42. refolded
  43. all -- all values are refolded.
  44. The default is 'long'.
  45. header_factory -- a callable that takes two arguments, 'name' and
  46. 'value', where 'name' is a header field name and
  47. 'value' is an unfolded header field value, and
  48. returns a string-like object that represents that
  49. header. A default header_factory is provided that
  50. understands some of the RFC5322 header field types.
  51. (Currently address fields and date fields have
  52. special treatment, while all other fields are
  53. treated as unstructured. This list will be
  54. completed before the extension is marked stable.)
  55. """
  56. refold_source = 'long'
  57. header_factory = HeaderRegistry()
  58. def __init__(self, **kw):
  59. # Ensure that each new instance gets a unique header factory
  60. # (as opposed to clones, which share the factory).
  61. if 'header_factory' not in kw:
  62. object.__setattr__(self, 'header_factory', HeaderRegistry())
  63. super().__init__(**kw)
  64. def header_max_count(self, name):
  65. """+
  66. The implementation for this class returns the max_count attribute from
  67. the specialized header class that would be used to construct a header
  68. of type 'name'.
  69. """
  70. return self.header_factory[name].max_count
  71. # The logic of the next three methods is chosen such that it is possible to
  72. # switch a Message object between a Compat32 policy and a policy derived
  73. # from this class and have the results stay consistent. This allows a
  74. # Message object constructed with this policy to be passed to a library
  75. # that only handles Compat32 objects, or to receive such an object and
  76. # convert it to use the newer style by just changing its policy. It is
  77. # also chosen because it postpones the relatively expensive full rfc5322
  78. # parse until as late as possible when parsing from source, since in many
  79. # applications only a few headers will actually be inspected.
  80. def header_source_parse(self, sourcelines):
  81. """+
  82. The name is parsed as everything up to the ':' and returned unmodified.
  83. The value is determined by stripping leading whitespace off the
  84. remainder of the first line, joining all subsequent lines together, and
  85. stripping any trailing carriage return or linefeed characters. (This
  86. is the same as Compat32).
  87. """
  88. name, value = sourcelines[0].split(':', 1)
  89. value = value.lstrip(' \t') + ''.join(sourcelines[1:])
  90. return (name, value.rstrip('\r\n'))
  91. def header_store_parse(self, name, value):
  92. """+
  93. The name is returned unchanged. If the input value has a 'name'
  94. attribute and it matches the name ignoring case, the value is returned
  95. unchanged. Otherwise the name and value are passed to header_factory
  96. method, and the resulting custom header object is returned as the
  97. value. In this case a ValueError is raised if the input value contains
  98. CR or LF characters.
  99. """
  100. if hasattr(value, 'name') and value.name.lower() == name.lower():
  101. return (name, value)
  102. if isinstance(value, str) and len(value.splitlines())>1:
  103. raise ValueError("Header values may not contain linefeed "
  104. "or carriage return characters")
  105. return (name, self.header_factory(name, value))
  106. def header_fetch_parse(self, name, value):
  107. """+
  108. If the value has a 'name' attribute, it is returned to unmodified.
  109. Otherwise the name and the value with any linesep characters removed
  110. are passed to the header_factory method, and the resulting custom
  111. header object is returned. Any surrogateescaped bytes get turned
  112. into the unicode unknown-character glyph.
  113. """
  114. if hasattr(value, 'name'):
  115. return value
  116. return self.header_factory(name, ''.join(value.splitlines()))
  117. def fold(self, name, value):
  118. """+
  119. Header folding is controlled by the refold_source policy setting. A
  120. value is considered to be a 'source value' if and only if it does not
  121. have a 'name' attribute (having a 'name' attribute means it is a header
  122. object of some sort). If a source value needs to be refolded according
  123. to the policy, it is converted into a custom header object by passing
  124. the name and the value with any linesep characters removed to the
  125. header_factory method. Folding of a custom header object is done by
  126. calling its fold method with the current policy.
  127. Source values are split into lines using splitlines. If the value is
  128. not to be refolded, the lines are rejoined using the linesep from the
  129. policy and returned. The exception is lines containing non-ascii
  130. binary data. In that case the value is refolded regardless of the
  131. refold_source setting, which causes the binary data to be CTE encoded
  132. using the unknown-8bit charset.
  133. """
  134. return self._fold(name, value, refold_binary=True)
  135. def fold_binary(self, name, value):
  136. """+
  137. The same as fold if cte_type is 7bit, except that the returned value is
  138. bytes.
  139. If cte_type is 8bit, non-ASCII binary data is converted back into
  140. bytes. Headers with binary data are not refolded, regardless of the
  141. refold_header setting, since there is no way to know whether the binary
  142. data consists of single byte characters or multibyte characters.
  143. """
  144. folded = self._fold(name, value, refold_binary=self.cte_type=='7bit')
  145. return folded.encode('ascii', 'surrogateescape')
  146. def _fold(self, name, value, refold_binary=False):
  147. if hasattr(value, 'name'):
  148. return value.fold(policy=self)
  149. maxlen = self.max_line_length if self.max_line_length else float('inf')
  150. lines = value.splitlines()
  151. refold = (self.refold_source == 'all' or
  152. self.refold_source == 'long' and
  153. (lines and len(lines[0])+len(name)+2 > maxlen or
  154. any(len(x) > maxlen for x in lines[1:])))
  155. if refold or refold_binary and _has_surrogates(value):
  156. return self.header_factory(name, ''.join(lines)).fold(policy=self)
  157. return name + ': ' + self.linesep.join(lines) + self.linesep
  158. default = EmailPolicy()
  159. # Make the default policy use the class default header_factory
  160. del default.header_factory
  161. strict = default.clone(raise_on_defect=True)
  162. SMTP = default.clone(linesep='\r\n')
  163. HTTP = default.clone(linesep='\r\n', max_line_length=None)