From 763263d157970546cfa09d44e2a9645cb7ae0f65 Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Thu, 28 Apr 2016 15:26:22 +0530 Subject: [PATCH] [fix] use response.mimetype and response.charset instead of setting 'Content-Type' directly in response.header --- frappe/utils/response.py | 10 ++++++---- frappe/website/render.py | 12 +++++++----- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/frappe/utils/response.py b/frappe/utils/response.py index 94c9f04892..e5a892fe49 100644 --- a/frappe/utils/response.py +++ b/frappe/utils/response.py @@ -43,14 +43,15 @@ def build_response(response_type=None): def as_csv(): response = Response() - response.headers[b"Content-Type"] = b"text/csv; charset: utf-8" + response.mimetype = 'text/csv' + response.charset = 'utf-8' response.headers[b"Content-Disposition"] = ("attachment; filename=\"%s.csv\"" % frappe.response['doctype'].replace(' ', '_')).encode("utf-8") response.data = frappe.response['result'] return response def as_raw(): response = Response() - response.headers[b"Content-Type"] = frappe.response.get("content_type") or mimetypes.guess_type(frappe.response['filename'])[0] or b"application/unknown" + response.mimetype = frappe.response.get("content_type") or mimetypes.guess_type(frappe.response['filename'])[0] or b"application/unknown" response.headers[b"Content-Disposition"] = ("filename=\"%s\"" % frappe.response['filename'].replace(' ', '_')).encode("utf-8") response.data = frappe.response['filecontent'] return response @@ -62,7 +63,8 @@ def as_json(): response.status_code = frappe.local.response['http_status_code'] del frappe.local.response['http_status_code'] - response.headers[b"Content-Type"] = b"application/json; charset: utf-8" + response.mimetype = 'application/json' + response.charset = 'utf-8' response.data = json.dumps(frappe.local.response, default=json_handler, separators=(',',':')) return response @@ -150,7 +152,7 @@ def send_private_file(path): # no need for content disposition and force download. let browser handle its opening. # response.headers.add(b'Content-Disposition', b'attachment', filename=filename.encode("utf-8")) - response.headers[b'Content-Type'] = mimetypes.guess_type(filename)[0] or b'application/octet-stream' + response.mimetype = mimetypes.guess_type(filename)[0] or b'application/octet-stream' return response diff --git a/frappe/website/render.py b/frappe/website/render.py index 1423a33ff3..50c7a373a8 100644 --- a/frappe/website/render.py +++ b/frappe/website/render.py @@ -172,17 +172,19 @@ def resolve_from_map(path): def set_content_type(response, data, path): if isinstance(data, dict): - response.headers[b"Content-Type"] = b"application/json; charset: utf-8" + response.mimetype = 'application/json' + response.charset = 'utf-8' data = json.dumps(data) return data - response.headers[b"Content-Type"] = b"text/html; charset: utf-8" + response.mimetype = 'text/html' + response.charset = 'utf-8' if "." in path: content_type, encoding = mimetypes.guess_type(path) - if not content_type: - content_type = "text/html; charset: utf-8" - response.headers[b"Content-Type"] = content_type.encode("utf-8") + if content_type: + response.mimetype = content_type + response.charset = encoding return data