1 | n/a | # Copyright (C) 2002-2007 Python Software Foundation |
---|
2 | n/a | # Author: Ben Gertzfield, Barry Warsaw |
---|
3 | n/a | # Contact: email-sig@python.org |
---|
4 | n/a | |
---|
5 | n/a | """Header encoding and decoding functionality.""" |
---|
6 | n/a | |
---|
7 | n/a | __all__ = [ |
---|
8 | n/a | 'Header', |
---|
9 | n/a | 'decode_header', |
---|
10 | n/a | 'make_header', |
---|
11 | n/a | ] |
---|
12 | n/a | |
---|
13 | n/a | import re |
---|
14 | n/a | import binascii |
---|
15 | n/a | |
---|
16 | n/a | import email.quoprimime |
---|
17 | n/a | import email.base64mime |
---|
18 | n/a | |
---|
19 | n/a | from email.errors import HeaderParseError |
---|
20 | n/a | from email import charset as _charset |
---|
21 | n/a | Charset = _charset.Charset |
---|
22 | n/a | |
---|
23 | n/a | NL = '\n' |
---|
24 | n/a | SPACE = ' ' |
---|
25 | n/a | BSPACE = b' ' |
---|
26 | n/a | SPACE8 = ' ' * 8 |
---|
27 | n/a | EMPTYSTRING = '' |
---|
28 | n/a | MAXLINELEN = 78 |
---|
29 | n/a | FWS = ' \t' |
---|
30 | n/a | |
---|
31 | n/a | USASCII = Charset('us-ascii') |
---|
32 | n/a | UTF8 = Charset('utf-8') |
---|
33 | n/a | |
---|
34 | n/a | # Match encoded-word strings in the form =?charset?q?Hello_World?= |
---|
35 | n/a | ecre = re.compile(r''' |
---|
36 | n/a | =\? # literal =? |
---|
37 | n/a | (?P<charset>[^?]*?) # non-greedy up to the next ? is the charset |
---|
38 | n/a | \? # literal ? |
---|
39 | n/a | (?P<encoding>[qb]) # either a "q" or a "b", case insensitive |
---|
40 | n/a | \? # literal ? |
---|
41 | n/a | (?P<encoded>.*?) # non-greedy up to the next ?= is the encoded string |
---|
42 | n/a | \?= # literal ?= |
---|
43 | n/a | ''', re.VERBOSE | re.IGNORECASE | re.MULTILINE) |
---|
44 | n/a | |
---|
45 | n/a | # Field name regexp, including trailing colon, but not separating whitespace, |
---|
46 | n/a | # according to RFC 2822. Character range is from tilde to exclamation mark. |
---|
47 | n/a | # For use with .match() |
---|
48 | n/a | fcre = re.compile(r'[\041-\176]+:$') |
---|
49 | n/a | |
---|
50 | n/a | # Find a header embedded in a putative header value. Used to check for |
---|
51 | n/a | # header injection attack. |
---|
52 | n/a | _embedded_header = re.compile(r'\n[^ \t]+:') |
---|
53 | n/a | |
---|
54 | n/a | |
---|
55 | n/a | |
---|
56 | n/a | # Helpers |
---|
57 | n/a | _max_append = email.quoprimime._max_append |
---|
58 | n/a | |
---|
59 | n/a | |
---|
60 | n/a | |
---|
61 | n/a | def decode_header(header): |
---|
62 | n/a | """Decode a message header value without converting charset. |
---|
63 | n/a | |
---|
64 | n/a | Returns a list of (string, charset) pairs containing each of the decoded |
---|
65 | n/a | parts of the header. Charset is None for non-encoded parts of the header, |
---|
66 | n/a | otherwise a lower-case string containing the name of the character set |
---|
67 | n/a | specified in the encoded string. |
---|
68 | n/a | |
---|
69 | n/a | header may be a string that may or may not contain RFC2047 encoded words, |
---|
70 | n/a | or it may be a Header object. |
---|
71 | n/a | |
---|
72 | n/a | An email.errors.HeaderParseError may be raised when certain decoding error |
---|
73 | n/a | occurs (e.g. a base64 decoding exception). |
---|
74 | n/a | """ |
---|
75 | n/a | # If it is a Header object, we can just return the encoded chunks. |
---|
76 | n/a | if hasattr(header, '_chunks'): |
---|
77 | n/a | return [(_charset._encode(string, str(charset)), str(charset)) |
---|
78 | n/a | for string, charset in header._chunks] |
---|
79 | n/a | # If no encoding, just return the header with no charset. |
---|
80 | n/a | if not ecre.search(header): |
---|
81 | n/a | return [(header, None)] |
---|
82 | n/a | # First step is to parse all the encoded parts into triplets of the form |
---|
83 | n/a | # (encoded_string, encoding, charset). For unencoded strings, the last |
---|
84 | n/a | # two parts will be None. |
---|
85 | n/a | words = [] |
---|
86 | n/a | for line in header.splitlines(): |
---|
87 | n/a | parts = ecre.split(line) |
---|
88 | n/a | first = True |
---|
89 | n/a | while parts: |
---|
90 | n/a | unencoded = parts.pop(0) |
---|
91 | n/a | if first: |
---|
92 | n/a | unencoded = unencoded.lstrip() |
---|
93 | n/a | first = False |
---|
94 | n/a | if unencoded: |
---|
95 | n/a | words.append((unencoded, None, None)) |
---|
96 | n/a | if parts: |
---|
97 | n/a | charset = parts.pop(0).lower() |
---|
98 | n/a | encoding = parts.pop(0).lower() |
---|
99 | n/a | encoded = parts.pop(0) |
---|
100 | n/a | words.append((encoded, encoding, charset)) |
---|
101 | n/a | # Now loop over words and remove words that consist of whitespace |
---|
102 | n/a | # between two encoded strings. |
---|
103 | n/a | droplist = [] |
---|
104 | n/a | for n, w in enumerate(words): |
---|
105 | n/a | if n>1 and w[1] and words[n-2][1] and words[n-1][0].isspace(): |
---|
106 | n/a | droplist.append(n-1) |
---|
107 | n/a | for d in reversed(droplist): |
---|
108 | n/a | del words[d] |
---|
109 | n/a | |
---|
110 | n/a | # The next step is to decode each encoded word by applying the reverse |
---|
111 | n/a | # base64 or quopri transformation. decoded_words is now a list of the |
---|
112 | n/a | # form (decoded_word, charset). |
---|
113 | n/a | decoded_words = [] |
---|
114 | n/a | for encoded_string, encoding, charset in words: |
---|
115 | n/a | if encoding is None: |
---|
116 | n/a | # This is an unencoded word. |
---|
117 | n/a | decoded_words.append((encoded_string, charset)) |
---|
118 | n/a | elif encoding == 'q': |
---|
119 | n/a | word = email.quoprimime.header_decode(encoded_string) |
---|
120 | n/a | decoded_words.append((word, charset)) |
---|
121 | n/a | elif encoding == 'b': |
---|
122 | n/a | paderr = len(encoded_string) % 4 # Postel's law: add missing padding |
---|
123 | n/a | if paderr: |
---|
124 | n/a | encoded_string += '==='[:4 - paderr] |
---|
125 | n/a | try: |
---|
126 | n/a | word = email.base64mime.decode(encoded_string) |
---|
127 | n/a | except binascii.Error: |
---|
128 | n/a | raise HeaderParseError('Base64 decoding error') |
---|
129 | n/a | else: |
---|
130 | n/a | decoded_words.append((word, charset)) |
---|
131 | n/a | else: |
---|
132 | n/a | raise AssertionError('Unexpected encoding: ' + encoding) |
---|
133 | n/a | # Now convert all words to bytes and collapse consecutive runs of |
---|
134 | n/a | # similarly encoded words. |
---|
135 | n/a | collapsed = [] |
---|
136 | n/a | last_word = last_charset = None |
---|
137 | n/a | for word, charset in decoded_words: |
---|
138 | n/a | if isinstance(word, str): |
---|
139 | n/a | word = bytes(word, 'raw-unicode-escape') |
---|
140 | n/a | if last_word is None: |
---|
141 | n/a | last_word = word |
---|
142 | n/a | last_charset = charset |
---|
143 | n/a | elif charset != last_charset: |
---|
144 | n/a | collapsed.append((last_word, last_charset)) |
---|
145 | n/a | last_word = word |
---|
146 | n/a | last_charset = charset |
---|
147 | n/a | elif last_charset is None: |
---|
148 | n/a | last_word += BSPACE + word |
---|
149 | n/a | else: |
---|
150 | n/a | last_word += word |
---|
151 | n/a | collapsed.append((last_word, last_charset)) |
---|
152 | n/a | return collapsed |
---|
153 | n/a | |
---|
154 | n/a | |
---|
155 | n/a | |
---|
156 | n/a | def make_header(decoded_seq, maxlinelen=None, header_name=None, |
---|
157 | n/a | continuation_ws=' '): |
---|
158 | n/a | """Create a Header from a sequence of pairs as returned by decode_header() |
---|
159 | n/a | |
---|
160 | n/a | decode_header() takes a header value string and returns a sequence of |
---|
161 | n/a | pairs of the format (decoded_string, charset) where charset is the string |
---|
162 | n/a | name of the character set. |
---|
163 | n/a | |
---|
164 | n/a | This function takes one of those sequence of pairs and returns a Header |
---|
165 | n/a | instance. Optional maxlinelen, header_name, and continuation_ws are as in |
---|
166 | n/a | the Header constructor. |
---|
167 | n/a | """ |
---|
168 | n/a | h = Header(maxlinelen=maxlinelen, header_name=header_name, |
---|
169 | n/a | continuation_ws=continuation_ws) |
---|
170 | n/a | for s, charset in decoded_seq: |
---|
171 | n/a | # None means us-ascii but we can simply pass it on to h.append() |
---|
172 | n/a | if charset is not None and not isinstance(charset, Charset): |
---|
173 | n/a | charset = Charset(charset) |
---|
174 | n/a | h.append(s, charset) |
---|
175 | n/a | return h |
---|
176 | n/a | |
---|
177 | n/a | |
---|
178 | n/a | |
---|
179 | n/a | class Header: |
---|
180 | n/a | def __init__(self, s=None, charset=None, |
---|
181 | n/a | maxlinelen=None, header_name=None, |
---|
182 | n/a | continuation_ws=' ', errors='strict'): |
---|
183 | n/a | """Create a MIME-compliant header that can contain many character sets. |
---|
184 | n/a | |
---|
185 | n/a | Optional s is the initial header value. If None, the initial header |
---|
186 | n/a | value is not set. You can later append to the header with .append() |
---|
187 | n/a | method calls. s may be a byte string or a Unicode string, but see the |
---|
188 | n/a | .append() documentation for semantics. |
---|
189 | n/a | |
---|
190 | n/a | Optional charset serves two purposes: it has the same meaning as the |
---|
191 | n/a | charset argument to the .append() method. It also sets the default |
---|
192 | n/a | character set for all subsequent .append() calls that omit the charset |
---|
193 | n/a | argument. If charset is not provided in the constructor, the us-ascii |
---|
194 | n/a | charset is used both as s's initial charset and as the default for |
---|
195 | n/a | subsequent .append() calls. |
---|
196 | n/a | |
---|
197 | n/a | The maximum line length can be specified explicitly via maxlinelen. For |
---|
198 | n/a | splitting the first line to a shorter value (to account for the field |
---|
199 | n/a | header which isn't included in s, e.g. `Subject') pass in the name of |
---|
200 | n/a | the field in header_name. The default maxlinelen is 78 as recommended |
---|
201 | n/a | by RFC 2822. |
---|
202 | n/a | |
---|
203 | n/a | continuation_ws must be RFC 2822 compliant folding whitespace (usually |
---|
204 | n/a | either a space or a hard tab) which will be prepended to continuation |
---|
205 | n/a | lines. |
---|
206 | n/a | |
---|
207 | n/a | errors is passed through to the .append() call. |
---|
208 | n/a | """ |
---|
209 | n/a | if charset is None: |
---|
210 | n/a | charset = USASCII |
---|
211 | n/a | elif not isinstance(charset, Charset): |
---|
212 | n/a | charset = Charset(charset) |
---|
213 | n/a | self._charset = charset |
---|
214 | n/a | self._continuation_ws = continuation_ws |
---|
215 | n/a | self._chunks = [] |
---|
216 | n/a | if s is not None: |
---|
217 | n/a | self.append(s, charset, errors) |
---|
218 | n/a | if maxlinelen is None: |
---|
219 | n/a | maxlinelen = MAXLINELEN |
---|
220 | n/a | self._maxlinelen = maxlinelen |
---|
221 | n/a | if header_name is None: |
---|
222 | n/a | self._headerlen = 0 |
---|
223 | n/a | else: |
---|
224 | n/a | # Take the separating colon and space into account. |
---|
225 | n/a | self._headerlen = len(header_name) + 2 |
---|
226 | n/a | |
---|
227 | n/a | def __str__(self): |
---|
228 | n/a | """Return the string value of the header.""" |
---|
229 | n/a | self._normalize() |
---|
230 | n/a | uchunks = [] |
---|
231 | n/a | lastcs = None |
---|
232 | n/a | lastspace = None |
---|
233 | n/a | for string, charset in self._chunks: |
---|
234 | n/a | # We must preserve spaces between encoded and non-encoded word |
---|
235 | n/a | # boundaries, which means for us we need to add a space when we go |
---|
236 | n/a | # from a charset to None/us-ascii, or from None/us-ascii to a |
---|
237 | n/a | # charset. Only do this for the second and subsequent chunks. |
---|
238 | n/a | # Don't add a space if the None/us-ascii string already has |
---|
239 | n/a | # a space (trailing or leading depending on transition) |
---|
240 | n/a | nextcs = charset |
---|
241 | n/a | if nextcs == _charset.UNKNOWN8BIT: |
---|
242 | n/a | original_bytes = string.encode('ascii', 'surrogateescape') |
---|
243 | n/a | string = original_bytes.decode('ascii', 'replace') |
---|
244 | n/a | if uchunks: |
---|
245 | n/a | hasspace = string and self._nonctext(string[0]) |
---|
246 | n/a | if lastcs not in (None, 'us-ascii'): |
---|
247 | n/a | if nextcs in (None, 'us-ascii') and not hasspace: |
---|
248 | n/a | uchunks.append(SPACE) |
---|
249 | n/a | nextcs = None |
---|
250 | n/a | elif nextcs not in (None, 'us-ascii') and not lastspace: |
---|
251 | n/a | uchunks.append(SPACE) |
---|
252 | n/a | lastspace = string and self._nonctext(string[-1]) |
---|
253 | n/a | lastcs = nextcs |
---|
254 | n/a | uchunks.append(string) |
---|
255 | n/a | return EMPTYSTRING.join(uchunks) |
---|
256 | n/a | |
---|
257 | n/a | # Rich comparison operators for equality only. BAW: does it make sense to |
---|
258 | n/a | # have or explicitly disable <, <=, >, >= operators? |
---|
259 | n/a | def __eq__(self, other): |
---|
260 | n/a | # other may be a Header or a string. Both are fine so coerce |
---|
261 | n/a | # ourselves to a unicode (of the unencoded header value), swap the |
---|
262 | n/a | # args and do another comparison. |
---|
263 | n/a | return other == str(self) |
---|
264 | n/a | |
---|
265 | n/a | def append(self, s, charset=None, errors='strict'): |
---|
266 | n/a | """Append a string to the MIME header. |
---|
267 | n/a | |
---|
268 | n/a | Optional charset, if given, should be a Charset instance or the name |
---|
269 | n/a | of a character set (which will be converted to a Charset instance). A |
---|
270 | n/a | value of None (the default) means that the charset given in the |
---|
271 | n/a | constructor is used. |
---|
272 | n/a | |
---|
273 | n/a | s may be a byte string or a Unicode string. If it is a byte string |
---|
274 | n/a | (i.e. isinstance(s, str) is false), then charset is the encoding of |
---|
275 | n/a | that byte string, and a UnicodeError will be raised if the string |
---|
276 | n/a | cannot be decoded with that charset. If s is a Unicode string, then |
---|
277 | n/a | charset is a hint specifying the character set of the characters in |
---|
278 | n/a | the string. In either case, when producing an RFC 2822 compliant |
---|
279 | n/a | header using RFC 2047 rules, the string will be encoded using the |
---|
280 | n/a | output codec of the charset. If the string cannot be encoded to the |
---|
281 | n/a | output codec, a UnicodeError will be raised. |
---|
282 | n/a | |
---|
283 | n/a | Optional `errors' is passed as the errors argument to the decode |
---|
284 | n/a | call if s is a byte string. |
---|
285 | n/a | """ |
---|
286 | n/a | if charset is None: |
---|
287 | n/a | charset = self._charset |
---|
288 | n/a | elif not isinstance(charset, Charset): |
---|
289 | n/a | charset = Charset(charset) |
---|
290 | n/a | if not isinstance(s, str): |
---|
291 | n/a | input_charset = charset.input_codec or 'us-ascii' |
---|
292 | n/a | if input_charset == _charset.UNKNOWN8BIT: |
---|
293 | n/a | s = s.decode('us-ascii', 'surrogateescape') |
---|
294 | n/a | else: |
---|
295 | n/a | s = s.decode(input_charset, errors) |
---|
296 | n/a | # Ensure that the bytes we're storing can be decoded to the output |
---|
297 | n/a | # character set, otherwise an early error is raised. |
---|
298 | n/a | output_charset = charset.output_codec or 'us-ascii' |
---|
299 | n/a | if output_charset != _charset.UNKNOWN8BIT: |
---|
300 | n/a | try: |
---|
301 | n/a | s.encode(output_charset, errors) |
---|
302 | n/a | except UnicodeEncodeError: |
---|
303 | n/a | if output_charset!='us-ascii': |
---|
304 | n/a | raise |
---|
305 | n/a | charset = UTF8 |
---|
306 | n/a | self._chunks.append((s, charset)) |
---|
307 | n/a | |
---|
308 | n/a | def _nonctext(self, s): |
---|
309 | n/a | """True if string s is not a ctext character of RFC822. |
---|
310 | n/a | """ |
---|
311 | n/a | return s.isspace() or s in ('(', ')', '\\') |
---|
312 | n/a | |
---|
313 | n/a | def encode(self, splitchars=';, \t', maxlinelen=None, linesep='\n'): |
---|
314 | n/a | r"""Encode a message header into an RFC-compliant format. |
---|
315 | n/a | |
---|
316 | n/a | There are many issues involved in converting a given string for use in |
---|
317 | n/a | an email header. Only certain character sets are readable in most |
---|
318 | n/a | email clients, and as header strings can only contain a subset of |
---|
319 | n/a | 7-bit ASCII, care must be taken to properly convert and encode (with |
---|
320 | n/a | Base64 or quoted-printable) header strings. In addition, there is a |
---|
321 | n/a | 75-character length limit on any given encoded header field, so |
---|
322 | n/a | line-wrapping must be performed, even with double-byte character sets. |
---|
323 | n/a | |
---|
324 | n/a | Optional maxlinelen specifies the maximum length of each generated |
---|
325 | n/a | line, exclusive of the linesep string. Individual lines may be longer |
---|
326 | n/a | than maxlinelen if a folding point cannot be found. The first line |
---|
327 | n/a | will be shorter by the length of the header name plus ": " if a header |
---|
328 | n/a | name was specified at Header construction time. The default value for |
---|
329 | n/a | maxlinelen is determined at header construction time. |
---|
330 | n/a | |
---|
331 | n/a | Optional splitchars is a string containing characters which should be |
---|
332 | n/a | given extra weight by the splitting algorithm during normal header |
---|
333 | n/a | wrapping. This is in very rough support of RFC 2822's `higher level |
---|
334 | n/a | syntactic breaks': split points preceded by a splitchar are preferred |
---|
335 | n/a | during line splitting, with the characters preferred in the order in |
---|
336 | n/a | which they appear in the string. Space and tab may be included in the |
---|
337 | n/a | string to indicate whether preference should be given to one over the |
---|
338 | n/a | other as a split point when other split chars do not appear in the line |
---|
339 | n/a | being split. Splitchars does not affect RFC 2047 encoded lines. |
---|
340 | n/a | |
---|
341 | n/a | Optional linesep is a string to be used to separate the lines of |
---|
342 | n/a | the value. The default value is the most useful for typical |
---|
343 | n/a | Python applications, but it can be set to \r\n to produce RFC-compliant |
---|
344 | n/a | line separators when needed. |
---|
345 | n/a | """ |
---|
346 | n/a | self._normalize() |
---|
347 | n/a | if maxlinelen is None: |
---|
348 | n/a | maxlinelen = self._maxlinelen |
---|
349 | n/a | # A maxlinelen of 0 means don't wrap. For all practical purposes, |
---|
350 | n/a | # choosing a huge number here accomplishes that and makes the |
---|
351 | n/a | # _ValueFormatter algorithm much simpler. |
---|
352 | n/a | if maxlinelen == 0: |
---|
353 | n/a | maxlinelen = 1000000 |
---|
354 | n/a | formatter = _ValueFormatter(self._headerlen, maxlinelen, |
---|
355 | n/a | self._continuation_ws, splitchars) |
---|
356 | n/a | lastcs = None |
---|
357 | n/a | hasspace = lastspace = None |
---|
358 | n/a | for string, charset in self._chunks: |
---|
359 | n/a | if hasspace is not None: |
---|
360 | n/a | hasspace = string and self._nonctext(string[0]) |
---|
361 | n/a | if lastcs not in (None, 'us-ascii'): |
---|
362 | n/a | if not hasspace or charset not in (None, 'us-ascii'): |
---|
363 | n/a | formatter.add_transition() |
---|
364 | n/a | elif charset not in (None, 'us-ascii') and not lastspace: |
---|
365 | n/a | formatter.add_transition() |
---|
366 | n/a | lastspace = string and self._nonctext(string[-1]) |
---|
367 | n/a | lastcs = charset |
---|
368 | n/a | hasspace = False |
---|
369 | n/a | lines = string.splitlines() |
---|
370 | n/a | if lines: |
---|
371 | n/a | formatter.feed('', lines[0], charset) |
---|
372 | n/a | else: |
---|
373 | n/a | formatter.feed('', '', charset) |
---|
374 | n/a | for line in lines[1:]: |
---|
375 | n/a | formatter.newline() |
---|
376 | n/a | if charset.header_encoding is not None: |
---|
377 | n/a | formatter.feed(self._continuation_ws, ' ' + line.lstrip(), |
---|
378 | n/a | charset) |
---|
379 | n/a | else: |
---|
380 | n/a | sline = line.lstrip() |
---|
381 | n/a | fws = line[:len(line)-len(sline)] |
---|
382 | n/a | formatter.feed(fws, sline, charset) |
---|
383 | n/a | if len(lines) > 1: |
---|
384 | n/a | formatter.newline() |
---|
385 | n/a | if self._chunks: |
---|
386 | n/a | formatter.add_transition() |
---|
387 | n/a | value = formatter._str(linesep) |
---|
388 | n/a | if _embedded_header.search(value): |
---|
389 | n/a | raise HeaderParseError("header value appears to contain " |
---|
390 | n/a | "an embedded header: {!r}".format(value)) |
---|
391 | n/a | return value |
---|
392 | n/a | |
---|
393 | n/a | def _normalize(self): |
---|
394 | n/a | # Step 1: Normalize the chunks so that all runs of identical charsets |
---|
395 | n/a | # get collapsed into a single unicode string. |
---|
396 | n/a | chunks = [] |
---|
397 | n/a | last_charset = None |
---|
398 | n/a | last_chunk = [] |
---|
399 | n/a | for string, charset in self._chunks: |
---|
400 | n/a | if charset == last_charset: |
---|
401 | n/a | last_chunk.append(string) |
---|
402 | n/a | else: |
---|
403 | n/a | if last_charset is not None: |
---|
404 | n/a | chunks.append((SPACE.join(last_chunk), last_charset)) |
---|
405 | n/a | last_chunk = [string] |
---|
406 | n/a | last_charset = charset |
---|
407 | n/a | if last_chunk: |
---|
408 | n/a | chunks.append((SPACE.join(last_chunk), last_charset)) |
---|
409 | n/a | self._chunks = chunks |
---|
410 | n/a | |
---|
411 | n/a | |
---|
412 | n/a | |
---|
413 | n/a | class _ValueFormatter: |
---|
414 | n/a | def __init__(self, headerlen, maxlen, continuation_ws, splitchars): |
---|
415 | n/a | self._maxlen = maxlen |
---|
416 | n/a | self._continuation_ws = continuation_ws |
---|
417 | n/a | self._continuation_ws_len = len(continuation_ws) |
---|
418 | n/a | self._splitchars = splitchars |
---|
419 | n/a | self._lines = [] |
---|
420 | n/a | self._current_line = _Accumulator(headerlen) |
---|
421 | n/a | |
---|
422 | n/a | def _str(self, linesep): |
---|
423 | n/a | self.newline() |
---|
424 | n/a | return linesep.join(self._lines) |
---|
425 | n/a | |
---|
426 | n/a | def __str__(self): |
---|
427 | n/a | return self._str(NL) |
---|
428 | n/a | |
---|
429 | n/a | def newline(self): |
---|
430 | n/a | end_of_line = self._current_line.pop() |
---|
431 | n/a | if end_of_line != (' ', ''): |
---|
432 | n/a | self._current_line.push(*end_of_line) |
---|
433 | n/a | if len(self._current_line) > 0: |
---|
434 | n/a | if self._current_line.is_onlyws(): |
---|
435 | n/a | self._lines[-1] += str(self._current_line) |
---|
436 | n/a | else: |
---|
437 | n/a | self._lines.append(str(self._current_line)) |
---|
438 | n/a | self._current_line.reset() |
---|
439 | n/a | |
---|
440 | n/a | def add_transition(self): |
---|
441 | n/a | self._current_line.push(' ', '') |
---|
442 | n/a | |
---|
443 | n/a | def feed(self, fws, string, charset): |
---|
444 | n/a | # If the charset has no header encoding (i.e. it is an ASCII encoding) |
---|
445 | n/a | # then we must split the header at the "highest level syntactic break" |
---|
446 | n/a | # possible. Note that we don't have a lot of smarts about field |
---|
447 | n/a | # syntax; we just try to break on semi-colons, then commas, then |
---|
448 | n/a | # whitespace. Eventually, this should be pluggable. |
---|
449 | n/a | if charset.header_encoding is None: |
---|
450 | n/a | self._ascii_split(fws, string, self._splitchars) |
---|
451 | n/a | return |
---|
452 | n/a | # Otherwise, we're doing either a Base64 or a quoted-printable |
---|
453 | n/a | # encoding which means we don't need to split the line on syntactic |
---|
454 | n/a | # breaks. We can basically just find enough characters to fit on the |
---|
455 | n/a | # current line, minus the RFC 2047 chrome. What makes this trickier |
---|
456 | n/a | # though is that we have to split at octet boundaries, not character |
---|
457 | n/a | # boundaries but it's only safe to split at character boundaries so at |
---|
458 | n/a | # best we can only get close. |
---|
459 | n/a | encoded_lines = charset.header_encode_lines(string, self._maxlengths()) |
---|
460 | n/a | # The first element extends the current line, but if it's None then |
---|
461 | n/a | # nothing more fit on the current line so start a new line. |
---|
462 | n/a | try: |
---|
463 | n/a | first_line = encoded_lines.pop(0) |
---|
464 | n/a | except IndexError: |
---|
465 | n/a | # There are no encoded lines, so we're done. |
---|
466 | n/a | return |
---|
467 | n/a | if first_line is not None: |
---|
468 | n/a | self._append_chunk(fws, first_line) |
---|
469 | n/a | try: |
---|
470 | n/a | last_line = encoded_lines.pop() |
---|
471 | n/a | except IndexError: |
---|
472 | n/a | # There was only one line. |
---|
473 | n/a | return |
---|
474 | n/a | self.newline() |
---|
475 | n/a | self._current_line.push(self._continuation_ws, last_line) |
---|
476 | n/a | # Everything else are full lines in themselves. |
---|
477 | n/a | for line in encoded_lines: |
---|
478 | n/a | self._lines.append(self._continuation_ws + line) |
---|
479 | n/a | |
---|
480 | n/a | def _maxlengths(self): |
---|
481 | n/a | # The first line's length. |
---|
482 | n/a | yield self._maxlen - len(self._current_line) |
---|
483 | n/a | while True: |
---|
484 | n/a | yield self._maxlen - self._continuation_ws_len |
---|
485 | n/a | |
---|
486 | n/a | def _ascii_split(self, fws, string, splitchars): |
---|
487 | n/a | # The RFC 2822 header folding algorithm is simple in principle but |
---|
488 | n/a | # complex in practice. Lines may be folded any place where "folding |
---|
489 | n/a | # white space" appears by inserting a linesep character in front of the |
---|
490 | n/a | # FWS. The complication is that not all spaces or tabs qualify as FWS, |
---|
491 | n/a | # and we are also supposed to prefer to break at "higher level |
---|
492 | n/a | # syntactic breaks". We can't do either of these without intimate |
---|
493 | n/a | # knowledge of the structure of structured headers, which we don't have |
---|
494 | n/a | # here. So the best we can do here is prefer to break at the specified |
---|
495 | n/a | # splitchars, and hope that we don't choose any spaces or tabs that |
---|
496 | n/a | # aren't legal FWS. (This is at least better than the old algorithm, |
---|
497 | n/a | # where we would sometimes *introduce* FWS after a splitchar, or the |
---|
498 | n/a | # algorithm before that, where we would turn all white space runs into |
---|
499 | n/a | # single spaces or tabs.) |
---|
500 | n/a | parts = re.split("(["+FWS+"]+)", fws+string) |
---|
501 | n/a | if parts[0]: |
---|
502 | n/a | parts[:0] = [''] |
---|
503 | n/a | else: |
---|
504 | n/a | parts.pop(0) |
---|
505 | n/a | for fws, part in zip(*[iter(parts)]*2): |
---|
506 | n/a | self._append_chunk(fws, part) |
---|
507 | n/a | |
---|
508 | n/a | def _append_chunk(self, fws, string): |
---|
509 | n/a | self._current_line.push(fws, string) |
---|
510 | n/a | if len(self._current_line) > self._maxlen: |
---|
511 | n/a | # Find the best split point, working backward from the end. |
---|
512 | n/a | # There might be none, on a long first line. |
---|
513 | n/a | for ch in self._splitchars: |
---|
514 | n/a | for i in range(self._current_line.part_count()-1, 0, -1): |
---|
515 | n/a | if ch.isspace(): |
---|
516 | n/a | fws = self._current_line[i][0] |
---|
517 | n/a | if fws and fws[0]==ch: |
---|
518 | n/a | break |
---|
519 | n/a | prevpart = self._current_line[i-1][1] |
---|
520 | n/a | if prevpart and prevpart[-1]==ch: |
---|
521 | n/a | break |
---|
522 | n/a | else: |
---|
523 | n/a | continue |
---|
524 | n/a | break |
---|
525 | n/a | else: |
---|
526 | n/a | fws, part = self._current_line.pop() |
---|
527 | n/a | if self._current_line._initial_size > 0: |
---|
528 | n/a | # There will be a header, so leave it on a line by itself. |
---|
529 | n/a | self.newline() |
---|
530 | n/a | if not fws: |
---|
531 | n/a | # We don't use continuation_ws here because the whitespace |
---|
532 | n/a | # after a header should always be a space. |
---|
533 | n/a | fws = ' ' |
---|
534 | n/a | self._current_line.push(fws, part) |
---|
535 | n/a | return |
---|
536 | n/a | remainder = self._current_line.pop_from(i) |
---|
537 | n/a | self._lines.append(str(self._current_line)) |
---|
538 | n/a | self._current_line.reset(remainder) |
---|
539 | n/a | |
---|
540 | n/a | |
---|
541 | n/a | class _Accumulator(list): |
---|
542 | n/a | |
---|
543 | n/a | def __init__(self, initial_size=0): |
---|
544 | n/a | self._initial_size = initial_size |
---|
545 | n/a | super().__init__() |
---|
546 | n/a | |
---|
547 | n/a | def push(self, fws, string): |
---|
548 | n/a | self.append((fws, string)) |
---|
549 | n/a | |
---|
550 | n/a | def pop_from(self, i=0): |
---|
551 | n/a | popped = self[i:] |
---|
552 | n/a | self[i:] = [] |
---|
553 | n/a | return popped |
---|
554 | n/a | |
---|
555 | n/a | def pop(self): |
---|
556 | n/a | if self.part_count()==0: |
---|
557 | n/a | return ('', '') |
---|
558 | n/a | return super().pop() |
---|
559 | n/a | |
---|
560 | n/a | def __len__(self): |
---|
561 | n/a | return sum((len(fws)+len(part) for fws, part in self), |
---|
562 | n/a | self._initial_size) |
---|
563 | n/a | |
---|
564 | n/a | def __str__(self): |
---|
565 | n/a | return EMPTYSTRING.join((EMPTYSTRING.join((fws, part)) |
---|
566 | n/a | for fws, part in self)) |
---|
567 | n/a | |
---|
568 | n/a | def reset(self, startval=None): |
---|
569 | n/a | if startval is None: |
---|
570 | n/a | startval = [] |
---|
571 | n/a | self[:] = startval |
---|
572 | n/a | self._initial_size = 0 |
---|
573 | n/a | |
---|
574 | n/a | def is_onlyws(self): |
---|
575 | n/a | return self._initial_size==0 and (not self or str(self).isspace()) |
---|
576 | n/a | |
---|
577 | n/a | def part_count(self): |
---|
578 | n/a | return super().__len__() |
---|