Port parse_mime_headers() to Python 3
Port swift.common.utils.parse_mime_headers() to Python 3: * On Python 3, tries to decode headers from UTF-8. If an header was was not encoded to UTF-8, decode the header from Latin1. * Update the parse_mime_headers() tests: on Python 3, HTTP header values are Unicode strings. This change is a follow-up of the change Ia5ee2ead67e36e8c6416183667f64ae255887736. Change-Id: I042dd13e9eb0e9844ccd832d538cdac84359ed42
This commit is contained in:
parent
b9fd530657
commit
d9b22ac51c
@ -3421,11 +3421,21 @@ def parse_mime_headers(doc_file):
|
||||
headers = []
|
||||
while True:
|
||||
line = doc_file.readline()
|
||||
done = line in (b'\r\n', b'\n', b'')
|
||||
if six.PY3:
|
||||
try:
|
||||
line = line.decode('utf-8')
|
||||
except UnicodeDecodeError:
|
||||
line = line.decode('latin1')
|
||||
headers.append(line)
|
||||
if line in (b'\r\n', b'\n', b''):
|
||||
if done:
|
||||
break
|
||||
header_string = b''.join(headers)
|
||||
return HeaderKeyDict(email.parser.Parser().parsestr(header_string))
|
||||
if six.PY3:
|
||||
header_string = ''.join(headers)
|
||||
else:
|
||||
header_string = b''.join(headers)
|
||||
headers = email.parser.Parser().parsestr(header_string)
|
||||
return HeaderKeyDict(headers)
|
||||
|
||||
|
||||
def mime_to_document_iters(input_file, boundary, read_chunk_size=4096):
|
||||
|
@ -5006,6 +5006,10 @@ Utf-8: \xd0\xba\xd0\xbe\xd0\xbd\xd1\x82\xd0\xb5\xd0\xb9\xd0\xbd\xd0\xb5\xd1\x80
|
||||
This is the body
|
||||
""")
|
||||
headers = utils.parse_mime_headers(doc_file)
|
||||
utf8 = u'\u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440'
|
||||
if six.PY2:
|
||||
utf8 = utf8.encode('utf-8')
|
||||
|
||||
expected_headers = {
|
||||
'Content-Disposition': 'form-data; name="file_size"',
|
||||
'Foo': "Bar",
|
||||
@ -5015,8 +5019,7 @@ This is the body
|
||||
'Connexion': "=?iso8859-1?q?r=E9initialis=E9e_par_l=27homologue?=",
|
||||
'Status': "=?utf-8?b?5byA5aeL6YCa6L+H5a+56LGh5aSN5Yi2?=",
|
||||
'Latin-1': "Resincronizaci\xf3n realizada con \xe9xito",
|
||||
'Utf-8': ("\xd0\xba\xd0\xbe\xd0\xbd\xd1\x82\xd0\xb5\xd0\xb9\xd0"
|
||||
"\xbd\xd0\xb5\xd1\x80")
|
||||
'Utf-8': utf8,
|
||||
}
|
||||
self.assertEqual(expected_headers, headers)
|
||||
self.assertEqual(b"This is the body\n", doc_file.read())
|
||||
|
Loading…
x
Reference in New Issue
Block a user