diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2008-08-15 21:03:21 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2008-08-15 21:03:21 (GMT) |
commit | b90a8be96f8b6725fd207c74cb6d3211bf9bd684 (patch) | |
tree | d9c13566b3c28b64bc494fba4b5672d4adc490ff /Lib/email | |
parent | a174a37321aec46cbabb1f1296177cf23087bd56 (diff) | |
download | cpython-b90a8be96f8b6725fd207c74cb6d3211bf9bd684.zip cpython-b90a8be96f8b6725fd207c74cb6d3211bf9bd684.tar.gz cpython-b90a8be96f8b6725fd207c74cb6d3211bf9bd684.tar.bz2 |
#2676: email/message.py [Message.get_content_type]: Trivial regex hangs on pathological input
Diffstat (limited to 'Lib/email')
-rw-r--r-- | Lib/email/message.py | 18 |
1 files changed, 11 insertions, 7 deletions
diff --git a/Lib/email/message.py b/Lib/email/message.py index 287232b..e44217b 100644 --- a/Lib/email/message.py +++ b/Lib/email/message.py @@ -19,18 +19,22 @@ from email import errors SEMISPACE = '; ' -# Regular expression used to split header parameters. BAW: this may be too -# simple. It isn't strictly RFC 2045 (section 5.1) compliant, but it catches -# most headers found in the wild. We may eventually need a full fledged -# parser eventually. -paramre = re.compile(r'\s*;\s*') # Regular expression that matches `special' characters in parameters, the # existance of which force quoting of the parameter value. tspecials = re.compile(r'[ \(\)<>@,;:\\"/\[\]\?=]') - # Helper functions +def _splitparam(param): + # Split header parameters. BAW: this may be too simple. It isn't + # strictly RFC 2045 (section 5.1) compliant, but it catches most headers + # found in the wild. We may eventually need a full fledged parser + # eventually. + a, sep, b = param.partition(';') + if not sep: + return a.strip(), None + return a.strip(), b.strip() + def _formatparam(param, value=None, quote=True): """Convenience function to format and return a key=value pair. @@ -436,7 +440,7 @@ class Message: if value is missing: # This should have no parameters return self.get_default_type() - ctype = paramre.split(value)[0].lower().strip() + ctype = _splitparam(value)[0].lower() # RFC 2045, section 5.2 says if its invalid, use text/plain if ctype.count('/') != 1: return 'text/plain' |