def __init__(self, clientAddress, remoteHost, requestLine, headers, rfile, scheme="http"): """Populate a new Request object. clientAddress should be a tuple of client IP address, client Port remoteHost should be string of the client's IP address. 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. scheme should be a string, either "http" or "https". When __init__ is done, cherrypy.response 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. """ request = cherrypy.request request.method = "" request.requestLine = requestLine.strip() self.parseFirstLine() # Prepare cherrypy.request variables request.remoteAddr = clientAddress[0] request.remotePort = clientAddress[1] request.remoteHost = remoteHost request.paramList = [] # Only used for Xml-Rpc request.headers = headers request.headerMap = KeyTitlingDict() request.simpleCookie = Cookie.SimpleCookie() request.rfile = rfile request.scheme = scheme # Prepare cherrypy.response variables cherrypy.response.status = None cherrypy.response.headers = None cherrypy.response.body = None cherrypy.response.headerMap = KeyTitlingDict() cherrypy.response.headerMap.update({ "Content-Type": "text/html", "Server": "CherryPy/" + cherrypy.__version__, "Date": cptools.HTTPDate(), "Set-Cookie": [], "Content-Length": None }) cherrypy.response.simpleCookie = Cookie.SimpleCookie() self.run() if request.method == "HEAD": # HEAD requests MUST NOT return a message-body in the response. cherrypy.response.body = [] _cputil.getSpecialAttribute("_cpLogAccess")()
def applyFilters(methodName): """Execute the given method for all registered filters.""" if methodName in ('onStartResource', 'beforeRequestBody', 'beforeMain'): filterList = (_cputil._cpDefaultInputFilterList + _cputil.getSpecialAttribute('_cpFilterList')) elif methodName in ('beforeFinalize', 'onEndResource', 'beforeErrorResponse', 'afterErrorResponse'): filterList = (_cputil.getSpecialAttribute('_cpFilterList') + _cputil._cpDefaultOutputFilterList) #else: # # '', # # 'beforeErrorResponse', 'afterErrorResponse' # filterList = (_cputil._cpDefaultInputFilterList + # _cputil.getSpecialAttribute('_cpFilterList') + # _cputil._cpDefaultOutputFilterList) else: assert False # Wrong methodName for the filter for filter in filterList: method = getattr(filter, methodName, None) if method: method()
def handleError(exc): """Set status, headers, and body when an unanticipated error occurs.""" try: applyFilters('beforeErrorResponse') # _cpOnError will probably change cherrypy.response.body. # It may also change the headerMap, etc. _cputil.getSpecialAttribute('_cpOnError')() finalize() applyFilters('afterErrorResponse') return except (cherrypy.HTTPRedirect, cherrypy.HTTPError), inst: try: inst.set_response() finalize() return except (KeyboardInterrupt, SystemExit): raise except: # Fall through to the second error handler pass