policy.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. """This will be the home for the policy that hooks in the new
  2. code that adds all the email6 features.
  3. """
  4. import re
  5. import sys
  6. from email._policybase import Policy, Compat32, compat32, _extend_docstrings
  7. from email.utils import _has_surrogates
  8. from email.headerregistry import HeaderRegistry as HeaderRegistry
  9. from email.contentmanager import raw_data_manager
  10. from email.message import EmailMessage
  11. __all__ = [
  12. 'Compat32',
  13. 'compat32',
  14. 'Policy',
  15. 'EmailPolicy',
  16. 'default',
  17. 'strict',
  18. 'SMTP',
  19. 'HTTP',
  20. ]
  21. linesep_splitter = re.compile(r'\n|\r')
  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. utf8 -- if False (the default) message headers will be
  35. serialized as ASCII, using encoded words to encode
  36. any non-ASCII characters in the source strings. If
  37. True, the message headers will be serialized using
  38. utf8 and will not contain encoded words (see RFC
  39. 6532 for more on this serialization format).
  40. refold_source -- if the value for a header in the Message object
  41. came from the parsing of some source, this attribute
  42. indicates whether or not a generator should refold
  43. that value when transforming the message back into
  44. stream form. The possible values are:
  45. none -- all source values use original folding
  46. long -- source values that have any line that is
  47. longer than max_line_length will be
  48. refolded
  49. all -- all values are refolded.
  50. The default is 'long'.
  51. header_factory -- a callable that takes two arguments, 'name' and
  52. 'value', where 'name' is a header field name and
  53. 'value' is an unfolded header field value, and
  54. returns a string-like object that represents that
  55. header. A default header_factory is provided that
  56. understands some of the RFC5322 header field types.
  57. (Currently address fields and date fields have
  58. special treatment, while all other fields are
  59. treated as unstructured. This list will be
  60. completed before the extension is marked stable.)
  61. content_manager -- an object with at least two methods: get_content
  62. and set_content. When the get_content or
  63. set_content method of a Message object is called,
  64. it calls the corresponding method of this object,
  65. passing it the message object as its first argument,
  66. and any arguments or keywords that were passed to
  67. it as additional arguments. The default
  68. content_manager is
  69. :data:`~email.contentmanager.raw_data_manager`.
  70. """
  71. message_factory = EmailMessage
  72. utf8 = False
  73. refold_source = 'long'
  74. header_factory = HeaderRegistry()
  75. content_manager = raw_data_manager
  76. def __init__(self, **kw):
  77. # Ensure that each new instance gets a unique header factory
  78. # (as opposed to clones, which share the factory).
  79. if 'header_factory' not in kw:
  80. object.__setattr__(self, 'header_factory', HeaderRegistry())
  81. super().__init__(**kw)
  82. def header_max_count(self, name):
  83. """+
  84. The implementation for this class returns the max_count attribute from
  85. the specialized header class that would be used to construct a header
  86. of type 'name'.
  87. """
  88. return self.header_factory[name].max_count
  89. # The logic of the next three methods is chosen such that it is possible to
  90. # switch a Message object between a Compat32 policy and a policy derived
  91. # from this class and have the results stay consistent. This allows a
  92. # Message object constructed with this policy to be passed to a library
  93. # that only handles Compat32 objects, or to receive such an object and
  94. # convert it to use the newer style by just changing its policy. It is
  95. # also chosen because it postpones the relatively expensive full rfc5322
  96. # parse until as late as possible when parsing from source, since in many
  97. # applications only a few headers will actually be inspected.
  98. def header_source_parse(self, sourcelines):
  99. """+
  100. The name is parsed as everything up to the ':' and returned unmodified.
  101. The value is determined by stripping leading whitespace off the
  102. remainder of the first line, joining all subsequent lines together, and
  103. stripping any trailing carriage return or linefeed characters. (This
  104. is the same as Compat32).
  105. """
  106. name, value = sourcelines[0].split(':', 1)
  107. value = value.lstrip(' \t') + ''.join(sourcelines[1:])
  108. return (name, value.rstrip('\r\n'))
  109. def header_store_parse(self, name, value):
  110. """+
  111. The name is returned unchanged. If the input value has a 'name'
  112. attribute and it matches the name ignoring case, the value is returned
  113. unchanged. Otherwise the name and value are passed to header_factory
  114. method, and the resulting custom header object is returned as the
  115. value. In this case a ValueError is raised if the input value contains
  116. CR or LF characters.
  117. """
  118. if hasattr(value, 'name') and value.name.lower() == name.lower():
  119. return (name, value)
  120. if isinstance(value, str) and len(value.splitlines())>1:
  121. # XXX this error message isn't quite right when we use splitlines
  122. # (see issue 22233), but I'm not sure what should happen here.
  123. raise ValueError("Header values may not contain linefeed "
  124. "or carriage return characters")
  125. return (name, self.header_factory(name, value))
  126. def header_fetch_parse(self, name, value):
  127. """+
  128. If the value has a 'name' attribute, it is returned to unmodified.
  129. Otherwise the name and the value with any linesep characters removed
  130. are passed to the header_factory method, and the resulting custom
  131. header object is returned. Any surrogateescaped bytes get turned
  132. into the unicode unknown-character glyph.
  133. """
  134. if hasattr(value, 'name'):
  135. return value
  136. # We can't use splitlines here because it splits on more than \r and \n.
  137. value = ''.join(linesep_splitter.split(value))
  138. return self.header_factory(name, value)
  139. def fold(self, name, value):
  140. """+
  141. Header folding is controlled by the refold_source policy setting. A
  142. value is considered to be a 'source value' if and only if it does not
  143. have a 'name' attribute (having a 'name' attribute means it is a header
  144. object of some sort). If a source value needs to be refolded according
  145. to the policy, it is converted into a custom header object by passing
  146. the name and the value with any linesep characters removed to the
  147. header_factory method. Folding of a custom header object is done by
  148. calling its fold method with the current policy.
  149. Source values are split into lines using splitlines. If the value is
  150. not to be refolded, the lines are rejoined using the linesep from the
  151. policy and returned. The exception is lines containing non-ascii
  152. binary data. In that case the value is refolded regardless of the
  153. refold_source setting, which causes the binary data to be CTE encoded
  154. using the unknown-8bit charset.
  155. """
  156. return self._fold(name, value, refold_binary=True)
  157. def fold_binary(self, name, value):
  158. """+
  159. The same as fold if cte_type is 7bit, except that the returned value is
  160. bytes.
  161. If cte_type is 8bit, non-ASCII binary data is converted back into
  162. bytes. Headers with binary data are not refolded, regardless of the
  163. refold_header setting, since there is no way to know whether the binary
  164. data consists of single byte characters or multibyte characters.
  165. If utf8 is true, headers are encoded to utf8, otherwise to ascii with
  166. non-ASCII unicode rendered as encoded words.
  167. """
  168. folded = self._fold(name, value, refold_binary=self.cte_type=='7bit')
  169. charset = 'utf8' if self.utf8 else 'ascii'
  170. return folded.encode(charset, 'surrogateescape')
  171. def _fold(self, name, value, refold_binary=False):
  172. if hasattr(value, 'name'):
  173. return value.fold(policy=self)
  174. maxlen = self.max_line_length if self.max_line_length else sys.maxsize
  175. lines = value.splitlines()
  176. refold = (self.refold_source == 'all' or
  177. self.refold_source == 'long' and
  178. (lines and len(lines[0])+len(name)+2 > maxlen or
  179. any(len(x) > maxlen for x in lines[1:])))
  180. if refold or refold_binary and _has_surrogates(value):
  181. return self.header_factory(name, ''.join(lines)).fold(policy=self)
  182. return name + ': ' + self.linesep.join(lines) + self.linesep
  183. default = EmailPolicy()
  184. # Make the default policy use the class default header_factory
  185. del default.header_factory
  186. strict = default.clone(raise_on_defect=True)
  187. SMTP = default.clone(linesep='\r\n')
  188. HTTP = default.clone(linesep='\r\n', max_line_length=None)
  189. SMTPUTF8 = SMTP.clone(utf8=True)