diff mercurial/hgweb/request.py @ 45004:2632c1ed8f34 stable

hgweb: encode WSGI environment like OS environment Previously, the WSGI environment keys and values were encoded using latin-1. This resulted in a crash if a WSGI environment key or value could not be encoded using latin-1. On Unix, the OS environment is byte-based. Therefore we should do the reverse of what Python does for os.environ. On Windows, there’s no native byte-based OS environment. Therefore we should do the same as what mercurial.encoding does with the OS environment.
author Manuel Jacob <me@manueljacob.de>
date Thu, 25 Jun 2020 03:46:07 +0200
parents 839328c5a728
children 89a2afe31e82
line wrap: on
line diff
--- a/mercurial/hgweb/request.py	Thu Jun 25 03:10:13 2020 +0200
+++ b/mercurial/hgweb/request.py	Thu Jun 25 03:46:07 2020 +0200
@@ -12,6 +12,7 @@
 
 from ..thirdparty import attr
 from .. import (
+    encoding,
     error,
     pycompat,
     util,
@@ -162,10 +163,18 @@
     # strings on Python 3 must be between \00000-\000FF. We deal with bytes
     # in Mercurial, so mass convert string keys and values to bytes.
     if pycompat.ispy3:
+
         def tobytes(s):
             if not isinstance(s, str):
                 return s
-            return s.encode('latin-1')
+            if pycompat.iswindows:
+                # This is what mercurial.encoding does for os.environ on
+                # Windows.
+                return encoding.strtolocal(s)
+            else:
+                # This is what is documented to be used for os.environ on Unix.
+                return pycompat.fsencode(s)
+
         env = {tobytes(k): tobytes(v) for k, v in pycompat.iteritems(env)}
 
     # Some hosting solutions are emulating hgwebdir, and dispatching directly