Skip to content
Snippets Groups Projects
Commit 0caa6538 authored by Manuel Jacob's avatar Manuel Jacob
Browse files

hgweb: encode WSGI environment using the ISO-8859-1 codec

The WSGI specification (PEP 3333) specifies that on Python 3 all strings passed
by the server must be of type str with code points encodable using the ISO
8859-1 codec.

For some reason, I introduced a bug in 2632c1ed8f34 by applying the reverse
change. Maybe I got confused because PEP 3333 says that arbitrary operating
system environment variables may be contained in the WSGI environment and
therefore we need to handle the WSGI environment variables like we would handle
operating system environment variables.

The bug mentioned in the previous paragraph and fixed by this changeset
manifested e.g. in the path of the URL being encoded in the wrong way. Browsers
encode non-ASCII bytes with the percent-encoding. WSGI servers will decode the
percent-encoded bytes and pass them to the application as strings where each
byte is mapped to the corresponding code point with the same ordinal (i.e. it
is decoded using the ISO-8859-1 codec). Mercurial uses the bytes type for these
strings (which makes much more sense), so we need to encode it again using the
ISO-8859-1 codec. If we use another codec, it can result in nonsense.
parent 04d5cde2
No related branches found
No related tags found
No related merge requests found
......@@ -167,13 +167,7 @@
def tobytes(s):
if not isinstance(s, str):
return s
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)
return s.encode('iso8859-1')
env = {tobytes(k): tobytes(v) for k, v in env.items()}
......
......@@ -500,16 +500,9 @@
self.assertEqual(r.reponame, b'repo')
def testenvencoding(self):
if pycompat.iswindows:
# On Windows, we can't generally know which non-ASCII characters
# are supported.
r = parse(DEFAULT_ENV, extra={'foo': 'bar'})
self.assertEqual(r.rawenv[b'foo'], b'bar')
else:
# Unix is byte-based. Therefore we test all possible bytes.
b = b''.join(pycompat.bytechr(i) for i in range(256))
r = parse(DEFAULT_ENV, extra={'foo': pycompat.fsdecode(b)})
self.assertEqual(r.rawenv[b'foo'], b)
b = b''.join(pycompat.bytechr(i) for i in range(256))
r = parse(DEFAULT_ENV, extra={'foo': b.decode('iso8859-1')})
self.assertEqual(r.rawenv[b'foo'], b)
if __name__ == '__main__':
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment