158 # We first validate that the incoming object conforms with the WSGI spec. |
158 # We first validate that the incoming object conforms with the WSGI spec. |
159 # We only want to be dealing with spec-conforming WSGI implementations. |
159 # We only want to be dealing with spec-conforming WSGI implementations. |
160 # TODO enable this once we fix internal violations. |
160 # TODO enable this once we fix internal violations. |
161 # wsgiref.validate.check_environ(env) |
161 # wsgiref.validate.check_environ(env) |
162 |
162 |
163 # PEP-0333 states that environment keys and values are native strings |
163 # PEP-0333 states that environment keys and values are native strings. |
164 # (bytes on Python 2 and str on Python 3). The code points for the Unicode |
164 # The code points for the Unicode strings on Python 3 must be between |
165 # strings on Python 3 must be between \00000-\000FF. We deal with bytes |
165 # \00000-\000FF. We deal with bytes in Mercurial, so mass convert string |
166 # in Mercurial, so mass convert string keys and values to bytes. |
166 # keys and values to bytes. |
167 if pycompat.ispy3: |
167 def tobytes(s): |
168 |
168 if not isinstance(s, str): |
169 def tobytes(s): |
169 return s |
170 if not isinstance(s, str): |
170 if pycompat.iswindows: |
171 return s |
171 # This is what mercurial.encoding does for os.environ on |
172 if pycompat.iswindows: |
172 # Windows. |
173 # This is what mercurial.encoding does for os.environ on |
173 return encoding.strtolocal(s) |
174 # Windows. |
174 else: |
175 return encoding.strtolocal(s) |
175 # This is what is documented to be used for os.environ on Unix. |
176 else: |
176 return pycompat.fsencode(s) |
177 # This is what is documented to be used for os.environ on Unix. |
177 |
178 return pycompat.fsencode(s) |
178 env = {tobytes(k): tobytes(v) for k, v in pycompat.iteritems(env)} |
179 |
|
180 env = {tobytes(k): tobytes(v) for k, v in pycompat.iteritems(env)} |
|
181 |
179 |
182 # Some hosting solutions are emulating hgwebdir, and dispatching directly |
180 # Some hosting solutions are emulating hgwebdir, and dispatching directly |
183 # to an hgweb instance using this environment variable. This was always |
181 # to an hgweb instance using this environment variable. This was always |
184 # checked prior to d7fd203e36cc; keep doing so to avoid breaking them. |
182 # checked prior to d7fd203e36cc; keep doing so to avoid breaking them. |
185 if not reponame: |
183 if not reponame: |