blob: 5e24afede013f775e836a5931c35f6ca3edfd1a9 [file] [log] [blame]
Barry Warsaw3d1f3972004-05-09 03:40:17 +00001# Copyright (C) 2002-2004 Python Software Foundation
Barry Warsawbb113862004-10-03 03:16:19 +00002# Author: Ben Gertzfield, Barry Warsaw
3# Contact: email-sig@python.org
Barry Warsaw409a4c02002-04-10 21:01:31 +00004
5"""Header encoding and decoding functionality."""
6
7import re
Barry Warsawe899e512003-03-06 05:39:46 +00008import binascii
Barry Warsaw174aa492002-09-30 15:51:31 +00009
Barry Warsaw409a4c02002-04-10 21:01:31 +000010import email.quopriMIME
11import email.base64MIME
Barry Warsawe899e512003-03-06 05:39:46 +000012from email.Errors import HeaderParseError
Barry Warsaw409a4c02002-04-10 21:01:31 +000013from email.Charset import Charset
14
Barry Warsaw76612502002-06-28 23:46:53 +000015NL = '\n'
Barry Warsawe899e512003-03-06 05:39:46 +000016SPACE = ' '
Barry Warsaw48488052003-03-06 16:10:30 +000017USPACE = u' '
Barry Warsaw76612502002-06-28 23:46:53 +000018SPACE8 = ' ' * 8
Barry Warsaw48488052003-03-06 16:10:30 +000019UEMPTYSTRING = u''
Barry Warsaw409a4c02002-04-10 21:01:31 +000020
21MAXLINELEN = 76
22
Barry Warsaw174aa492002-09-30 15:51:31 +000023USASCII = Charset('us-ascii')
24UTF8 = Charset('utf-8')
25
Barry Warsaw409a4c02002-04-10 21:01:31 +000026# Match encoded-word strings in the form =?charset?q?Hello_World?=
27ecre = re.compile(r'''
28 =\? # literal =?
29 (?P<charset>[^?]*?) # non-greedy up to the next ? is the charset
30 \? # literal ?
31 (?P<encoding>[qb]) # either a "q" or a "b", case insensitive
32 \? # literal ?
33 (?P<encoded>.*?) # non-greedy up to the next ?= is the encoded string
34 \?= # literal ?=
35 ''', re.VERBOSE | re.IGNORECASE)
36
Barry Warsawe899e512003-03-06 05:39:46 +000037# Field name regexp, including trailing colon, but not separating whitespace,
38# according to RFC 2822. Character range is from tilde to exclamation mark.
39# For use with .match()
40fcre = re.compile(r'[\041-\176]+:$')
41
Barry Warsaw409a4c02002-04-10 21:01:31 +000042
43
44# Helpers
45_max_append = email.quopriMIME._max_append
46
47
48
49def decode_header(header):
50 """Decode a message header value without converting charset.
51
52 Returns a list of (decoded_string, charset) pairs containing each of the
53 decoded parts of the header. Charset is None for non-encoded parts of the
54 header, otherwise a lower-case string containing the name of the character
55 set specified in the encoded string.
Barry Warsawe899e512003-03-06 05:39:46 +000056
57 An email.Errors.HeaderParseError may be raised when certain decoding error
58 occurs (e.g. a base64 decoding exception).
Barry Warsaw409a4c02002-04-10 21:01:31 +000059 """
60 # If no encoding, just return the header
61 header = str(header)
62 if not ecre.search(header):
63 return [(header, None)]
Barry Warsaw409a4c02002-04-10 21:01:31 +000064 decoded = []
65 dec = ''
66 for line in header.splitlines():
67 # This line might not have an encoding in it
68 if not ecre.search(line):
69 decoded.append((line, None))
70 continue
Barry Warsaw409a4c02002-04-10 21:01:31 +000071 parts = ecre.split(line)
72 while parts:
73 unenc = parts.pop(0).strip()
74 if unenc:
75 # Should we continue a long line?
76 if decoded and decoded[-1][1] is None:
Barry Warsaw671c3e62003-03-06 06:37:42 +000077 decoded[-1] = (decoded[-1][0] + SPACE + unenc, None)
Barry Warsaw409a4c02002-04-10 21:01:31 +000078 else:
79 decoded.append((unenc, None))
80 if parts:
81 charset, encoding = [s.lower() for s in parts[0:2]]
82 encoded = parts[2]
Barry Warsawe899e512003-03-06 05:39:46 +000083 dec = None
Barry Warsaw409a4c02002-04-10 21:01:31 +000084 if encoding == 'q':
85 dec = email.quopriMIME.header_decode(encoded)
86 elif encoding == 'b':
Barry Warsawe899e512003-03-06 05:39:46 +000087 try:
88 dec = email.base64MIME.decode(encoded)
89 except binascii.Error:
90 # Turn this into a higher level exception. BAW: Right
91 # now we throw the lower level exception away but
92 # when/if we get exception chaining, we'll preserve it.
93 raise HeaderParseError
94 if dec is None:
Barry Warsaw409a4c02002-04-10 21:01:31 +000095 dec = encoded
96
97 if decoded and decoded[-1][1] == charset:
98 decoded[-1] = (decoded[-1][0] + dec, decoded[-1][1])
99 else:
100 decoded.append((dec, charset))
101 del parts[0:3]
102 return decoded
103
104
105
Barry Warsaw8da39aa2002-07-09 16:33:47 +0000106def make_header(decoded_seq, maxlinelen=None, header_name=None,
107 continuation_ws=' '):
108 """Create a Header from a sequence of pairs as returned by decode_header()
109
110 decode_header() takes a header value string and returns a sequence of
111 pairs of the format (decoded_string, charset) where charset is the string
112 name of the character set.
113
114 This function takes one of those sequence of pairs and returns a Header
115 instance. Optional maxlinelen, header_name, and continuation_ws are as in
116 the Header constructor.
117 """
118 h = Header(maxlinelen=maxlinelen, header_name=header_name,
119 continuation_ws=continuation_ws)
120 for s, charset in decoded_seq:
Barry Warsaw15d37392002-07-23 04:29:54 +0000121 # None means us-ascii but we can simply pass it on to h.append()
122 if charset is not None and not isinstance(charset, Charset):
Barry Warsaw8da39aa2002-07-09 16:33:47 +0000123 charset = Charset(charset)
124 h.append(s, charset)
125 return h
126
127
128
Barry Warsaw409a4c02002-04-10 21:01:31 +0000129class Header:
Barry Warsawe899e512003-03-06 05:39:46 +0000130 def __init__(self, s=None, charset=None,
131 maxlinelen=None, header_name=None,
Barry Warsawf4fdff72002-12-30 19:13:00 +0000132 continuation_ws=' ', errors='strict'):
Barry Warsaw174aa492002-09-30 15:51:31 +0000133 """Create a MIME-compliant header that can contain many character sets.
Barry Warsaw409a4c02002-04-10 21:01:31 +0000134
Barry Warsaw174aa492002-09-30 15:51:31 +0000135 Optional s is the initial header value. If None, the initial header
136 value is not set. You can later append to the header with .append()
137 method calls. s may be a byte string or a Unicode string, but see the
138 .append() documentation for semantics.
Barry Warsaw8da39aa2002-07-09 16:33:47 +0000139
Barry Warsaw174aa492002-09-30 15:51:31 +0000140 Optional charset serves two purposes: it has the same meaning as the
141 charset argument to the .append() method. It also sets the default
142 character set for all subsequent .append() calls that omit the charset
143 argument. If charset is not provided in the constructor, the us-ascii
144 charset is used both as s's initial charset and as the default for
145 subsequent .append() calls.
Barry Warsaw409a4c02002-04-10 21:01:31 +0000146
Barry Warsaw76612502002-06-28 23:46:53 +0000147 The maximum line length can be specified explicit via maxlinelen. For
148 splitting the first line to a shorter value (to account for the field
149 header which isn't included in s, e.g. `Subject') pass in the name of
150 the field in header_name. The default maxlinelen is 76.
151
152 continuation_ws must be RFC 2822 compliant folding whitespace (usually
153 either a space or a hard tab) which will be prepended to continuation
154 lines.
Barry Warsawf4fdff72002-12-30 19:13:00 +0000155
156 errors is passed through to the .append() call.
Barry Warsaw409a4c02002-04-10 21:01:31 +0000157 """
158 if charset is None:
Barry Warsaw174aa492002-09-30 15:51:31 +0000159 charset = USASCII
Barry Warsaw5e3bcff2002-10-14 15:13:17 +0000160 if not isinstance(charset, Charset):
161 charset = Charset(charset)
Barry Warsaw409a4c02002-04-10 21:01:31 +0000162 self._charset = charset
Barry Warsaw76612502002-06-28 23:46:53 +0000163 self._continuation_ws = continuation_ws
164 cws_expanded_len = len(continuation_ws.replace('\t', SPACE8))
Barry Warsaw409a4c02002-04-10 21:01:31 +0000165 # BAW: I believe `chunks' and `maxlinelen' should be non-public.
166 self._chunks = []
Barry Warsaw8da39aa2002-07-09 16:33:47 +0000167 if s is not None:
Barry Warsawf4fdff72002-12-30 19:13:00 +0000168 self.append(s, charset, errors)
Barry Warsaw812031b2002-05-19 23:47:53 +0000169 if maxlinelen is None:
Barry Warsaw76612502002-06-28 23:46:53 +0000170 maxlinelen = MAXLINELEN
171 if header_name is None:
172 # We don't know anything about the field header so the first line
173 # is the same length as subsequent lines.
174 self._firstlinelen = maxlinelen
Barry Warsaw812031b2002-05-19 23:47:53 +0000175 else:
Barry Warsaw76612502002-06-28 23:46:53 +0000176 # The first line should be shorter to take into account the field
177 # header. Also subtract off 2 extra for the colon and space.
178 self._firstlinelen = maxlinelen - len(header_name) - 2
179 # Second and subsequent lines should subtract off the length in
180 # columns of the continuation whitespace prefix.
181 self._maxlinelen = maxlinelen - cws_expanded_len
Barry Warsaw409a4c02002-04-10 21:01:31 +0000182
183 def __str__(self):
184 """A synonym for self.encode()."""
185 return self.encode()
186
Barry Warsaw8e69bda2002-06-29 03:26:58 +0000187 def __unicode__(self):
188 """Helper for the built-in unicode function."""
Barry Warsaw48488052003-03-06 16:10:30 +0000189 uchunks = []
190 lastcs = None
191 for s, charset in self._chunks:
192 # We must preserve spaces between encoded and non-encoded word
193 # boundaries, which means for us we need to add a space when we go
194 # from a charset to None/us-ascii, or from None/us-ascii to a
195 # charset. Only do this for the second and subsequent chunks.
196 nextcs = charset
197 if uchunks:
Barry Warsawba1548a2003-03-30 20:46:47 +0000198 if lastcs not in (None, 'us-ascii'):
199 if nextcs in (None, 'us-ascii'):
Barry Warsaw48488052003-03-06 16:10:30 +0000200 uchunks.append(USPACE)
201 nextcs = None
Barry Warsawba1548a2003-03-30 20:46:47 +0000202 elif nextcs not in (None, 'us-ascii'):
Barry Warsaw48488052003-03-06 16:10:30 +0000203 uchunks.append(USPACE)
204 lastcs = nextcs
205 uchunks.append(unicode(s, str(charset)))
206 return UEMPTYSTRING.join(uchunks)
Barry Warsaw8e69bda2002-06-29 03:26:58 +0000207
Barry Warsaw8da39aa2002-07-09 16:33:47 +0000208 # Rich comparison operators for equality only. BAW: does it make sense to
209 # have or explicitly disable <, <=, >, >= operators?
210 def __eq__(self, other):
211 # other may be a Header or a string. Both are fine so coerce
212 # ourselves to a string, swap the args and do another comparison.
213 return other == self.encode()
214
215 def __ne__(self, other):
216 return not self == other
217
Barry Warsawf4fdff72002-12-30 19:13:00 +0000218 def append(self, s, charset=None, errors='strict'):
Barry Warsaw174aa492002-09-30 15:51:31 +0000219 """Append a string to the MIME header.
Barry Warsaw409a4c02002-04-10 21:01:31 +0000220
Barry Warsaw174aa492002-09-30 15:51:31 +0000221 Optional charset, if given, should be a Charset instance or the name
222 of a character set (which will be converted to a Charset instance). A
223 value of None (the default) means that the charset given in the
224 constructor is used.
225
226 s may be a byte string or a Unicode string. If it is a byte string
Barry Warsaw3d1f3972004-05-09 03:40:17 +0000227 (i.e. isinstance(s, str) is true), then charset is the encoding of
228 that byte string, and a UnicodeError will be raised if the string
Barry Warsaw48330682002-09-30 23:07:35 +0000229 cannot be decoded with that charset. If s is a Unicode string, then
Barry Warsaw174aa492002-09-30 15:51:31 +0000230 charset is a hint specifying the character set of the characters in
231 the string. In this case, when producing an RFC 2822 compliant header
232 using RFC 2047 rules, the Unicode string will be encoded using the
Barry Warsaw48330682002-09-30 23:07:35 +0000233 following charsets in order: us-ascii, the charset hint, utf-8. The
234 first character set not to provoke a UnicodeError is used.
Barry Warsawf4fdff72002-12-30 19:13:00 +0000235
236 Optional `errors' is passed as the third argument to any unicode() or
237 ustr.encode() call.
Barry Warsaw409a4c02002-04-10 21:01:31 +0000238 """
239 if charset is None:
240 charset = self._charset
Barry Warsaw92825a92002-07-23 06:08:10 +0000241 elif not isinstance(charset, Charset):
242 charset = Charset(charset)
Barry Warsaw67f8f2f2002-10-14 16:52:41 +0000243 # If the charset is our faux 8bit charset, leave the string unchanged
244 if charset <> '8bit':
245 # We need to test that the string can be converted to unicode and
246 # back to a byte string, given the input and output codecs of the
247 # charset.
Barry Warsaw3d1f3972004-05-09 03:40:17 +0000248 if isinstance(s, str):
Barry Warsaw67f8f2f2002-10-14 16:52:41 +0000249 # Possibly raise UnicodeError if the byte string can't be
250 # converted to a unicode with the input codec of the charset.
251 incodec = charset.input_codec or 'us-ascii'
Barry Warsawf4fdff72002-12-30 19:13:00 +0000252 ustr = unicode(s, incodec, errors)
Barry Warsaw67f8f2f2002-10-14 16:52:41 +0000253 # Now make sure that the unicode could be converted back to a
254 # byte string with the output codec, which may be different
255 # than the iput coded. Still, use the original byte string.
256 outcodec = charset.output_codec or 'us-ascii'
Barry Warsawf4fdff72002-12-30 19:13:00 +0000257 ustr.encode(outcodec, errors)
Barry Warsaw3d1f3972004-05-09 03:40:17 +0000258 elif isinstance(s, unicode):
Barry Warsaw67f8f2f2002-10-14 16:52:41 +0000259 # Now we have to be sure the unicode string can be converted
260 # to a byte string with a reasonable output codec. We want to
261 # use the byte string in the chunk.
262 for charset in USASCII, charset, UTF8:
263 try:
264 outcodec = charset.output_codec or 'us-ascii'
Barry Warsawf4fdff72002-12-30 19:13:00 +0000265 s = s.encode(outcodec, errors)
Barry Warsaw67f8f2f2002-10-14 16:52:41 +0000266 break
267 except UnicodeError:
268 pass
269 else:
270 assert False, 'utf-8 conversion failed'
Barry Warsaw409a4c02002-04-10 21:01:31 +0000271 self._chunks.append((s, charset))
Tim Peters8ac14952002-05-23 15:15:30 +0000272
Barry Warsawe899e512003-03-06 05:39:46 +0000273 def _split(self, s, charset, maxlinelen, splitchars):
Barry Warsaw5e3bcff2002-10-14 15:13:17 +0000274 # Split up a header safely for use with encode_chunks.
Barry Warsaw409a4c02002-04-10 21:01:31 +0000275 splittable = charset.to_splittable(s)
Barry Warsawe899e512003-03-06 05:39:46 +0000276 encoded = charset.from_splittable(splittable, True)
Barry Warsaw812031b2002-05-19 23:47:53 +0000277 elen = charset.encoded_header_len(encoded)
Barry Warsawe899e512003-03-06 05:39:46 +0000278 # If the line's encoded length first, just return it
279 if elen <= maxlinelen:
Barry Warsaw409a4c02002-04-10 21:01:31 +0000280 return [(encoded, charset)]
Barry Warsaw5e3bcff2002-10-14 15:13:17 +0000281 # If we have undetermined raw 8bit characters sitting in a byte
282 # string, we really don't know what the right thing to do is. We
283 # can't really split it because it might be multibyte data which we
284 # could break if we split it between pairs. The least harm seems to
285 # be to not split the header at all, but that means they could go out
286 # longer than maxlinelen.
Barry Warsawe899e512003-03-06 05:39:46 +0000287 if charset == '8bit':
Barry Warsaw5e3bcff2002-10-14 15:13:17 +0000288 return [(s, charset)]
Barry Warsaw76612502002-06-28 23:46:53 +0000289 # BAW: I'm not sure what the right test here is. What we're trying to
290 # do is be faithful to RFC 2822's recommendation that ($2.2.3):
291 #
292 # "Note: Though structured field bodies are defined in such a way that
293 # folding can take place between many of the lexical tokens (and even
294 # within some of the lexical tokens), folding SHOULD be limited to
295 # placing the CRLF at higher-level syntactic breaks."
296 #
297 # For now, I can only imagine doing this when the charset is us-ascii,
298 # although it's possible that other charsets may also benefit from the
299 # higher-level syntactic breaks.
Barry Warsaw76612502002-06-28 23:46:53 +0000300 elif charset == 'us-ascii':
Barry Warsawe899e512003-03-06 05:39:46 +0000301 return self._split_ascii(s, charset, maxlinelen, splitchars)
Barry Warsaw812031b2002-05-19 23:47:53 +0000302 # BAW: should we use encoded?
303 elif elen == len(s):
304 # We can split on _maxlinelen boundaries because we know that the
305 # encoding won't change the size of the string
Barry Warsawe899e512003-03-06 05:39:46 +0000306 splitpnt = maxlinelen
Barry Warsaw174aa492002-09-30 15:51:31 +0000307 first = charset.from_splittable(splittable[:splitpnt], False)
308 last = charset.from_splittable(splittable[splitpnt:], False)
Barry Warsaw409a4c02002-04-10 21:01:31 +0000309 else:
Barry Warsawe899e512003-03-06 05:39:46 +0000310 # Binary search for split point
311 first, last = _binsplit(splittable, charset, maxlinelen)
312 # first is of the proper length so just wrap it in the appropriate
313 # chrome. last must be recursively split.
314 fsplittable = charset.to_splittable(first)
315 fencoded = charset.from_splittable(fsplittable, True)
316 chunk = [(fencoded, charset)]
317 return chunk + self._split(last, charset, self._maxlinelen, splitchars)
Barry Warsaw76612502002-06-28 23:46:53 +0000318
Barry Warsawe899e512003-03-06 05:39:46 +0000319 def _split_ascii(self, s, charset, firstlen, splitchars):
Barry Warsaw5b8c69f2003-03-10 15:14:08 +0000320 chunks = _split_ascii(s, firstlen, self._maxlinelen,
321 self._continuation_ws, splitchars)
322 return zip(chunks, [charset]*len(chunks))
Barry Warsaw76612502002-06-28 23:46:53 +0000323
Barry Warsaw9f3fcd92003-03-07 15:39:37 +0000324 def _encode_chunks(self, newchunks, maxlinelen):
Barry Warsaw0c358252002-10-13 04:06:28 +0000325 # MIME-encode a header with many different charsets and/or encodings.
326 #
327 # Given a list of pairs (string, charset), return a MIME-encoded
328 # string suitable for use in a header field. Each pair may have
329 # different charsets and/or encodings, and the resulting header will
330 # accurately reflect each setting.
331 #
332 # Each encoding can be email.Utils.QP (quoted-printable, for
333 # ASCII-like character sets like iso-8859-1), email.Utils.BASE64
334 # (Base64, for non-ASCII like character sets like KOI8-R and
335 # iso-2022-jp), or None (no encoding).
336 #
337 # Each pair will be represented on a separate line; the resulting
338 # string will be in the format:
339 #
340 # =?charset1?q?Mar=EDa_Gonz=E1lez_Alonso?=\n
341 # =?charset2?b?SvxyZ2VuIEL2aW5n?="
Barry Warsaw76612502002-06-28 23:46:53 +0000342 chunks = []
Barry Warsaw0c358252002-10-13 04:06:28 +0000343 for header, charset in newchunks:
Barry Warsaw6613fb82003-03-17 20:36:20 +0000344 if not header:
345 continue
Barry Warsaw76612502002-06-28 23:46:53 +0000346 if charset is None or charset.header_encoding is None:
Barry Warsawe899e512003-03-06 05:39:46 +0000347 s = header
Barry Warsaw76612502002-06-28 23:46:53 +0000348 else:
Barry Warsawe899e512003-03-06 05:39:46 +0000349 s = charset.header_encode(header)
Barry Warsaw5b8c69f2003-03-10 15:14:08 +0000350 # Don't add more folding whitespace than necessary
351 if chunks and chunks[-1].endswith(' '):
352 extra = ''
353 else:
354 extra = ' '
355 _max_append(chunks, s, maxlinelen, extra)
Barry Warsaw76612502002-06-28 23:46:53 +0000356 joiner = NL + self._continuation_ws
357 return joiner.join(chunks)
Barry Warsaw409a4c02002-04-10 21:01:31 +0000358
Barry Warsawe899e512003-03-06 05:39:46 +0000359 def encode(self, splitchars=';, '):
Barry Warsaw48330682002-09-30 23:07:35 +0000360 """Encode a message header into an RFC-compliant format.
Barry Warsaw409a4c02002-04-10 21:01:31 +0000361
362 There are many issues involved in converting a given string for use in
363 an email header. Only certain character sets are readable in most
364 email clients, and as header strings can only contain a subset of
365 7-bit ASCII, care must be taken to properly convert and encode (with
366 Base64 or quoted-printable) header strings. In addition, there is a
367 75-character length limit on any given encoded header field, so
368 line-wrapping must be performed, even with double-byte character sets.
Tim Peters8ac14952002-05-23 15:15:30 +0000369
Barry Warsaw409a4c02002-04-10 21:01:31 +0000370 This method will do its best to convert the string to the correct
371 character set used in email, and encode and line wrap it safely with
372 the appropriate scheme for that character set.
373
374 If the given charset is not known or an error occurs during
375 conversion, this function will return the header untouched.
Barry Warsawe899e512003-03-06 05:39:46 +0000376
377 Optional splitchars is a string containing characters to split long
378 ASCII lines on, in rough support of RFC 2822's `highest level
379 syntactic breaks'. This doesn't affect RFC 2047 encoded lines.
Barry Warsaw409a4c02002-04-10 21:01:31 +0000380 """
381 newchunks = []
Barry Warsawe899e512003-03-06 05:39:46 +0000382 maxlinelen = self._firstlinelen
383 lastlen = 0
Barry Warsaw409a4c02002-04-10 21:01:31 +0000384 for s, charset in self._chunks:
Barry Warsawe899e512003-03-06 05:39:46 +0000385 # The first bit of the next chunk should be just long enough to
386 # fill the next line. Don't forget the space separating the
387 # encoded words.
388 targetlen = maxlinelen - lastlen - 1
389 if targetlen < charset.encoded_header_len(''):
390 # Stick it on the next line
391 targetlen = maxlinelen
392 newchunks += self._split(s, charset, targetlen, splitchars)
393 lastchunk, lastcharset = newchunks[-1]
394 lastlen = lastcharset.encoded_header_len(lastchunk)
Barry Warsaw9f3fcd92003-03-07 15:39:37 +0000395 return self._encode_chunks(newchunks, maxlinelen)
Barry Warsawe899e512003-03-06 05:39:46 +0000396
397
398
399def _split_ascii(s, firstlen, restlen, continuation_ws, splitchars):
400 lines = []
401 maxlen = firstlen
402 for line in s.splitlines():
Barry Warsaw33975ea2003-03-07 23:24:34 +0000403 # Ignore any leading whitespace (i.e. continuation whitespace) already
404 # on the line, since we'll be adding our own.
405 line = line.lstrip()
Barry Warsawe899e512003-03-06 05:39:46 +0000406 if len(line) < maxlen:
407 lines.append(line)
408 maxlen = restlen
409 continue
410 # Attempt to split the line at the highest-level syntactic break
411 # possible. Note that we don't have a lot of smarts about field
412 # syntax; we just try to break on semi-colons, then commas, then
413 # whitespace.
414 for ch in splitchars:
Barry Warsaw6f3b0332004-05-10 14:44:04 +0000415 if ch in line:
Barry Warsawe899e512003-03-06 05:39:46 +0000416 break
417 else:
418 # There's nothing useful to split the line on, not even spaces, so
419 # just append this line unchanged
420 lines.append(line)
421 maxlen = restlen
422 continue
423 # Now split the line on the character plus trailing whitespace
424 cre = re.compile(r'%s\s*' % ch)
425 if ch in ';,':
426 eol = ch
427 else:
428 eol = ''
429 joiner = eol + ' '
430 joinlen = len(joiner)
431 wslen = len(continuation_ws.replace('\t', SPACE8))
432 this = []
433 linelen = 0
434 for part in cre.split(line):
435 curlen = linelen + max(0, len(this)-1) * joinlen
436 partlen = len(part)
437 onfirstline = not lines
438 # We don't want to split after the field name, if we're on the
439 # first line and the field name is present in the header string.
440 if ch == ' ' and onfirstline and \
441 len(this) == 1 and fcre.match(this[0]):
442 this.append(part)
443 linelen += partlen
444 elif curlen + partlen > maxlen:
445 if this:
446 lines.append(joiner.join(this) + eol)
Barry Warsawbd836df2003-03-06 20:33:04 +0000447 # If this part is longer than maxlen and we aren't already
448 # splitting on whitespace, try to recursively split this line
449 # on whitespace.
450 if partlen > maxlen and ch <> ' ':
Barry Warsaw5b8c69f2003-03-10 15:14:08 +0000451 subl = _split_ascii(part, maxlen, restlen,
Barry Warsaw9f3fcd92003-03-07 15:39:37 +0000452 continuation_ws, ' ')
Barry Warsaw9f3fcd92003-03-07 15:39:37 +0000453 lines.extend(subl[:-1])
454 this = [subl[-1]]
Barry Warsawbd836df2003-03-06 20:33:04 +0000455 else:
456 this = [part]
Barry Warsaw9f3fcd92003-03-07 15:39:37 +0000457 linelen = wslen + len(this[-1])
Barry Warsawe899e512003-03-06 05:39:46 +0000458 maxlen = restlen
459 else:
460 this.append(part)
461 linelen += partlen
462 # Put any left over parts on a line by themselves
463 if this:
464 lines.append(joiner.join(this))
Barry Warsaw5b8c69f2003-03-10 15:14:08 +0000465 return lines
Barry Warsawe899e512003-03-06 05:39:46 +0000466
467
468
469def _binsplit(splittable, charset, maxlinelen):
470 i = 0
471 j = len(splittable)
472 while i < j:
473 # Invariants:
474 # 1. splittable[:k] fits for all k <= i (note that we *assume*,
475 # at the start, that splittable[:0] fits).
476 # 2. splittable[:k] does not fit for any k > j (at the start,
477 # this means we shouldn't look at any k > len(splittable)).
478 # 3. We don't know about splittable[:k] for k in i+1..j.
479 # 4. We want to set i to the largest k that fits, with i <= k <= j.
480 #
481 m = (i+j+1) >> 1 # ceiling((i+j)/2); i < m <= j
482 chunk = charset.from_splittable(splittable[:m], True)
483 chunklen = charset.encoded_header_len(chunk)
484 if chunklen <= maxlinelen:
485 # m is acceptable, so is a new lower bound.
486 i = m
487 else:
Tim Peters2b482132003-03-06 23:41:58 +0000488 # m is not acceptable, so final i must be < m.
Barry Warsawe899e512003-03-06 05:39:46 +0000489 j = m - 1
490 # i == j. Invariant #1 implies that splittable[:i] fits, and
491 # invariant #2 implies that splittable[:i+1] does not fit, so i
492 # is what we're looking for.
493 first = charset.from_splittable(splittable[:i], False)
494 last = charset.from_splittable(splittable[i:], False)
495 return first, last