tornado study notes 18 _RequestDispatcher Request Dispatcher

6629 단어
According to the configuration of the Application, it is mainly responsible for distributing the client's request to the specific RequestHandler. This class implements the HTTPMessageDelegate interface.

18.1 Constructors


Definition:

def __init__(self, application, connection):

Parameters:

application: Application object.
connection: Request a connection, an instance of HTTP1Connection.

Implementation analysis:

It is to initialize the properties of this class. Such as chunks, handler_class (RequestHandler processing class), handler_kwargs (processing class parameters), path_args, path_kwargs, etc.

18.2 HTTPMessageDelegate interface implementation


1.2.1 header_received
Definition

def headers_received(self, start_line, headers):

Implementation analysis:

Instantiate the HTTPServerRequest object, call the set_request method, and find the matching request handler class (RequestHandler) in the set_request method. If the request processing class implements the _stream_request_body method, the execute method is called directly.

1.2.2 data_received
Definition

def data_received(self, data):

Implementation process:

If RequestHandler implements the _stream_request_body method, call the handler's data_received method, if not, add the data block to chunks.

1.2.3 finish

This method is called when the message data block has been received. The implementation of this method is to connect the data blocks, and then call the _parse_body method of HTTPServerReuqest to parse the body message. Then call the execute method.

18.3 Other methods


1.3.1 _find_handler

This method is very important and one of the core methods. Implement the matching of Application's configuration properties and request paths, and find the matching RequestHandler.

Implementation process:

(1) Call the _get_host_handlers method of Application to obtain the matching set of handlers;
(2) If no suitable handlers are matched, set handler_class to RedirectHandler, set handler_kwargs, and return.
(3) If the appropriate handlers are matched, loop each element URLSpec in the handlers to determine whether the path regular expression in it matches the request path.
(4) If it matches the appropriate RequestHandler, set handler_class and handler_kwargs, and then set path_kwargs
(5) If the RequestHandler is not matched, judge whether the Application has set the default_handler_class option, and set the handler_class as its value. If the default_handler_class option is not set, the handler_class attribute is set to ErrorHandler, and the status code is set to 404 error, that is, not found error.

18.3.2 execute

This method is very core and very important. When the request information is processed (after calling the finish method), the execute method will be executed. Methods as below:

def execute(self):    # If template cache is disabled (usually in the debug mode),
    # re-compile templates and reload static files on every
    # request so you don't need to restart to see changes
    if not self.application.settings.get("compiled_template_cache", True):
        with RequestHandler._template_loader_lock:
            for loader in RequestHandler._template_loaders.values():
                loader.reset()
    if not self.application.settings.get('static_hash_cache', True):
        StaticFileHandler.reset()

    self.handler = self.handler_class(self.application, self.request,
                                      **self.handler_kwargs)
    transforms = [t(self.request) for t in self.application.transforms]

    if self.stream_request_body:
        self.handler._prepared_future = Future()
    # Note that if an exception escapes handler._execute it will be
    # trapped in the Future it returns (which we are ignoring here,
    # leaving it to be logged when the Future is GC'd).
    # However, that shouldn't happen because _execute has a blanket
    # except handler, and we cannot easily access the IOLoop here to
    # call add_future (because of the requirement to remain compatible
    # with WSGI)
    f = self.handler._execute(transforms, *self.path_args,
                              **self.path_kwargs)
    # If we are streaming the request body, then execute() is finished
    # when the handler has prepared to receive the body.  If not,
    # it doesn't matter when execute() finishes (so we return None)
    return self.handler._prepared_future


The implementation process is described as follows:

(1) Determine whether the application is set to True for compiled_template_cache. If not, reset the template loader. It is equivalent to resetting the template loader if the compiled template is not cached.
(2) Determine whether application is set to True for static_hash_cache. If not, call the reset method of StaticFileHandler. It is equivalent to calling the reset method if the static files of the website are not cached.
(3) Initialize a custom RequestHanlder according to the handler_class attribute set by the _find_handler method
(4) Calling the _exucute method of requestHandler is to call the corresponding method. The key is the get method, either the post method, or other supported http methods. Please refer to the RequestHandler class for specific implementation details.

좋은 웹페이지 즐겨찾기