Source code distributed/http/routing.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import os
from tornado import web
import tornado.httputil
import tornado.routing


def _descend_routes(router, routers=set(), out=set()):
    if router in routers:
        return
    routers.add(router)
    for rule in list(router.named_rules.values()) + router.rules:
        if isinstance(rule.matcher, tornado.routing.PathMatches):
            if issubclass(rule.target, tornado.web.StaticFileHandler):
                prefix = rule.matcher.regex.pattern.rstrip("(.*)$").rstrip("/")
                path = rule.target_kwargs["path"]
                for d, dirs, files in os.walk(path):
                    for fn in files:
                        fullpath = d + "/" + fn
                        ourpath = fullpath.replace(path, prefix).replace("\\", "/")
                        out.add(ourpath)
            else:
                out.add(rule.matcher.regex.pattern.rstrip("$"))
        if isinstance(rule.target, tornado.routing.RuleRouter):
            _descend_routes(rule.target, routers, out)


class DirectoryHandler(web.RequestHandler):
    """Crawls the HTTP application to find all routes"""

    def get(self):
        out = set()
        routers = set()
        for app in self.application.applications + [self.application]:
            if "bokeh" in str(app):
                out.update(set(app.app_paths))
            else:
                _descend_routes(app.default_router, routers, out)
                _descend_routes(app.wildcard_router, routers, out)
        self.write({"paths": sorted(out)})


class RoutingApplication(web.Application):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.applications = []
        self.add_handlers(".*$", [(r"/sitemap.json", DirectoryHandler)])

    def find_handler(self, request: tornado.httputil.HTTPServerRequest, **kwargs):
        handler = super().find_handler(request, **kwargs)
        if handler and not issubclass(handler.handler_class, web.ErrorHandler):
            return handler
        else:
            for app in self.applications:
                handler = app.find_handler(request, **kwargs) or handler
                if handler and not issubclass(handler.handler_class, web.ErrorHandler):
                    break
            return handler

    def add_application(self, application: web.Application):
        self.applications.append(application)