tornado学习笔记18 _RequestDispatcher 请求分发器

根据Application的配置,主要负责将客户端的请求分发到具体的RequestHandler。这个类实现了HTTPMessageDelegate接口。正则表达式

18.1 构造函数

定义:缓存

def __init__(self, application, connection):

参数:app

application:Application对象。函数

connection:请求链接,为HTTP1Connection实例。oop

实现分析:post

就是对这个类的属性进行初始化赋值。好比chunks,handler_class(RequestHandler处理类)、handler_kwargs(处理类参数)、path_args、path_kwargs等。网站

18.2 HTTPMessageDelegate接口实现

1.2.1 header_received

定义ui

def headers_received(self, start_line, headers):

实现分析:spa

实例化HTTPServerRequest对象,调用set_request方法,set_request方法中去查找匹配的请求处理类(RequestHandler)。若是请求处理类实现了_stream_request_body方法,则直接调用execute方法。debug

1.2.2 data_received

定义

def data_received(self, data):

实现过程:

若是RequestHandler实现了_stream_request_body方法,则调用handler的data_received方法,若是没实现,则将数据块添加至chunks中。

1.2.3 finish

当消息数据块接收完毕后,调用此方法。此方法的实现就是将数据块链接起来,而后调用HTTPServerReuqest的_parse_body方法,实现对body体消息的解析。而后调用excute方法。

18.3 其余方法

1.3.1 _find_handler

这个方法很重要,也是核心方法之一。实现Application的配置属性以及请求路径的匹配,找到匹配的RequestHandler。

实现过程:

(1) 调用Application的_get_host_handlers方法,得到匹配的hanlders集合;

(2) 若是没有匹配到合适的handlers,将handler_class设置成RedirectHandler,并设置handler_kwargs,而后返回。

(3) 若是匹配到了合适的handlers,循环handlers中的每一元素URLSpec,判断其中的路径正则表达式是否与请求路径相匹配。

(4) 若是匹配合适的RequestHandler,设置handler_class以及handler_kwargs,然后设置path_kwargs

(5) 没有没有匹配到RequestHandler,判断Application是否设置了default_handler_class选项,并设置handler_class为其值。若是没有设置default_handler_class选项,则将handler_class属性设置成 ErrorHandler,状态码设置成404错误,也就是not found错误。

18.3.2 execute

这个方法很核心,也很重要。当请求信息处理完毕后(调用finish方法后),会执行execute方法。方法以下:

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

实现过程描述以下:

(1) 判断application是否对complied_template_cache是否设置成True.若是没有,则将模板加载器重置。至关于不对编译后模板缓存的话,就重置模板加载器。

(2) 判断applacation是否对static_hash_cache是否设置成True. 若是没有,则调用StaticFileHandler的reset方法。至关于不对网站的静态文件进行缓存的话,就调用重置的方法。

(3) 根据_find_handler方法设置的handler_class属性初始化自定义的RequestHanlder

(4) 调用requestHandler的_exucute方法,就是调用相应的方法,要门是get方法,要么是post,要么是其余支持的http方法,具体实现详情请查看RequestHandler类。

相关文章
相关标签/搜索