diff options
| author | dobesv <dobesv@gmail.com> | 2014-02-27 16:22:36 -0800 |
|---|---|---|
| committer | Michael Merickel <michael@merickel.org> | 2014-05-16 21:42:02 -0500 |
| commit | 8dbe013e65134001bd48eb54935beb71d7333a37 (patch) | |
| tree | 5659d8e73d42aebed896b621b2d877bd5e3620cd | |
| parent | 61afb8909caa8e1cc3c6d36023aab6b96fcf7171 (diff) | |
| download | pyramid-8dbe013e65134001bd48eb54935beb71d7333a37.tar.gz pyramid-8dbe013e65134001bd48eb54935beb71d7333a37.tar.bz2 pyramid-8dbe013e65134001bd48eb54935beb71d7333a37.zip | |
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.
| -rw-r--r-- | pyramid/response.py | 17 |
1 files 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 |
