mirror of
https://github.com/AdaCore/cpython.git
synced 2026-02-12 12:57:15 -08:00
decode_rfc2231(): Be more robust against buggy RFC 2231 encodings. Specifically, instead of raising a ValueError when there is a single tick in the parameter, simply return that the entire string unquoted, with None for both the charset and the language. Also, if there are more than 2 ticks in the parameter, interpret the first three parts as the standard RFC 2231 parts, then the rest of the parts as the encoded string. More RFC 2231 improvements for the email 4.0 package. As Mark Sapiro rightly points out there are really two types of continued headers defined in this RFC (i.e. "encoded" parameters with the form "name*0*=" and unencoded parameters with the form "name*0="), but we were were handling them both the same way and that isn't correct. This patch should be much more RFC compliant in that only encoded params are %-decoded and the charset/language information is only extract if there are any encoded params in the segments. If there are no encoded params then the RFC says that there will be no charset/language parts. Note however that this will change the return value for Message.get_param() in some cases. For example, whereas before if you had all unencoded param continuations you would have still gotten a 3-tuple back from this method (with charset and language == None), you will now get just a string. I don't believe this is a backward incompatible change though because the documentation for this method already indicates that either return value is possible and that you must do an isinstance(val, tuple) check to discriminate between the two. (Yeah that API kind of sucks but we can't change /that/ without breaking code.) Test cases, some documentation updates, and a NEWS item accompany this patch. Original fewer-than-3-parts fix by Tokio Kikuchi. Resolves SF bug # 1218081. Also, bump the package version number to 2.5.8 for release.
362 lines
11 KiB
Python
362 lines
11 KiB
Python
# Copyright (C) 2001-2006 Python Software Foundation
|
||
# Author: Barry Warsaw
|
||
# Contact: email-sig@python.org
|
||
|
||
"""Miscellaneous utilities."""
|
||
|
||
import time
|
||
import socket
|
||
import re
|
||
import random
|
||
import os
|
||
import urllib
|
||
import warnings
|
||
from cStringIO import StringIO
|
||
from types import ListType
|
||
|
||
from email._parseaddr import quote
|
||
from email._parseaddr import AddressList as _AddressList
|
||
from email._parseaddr import mktime_tz
|
||
|
||
# We need wormarounds for bugs in these methods in older Pythons (see below)
|
||
from email._parseaddr import parsedate as _parsedate
|
||
from email._parseaddr import parsedate_tz as _parsedate_tz
|
||
|
||
try:
|
||
True, False
|
||
except NameError:
|
||
True = 1
|
||
False = 0
|
||
|
||
try:
|
||
from quopri import decodestring as _qdecode
|
||
except ImportError:
|
||
# Python 2.1 doesn't have quopri.decodestring()
|
||
def _qdecode(s):
|
||
import quopri as _quopri
|
||
|
||
if not s:
|
||
return s
|
||
infp = StringIO(s)
|
||
outfp = StringIO()
|
||
_quopri.decode(infp, outfp)
|
||
value = outfp.getvalue()
|
||
if not s.endswith('\n') and value.endswith('\n'):
|
||
return value[:-1]
|
||
return value
|
||
|
||
import base64
|
||
|
||
# Intrapackage imports
|
||
from email.Encoders import _bencode, _qencode
|
||
|
||
COMMASPACE = ', '
|
||
EMPTYSTRING = ''
|
||
UEMPTYSTRING = u''
|
||
CRLF = '\r\n'
|
||
TICK = "'"
|
||
|
||
specialsre = re.compile(r'[][\\()<>@,:;".]')
|
||
escapesre = re.compile(r'[][\\()"]')
|
||
|
||
|
||
|
||
# Helpers
|
||
|
||
def _identity(s):
|
||
return s
|
||
|
||
|
||
def _bdecode(s):
|
||
# We can't quite use base64.encodestring() since it tacks on a "courtesy
|
||
# newline". Blech!
|
||
if not s:
|
||
return s
|
||
value = base64.decodestring(s)
|
||
if not s.endswith('\n') and value.endswith('\n'):
|
||
return value[:-1]
|
||
return value
|
||
|
||
|
||
|
||
def fix_eols(s):
|
||
"""Replace all line-ending characters with \r\n."""
|
||
# Fix newlines with no preceding carriage return
|
||
s = re.sub(r'(?<!\r)\n', CRLF, s)
|
||
# Fix carriage returns with no following newline
|
||
s = re.sub(r'\r(?!\n)', CRLF, s)
|
||
return s
|
||
|
||
|
||
|
||
def formataddr(pair):
|
||
"""The inverse of parseaddr(), this takes a 2-tuple of the form
|
||
(realname, email_address) and returns the string value suitable
|
||
for an RFC 2822 From, To or Cc header.
|
||
|
||
If the first element of pair is false, then the second element is
|
||
returned unmodified.
|
||
"""
|
||
name, address = pair
|
||
if name:
|
||
quotes = ''
|
||
if specialsre.search(name):
|
||
quotes = '"'
|
||
name = escapesre.sub(r'\\\g<0>', name)
|
||
return '%s%s%s <%s>' % (quotes, name, quotes, address)
|
||
return address
|
||
|
||
# For backwards compatibility
|
||
def dump_address_pair(pair):
|
||
warnings.warn('Use email.Utils.formataddr() instead',
|
||
DeprecationWarning, 2)
|
||
return formataddr(pair)
|
||
|
||
|
||
|
||
def getaddresses(fieldvalues):
|
||
"""Return a list of (REALNAME, EMAIL) for each fieldvalue."""
|
||
all = COMMASPACE.join(fieldvalues)
|
||
a = _AddressList(all)
|
||
return a.addresslist
|
||
|
||
|
||
|
||
ecre = re.compile(r'''
|
||
=\? # literal =?
|
||
(?P<charset>[^?]*?) # non-greedy up to the next ? is the charset
|
||
\? # literal ?
|
||
(?P<encoding>[qb]) # either a "q" or a "b", case insensitive
|
||
\? # literal ?
|
||
(?P<atom>.*?) # non-greedy up to the next ?= is the atom
|
||
\?= # literal ?=
|
||
''', re.VERBOSE | re.IGNORECASE)
|
||
|
||
|
||
def decode(s):
|
||
"""Return a decoded string according to RFC 2047, as a unicode string.
|
||
|
||
NOTE: This function is deprecated. Use Header.decode_header() instead.
|
||
"""
|
||
warnings.warn('Use Header.decode_header() instead.', DeprecationWarning, 2)
|
||
# Intra-package import here to avoid circular import problems.
|
||
from email.Header import decode_header
|
||
L = decode_header(s)
|
||
if not isinstance(L, ListType):
|
||
# s wasn't decoded
|
||
return s
|
||
|
||
rtn = []
|
||
for atom, charset in L:
|
||
if charset is None:
|
||
rtn.append(atom)
|
||
else:
|
||
# Convert the string to Unicode using the given encoding. Leave
|
||
# Unicode conversion errors to strict.
|
||
rtn.append(unicode(atom, charset))
|
||
# Now that we've decoded everything, we just need to join all the parts
|
||
# together into the final string.
|
||
return UEMPTYSTRING.join(rtn)
|
||
|
||
|
||
|
||
def encode(s, charset='iso-8859-1', encoding='q'):
|
||
"""Encode a string according to RFC 2047."""
|
||
warnings.warn('Use Header.Header.encode() instead.', DeprecationWarning, 2)
|
||
encoding = encoding.lower()
|
||
if encoding == 'q':
|
||
estr = _qencode(s)
|
||
elif encoding == 'b':
|
||
estr = _bencode(s)
|
||
else:
|
||
raise ValueError, 'Illegal encoding code: ' + encoding
|
||
return '=?%s?%s?%s?=' % (charset.lower(), encoding, estr)
|
||
|
||
|
||
|
||
def formatdate(timeval=None, localtime=False):
|
||
"""Returns a date string as specified by RFC 2822, e.g.:
|
||
|
||
Fri, 09 Nov 2001 01:08:47 -0000
|
||
|
||
Optional timeval if given is a floating point time value as accepted by
|
||
gmtime() and localtime(), otherwise the current time is used.
|
||
|
||
Optional localtime is a flag that when True, interprets timeval, and
|
||
returns a date relative to the local timezone instead of UTC, properly
|
||
taking daylight savings time into account.
|
||
"""
|
||
# Note: we cannot use strftime() because that honors the locale and RFC
|
||
# 2822 requires that day and month names be the English abbreviations.
|
||
if timeval is None:
|
||
timeval = time.time()
|
||
if localtime:
|
||
now = time.localtime(timeval)
|
||
# Calculate timezone offset, based on whether the local zone has
|
||
# daylight savings time, and whether DST is in effect.
|
||
if time.daylight and now[-1]:
|
||
offset = time.altzone
|
||
else:
|
||
offset = time.timezone
|
||
hours, minutes = divmod(abs(offset), 3600)
|
||
# Remember offset is in seconds west of UTC, but the timezone is in
|
||
# minutes east of UTC, so the signs differ.
|
||
if offset > 0:
|
||
sign = '-'
|
||
else:
|
||
sign = '+'
|
||
zone = '%s%02d%02d' % (sign, hours, minutes / 60)
|
||
else:
|
||
now = time.gmtime(timeval)
|
||
# Timezone offset is always -0000
|
||
zone = '-0000'
|
||
return '%s, %02d %s %04d %02d:%02d:%02d %s' % (
|
||
['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'][now[6]],
|
||
now[2],
|
||
['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
|
||
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][now[1] - 1],
|
||
now[0], now[3], now[4], now[5],
|
||
zone)
|
||
|
||
|
||
|
||
def make_msgid(idstring=None):
|
||
"""Returns a string suitable for RFC 2822 compliant Message-ID, e.g:
|
||
|
||
<20020201195627.33539.96671@nightshade.la.mastaler.com>
|
||
|
||
Optional idstring if given is a string used to strengthen the
|
||
uniqueness of the message id.
|
||
"""
|
||
timeval = time.time()
|
||
utcdate = time.strftime('%Y%m%d%H%M%S', time.gmtime(timeval))
|
||
pid = os.getpid()
|
||
randint = random.randrange(100000)
|
||
if idstring is None:
|
||
idstring = ''
|
||
else:
|
||
idstring = '.' + idstring
|
||
idhost = socket.getfqdn()
|
||
msgid = '<%s.%s.%s%s@%s>' % (utcdate, pid, randint, idstring, idhost)
|
||
return msgid
|
||
|
||
|
||
|
||
# These functions are in the standalone mimelib version only because they've
|
||
# subsequently been fixed in the latest Python versions. We use this to worm
|
||
# around broken older Pythons.
|
||
def parsedate(data):
|
||
if not data:
|
||
return None
|
||
return _parsedate(data)
|
||
|
||
|
||
def parsedate_tz(data):
|
||
if not data:
|
||
return None
|
||
return _parsedate_tz(data)
|
||
|
||
|
||
def parseaddr(addr):
|
||
addrs = _AddressList(addr).addresslist
|
||
if not addrs:
|
||
return '', ''
|
||
return addrs[0]
|
||
|
||
|
||
# rfc822.unquote() doesn't properly de-backslash-ify in Python pre-2.3.
|
||
def unquote(str):
|
||
"""Remove quotes from a string."""
|
||
if len(str) > 1:
|
||
if str.startswith('"') and str.endswith('"'):
|
||
return str[1:-1].replace('\\\\', '\\').replace('\\"', '"')
|
||
if str.startswith('<') and str.endswith('>'):
|
||
return str[1:-1]
|
||
return str
|
||
|
||
|
||
|
||
# RFC2231-related functions - parameter encoding and decoding
|
||
def decode_rfc2231(s):
|
||
"""Decode string according to RFC 2231"""
|
||
parts = s.split(TICK, 2)
|
||
if len(parts) <= 2:
|
||
return None, None, urllib.unquote(s)
|
||
if len(parts) > 3:
|
||
charset, language = pars[:2]
|
||
s = TICK.join(parts[2:])
|
||
return charset, language, s
|
||
return parts
|
||
|
||
|
||
def encode_rfc2231(s, charset=None, language=None):
|
||
"""Encode string according to RFC 2231.
|
||
|
||
If neither charset nor language is given, then s is returned as-is. If
|
||
charset is given but not language, the string is encoded using the empty
|
||
string for language.
|
||
"""
|
||
import urllib
|
||
s = urllib.quote(s, safe='')
|
||
if charset is None and language is None:
|
||
return s
|
||
if language is None:
|
||
language = ''
|
||
return "%s'%s'%s" % (charset, language, s)
|
||
|
||
|
||
rfc2231_continuation = re.compile(r'^(?P<name>\w+)\*((?P<num>[0-9]+)\*?)?$')
|
||
|
||
def decode_params(params):
|
||
"""Decode parameters list according to RFC 2231.
|
||
|
||
params is a sequence of 2-tuples containing (param name, string value).
|
||
"""
|
||
# Copy params so we don't mess with the original
|
||
params = params[:]
|
||
new_params = []
|
||
# Map parameter's name to a list of continuations. The values are a
|
||
# 3-tuple of the continuation number, the string value, and a flag
|
||
# specifying whether a particular segment is %-encoded.
|
||
rfc2231_params = {}
|
||
name, value = params.pop(0)
|
||
new_params.append((name, value))
|
||
while params:
|
||
name, value = params.pop(0)
|
||
if name.endswith('*'):
|
||
encoded = True
|
||
else:
|
||
encoded = False
|
||
value = unquote(value)
|
||
mo = rfc2231_continuation.match(name)
|
||
if mo:
|
||
name, num = mo.group('name', 'num')
|
||
if num is not None:
|
||
num = int(num)
|
||
rfc2231_params.setdefault(name, []).append((num, value, encoded))
|
||
else:
|
||
new_params.append((name, '"%s"' % quote(value)))
|
||
if rfc2231_params:
|
||
for name, continuations in rfc2231_params.items():
|
||
value = []
|
||
extended = False
|
||
# Sort by number
|
||
continuations.sort()
|
||
# And now append all values in numerical order, converting
|
||
# %-encodings for the encoded segments. If any of the
|
||
# continuation names ends in a *, then the entire string, after
|
||
# decoding segments and concatenating, must have the charset and
|
||
# language specifiers at the beginning of the string.
|
||
for num, s, encoded in continuations:
|
||
if encoded:
|
||
s = urllib.unquote(s)
|
||
extended = True
|
||
value.append(s)
|
||
value = quote(EMPTYSTRING.join(value))
|
||
if extended:
|
||
charset, language, value = decode_rfc2231(value)
|
||
new_params.append((name, (charset, language, '"%s"' % value)))
|
||
else:
|
||
new_params.append((name, '"%s"' % value))
|
||
return new_params
|