jpayne@69: """Base classes for server/gateway implementations""" jpayne@69: jpayne@69: from .util import FileWrapper, guess_scheme, is_hop_by_hop jpayne@69: from .headers import Headers jpayne@69: jpayne@69: import sys, os, time jpayne@69: jpayne@69: __all__ = [ jpayne@69: 'BaseHandler', 'SimpleHandler', 'BaseCGIHandler', 'CGIHandler', jpayne@69: 'IISCGIHandler', 'read_environ' jpayne@69: ] jpayne@69: jpayne@69: # Weekday and month names for HTTP date/time formatting; always English! jpayne@69: _weekdayname = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] jpayne@69: _monthname = [None, # Dummy so we can use 1-based month numbers jpayne@69: "Jan", "Feb", "Mar", "Apr", "May", "Jun", jpayne@69: "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] jpayne@69: jpayne@69: def format_date_time(timestamp): jpayne@69: year, month, day, hh, mm, ss, wd, y, z = time.gmtime(timestamp) jpayne@69: return "%s, %02d %3s %4d %02d:%02d:%02d GMT" % ( jpayne@69: _weekdayname[wd], day, _monthname[month], year, hh, mm, ss jpayne@69: ) jpayne@69: jpayne@69: _is_request = { jpayne@69: 'SCRIPT_NAME', 'PATH_INFO', 'QUERY_STRING', 'REQUEST_METHOD', 'AUTH_TYPE', jpayne@69: 'CONTENT_TYPE', 'CONTENT_LENGTH', 'HTTPS', 'REMOTE_USER', 'REMOTE_IDENT', jpayne@69: }.__contains__ jpayne@69: jpayne@69: def _needs_transcode(k): jpayne@69: return _is_request(k) or k.startswith('HTTP_') or k.startswith('SSL_') \ jpayne@69: or (k.startswith('REDIRECT_') and _needs_transcode(k[9:])) jpayne@69: jpayne@69: def read_environ(): jpayne@69: """Read environment, fixing HTTP variables""" jpayne@69: enc = sys.getfilesystemencoding() jpayne@69: esc = 'surrogateescape' jpayne@69: try: jpayne@69: ''.encode('utf-8', esc) jpayne@69: except LookupError: jpayne@69: esc = 'replace' jpayne@69: environ = {} jpayne@69: jpayne@69: # Take the basic environment from native-unicode os.environ. Attempt to jpayne@69: # fix up the variables that come from the HTTP request to compensate for jpayne@69: # the bytes->unicode decoding step that will already have taken place. jpayne@69: for k, v in os.environ.items(): jpayne@69: if _needs_transcode(k): jpayne@69: jpayne@69: # On win32, the os.environ is natively Unicode. Different servers jpayne@69: # decode the request bytes using different encodings. jpayne@69: if sys.platform == 'win32': jpayne@69: software = os.environ.get('SERVER_SOFTWARE', '').lower() jpayne@69: jpayne@69: # On IIS, the HTTP request will be decoded as UTF-8 as long jpayne@69: # as the input is a valid UTF-8 sequence. Otherwise it is jpayne@69: # decoded using the system code page (mbcs), with no way to jpayne@69: # detect this has happened. Because UTF-8 is the more likely jpayne@69: # encoding, and mbcs is inherently unreliable (an mbcs string jpayne@69: # that happens to be valid UTF-8 will not be decoded as mbcs) jpayne@69: # always recreate the original bytes as UTF-8. jpayne@69: if software.startswith('microsoft-iis/'): jpayne@69: v = v.encode('utf-8').decode('iso-8859-1') jpayne@69: jpayne@69: # Apache mod_cgi writes bytes-as-unicode (as if ISO-8859-1) direct jpayne@69: # to the Unicode environ. No modification needed. jpayne@69: elif software.startswith('apache/'): jpayne@69: pass jpayne@69: jpayne@69: # Python 3's http.server.CGIHTTPRequestHandler decodes jpayne@69: # using the urllib.unquote default of UTF-8, amongst other jpayne@69: # issues. jpayne@69: elif ( jpayne@69: software.startswith('simplehttp/') jpayne@69: and 'python/3' in software jpayne@69: ): jpayne@69: v = v.encode('utf-8').decode('iso-8859-1') jpayne@69: jpayne@69: # For other servers, guess that they have written bytes to jpayne@69: # the environ using stdio byte-oriented interfaces, ending up jpayne@69: # with the system code page. jpayne@69: else: jpayne@69: v = v.encode(enc, 'replace').decode('iso-8859-1') jpayne@69: jpayne@69: # Recover bytes from unicode environ, using surrogate escapes jpayne@69: # where available (Python 3.1+). jpayne@69: else: jpayne@69: v = v.encode(enc, esc).decode('iso-8859-1') jpayne@69: jpayne@69: environ[k] = v jpayne@69: return environ jpayne@69: jpayne@69: jpayne@69: class BaseHandler: jpayne@69: """Manage the invocation of a WSGI application""" jpayne@69: jpayne@69: # Configuration parameters; can override per-subclass or per-instance jpayne@69: wsgi_version = (1,0) jpayne@69: wsgi_multithread = True jpayne@69: wsgi_multiprocess = True jpayne@69: wsgi_run_once = False jpayne@69: jpayne@69: origin_server = True # We are transmitting direct to client jpayne@69: http_version = "1.0" # Version that should be used for response jpayne@69: server_software = None # String name of server software, if any jpayne@69: jpayne@69: # os_environ is used to supply configuration from the OS environment: jpayne@69: # by default it's a copy of 'os.environ' as of import time, but you can jpayne@69: # override this in e.g. your __init__ method. jpayne@69: os_environ= read_environ() jpayne@69: jpayne@69: # Collaborator classes jpayne@69: wsgi_file_wrapper = FileWrapper # set to None to disable jpayne@69: headers_class = Headers # must be a Headers-like class jpayne@69: jpayne@69: # Error handling (also per-subclass or per-instance) jpayne@69: traceback_limit = None # Print entire traceback to self.get_stderr() jpayne@69: error_status = "500 Internal Server Error" jpayne@69: error_headers = [('Content-Type','text/plain')] jpayne@69: error_body = b"A server error occurred. Please contact the administrator." jpayne@69: jpayne@69: # State variables (don't mess with these) jpayne@69: status = result = None jpayne@69: headers_sent = False jpayne@69: headers = None jpayne@69: bytes_sent = 0 jpayne@69: jpayne@69: def run(self, application): jpayne@69: """Invoke the application""" jpayne@69: # Note to self: don't move the close()! Asynchronous servers shouldn't jpayne@69: # call close() from finish_response(), so if you close() anywhere but jpayne@69: # the double-error branch here, you'll break asynchronous servers by jpayne@69: # prematurely closing. Async servers must return from 'run()' without jpayne@69: # closing if there might still be output to iterate over. jpayne@69: try: jpayne@69: self.setup_environ() jpayne@69: self.result = application(self.environ, self.start_response) jpayne@69: self.finish_response() jpayne@69: except (ConnectionAbortedError, BrokenPipeError, ConnectionResetError): jpayne@69: # We expect the client to close the connection abruptly from time jpayne@69: # to time. jpayne@69: return jpayne@69: except: jpayne@69: try: jpayne@69: self.handle_error() jpayne@69: except: jpayne@69: # If we get an error handling an error, just give up already! jpayne@69: self.close() jpayne@69: raise # ...and let the actual server figure it out. jpayne@69: jpayne@69: jpayne@69: def setup_environ(self): jpayne@69: """Set up the environment for one request""" jpayne@69: jpayne@69: env = self.environ = self.os_environ.copy() jpayne@69: self.add_cgi_vars() jpayne@69: jpayne@69: env['wsgi.input'] = self.get_stdin() jpayne@69: env['wsgi.errors'] = self.get_stderr() jpayne@69: env['wsgi.version'] = self.wsgi_version jpayne@69: env['wsgi.run_once'] = self.wsgi_run_once jpayne@69: env['wsgi.url_scheme'] = self.get_scheme() jpayne@69: env['wsgi.multithread'] = self.wsgi_multithread jpayne@69: env['wsgi.multiprocess'] = self.wsgi_multiprocess jpayne@69: jpayne@69: if self.wsgi_file_wrapper is not None: jpayne@69: env['wsgi.file_wrapper'] = self.wsgi_file_wrapper jpayne@69: jpayne@69: if self.origin_server and self.server_software: jpayne@69: env.setdefault('SERVER_SOFTWARE',self.server_software) jpayne@69: jpayne@69: jpayne@69: def finish_response(self): jpayne@69: """Send any iterable data, then close self and the iterable jpayne@69: jpayne@69: Subclasses intended for use in asynchronous servers will jpayne@69: want to redefine this method, such that it sets up callbacks jpayne@69: in the event loop to iterate over the data, and to call jpayne@69: 'self.close()' once the response is finished. jpayne@69: """ jpayne@69: try: jpayne@69: if not self.result_is_file() or not self.sendfile(): jpayne@69: for data in self.result: jpayne@69: self.write(data) jpayne@69: self.finish_content() jpayne@69: except: jpayne@69: # Call close() on the iterable returned by the WSGI application jpayne@69: # in case of an exception. jpayne@69: if hasattr(self.result, 'close'): jpayne@69: self.result.close() jpayne@69: raise jpayne@69: else: jpayne@69: # We only call close() when no exception is raised, because it jpayne@69: # will set status, result, headers, and environ fields to None. jpayne@69: # See bpo-29183 for more details. jpayne@69: self.close() jpayne@69: jpayne@69: jpayne@69: def get_scheme(self): jpayne@69: """Return the URL scheme being used""" jpayne@69: return guess_scheme(self.environ) jpayne@69: jpayne@69: jpayne@69: def set_content_length(self): jpayne@69: """Compute Content-Length or switch to chunked encoding if possible""" jpayne@69: try: jpayne@69: blocks = len(self.result) jpayne@69: except (TypeError,AttributeError,NotImplementedError): jpayne@69: pass jpayne@69: else: jpayne@69: if blocks==1: jpayne@69: self.headers['Content-Length'] = str(self.bytes_sent) jpayne@69: return jpayne@69: # XXX Try for chunked encoding if origin server and client is 1.1 jpayne@69: jpayne@69: jpayne@69: def cleanup_headers(self): jpayne@69: """Make any necessary header changes or defaults jpayne@69: jpayne@69: Subclasses can extend this to add other defaults. jpayne@69: """ jpayne@69: if 'Content-Length' not in self.headers: jpayne@69: self.set_content_length() jpayne@69: jpayne@69: def start_response(self, status, headers,exc_info=None): jpayne@69: """'start_response()' callable as specified by PEP 3333""" jpayne@69: jpayne@69: if exc_info: jpayne@69: try: jpayne@69: if self.headers_sent: jpayne@69: # Re-raise original exception if headers sent jpayne@69: raise exc_info[0](exc_info[1]).with_traceback(exc_info[2]) jpayne@69: finally: jpayne@69: exc_info = None # avoid dangling circular ref jpayne@69: elif self.headers is not None: jpayne@69: raise AssertionError("Headers already set!") jpayne@69: jpayne@69: self.status = status jpayne@69: self.headers = self.headers_class(headers) jpayne@69: status = self._convert_string_type(status, "Status") jpayne@69: assert len(status)>=4,"Status must be at least 4 characters" jpayne@69: assert status[:3].isdigit(), "Status message must begin w/3-digit code" jpayne@69: assert status[3]==" ", "Status message must have a space after code" jpayne@69: jpayne@69: if __debug__: jpayne@69: for name, val in headers: jpayne@69: name = self._convert_string_type(name, "Header name") jpayne@69: val = self._convert_string_type(val, "Header value") jpayne@69: assert not is_hop_by_hop(name),\ jpayne@69: f"Hop-by-hop header, '{name}: {val}', not allowed" jpayne@69: jpayne@69: return self.write jpayne@69: jpayne@69: def _convert_string_type(self, value, title): jpayne@69: """Convert/check value type.""" jpayne@69: if type(value) is str: jpayne@69: return value jpayne@69: raise AssertionError( jpayne@69: "{0} must be of type str (got {1})".format(title, repr(value)) jpayne@69: ) jpayne@69: jpayne@69: def send_preamble(self): jpayne@69: """Transmit version/status/date/server, via self._write()""" jpayne@69: if self.origin_server: jpayne@69: if self.client_is_modern(): jpayne@69: self._write(('HTTP/%s %s\r\n' % (self.http_version,self.status)).encode('iso-8859-1')) jpayne@69: if 'Date' not in self.headers: jpayne@69: self._write( jpayne@69: ('Date: %s\r\n' % format_date_time(time.time())).encode('iso-8859-1') jpayne@69: ) jpayne@69: if self.server_software and 'Server' not in self.headers: jpayne@69: self._write(('Server: %s\r\n' % self.server_software).encode('iso-8859-1')) jpayne@69: else: jpayne@69: self._write(('Status: %s\r\n' % self.status).encode('iso-8859-1')) jpayne@69: jpayne@69: def write(self, data): jpayne@69: """'write()' callable as specified by PEP 3333""" jpayne@69: jpayne@69: assert type(data) is bytes, \ jpayne@69: "write() argument must be a bytes instance" jpayne@69: jpayne@69: if not self.status: jpayne@69: raise AssertionError("write() before start_response()") jpayne@69: jpayne@69: elif not self.headers_sent: jpayne@69: # Before the first output, send the stored headers jpayne@69: self.bytes_sent = len(data) # make sure we know content-length jpayne@69: self.send_headers() jpayne@69: else: jpayne@69: self.bytes_sent += len(data) jpayne@69: jpayne@69: # XXX check Content-Length and truncate if too many bytes written? jpayne@69: self._write(data) jpayne@69: self._flush() jpayne@69: jpayne@69: jpayne@69: def sendfile(self): jpayne@69: """Platform-specific file transmission jpayne@69: jpayne@69: Override this method in subclasses to support platform-specific jpayne@69: file transmission. It is only called if the application's jpayne@69: return iterable ('self.result') is an instance of jpayne@69: 'self.wsgi_file_wrapper'. jpayne@69: jpayne@69: This method should return a true value if it was able to actually jpayne@69: transmit the wrapped file-like object using a platform-specific jpayne@69: approach. It should return a false value if normal iteration jpayne@69: should be used instead. An exception can be raised to indicate jpayne@69: that transmission was attempted, but failed. jpayne@69: jpayne@69: NOTE: this method should call 'self.send_headers()' if jpayne@69: 'self.headers_sent' is false and it is going to attempt direct jpayne@69: transmission of the file. jpayne@69: """ jpayne@69: return False # No platform-specific transmission by default jpayne@69: jpayne@69: jpayne@69: def finish_content(self): jpayne@69: """Ensure headers and content have both been sent""" jpayne@69: if not self.headers_sent: jpayne@69: # Only zero Content-Length if not set by the application (so jpayne@69: # that HEAD requests can be satisfied properly, see #3839) jpayne@69: self.headers.setdefault('Content-Length', "0") jpayne@69: self.send_headers() jpayne@69: else: jpayne@69: pass # XXX check if content-length was too short? jpayne@69: jpayne@69: def close(self): jpayne@69: """Close the iterable (if needed) and reset all instance vars jpayne@69: jpayne@69: Subclasses may want to also drop the client connection. jpayne@69: """ jpayne@69: try: jpayne@69: if hasattr(self.result,'close'): jpayne@69: self.result.close() jpayne@69: finally: jpayne@69: self.result = self.headers = self.status = self.environ = None jpayne@69: self.bytes_sent = 0; self.headers_sent = False jpayne@69: jpayne@69: jpayne@69: def send_headers(self): jpayne@69: """Transmit headers to the client, via self._write()""" jpayne@69: self.cleanup_headers() jpayne@69: self.headers_sent = True jpayne@69: if not self.origin_server or self.client_is_modern(): jpayne@69: self.send_preamble() jpayne@69: self._write(bytes(self.headers)) jpayne@69: jpayne@69: jpayne@69: def result_is_file(self): jpayne@69: """True if 'self.result' is an instance of 'self.wsgi_file_wrapper'""" jpayne@69: wrapper = self.wsgi_file_wrapper jpayne@69: return wrapper is not None and isinstance(self.result,wrapper) jpayne@69: jpayne@69: jpayne@69: def client_is_modern(self): jpayne@69: """True if client can accept status and headers""" jpayne@69: return self.environ['SERVER_PROTOCOL'].upper() != 'HTTP/0.9' jpayne@69: jpayne@69: jpayne@69: def log_exception(self,exc_info): jpayne@69: """Log the 'exc_info' tuple in the server log jpayne@69: jpayne@69: Subclasses may override to retarget the output or change its format. jpayne@69: """ jpayne@69: try: jpayne@69: from traceback import print_exception jpayne@69: stderr = self.get_stderr() jpayne@69: print_exception( jpayne@69: exc_info[0], exc_info[1], exc_info[2], jpayne@69: self.traceback_limit, stderr jpayne@69: ) jpayne@69: stderr.flush() jpayne@69: finally: jpayne@69: exc_info = None jpayne@69: jpayne@69: def handle_error(self): jpayne@69: """Log current error, and send error output to client if possible""" jpayne@69: self.log_exception(sys.exc_info()) jpayne@69: if not self.headers_sent: jpayne@69: self.result = self.error_output(self.environ, self.start_response) jpayne@69: self.finish_response() jpayne@69: # XXX else: attempt advanced recovery techniques for HTML or text? jpayne@69: jpayne@69: def error_output(self, environ, start_response): jpayne@69: """WSGI mini-app to create error output jpayne@69: jpayne@69: By default, this just uses the 'error_status', 'error_headers', jpayne@69: and 'error_body' attributes to generate an output page. It can jpayne@69: be overridden in a subclass to dynamically generate diagnostics, jpayne@69: choose an appropriate message for the user's preferred language, etc. jpayne@69: jpayne@69: Note, however, that it's not recommended from a security perspective to jpayne@69: spit out diagnostics to any old user; ideally, you should have to do jpayne@69: something special to enable diagnostic output, which is why we don't jpayne@69: include any here! jpayne@69: """ jpayne@69: start_response(self.error_status,self.error_headers[:],sys.exc_info()) jpayne@69: return [self.error_body] jpayne@69: jpayne@69: jpayne@69: # Pure abstract methods; *must* be overridden in subclasses jpayne@69: jpayne@69: def _write(self,data): jpayne@69: """Override in subclass to buffer data for send to client jpayne@69: jpayne@69: It's okay if this method actually transmits the data; BaseHandler jpayne@69: just separates write and flush operations for greater efficiency jpayne@69: when the underlying system actually has such a distinction. jpayne@69: """ jpayne@69: raise NotImplementedError jpayne@69: jpayne@69: def _flush(self): jpayne@69: """Override in subclass to force sending of recent '_write()' calls jpayne@69: jpayne@69: It's okay if this method is a no-op (i.e., if '_write()' actually jpayne@69: sends the data. jpayne@69: """ jpayne@69: raise NotImplementedError jpayne@69: jpayne@69: def get_stdin(self): jpayne@69: """Override in subclass to return suitable 'wsgi.input'""" jpayne@69: raise NotImplementedError jpayne@69: jpayne@69: def get_stderr(self): jpayne@69: """Override in subclass to return suitable 'wsgi.errors'""" jpayne@69: raise NotImplementedError jpayne@69: jpayne@69: def add_cgi_vars(self): jpayne@69: """Override in subclass to insert CGI variables in 'self.environ'""" jpayne@69: raise NotImplementedError jpayne@69: jpayne@69: jpayne@69: class SimpleHandler(BaseHandler): jpayne@69: """Handler that's just initialized with streams, environment, etc. jpayne@69: jpayne@69: This handler subclass is intended for synchronous HTTP/1.0 origin servers, jpayne@69: and handles sending the entire response output, given the correct inputs. jpayne@69: jpayne@69: Usage:: jpayne@69: jpayne@69: handler = SimpleHandler( jpayne@69: inp,out,err,env, multithread=False, multiprocess=True jpayne@69: ) jpayne@69: handler.run(app)""" jpayne@69: jpayne@69: def __init__(self,stdin,stdout,stderr,environ, jpayne@69: multithread=True, multiprocess=False jpayne@69: ): jpayne@69: self.stdin = stdin jpayne@69: self.stdout = stdout jpayne@69: self.stderr = stderr jpayne@69: self.base_env = environ jpayne@69: self.wsgi_multithread = multithread jpayne@69: self.wsgi_multiprocess = multiprocess jpayne@69: jpayne@69: def get_stdin(self): jpayne@69: return self.stdin jpayne@69: jpayne@69: def get_stderr(self): jpayne@69: return self.stderr jpayne@69: jpayne@69: def add_cgi_vars(self): jpayne@69: self.environ.update(self.base_env) jpayne@69: jpayne@69: def _write(self,data): jpayne@69: result = self.stdout.write(data) jpayne@69: if result is None or result == len(data): jpayne@69: return jpayne@69: from warnings import warn jpayne@69: warn("SimpleHandler.stdout.write() should not do partial writes", jpayne@69: DeprecationWarning) jpayne@69: while True: jpayne@69: data = data[result:] jpayne@69: if not data: jpayne@69: break jpayne@69: result = self.stdout.write(data) jpayne@69: jpayne@69: def _flush(self): jpayne@69: self.stdout.flush() jpayne@69: self._flush = self.stdout.flush jpayne@69: jpayne@69: jpayne@69: class BaseCGIHandler(SimpleHandler): jpayne@69: jpayne@69: """CGI-like systems using input/output/error streams and environ mapping jpayne@69: jpayne@69: Usage:: jpayne@69: jpayne@69: handler = BaseCGIHandler(inp,out,err,env) jpayne@69: handler.run(app) jpayne@69: jpayne@69: This handler class is useful for gateway protocols like ReadyExec and jpayne@69: FastCGI, that have usable input/output/error streams and an environment jpayne@69: mapping. It's also the base class for CGIHandler, which just uses jpayne@69: sys.stdin, os.environ, and so on. jpayne@69: jpayne@69: The constructor also takes keyword arguments 'multithread' and jpayne@69: 'multiprocess' (defaulting to 'True' and 'False' respectively) to control jpayne@69: the configuration sent to the application. It sets 'origin_server' to jpayne@69: False (to enable CGI-like output), and assumes that 'wsgi.run_once' is jpayne@69: False. jpayne@69: """ jpayne@69: jpayne@69: origin_server = False jpayne@69: jpayne@69: jpayne@69: class CGIHandler(BaseCGIHandler): jpayne@69: jpayne@69: """CGI-based invocation via sys.stdin/stdout/stderr and os.environ jpayne@69: jpayne@69: Usage:: jpayne@69: jpayne@69: CGIHandler().run(app) jpayne@69: jpayne@69: The difference between this class and BaseCGIHandler is that it always jpayne@69: uses 'wsgi.run_once' of 'True', 'wsgi.multithread' of 'False', and jpayne@69: 'wsgi.multiprocess' of 'True'. It does not take any initialization jpayne@69: parameters, but always uses 'sys.stdin', 'os.environ', and friends. jpayne@69: jpayne@69: If you need to override any of these parameters, use BaseCGIHandler jpayne@69: instead. jpayne@69: """ jpayne@69: jpayne@69: wsgi_run_once = True jpayne@69: # Do not allow os.environ to leak between requests in Google App Engine jpayne@69: # and other multi-run CGI use cases. This is not easily testable. jpayne@69: # See http://bugs.python.org/issue7250 jpayne@69: os_environ = {} jpayne@69: jpayne@69: def __init__(self): jpayne@69: BaseCGIHandler.__init__( jpayne@69: self, sys.stdin.buffer, sys.stdout.buffer, sys.stderr, jpayne@69: read_environ(), multithread=False, multiprocess=True jpayne@69: ) jpayne@69: jpayne@69: jpayne@69: class IISCGIHandler(BaseCGIHandler): jpayne@69: """CGI-based invocation with workaround for IIS path bug jpayne@69: jpayne@69: This handler should be used in preference to CGIHandler when deploying on jpayne@69: Microsoft IIS without having set the config allowPathInfo option (IIS>=7) jpayne@69: or metabase allowPathInfoForScriptMappings (IIS<7). jpayne@69: """ jpayne@69: wsgi_run_once = True jpayne@69: os_environ = {} jpayne@69: jpayne@69: # By default, IIS gives a PATH_INFO that duplicates the SCRIPT_NAME at jpayne@69: # the front, causing problems for WSGI applications that wish to implement jpayne@69: # routing. This handler strips any such duplicated path. jpayne@69: jpayne@69: # IIS can be configured to pass the correct PATH_INFO, but this causes jpayne@69: # another bug where PATH_TRANSLATED is wrong. Luckily this variable is jpayne@69: # rarely used and is not guaranteed by WSGI. On IIS<7, though, the jpayne@69: # setting can only be made on a vhost level, affecting all other script jpayne@69: # mappings, many of which break when exposed to the PATH_TRANSLATED bug. jpayne@69: # For this reason IIS<7 is almost never deployed with the fix. (Even IIS7 jpayne@69: # rarely uses it because there is still no UI for it.) jpayne@69: jpayne@69: # There is no way for CGI code to tell whether the option was set, so a jpayne@69: # separate handler class is provided. jpayne@69: def __init__(self): jpayne@69: environ= read_environ() jpayne@69: path = environ.get('PATH_INFO', '') jpayne@69: script = environ.get('SCRIPT_NAME', '') jpayne@69: if (path+'/').startswith(script+'/'): jpayne@69: environ['PATH_INFO'] = path[len(script):] jpayne@69: BaseCGIHandler.__init__( jpayne@69: self, sys.stdin.buffer, sys.stdout.buffer, sys.stderr, jpayne@69: environ, multithread=False, multiprocess=True jpayne@69: )