| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
"""Header encoding and decoding functionality.""" |
| 6 |
|
| 7 |
__all__ = [ |
| 8 |
'Header', |
| 9 |
'decode_header', |
| 10 |
'make_header', |
| 11 |
] |
| 12 |
|
| 13 |
import re |
| 14 |
import binascii |
| 15 |
|
| 16 |
import email.quoprimime |
| 17 |
import email.base64mime |
| 18 |
|
| 19 |
from email.errors import HeaderParseError |
| 20 |
from email.charset import Charset |
| 21 |
|
| 22 |
NL = '\n' |
| 23 |
SPACE = ' ' |
| 24 |
USPACE = u' ' |
| 25 |
SPACE8 = ' ' * 8 |
| 26 |
UEMPTYSTRING = u'' |
| 27 |
|
| 28 |
MAXLINELEN = 76 |
| 29 |
|
| 30 |
USASCII = Charset('us-ascii') |
| 31 |
UTF8 = Charset('utf-8') |
| 32 |
|
| 33 |
|
| 34 |
ecre = re.compile(r''' |
| 35 |
=\? # literal =? |
| 36 |
(?P<charset>[^?]*?) # non-greedy up to the next ? is the charset |
| 37 |
\? # literal ? |
| 38 |
(?P<encoding>[qb]) # either a "q" or a "b", case insensitive |
| 39 |
\? # literal ? |
| 40 |
(?P<encoded>.*?) # non-greedy up to the next ?= is the encoded string |
| 41 |
\?= # literal ?= |
| 42 |
(?=[ \t]|$) # whitespace or the end of the string |
| 43 |
''', re.VERBOSE | re.IGNORECASE | re.MULTILINE) |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
fcre = re.compile(r'[\041-\176]+:$') |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
_embeded_header = re.compile(r'\n[^ \t]+:') |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
_max_append = email.quoprimime._max_append |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
def decode_header(header): |
| 62 |
"""Decode a message header value without converting charset. |
| 63 |
|
| 64 |
Returns a list of (decoded_string, charset) pairs containing each of the |
| 65 |
decoded parts of the header. Charset is None for non-encoded parts of the |
| 66 |
header, otherwise a lower-case string containing the name of the character |
| 67 |
set specified in the encoded string. |
| 68 |
|
| 69 |
An email.errors.HeaderParseError may be raised when certain decoding error |
| 70 |
occurs (e.g. a base64 decoding exception). |
| 71 |
""" |
| 72 |
|
| 73 |
header = str(header) |
| 74 |
if not ecre.search(header): |
| 75 |
return [(header, None)] |
| 76 |
decoded = [] |
| 77 |
dec = '' |
| 78 |
for line in header.splitlines(): |
| 79 |
|
| 80 |
if not ecre.search(line): |
| 81 |
decoded.append((line, None)) |
| 82 |
continue |
| 83 |
parts = ecre.split(line) |
| 84 |
while parts: |
| 85 |
unenc = parts.pop(0).strip() |
| 86 |
if unenc: |
| 87 |
|
| 88 |
if decoded and decoded[-1][1] is None: |
| 89 |
decoded[-1] = (decoded[-1][0] + SPACE + unenc, None) |
| 90 |
else: |
| 91 |
decoded.append((unenc, None)) |
| 92 |
if parts: |
| 93 |
charset, encoding = [s.lower() for s in parts[0:2]] |
| 94 |
encoded = parts[2] |
| 95 |
dec = None |
| 96 |
if encoding == 'q': |
| 97 |
dec = email.quoprimime.header_decode(encoded) |
| 98 |
elif encoding == 'b': |
| 99 |
paderr = len(encoded) % 4 |
| 100 |
if paderr: |
| 101 |
encoded += '==='[:4 - paderr] |
| 102 |
try: |
| 103 |
dec = email.base64mime.decode(encoded) |
| 104 |
except binascii.Error: |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
raise HeaderParseError |
| 109 |
if dec is None: |
| 110 |
dec = encoded |
| 111 |
|
| 112 |
if decoded and decoded[-1][1] == charset: |
| 113 |
decoded[-1] = (decoded[-1][0] + dec, decoded[-1][1]) |
| 114 |
else: |
| 115 |
decoded.append((dec, charset)) |
| 116 |
del parts[0:3] |
| 117 |
return decoded |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
def make_header(decoded_seq, maxlinelen=None, header_name=None, |
| 122 |
continuation_ws=' '): |
| 123 |
"""Create a Header from a sequence of pairs as returned by decode_header() |
| 124 |
|
| 125 |
decode_header() takes a header value string and returns a sequence of |
| 126 |
pairs of the format (decoded_string, charset) where charset is the string |
| 127 |
name of the character set. |
| 128 |
|
| 129 |
This function takes one of those sequence of pairs and returns a Header |
| 130 |
instance. Optional maxlinelen, header_name, and continuation_ws are as in |
| 131 |
the Header constructor. |
| 132 |
""" |
| 133 |
h = Header(maxlinelen=maxlinelen, header_name=header_name, |
| 134 |
continuation_ws=continuation_ws) |
| 135 |
for s, charset in decoded_seq: |
| 136 |
|
| 137 |
if charset is not None and not isinstance(charset, Charset): |
| 138 |
charset = Charset(charset) |
| 139 |
h.append(s, charset) |
| 140 |
return h |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
class Header: |
| 145 |
def __init__(self, s=None, charset=None, |
| 146 |
maxlinelen=None, header_name=None, |
| 147 |
continuation_ws=' ', errors='strict'): |
| 148 |
"""Create a MIME-compliant header that can contain many character sets. |
| 149 |
|
| 150 |
Optional s is the initial header value. If None, the initial header |
| 151 |
value is not set. You can later append to the header with .append() |
| 152 |
method calls. s may be a byte string or a Unicode string, but see the |
| 153 |
.append() documentation for semantics. |
| 154 |
|
| 155 |
Optional charset serves two purposes: it has the same meaning as the |
| 156 |
charset argument to the .append() method. It also sets the default |
| 157 |
character set for all subsequent .append() calls that omit the charset |
| 158 |
argument. If charset is not provided in the constructor, the us-ascii |
| 159 |
charset is used both as s's initial charset and as the default for |
| 160 |
subsequent .append() calls. |
| 161 |
|
| 162 |
The maximum line length can be specified explicit via maxlinelen. For |
| 163 |
splitting the first line to a shorter value (to account for the field |
| 164 |
header which isn't included in s, e.g. `Subject') pass in the name of |
| 165 |
the field in header_name. The default maxlinelen is 76. |
| 166 |
|
| 167 |
continuation_ws must be RFC 2822 compliant folding whitespace (usually |
| 168 |
either a space or a hard tab) which will be prepended to continuation |
| 169 |
lines. |
| 170 |
|
| 171 |
errors is passed through to the .append() call. |
| 172 |
""" |
| 173 |
if charset is None: |
| 174 |
charset = USASCII |
| 175 |
if not isinstance(charset, Charset): |
| 176 |
charset = Charset(charset) |
| 177 |
self._charset = charset |
| 178 |
self._continuation_ws = continuation_ws |
| 179 |
cws_expanded_len = len(continuation_ws.replace('\t', SPACE8)) |
| 180 |
|
| 181 |
self._chunks = [] |
| 182 |
if s is not None: |
| 183 |
self.append(s, charset, errors) |
| 184 |
if maxlinelen is None: |
| 185 |
maxlinelen = MAXLINELEN |
| 186 |
if header_name is None: |
| 187 |
|
| 188 |
|
| 189 |
self._firstlinelen = maxlinelen |
| 190 |
else: |
| 191 |
|
| 192 |
|
| 193 |
self._firstlinelen = maxlinelen - len(header_name) - 2 |
| 194 |
|
| 195 |
|
| 196 |
self._maxlinelen = maxlinelen - cws_expanded_len |
| 197 |
|
| 198 |
def __str__(self): |
| 199 |
"""A synonym for self.encode().""" |
| 200 |
return self.encode() |
| 201 |
|
| 202 |
def __unicode__(self): |
| 203 |
"""Helper for the built-in unicode function.""" |
| 204 |
uchunks = [] |
| 205 |
lastcs = None |
| 206 |
for s, charset in self._chunks: |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
nextcs = charset |
| 212 |
if uchunks: |
| 213 |
if lastcs not in (None, 'us-ascii'): |
| 214 |
if nextcs in (None, 'us-ascii'): |
| 215 |
uchunks.append(USPACE) |
| 216 |
nextcs = None |
| 217 |
elif nextcs not in (None, 'us-ascii'): |
| 218 |
uchunks.append(USPACE) |
| 219 |
lastcs = nextcs |
| 220 |
uchunks.append(unicode(s, str(charset))) |
| 221 |
return UEMPTYSTRING.join(uchunks) |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
def __eq__(self, other): |
| 226 |
|
| 227 |
|
| 228 |
return other == self.encode() |
| 229 |
|
| 230 |
def __ne__(self, other): |
| 231 |
return not self == other |
| 232 |
|
| 233 |
def append(self, s, charset=None, errors='strict'): |
| 234 |
"""Append a string to the MIME header. |
| 235 |
|
| 236 |
Optional charset, if given, should be a Charset instance or the name |
| 237 |
of a character set (which will be converted to a Charset instance). A |
| 238 |
value of None (the default) means that the charset given in the |
| 239 |
constructor is used. |
| 240 |
|
| 241 |
s may be a byte string or a Unicode string. If it is a byte string |
| 242 |
(i.e. isinstance(s, str) is true), then charset is the encoding of |
| 243 |
that byte string, and a UnicodeError will be raised if the string |
| 244 |
cannot be decoded with that charset. If s is a Unicode string, then |
| 245 |
charset is a hint specifying the character set of the characters in |
| 246 |
the string. In this case, when producing an RFC 2822 compliant header |
| 247 |
using RFC 2047 rules, the Unicode string will be encoded using the |
| 248 |
following charsets in order: us-ascii, the charset hint, utf-8. The |
| 249 |
first character set not to provoke a UnicodeError is used. |
| 250 |
|
| 251 |
Optional `errors' is passed as the third argument to any unicode() or |
| 252 |
ustr.encode() call. |
| 253 |
""" |
| 254 |
if charset is None: |
| 255 |
charset = self._charset |
| 256 |
elif not isinstance(charset, Charset): |
| 257 |
charset = Charset(charset) |
| 258 |
|
| 259 |
if charset != '8bit': |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
if isinstance(s, str): |
| 264 |
|
| 265 |
|
| 266 |
incodec = charset.input_codec or 'us-ascii' |
| 267 |
ustr = unicode(s, incodec, errors) |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
outcodec = charset.output_codec or 'us-ascii' |
| 272 |
ustr.encode(outcodec, errors) |
| 273 |
elif isinstance(s, unicode): |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
for charset in USASCII, charset, UTF8: |
| 278 |
try: |
| 279 |
outcodec = charset.output_codec or 'us-ascii' |
| 280 |
s = s.encode(outcodec, errors) |
| 281 |
break |
| 282 |
except UnicodeError: |
| 283 |
pass |
| 284 |
else: |
| 285 |
assert False, 'utf-8 conversion failed' |
| 286 |
self._chunks.append((s, charset)) |
| 287 |
|
| 288 |
def _split(self, s, charset, maxlinelen, splitchars): |
| 289 |
|
| 290 |
splittable = charset.to_splittable(s) |
| 291 |
encoded = charset.from_splittable(splittable, True) |
| 292 |
elen = charset.encoded_header_len(encoded) |
| 293 |
|
| 294 |
if elen <= maxlinelen: |
| 295 |
return [(encoded, charset)] |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
if charset == '8bit': |
| 303 |
return [(s, charset)] |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
elif charset == 'us-ascii': |
| 316 |
return self._split_ascii(s, charset, maxlinelen, splitchars) |
| 317 |
|
| 318 |
elif elen == len(s): |
| 319 |
|
| 320 |
|
| 321 |
splitpnt = maxlinelen |
| 322 |
first = charset.from_splittable(splittable[:splitpnt], False) |
| 323 |
last = charset.from_splittable(splittable[splitpnt:], False) |
| 324 |
else: |
| 325 |
|
| 326 |
first, last = _binsplit(splittable, charset, maxlinelen) |
| 327 |
|
| 328 |
|
| 329 |
fsplittable = charset.to_splittable(first) |
| 330 |
fencoded = charset.from_splittable(fsplittable, True) |
| 331 |
chunk = [(fencoded, charset)] |
| 332 |
return chunk + self._split(last, charset, self._maxlinelen, splitchars) |
| 333 |
|
| 334 |
def _split_ascii(self, s, charset, firstlen, splitchars): |
| 335 |
chunks = _split_ascii(s, firstlen, self._maxlinelen, |
| 336 |
self._continuation_ws, splitchars) |
| 337 |
return zip(chunks, [charset]*len(chunks)) |
| 338 |
|
| 339 |
def _encode_chunks(self, newchunks, maxlinelen): |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
chunks = [] |
| 358 |
for header, charset in newchunks: |
| 359 |
if not header: |
| 360 |
continue |
| 361 |
if charset is None or charset.header_encoding is None: |
| 362 |
s = header |
| 363 |
else: |
| 364 |
s = charset.header_encode(header) |
| 365 |
|
| 366 |
if chunks and chunks[-1].endswith(' '): |
| 367 |
extra = '' |
| 368 |
else: |
| 369 |
extra = ' ' |
| 370 |
_max_append(chunks, s, maxlinelen, extra) |
| 371 |
joiner = NL + self._continuation_ws |
| 372 |
return joiner.join(chunks) |
| 373 |
|
| 374 |
def encode(self, splitchars=';, '): |
| 375 |
"""Encode a message header into an RFC-compliant format. |
| 376 |
|
| 377 |
There are many issues involved in converting a given string for use in |
| 378 |
an email header. Only certain character sets are readable in most |
| 379 |
email clients, and as header strings can only contain a subset of |
| 380 |
7-bit ASCII, care must be taken to properly convert and encode (with |
| 381 |
Base64 or quoted-printable) header strings. In addition, there is a |
| 382 |
75-character length limit on any given encoded header field, so |
| 383 |
line-wrapping must be performed, even with double-byte character sets. |
| 384 |
|
| 385 |
This method will do its best to convert the string to the correct |
| 386 |
character set used in email, and encode and line wrap it safely with |
| 387 |
the appropriate scheme for that character set. |
| 388 |
|
| 389 |
If the given charset is not known or an error occurs during |
| 390 |
conversion, this function will return the header untouched. |
| 391 |
|
| 392 |
Optional splitchars is a string containing characters to split long |
| 393 |
ASCII lines on, in rough support of RFC 2822's `highest level |
| 394 |
syntactic breaks'. This doesn't affect RFC 2047 encoded lines. |
| 395 |
""" |
| 396 |
newchunks = [] |
| 397 |
maxlinelen = self._firstlinelen |
| 398 |
lastlen = 0 |
| 399 |
for s, charset in self._chunks: |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
targetlen = maxlinelen - lastlen - 1 |
| 404 |
if targetlen < charset.encoded_header_len(''): |
| 405 |
|
| 406 |
targetlen = maxlinelen |
| 407 |
newchunks += self._split(s, charset, targetlen, splitchars) |
| 408 |
lastchunk, lastcharset = newchunks[-1] |
| 409 |
lastlen = lastcharset.encoded_header_len(lastchunk) |
| 410 |
value = self._encode_chunks(newchunks, maxlinelen) |
| 411 |
if _embeded_header.search(value): |
| 412 |
raise HeaderParseError("header value appears to contain " |
| 413 |
"an embedded header: {!r}".format(value)) |
| 414 |
return value |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
def _split_ascii(s, firstlen, restlen, continuation_ws, splitchars): |
| 419 |
lines = [] |
| 420 |
maxlen = firstlen |
| 421 |
for line in s.splitlines(): |
| 422 |
|
| 423 |
|
| 424 |
line = line.lstrip() |
| 425 |
if len(line) < maxlen: |
| 426 |
lines.append(line) |
| 427 |
maxlen = restlen |
| 428 |
continue |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
for ch in splitchars: |
| 434 |
if ch in line: |
| 435 |
break |
| 436 |
else: |
| 437 |
|
| 438 |
|
| 439 |
lines.append(line) |
| 440 |
maxlen = restlen |
| 441 |
continue |
| 442 |
|
| 443 |
cre = re.compile(r'%s\s*' % ch) |
| 444 |
if ch in ';,': |
| 445 |
eol = ch |
| 446 |
else: |
| 447 |
eol = '' |
| 448 |
joiner = eol + ' ' |
| 449 |
joinlen = len(joiner) |
| 450 |
wslen = len(continuation_ws.replace('\t', SPACE8)) |
| 451 |
this = [] |
| 452 |
linelen = 0 |
| 453 |
for part in cre.split(line): |
| 454 |
curlen = linelen + max(0, len(this)-1) * joinlen |
| 455 |
partlen = len(part) |
| 456 |
onfirstline = not lines |
| 457 |
|
| 458 |
|
| 459 |
if ch == ' ' and onfirstline and \ |
| 460 |
len(this) == 1 and fcre.match(this[0]): |
| 461 |
this.append(part) |
| 462 |
linelen += partlen |
| 463 |
elif curlen + partlen > maxlen: |
| 464 |
if this: |
| 465 |
lines.append(joiner.join(this) + eol) |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
if partlen > maxlen and ch != ' ': |
| 470 |
subl = _split_ascii(part, maxlen, restlen, |
| 471 |
continuation_ws, ' ') |
| 472 |
lines.extend(subl[:-1]) |
| 473 |
this = [subl[-1]] |
| 474 |
else: |
| 475 |
this = [part] |
| 476 |
linelen = wslen + len(this[-1]) |
| 477 |
maxlen = restlen |
| 478 |
else: |
| 479 |
this.append(part) |
| 480 |
linelen += partlen |
| 481 |
|
| 482 |
if this: |
| 483 |
lines.append(joiner.join(this)) |
| 484 |
return lines |
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
def _binsplit(splittable, charset, maxlinelen): |
| 489 |
i = 0 |
| 490 |
j = len(splittable) |
| 491 |
while i < j: |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
m = (i+j+1) >> 1 |
| 501 |
chunk = charset.from_splittable(splittable[:m], True) |
| 502 |
chunklen = charset.encoded_header_len(chunk) |
| 503 |
if chunklen <= maxlinelen: |
| 504 |
|
| 505 |
i = m |
| 506 |
else: |
| 507 |
|
| 508 |
j = m - 1 |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
first = charset.from_splittable(splittable[:i], False) |
| 513 |
last = charset.from_splittable(splittable[i:], False) |
| 514 |
return first, last |