summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhuangtianhua <huangtianhua@huawei.com>2016-07-07 10:23:42 +0800
committerhuangtianhua <huangtianhua@huawei.com>2017-01-14 10:30:32 +0800
commit9bc4d0ecf65997af4f819393786b7cd95d47e552 (patch)
tree890a4d0202e28a4ee91e0b2a0ff90d305ab3627e
parent91a8c498290be51254238b121b9e52a1285a6772 (diff)
downloadheat-9bc4d0ecf65997af4f819393786b7cd95d47e552.tar.gz
Use correct charset when create a text/* type MIME
Closes-Bug: #1599294 (cherry picked from commit 587d12897f6a2a93d19ca101f0afaf8f15969f16) Conflicts: heat/engine/clients/os/nova.py Change-Id: If0a0b8f906c9f4475156600e792021d7d9adc8f3
-rw-r--r--heat/engine/clients/os/nova.py10
-rw-r--r--heat/engine/resources/openstack/heat/multi_part.py11
2 files changed, 18 insertions, 3 deletions
diff --git a/heat/engine/clients/os/nova.py b/heat/engine/clients/os/nova.py
index 9d60c1c49..2698c5b1b 100644
--- a/heat/engine/clients/os/nova.py
+++ b/heat/engine/clients/os/nova.py
@@ -311,7 +311,15 @@ class NovaClientPlugin(client_plugin.ClientPlugin):
subtype = os.path.splitext(filename)[0]
if content is None:
content = ''
- msg = text.MIMEText(content, _subtype=subtype)
+
+ try:
+ content.encode('us-ascii')
+ charset = 'us-ascii'
+ except UnicodeEncodeError:
+ charset = 'utf-8'
+ msg = (text.MIMEText(content, _subtype=subtype, _charset=charset)
+ if subtype else text.MIMEText(content, _charset=charset))
+
msg.add_header('Content-Disposition', 'attachment',
filename=filename)
return msg
diff --git a/heat/engine/resources/openstack/heat/multi_part.py b/heat/engine/resources/openstack/heat/multi_part.py
index e18ca950f..b9c278e18 100644
--- a/heat/engine/resources/openstack/heat/multi_part.py
+++ b/heat/engine/resources/openstack/heat/multi_part.py
@@ -154,8 +154,15 @@ class MultipartMime(software_config.SoftwareConfig):
@staticmethod
def _create_message(part, subtype, filename):
- msg = (text.MIMEText(part, _subtype=subtype)
- if subtype else text.MIMEText(part))
+ charset = 'us-ascii'
+ try:
+ part.encode(charset)
+ except UnicodeEncodeError:
+ charset = 'utf-8'
+ msg = (text.MIMEText(part, _subtype=subtype,
+ _charset=charset)
+ if subtype else text.MIMEText(part, _charset=charset))
+
if filename:
msg.add_header('Content-Disposition', 'attachment',
filename=filename)