Skip to content

Commit

Permalink
Prevent UnicodeDecodeError when publishing image (bytes) responses wi…
Browse files Browse the repository at this point in the history
…thout content-type
  • Loading branch information
pbauer committed Oct 27, 2017
1 parent 1736580 commit cbaf775
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
+++++++

Expand Down
6 changes: 5 additions & 1 deletion src/ZPublisher/HTTPResponse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit cbaf775

Please sign in to comment.