Skip to content

Commit

Permalink
Restore our charset decoding logic
Browse files Browse the repository at this point in the history
Code stolen from https://github.com/zopefoundation/zope.publisher/pull/15/files

Co-Authored-By: Brian Sutherland <brian@sutherland.es>
  • Loading branch information
mgedmin and jinty committed Jul 10, 2019
1 parent 6f162e9 commit 795dd32
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/zope/publisher/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,20 @@ def _decode(self, text):
envadapter = IUserPreferredCharsets(self)
self.charsets = envadapter.getPreferredCharsets() or ['utf-8']
self.charsets = [c for c in self.charsets if c != '*']
if isinstance(text, bytes):
for charset in self.charsets:
try:
text = text.decode(charset)
break
except UnicodeError:
pass
if not PYTHON2:
if self.charsets and self.charsets[0] == 'utf-8':
# optimization: we are trying to decode something
# cgi.FieldStorage already decoded for us, let's just return it
# rather than waste time decoding...
return text
# undo what cgi.FieldStorage did and maintain backwards compat
text = text.encode('utf-8')
for charset in self.charsets:
try:
text = text.decode(charset)
break
except UnicodeError:
pass
return text

def processInputs(self):
Expand Down

0 comments on commit 795dd32

Please sign in to comment.