def run(self, requestLine, headers, rfile): """Process the Request. requestLine should be of the form "GET /path HTTP/1.0". headers should be a list of (name, value) tuples. rfile should be a file-like object containing the HTTP request entity. When run() is done, the returned object should have 3 attributes: status, e.g. "200 OK" headers, a list of (name, value) tuples body, an iterable yielding strings Consumer code (HTTP servers) should then access these response attributes to build the outbound stream. """ self.requestLine = requestLine.strip() self.header_list = list(headers) self.rfile = rfile self.headers = httptools.HeaderMap() self.headerMap = self.headers # Backward compatibility self.simple_cookie = Cookie.SimpleCookie() self.simpleCookie = self.simple_cookie # Backward compatibility if cherrypy.profiler: cherrypy.profiler.run(self._run) else: self._run() if self.method == "HEAD": # HEAD requests MUST NOT return a message-body in the response. cherrypy.response.body = [] _cputil.get_special_attribute("_cp_log_access", "_cpLogAccess")() return cherrypy.response
def handleError(self, exc): """Set status, headers, and body when an unanticipated error occurs.""" try: applyFilters('before_error_response') # _cp_on_error will probably change self.body. # It may also change the headers, etc. _cputil.get_special_attribute('_cp_on_error', '_cpOnError')() self.finalize() 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
def applyFilters(method_name, failsafe=False): """Execute the given method for all registered filters.""" special_methods = [] for f in _cputil.get_special_attribute("_cp_filters", "_cpFilterList"): if cherrypy.lowercase_api is False: # Try old name first old_method_name = backward_compatibility_dict.get(method_name) method = getattr(f, old_method_name, None) if (method is None): method = getattr(f, method_name, None) if method: special_methods.append(method) else: # We know for sure that user uses the new lowercase API method = getattr(f, method_name, None) if method: special_methods.append(method) if method_name in _input_methods: # Run special filters after defaults. methods = _filterhooks[method_name] + special_methods else: # Run special filters before defaults. methods = special_methods + _filterhooks[method_name] for method in methods: # The on_start_resource, on_end_resource, and on_end_request methods # are guaranteed to run even if other methods of the same name fail. # We will still log the failure, but proceed on to the next method. # The only way to stop all processing from one of these methods is # to raise SystemExit and stop the whole server. So, trap your own # errors in these methods! if failsafe: try: method() except (KeyboardInterrupt, SystemExit): raise except: cherrypy.log(traceback=True) else: method()