(self, environ, start_response)
return response
The main call handler that is called to return a response
The main call handler that is called to return a response
The main call handler that is called to return a response
def __call__(self, environ, start_response):
        """The main call handler that is called to return a response"""
        log_debug = self._pylons_log_debug

        # Keep a local reference to the req/response objects
        self._py_object = environ['pylons.pylons']

        # Keep private methods private
        try:
            if environ['pylons.routes_dict']['action'][:1] in ('_', '-'):
                if log_debug:
                    log.debug("Action starts with _, private action not "
                              "allowed. Returning a 404 response")
                return HTTPNotFound()(environ, start_response)
        except KeyError:
            # The check later will notice that there's no action
            pass

        start_response_called = []

        def repl_start_response(status, headers, exc_info=None):
            response = self._py_object.response
            start_response_called.append(None)

            # Copy the headers from the global response
            if log_debug:
                log.debug("Merging pylons.response headers into "
                          "start_response call, status: %s", status)
            headers.extend(header for header in response.headerlist
                           if header[0] == 'Set-Cookie' or
                           header[0].startswith('X-'))
            return start_response(status, headers, exc_info)
        self.start_response = repl_start_response

        if hasattr(self, '__before__'):
            response = self._inspect_call(self.__before__)
            if hasattr(response, '_exception'):
                return response(environ, self.start_response)

        response = self._dispatch_call()
        if not start_response_called:
            self.start_response = start_response
            py_response = self._py_object.response
            # If its not a WSGI response, and we have content, it needs to
            # be wrapped in the response object
            if isinstance(response, str):
                if log_debug:
                    log.debug("Controller returned a string "
                              ", writing it to pylons.response")
                py_response.body = py_response.body + response
            elif isinstance(response, unicode):
                if log_debug:
                    log.debug("Controller returned a unicode string "
                              ", writing it to pylons.response")
                py_response.unicode_body = py_response.unicode_body + \
                        response
            elif hasattr(response, 'wsgi_response'):
                # It's an exception that got tossed.
                if log_debug:
                    log.debug("Controller returned a Response object, merging "
                              "it with pylons.response")
                for name, value in py_response.headers.items():
                    if name.lower() == 'set-cookie':
                        response.headers.add(name, value)
                    else:
                        response.headers.setdefault(name, value)
                try:
                    registry = environ['paste.registry']
                    registry.replace(pylons.response, response)
                except KeyError:
                    # Ignore the case when someone removes the registry
                    pass
                py_response = response
            elif response is None:
                if log_debug:
                    log.debug("Controller returned None")
            else:
                if log_debug:
                    log.debug("Assuming controller returned an iterable, "
                              "setting it as pylons.response.app_iter")
                py_response.app_iter = response
            response = py_response

        if hasattr(self, '__after__'):
            after = self._inspect_call(self.__after__)
            if hasattr(after, '_exception'):
                after.wsgi_response = True
                response = after

        if hasattr(response, 'wsgi_response'):
            # Copy the response object into the testing vars if we're testing
            if 'paste.testing_variables' in environ:
                environ['paste.testing_variables']['response'] = response
            if log_debug:
                log.debug("Calling Response object to return WSGI data")

            return response(environ, self.start_response)

        if log_debug:
            log.debug("Response assumed to be WSGI content, returning "
                      "un-touched")
        return response
def __call__ ( self , environ , start_response ) : log_debug = self . _pylons_log_debug # Keep a local reference to the req/response objects self . _py_object = environ [ 'pylons.pylons' ] # Keep private methods private try : if environ [ 'pylons.routes_dict' ] [ 'action' ] [ : 1 ] in ( '_' , '-' ) : if log_debug : log . debug ( "Action starts with _, private action not " "allowed. Returning a 404 response" ) return HTTPNotFound ( ) ( environ , start_response ) except KeyError : # The check later will notice that there's no action pass start_response_called = [ ] def repl_start_response ( status , headers , exc_info = None ) : response = self . _py_object . response start_response_called . append ( None ) # Copy the headers from the global response if log_debug : log . debug ( "Merging pylons.response headers into " "start_response call, status: %s" , status ) headers . extend ( header for header in response . headerlist if header [ 0 ] == 'Set-Cookie' or header [ 0 ] . startswith ( 'X-' ) ) return start_response ( status , headers , exc_info ) self . start_response = repl_start_response if hasattr ( self , '__before__' ) : response = self . _inspect_call ( self . __before__ ) if hasattr ( response , '_exception' ) : return response ( environ , self . start_response ) response = self . _dispatch_call ( ) if not start_response_called : self . start_response = start_response py_response = self . _py_object . response # If its not a WSGI response, and we have content, it needs to # be wrapped in the response object if isinstance ( response , str ) : if log_debug : log . debug ( "Controller returned a string " ", writing it to pylons.response" ) py_response . body = py_response . body + response elif isinstance ( response , unicode ) : if log_debug : log . debug ( "Controller returned a unicode string " ", writing it to pylons.response" ) py_response . unicode_body = py_response . unicode_body + response elif hasattr ( response , 'wsgi_response' ) : # It's an exception that got tossed. if log_debug : log . debug ( "Controller returned a Response object, merging " "it with pylons.response" ) for name , value in py_response . headers . items ( ) : if name . lower ( ) == 'set-cookie' : response . headers . add ( name , value ) else : response . headers . setdefault ( name , value ) try : registry = environ [ 'paste.registry' ] registry . replace ( pylons . response , response ) except KeyError : # Ignore the case when someone removes the registry pass py_response = response elif response is None : if log_debug : log . debug ( "Controller returned None" ) else : if log_debug : log . debug ( "Assuming controller returned an iterable, " "setting it as pylons.response.app_iter" ) py_response . app_iter = response response = py_response if hasattr ( self , '__after__' ) : after = self . _inspect_call ( self . __after__ ) if hasattr ( after , '_exception' ) : after . wsgi_response = True response = after if hasattr ( response , 'wsgi_response' ) : # Copy the response object into the testing vars if we're testing if 'paste.testing_variables' in environ : environ [ 'paste.testing_variables' ] [ 'response' ] = response if log_debug : log . debug ( "Calling Response object to return WSGI data" ) return response ( environ , self . start_response ) if log_debug : log . debug ( "Response assumed to be WSGI content, returning " "un-touched" ) return response