comparison mercurial/hgweb/request.py @ 50767:9ed281bbf864 stable

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.
author Manuel Jacob <me@manueljacob.de>
date Mon, 07 Aug 2023 23:12:02 +0200
parents fda5a4b853ab
children 278af66e6595
comparison
equal deleted inserted replaced
50765:04d5cde28a7f 50767:9ed281bbf864
9 9
10 # import wsgiref.validate 10 # import wsgiref.validate
11 11
12 from ..thirdparty import attr 12 from ..thirdparty import attr
13 from .. import ( 13 from .. import (
14 encoding,
15 error, 14 error,
16 pycompat, 15 pycompat,
17 util, 16 util,
18 ) 17 )
19 from ..utils import ( 18 from ..utils import (
165 # \00000-\000FF. We deal with bytes in Mercurial, so mass convert string 164 # \00000-\000FF. We deal with bytes in Mercurial, so mass convert string
166 # keys and values to bytes. 165 # keys and values to bytes.
167 def tobytes(s): 166 def tobytes(s):
168 if not isinstance(s, str): 167 if not isinstance(s, str):
169 return s 168 return s
170 if pycompat.iswindows: 169 return s.encode('iso8859-1')
171 # This is what mercurial.encoding does for os.environ on
172 # Windows.
173 return encoding.strtolocal(s)
174 else:
175 # This is what is documented to be used for os.environ on Unix.
176 return pycompat.fsencode(s)
177 170
178 env = {tobytes(k): tobytes(v) for k, v in env.items()} 171 env = {tobytes(k): tobytes(v) for k, v in env.items()}
179 172
180 # Some hosting solutions are emulating hgwebdir, and dispatching directly 173 # Some hosting solutions are emulating hgwebdir, and dispatching directly
181 # to an hgweb instance using this environment variable. This was always 174 # to an hgweb instance using this environment variable. This was always