Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b6d4a8e

Browse files
committed
Implement http://bugs.python.org/issue10155 using And Clover's patch, w/added
docs and support for more client-generated CGI variables. (This should complete the WSGI 1.0.1 compliance changes for Python 3.x.)
1 parent 3c6830c commit b6d4a8e

5 files changed

Lines changed: 164 additions & 8 deletions

File tree

Doc/library/wsgiref.rst

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,32 @@ input, output, and error streams.
456456
environment.
457457

458458

459+
.. class:: IISCGIHandler()
460+
461+
A specialized alternative to :class:`CGIHandler`, for use when deploying on
462+
Microsoft's IIS web server, without having set the config allowPathInfo
463+
option (IIS>=7) or metabase allowPathInfoForScriptMappings (IIS<7).
464+
465+
By default, IIS gives a ``PATH_INFO`` that duplicates the ``SCRIPT_NAME`` at
466+
the front, causing problems for WSGI applications that wish to implement
467+
routing. This handler strips any such duplicated path.
468+
469+
IIS can be configured to pass the correct ``PATH_INFO``, but this causes
470+
another bug where ``PATH_TRANSLATED`` is wrong. Luckily this variable is
471+
rarely used and is not guaranteed by WSGI. On IIS<7, though, the
472+
setting can only be made on a vhost level, affecting all other script
473+
mappings, many of which break when exposed to the ``PATH_TRANSLATED`` bug.
474+
For this reason IIS<7 is almost never deployed with the fix. (Even IIS7
475+
rarely uses it because there is still no UI for it.)
476+
477+
There is no way for CGI code to tell whether the option was set, so a
478+
separate handler class is provided. It is used in the same way as
479+
:class:`CGIHandler`, i.e., by calling ``IISCGIHandler().run(app)``, where
480+
``app`` is the WSGI application object you wish to invoke.
481+
482+
.. versionadded:: 3.2
483+
484+
459485
.. class:: BaseCGIHandler(stdin, stdout, stderr, environ, multithread=True, multiprocess=False)
460486

461487
Similar to :class:`CGIHandler`, but instead of using the :mod:`sys` and
@@ -696,6 +722,24 @@ input, output, and error streams.
696722
version of the response set to the client. It defaults to ``"1.0"``.
697723

698724

725+
.. function:: read_environ()
726+
727+
Transcode CGI variables from ``os.environ`` to PEP 3333 "bytes in unicode"
728+
strings, returning a new dictionary. This function is used by
729+
:class:`CGIHandler` and :class:`IISCGIHandler` in place of directly using
730+
``os.environ``, which is not necessarily WSGI-compliant on all platforms
731+
and web servers using Python 3 -- specifically, ones where the OS's
732+
actual environment is Unicode (i.e. Windows), or ones where the environment
733+
is bytes, but the system encoding used by Python to decode it is anything
734+
other than ISO-8859-1 (e.g. Unix systems using UTF-8).
735+
736+
If you are implementing a CGI-based handler of your own, you probably want
737+
to use this routine instead of just copying values out of ``os.environ``
738+
directly.
739+
740+
.. versionadded:: 3.2
741+
742+
699743
Examples
700744
--------
701745

