summaryrefslogtreecommitdiff
path: root/odoo/http.py
diff options
context:
space:
mode:
Diffstat (limited to 'odoo/http.py')
-rw-r--r--odoo/http.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/odoo/http.py b/odoo/http.py
index ddb18eb1048..750c0b3c0d5 100644
--- a/odoo/http.py
+++ b/odoo/http.py
@@ -50,6 +50,7 @@ from .service.server import memory_info
from .service import security, model as service_model
from .tools.func import lazy_property
from .tools import ustr, consteq, frozendict, pycompat, unique, date_utils
+from .tools.mimetypes import guess_mimetype
from .modules.module import module_manifest
@@ -1617,5 +1618,39 @@ def content_disposition(filename):
return "attachment; filename*=UTF-8''%s" % escaped
+
+def set_safe_image_headers(headers, content):
+ """Return new headers based on `headers` but with `Content-Length` and
+ `Content-Type` set appropriately depending on the given `content` only if it
+ is safe to do."""
+ content_type = guess_mimetype(content)
+ safe_types = ['image/jpeg', 'image/png', 'image/gif', 'image/x-icon']
+ if content_type in safe_types:
+ headers = set_header_field(headers, 'Content-Type', content_type)
+ set_header_field(headers, 'Content-Length', len(content))
+ return headers
+
+
+def set_header_field(headers, name, value):
+ """ Return new headers based on `headers` but with `value` set for the
+ header field `name`.
+
+ :param headers: the existing headers
+ :type headers: list of tuples (name, value)
+
+ :param name: the header field name
+ :type name: string
+
+ :param value: the value to set for the `name` header
+ :type value: string
+
+ :return: the updated headers
+ :rtype: list of tuples (name, value)
+ """
+ dictheaders = dict(headers)
+ dictheaders[name] = value
+ return list(dictheaders.items())
+
+
# main wsgi handler
root = Root()