From 8dbe013e65134001bd48eb54935beb71d7333a37 Mon Sep 17 00:00:00 2001 From: dobesv Date: Thu, 27 Feb 2014 16:22:36 -0800 Subject: Don't incorrectly default charset on FileResponse By passing the content_type into the constructor to Response, we can allow it to decide intelligently whether the default charset should apply. Otherwise we'd have to replicate that logic somehow, or live with weird charset annotations on images, pdfs, and zips. --- pyramid/response.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/pyramid/response.py b/pyramid/response.py index 0f61af472..ff1dd25d5 100644 --- a/pyramid/response.py +++ b/pyramid/response.py @@ -28,6 +28,14 @@ _BLOCK_SIZE = 4096 * 64 # 256K class Response(_Response): pass +def maybe_guess_mimetype(content_type, content_encoding, path): + if content_type is None: + content_type, content_encoding = mimetypes.guess_type(path, + strict=False) + if content_type is None: + content_type = 'application/octet-stream' + return dict(content_type=content_type, content_encoding=content_encoding) + class FileResponse(Response): """ A Response object that can be used to serve a static file from disk @@ -52,15 +60,8 @@ class FileResponse(Response): """ def __init__(self, path, request=None, cache_max_age=None, content_type=None, content_encoding=None): - super(FileResponse, self).__init__(conditional_response=True) + super(FileResponse, self).__init__(conditional_response=True, **maybe_guess_mimetype(content_type, content_encoding, path)) self.last_modified = getmtime(path) - if content_type is None: - content_type, content_encoding = mimetypes.guess_type(path, - strict=False) - if content_type is None: - content_type = 'application/octet-stream' - self.content_type = content_type - self.content_encoding = content_encoding content_length = getsize(path) f = open(path, 'rb') app_iter = None -- cgit v1.2.3