def wsgiApp(environ, start_response): """The WSGI 'application object' for CherryPy.""" # Trap screen output from BaseHTTPRequestHandler.log_message() if not cherrypy.config.get("server.logToScreen"): sys.stderr = NullWriter() try: cherrypy.request.purge__() cherrypy.response.purge__() # LOGON_USER is served by IIS, and is the name of the # user after having been mapped to a local account. # Both IIS and Apache set REMOTE_USER, when possible. cherrypy.request.login = environ.get("LOGON_USER") or environ.get("REMOTE_USER") or None cherrypy.request.multithread = environ["wsgi.multithread"] cherrypy.request.multiprocess = environ["wsgi.multiprocess"] clientAddr = (environ.get("REMOTE_ADDR", ""), int(environ.get("REMOTE_PORT", -1))) cherrypy.server.request( clientAddr, environ.get("REMOTE_ADDR", ""), requestLine(environ), translate_headers(environ), environ["wsgi.input"], environ["wsgi.url_scheme"], ) except (KeyboardInterrupt, SystemExit): raise except: tb = _cputil.formatExc() cherrypy.log(tb) defaultOn = cherrypy.config.get("server.environment") == "development" if not cherrypy.config.get("server.showTracebacks", defaultOn): tb = "" s, h, b = _cphttptools.bareError(tb) exc = sys.exc_info() else: response = cherrypy.response s, h, b = response.status, response.headers, response.body exc = None try: start_response(s, h, exc) for chunk in b: # WSGI requires all data to be of type "str". This coercion should # not take any time at all if chunk is already of type "str". # If it's unicode, it could be a big performance hit (x ~500). chunk = str(chunk) yield chunk except (KeyboardInterrupt, SystemExit): raise except: tb = _cputil.formatExc() cherrypy.log(tb) s, h, b = _cphttptools.bareError() # CherryPy test suite expects bareError body to be output, # so don't call start_response (which, according to PEP 333, # may raise its own error at that point). for chunk in b: yield str(chunk)
def wsgiApp(environ, start_response): """The WSGI 'application object' for CherryPy.""" # Trap screen output from BaseHTTPRequestHandler.log_message() if not cherrypy.config.get('server.log_to_screen'): sys.stderr = NullWriter() request = None try: # LOGON_USER is served by IIS, and is the name of the # user after having been mapped to a local account. # Both IIS and Apache set REMOTE_USER, when possible. env = environ.get clientAddr = (env('REMOTE_ADDR', ''), int(env('REMOTE_PORT', -1))) request = cherrypy.server.request(clientAddr, env('REMOTE_ADDR', ''), environ['wsgi.url_scheme']) request.login = (env('LOGON_USER') or env('REMOTE_USER') or None) request.multithread = environ['wsgi.multithread'] request.multiprocess = environ['wsgi.multiprocess'] request.wsgi_environ = environ response = request.run(requestLine(environ), translate_headers(environ), environ['wsgi.input']) s, h, b = response.status, response.header_list, response.body exc = None except (KeyboardInterrupt, SystemExit): raise except: if cherrypy.config.get("server.throw_errors", False): raise tb = _cputil.formatExc() cherrypy.log(tb) if not cherrypy.config.get("server.show_tracebacks", False): tb = "" s, h, b = _cputil.bareError(tb) exc = sys.exc_info() try: start_response(s, h, exc) for chunk in b: # WSGI requires all data to be of type "str". This coercion should # not take any time at all if chunk is already of type "str". # If it's unicode, it could be a big performance hit (x ~500). chunk = str(chunk) yield chunk if request: request.close() request = None except (KeyboardInterrupt, SystemExit), ex: try: if request: request.close() except: cherrypy.log(traceback=True) request = None raise ex
def set_response(self): import cherrypy from cherrypy._cputil import getErrorPage, formatExc tb = formatExc() if cherrypy.config.get('server.logTracebacks', True): cherrypy.log(tb) defaultOn = (cherrypy.config.get('server.environment') == 'development') if not cherrypy.config.get('server.showTracebacks', defaultOn): tb = None # In all cases, finalize will be called after this method, # so don't bother cleaning up response values here. cherrypy.response.status = self.status cherrypy.response.body = getErrorPage(self.status, traceback=tb, message=self.message) if cherrypy.response.headerMap.has_key("Content-Encoding"): del cherrypy.response.headerMap['Content-Encoding']
def handle_error(self, request, client_address): """Handle an error gracefully. May be overridden.""" errorBody = _cputil.formatExc() cherrypy.log(errorBody)
def handle_error(self, request, client_address): errorBody = _cputil.formatExc() cherrypy.log(errorBody)
except (KeyboardInterrupt, SystemExit): raise except: # Fall through to the second error handler pass except (KeyboardInterrupt, SystemExit): raise except: # Fall through to the second error handler pass # Failure in _cpOnError, error filter, or finalize. # Bypass them all. defaultOn = (cherrypy.config.get('server.environment') == 'development') if cherrypy.config.get('server.showTracebacks', defaultOn): body = dbltrace % (_cputil.formatExc(exc), _cputil.formatExc()) else: body = "" response = cherrypy.response response.status, response.headers, response.body = bareError(body) def bareError(extrabody=None): """Produce status, headers, body for a critical error. Returns a triple without calling any other questionable functions, so it should be as error-free as possible. Call it from an HTTP server if you get errors after Request() is done. If extrabody is None, a friendly but rather unhelpful error message is set in the body. If extrabody is a string, it will be appended as-is to the body.
applyFilters('after_error_response') return except cherrypy.HTTPRedirect, inst: try: inst.set_response() self.finalize() return except (KeyboardInterrupt, SystemExit): raise except: # Fall through to the second error handler pass except (KeyboardInterrupt, SystemExit): raise except: # Fall through to the second error handler pass # Failure in _cp_on_error, error filter, or finalize. # Bypass them all. if cherrypy.config.get('server.show_tracebacks', False): body = self.dbltrace % (_cputil.formatExc(exc), _cputil.formatExc()) else: body = "" self.setBareError(body) def setBareError(self, body=None): self.status, self.header_list, self.body = _cputil.bareError(body)