Lib/test/test_wsgiref.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class IntegrationTests(TestCase):
131131
def check_hello(self, out, has_length=True):
132132
self.assertEqual(out,
133133
("HTTP/1.0 200 OK\r\n"
134-
"Server: WSGIServer/0.1 Python/"+sys.version.split()[0]+"\r\n"
134+
"Server: WSGIServer/0.2 Python/"+sys.version.split()[0]+"\r\n"
135135
"Content-Type: text/plain\r\n"
136136
"Date: Mon, 05 Jun 2006 18:49:54 GMT\r\n" +
137137
(has_length and "Content-Length: 13\r\n" or "") +
@@ -187,7 +187,7 @@ def app(e, s):
187187
ver = sys.version.split()[0].encode('ascii')
188188
self.assertEqual(
189189
b"HTTP/1.0 200 OK\r\n"
190-
b"Server: WSGIServer/0.1 Python/" + ver + b"\r\n"
190+
b"Server: WSGIServer/0.2 Python/" + ver + b"\r\n"
191191
b"Content-Type: text/plain; charset=utf-8\r\n"
192192
b"Date: Wed, 24 Dec 2008 13:29:32 GMT\r\n"
193193
b"\r\n"

Lib/wsgiref/handlers.py

Lines changed: 111 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55

66
import sys, os, time
77

8-
__all__ = ['BaseHandler', 'SimpleHandler', 'BaseCGIHandler', 'CGIHandler']
8+
__all__ = [
9+
'BaseHandler', 'SimpleHandler', 'BaseCGIHandler', 'CGIHandler',
10+
'IISCGIHandler', 'read_environ'
11+
]
912

1013
# Weekday and month names for HTTP date/time formatting; always English!
1114
_weekdayname = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
@@ -19,6 +22,74 @@ def format_date_time(timestamp):
1922
_weekdayname[wd], day, _monthname[month], year, hh, mm, ss
2023
)
2124

25+
_is_request = {
26+
'SCRIPT_NAME', 'PATH_INFO', 'QUERY_STRING', 'REQUEST_METHOD', 'AUTH_TYPE',
27+
'CONTENT_TYPE', 'CONTENT_LENGTH', 'HTTPS', 'REMOTE_USER', 'REMOTE_IDENT',
28+
}.__contains__
29+
30+
def _needs_transcode(k):
31+
return _is_request(k) or k.startswith('HTTP_') or k.startswith('SSL_') \
32+
or (k.startswith('REDIRECT_') and _needs_transcode(k[9:]))
33+
34+
def read_environ():
35+
"""Read environment, fixing HTTP variables"""
36+
enc = sys.getfilesystemencoding()
37+
esc = 'surrogateescape'
38+
try:
39+
''.encode('utf-8', esc)
40+
except LookupError:
41+
esc = 'replace'
42+
environ = {}
43+
44+
# Take the basic environment from native-unicode os.environ. Attempt to
45+
# fix up the variables that come from the HTTP request to compensate for
46+
# the bytes->unicode decoding step that will already have taken place.
47+
for k, v in os.environ.items():
48+
if _needs_transcode(k):
49+
50+
# On win32, the os.environ is natively Unicode. Different servers
51+
# decode the request bytes using different encodings.
52+
if sys.platform == 'win32':
53+
software = os.environ.get('SERVER_SOFTWARE', '').lower()
54+
55+
# On IIS, the HTTP request will be decoded as UTF-8 as long
56+
# as the input is a valid UTF-8 sequence. Otherwise it is
57+
# decoded using the system code page (mbcs), with no way to
58+
# detect this has happened. Because UTF-8 is the more likely
59+
# encoding, and mbcs is inherently unreliable (an mbcs string
60+
# that happens to be valid UTF-8 will not be decoded as mbcs)
61+
# always recreate the original bytes as UTF-8.
62+
if software.startswith('microsoft-iis/'):
63+
v = v.encode('utf-8').decode('iso-8859-1')
64+
65+
# Apache mod_cgi writes bytes-as-unicode (as if ISO-8859-1) direct
66+
# to the Unicode environ. No modification needed.
67+
elif software.startswith('apache/'):
68+
pass
69+
70+
# Python 3's http.server.CGIHTTPRequestHandler decodes
71+
# using the urllib.unquote default of UTF-8, amongst other
72+
# issues.
73+
elif (
74+
software.startswith('simplehttp/')
75+
and 'python/3' in software
76+
):
77+
v = v.encode('utf-8').decode('iso-8859-1')
78+
79+
# For other servers, guess that they have written bytes to
80+
# the environ using stdio byte-oriented interfaces, ending up
81+
# with the system code page.
82+
else:
83+
v = v.encode(enc, 'replace').decode('iso-8859-1')
84+
85+
# Recover bytes from unicode environ, using surrogate escapes
86+
# where available (Python 3.1+).
87+
else:
88+
v = v.encode(enc, esc).decode('iso-8859-1')
89+
90+
environ[k] = v
91+
return environ
92+
2293

2394
class BaseHandler:
2495
"""Manage the invocation of a WSGI application"""
@@ -36,7 +107,7 @@ class BaseHandler:
36107
# os_environ is used to supply configuration from the OS environment:
37108
# by default it's a copy of 'os.environ' as of import time, but you can
38109
# override this in e.g. your __init__ method.
39-
os_environ = dict(os.environ.items())
110+
os_environ= read_environ()
40111

41112
# Collaborator classes
42113
wsgi_file_wrapper = FileWrapper # set to None to disable
@@ -431,6 +502,42 @@ class CGIHandler(BaseCGIHandler):
431502

432503
def __init__(self):
433504
BaseCGIHandler.__init__(
434-
self, sys.stdin, sys.stdout, sys.stderr, dict(os.environ.items()),
435-
multithread=False, multiprocess=True
505+
self, sys.stdin.buffer, sys.stdout.buffer, sys.stderr,
506+
read_environ(), multithread=False, multiprocess=True
507+
)
508+
509+
510+
class IISCGIHandler(BaseCGIHandler):
511+
"""CGI-based invocation with workaround for IIS path bug
512+
513+
This handler should be used in preference to CGIHandler when deploying on
514+
Microsoft IIS without having set the config allowPathInfo option (IIS>=7)
515+
or metabase allowPathInfoForScriptMappings (IIS<7).
516+
"""
517+
wsgi_run_once = True
518+
os_environ = {}
519+
520+
# By default, IIS gives a PATH_INFO that duplicates the SCRIPT_NAME at
521+
# the front, causing problems for WSGI applications that wish to implement
522+
# routing. This handler strips any such duplicated path.
523+
524+
# IIS can be configured to pass the correct PATH_INFO, but this causes
525+
# another bug where PATH_TRANSLATED is wrong. Luckily this variable is
526+
# rarely used and is not guaranteed by WSGI. On IIS<7, though, the
527+
# setting can only be made on a vhost level, affecting all other script
528+
# mappings, many of which break when exposed to the PATH_TRANSLATED bug.
529+
# For this reason IIS<7 is almost never deployed with the fix. (Even IIS7
530+
# rarely uses it because there is still no UI for it.)
531+
532+
# There is no way for CGI code to tell whether the option was set, so a
533+
# separate handler class is provided.
534+
def __init__(self):
535+
environ= read_environ()
536+
path = environ.get('PATH_INFO', '')
537+
script = environ.get('SCRIPT_NAME', '')
538+
if (path+'/').startswith(script+'/'):
539+
environ['PATH_INFO'] = path[len(script):]
540+
BaseCGIHandler.__init__(
541+
self, sys.stdin.buffer, sys.stdout.buffer, sys.stderr,
542+
environ, multithread=False, multiprocess=True
436543
)

Lib/wsgiref/simple_server.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import urllib.parse
1616
from wsgiref.handlers import SimpleHandler
1717

18-
__version__ = "0.1"
18+
__version__ = "0.2"
1919
__all__ = ['WSGIServer', 'WSGIRequestHandler', 'demo_app', 'make_server']
2020

2121

@@ -74,13 +74,14 @@ class WSGIRequestHandler(BaseHTTPRequestHandler):
7474
def get_environ(self):
7575
env = self.server.base_environ.copy()
7676
env['SERVER_PROTOCOL'] = self.request_version
77+
env['SERVER_SOFTWARE'] = self.server_version
7778
env['REQUEST_METHOD'] = self.command
7879
if '?' in self.path:
7980
path,query = self.path.split('?',1)
8081
else:
8182
path,query = self.path,''
8283

83-
env['PATH_INFO'] = urllib.parse.unquote(path)
84+
env['PATH_INFO'] = urllib.parse.unquote_to_bytes(path).decode('iso-8859-1')
8485
env['QUERY_STRING'] = query
8586

8687
host = self.address_string()

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ Core and Builtins
5959
Library
6060
-------
6161

62+
- Issue #10155: Add IISCGIHandler to wsgiref.handlers to support IIS
63+
CGI environment better, and to correct unicode environment values
64+
for WSGI 1.0.1.
65+
6266
- Issue #10281: nntplib now returns None for absent fields in the OVER/XOVER
6367
response, instead of raising an exception.
6468

0 commit comments

Comments
 (0)