'Special handler for WSGI.'

def handler(self, req):

    if (req.role != FCGI_RESPONDER):
        return (FCGI_UNKNOWN_ROLE, 0)
    environ = req.params
    environ.update(self.environ)
    environ['wsgi.version'] = (1, 0)
    environ['wsgi.input'] = req.stdin
    if (self._bindAddress is None):
        stderr = req.stderr
    else:
        stderr = TeeOutputStream((sys.stderr, req.stderr))
    environ['wsgi.errors'] = stderr
    environ['wsgi.multithread'] = ((not isinstance(req, CGIRequest)) and thread_available and self.multithreaded)
    environ['wsgi.multiprocess'] = True
    environ['wsgi.run_once'] = isinstance(req, CGIRequest)
    if (environ.get('HTTPS', 'off') in ('on', '1')):
        environ['wsgi.url_scheme'] = 'https'
    else:
        environ['wsgi.url_scheme'] = 'http'
    self._sanitizeEnv(environ)
    headers_set = []
    headers_sent = []
    result = None
    def write(data):
        assert (type(data) in [str, unicode]), 'write()    argument    must    be    string'
        assert headers_set, 'write()    before    start_response()'
        if (not headers_sent):
            (status, responseHeaders) = headers_sent[:] = headers_set
            found = False
            for (header, value) in responseHeaders:
                if (header.lower() == 'content-length'):
                    found = True
                    break
            if ((not found) and (result is not None)):
                try:
                    if (len(result) == 1):
                        responseHeaders.append(('Content-Length', str(len(data))))
                except:
                    pass
            s = ('Status:    %s\r\n' % status)
            for header in responseHeaders:
                s += ('%s:    %s\r\n' % header)
            s += '\r\n'
            req.stdout.write(s)
        req.stdout.write(data)
        req.stdout.flush()
    def start_response(status, response_headers, exc_info=None):
        if exc_info:
            try:
                if headers_sent:
                    raise exc_info[0], exc_info[1], exc_info[2]
            finally:
                exc_info = None
        else:
            assert (not headers_set), 'Headers    already    set!'
        assert (type(status) in [str, unicode]), 'Status    must    be    a    string'
        assert (len(status) >= 4), 'Status    must    be    at    least    4    characters'
        assert int(status[:3]), 'Status    must    begin    with    3-digit    code'
        assert (status[3] == '    '), 'Status    must    have    a    space    after    code'
        assert (type(response_headers) is list), 'Headers    must    be    a    list'
        if __debug__:
            for (name, val) in response_headers:
                assert (type(name) in [str, unicode]), 'Header    names    must    be    strings'
                assert (type(val) in [str, unicode]), 'Header    values    must    be    strings'
        headers_set[:] = [status, response_headers]
        return write
    if (not self.multithreaded):
        self._app_lock.acquire()
    try:
        result = self.application(environ, start_response)
        try:
            for data in result:
                if data:
                    write(data)
            if (not headers_sent):
                write('')
        finally:
            if hasattr(result, 'close'):
                result.close()
    finally:
        if (not self.multithreaded):
            self._app_lock.release()
    return (FCGI_REQUEST_COMPLETE, 0)
