diff --git a/CHANGES.rst b/CHANGES.rst index a8d96a10de..1f175d84b5 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -23,6 +23,8 @@ Bugfixes - Fix unpickling of instances created before 4.0b2 those classes changed from old-style classes to new-style classes. +- Prevent UnicodeDecodeError when publishing image (bytes) responses without content-type + Changes +++++++ diff --git a/src/ZPublisher/HTTPResponse.py b/src/ZPublisher/HTTPResponse.py index 96e536f406..7839a55d26 100644 --- a/src/ZPublisher/HTTPResponse.py +++ b/src/ZPublisher/HTTPResponse.py @@ -433,6 +433,10 @@ def insertBase(self): self.setHeader('content-length', len(self.body)) def isHTML(self, text): + try: + text = text.decode(self.charset) + except UnicodeDecodeError: + pass text = text.lstrip() # Note that the string can be big, so text.lower().startswith() # is more expensive than s[:n].lower(). @@ -514,7 +518,7 @@ def setBody(self, body, title='', is_error=False, lock=None): content_type = self.headers.get('content-type') if content_type is None: - if self.isHTML(body.decode(self.charset)): + if self.isHTML(body): content_type = 'text/html; charset=%s' % self.charset else: content_type = 'text/plain; charset=%s' % self.charset