Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f36d804

Browse files
committed
get_content_type(), get_content_maintype(), get_content_subtype(): RFC
2045, section 5.2 states that if the Content-Type: header is syntactically invalid, the default type should be text/plain. Implement minimal sanity checking of the header -- it must have exactly one slash in it. This closes SF patch #597593 by Skip, but in a different way. Note that these methods used to raise ValueError for invalid ctypes, but now they won't.
1 parent dfea3b3 commit f36d804

1 file changed

Lines changed: 5 additions & 5 deletions

File tree

Lib/email/Message.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,11 @@ def get_content_type(self):
422422
if value is missing:
423423
# This should have no parameters
424424
return self.get_default_type()
425-
return paramre.split(value)[0].lower().strip()
425+
ctype = paramre.split(value)[0].lower().strip()
426+
# RFC 2045, section 5.2 says if its invalid, use text/plain
427+
if ctype.count('/') <> 1:
428+
return 'text/plain'
429+
return ctype
426430

427431
def get_content_maintype(self):
428432
"""Returns the message's main content type.
@@ -432,8 +436,6 @@ def get_content_maintype(self):
432436
ValueError is raised.
433437
"""
434438
ctype = self.get_content_type()
435-
if ctype.count('/') <> 1:
436-
raise ValueError, 'No maintype found in: %s' % ctype
437439
return ctype.split('/')[0]
438440

439441
def get_content_subtype(self):
@@ -444,8 +446,6 @@ def get_content_subtype(self):
444446
ValueError is raised.
445447
"""
446448
ctype = self.get_content_type()
447-
if ctype.count('/') <> 1:
448-
raise ValueError, 'No subtype found in: %s' % ctype
449449
return ctype.split('/')[1]
450450

451451
def get_default_type(self):

0 commit comments

Comments
 (0)