Пример #1
0
    def _get(self, url, method, host):
        """Get a request handler based on the URL of the request, or raises an
        error.  Internal method for caching.

        :param url: request URL
        :param method: request method
        :return: handler, arguments, keyword arguments
        """
        url = unquote(host + url)
        # Check against known static routes
        route = self.routes_static.get(url)
        method_not_supported = MethodNotSupported(
            f"Method {method} not allowed for URL {url}",
            method=method,
            allowed_methods=self.get_supported_methods(url),
        )

        if route:
            if route.methods and method not in route.methods:
                raise method_not_supported
            match = route.pattern.match(url)
        else:
            route_found = False
            # Move on to testing all regex routes
            for route in self.routes_dynamic[url_hash(url)]:
                match = route.pattern.match(url)
                route_found |= match is not None
                # Do early method checking
                if match and method in route.methods:
                    break
            else:
                # Lastly, check against all regex routes that cannot be hashed
                for route in self.routes_always_check:
                    match = route.pattern.match(url)
                    route_found |= match is not None
                    # Do early method checking
                    if match and method in route.methods:
                        break
                else:
                    # Route was found but the methods didn't match
                    if route_found:
                        raise method_not_supported
                    raise NotFound(f"Requested URL {url} not found")

        kwargs = {
            p.name: p.cast(value)
            for value, p in zip(match.groups(1), route.parameters)
        }
        route_handler = route.handler
        if hasattr(route_handler, "handlers"):
            route_handler = route_handler.handlers[method]

        return route_handler, [], kwargs, route.uri, route.name, route.endpoint
Пример #2
0
 def _get(
         self, path: str, method: str,
         host: Optional[str]) -> Tuple[Route, RouteHandler, Dict[str, Any]]:
     try:
         return self.resolve(
             path=path,
             method=method,
             extra={"host": host} if host else None,
         )
     except RoutingNotFound as e:
         raise NotFound("Requested URL {} not found".format(e.path))
     except NoMethod as e:
         raise MethodNotSupported(
             "Method {} not allowed for URL {}".format(method, path),
             method=method,
             allowed_methods=e.allowed_methods,
         )
Пример #3
0
    def _get(self, path, method,
             host) -> Tuple[Route, RouteHandler, Dict[str, Any]]:
        try:
            route, handler, params = self.resolve(
                path=path,
                method=method,
                extra={"host": host},
            )
        except RoutingNotFound as e:
            raise NotFound("Requested URL {} not found".format(e.path))
        except NoMethod as e:
            raise MethodNotSupported(
                "Method {} not allowed for URL {}".format(method, path),
                method=method,
                allowed_methods=e.allowed_methods,
            )

        return (
            route,
            handler,
            params,
        )
Пример #4
0
    def _get(self, path, method, host):
        try:
            route, handler, params = self.resolve(
                path=path,
                method=method,
                extra={"host": host},
            )
        except RoutingNotFound as e:
            raise NotFound("Requested URL {} not found".format(e.path))
        except NoMethod as e:
            raise MethodNotSupported(
                "Method {} not allowed for URL {}".format(method, path),
                method=method,
                allowed_methods=e.allowed_methods,
            )

        return (
            handler,
            params,
            route.path,
            route.name,
            route.ctx.ignore_body,
        )
Пример #5
0
    def _get(self, url, method, host):
        """Get a request handler based on the URL of the request, or raises an
        error.  Internal method for caching.

        :param url: request URL
        :param method: request method
        :return: handler, arguments, keyword arguments
        """
        # 去掉url中的转义字符,编码序列替换为uncode
        url = unquote(host + url)
        # Check against known static routes
        # Route = namedtuple(
        # "Route", ["handler", "methods", "pattern", "parameters", "name", "uri"]
        # )
        route = self.routes_static.get(url)
        method_not_supported = MethodNotSupported(
            f"Method {method} not allowed for URL {url}",
            method=method,
            allowed_methods=self.get_supported_methods(url),
        )

        if route:
            if route.methods and method not in route.methods:
                raise method_not_supported
            match = route.pattern.match(url)
        else:
            route_found = False
            # Move on to testing all regex routes
            for route in self.routes_dynamic[url_hash(url)]:
                match = route.pattern.match(url)
                # 只要match 是1就是1  TODO not_know
                route_found |= match is not None
                # Do early method checking
                if match and method in route.methods:
                    # 跳过finally,不再检测routes_always_check
                    break
            else:  #like finally
                # Lastly, check against all regex routes that cannot be hashed
                for route in self.routes_always_check:
                    match = route.pattern.match(url)
                    route_found |= match is not None
                    # Do early method checking
                    if match and method in route.methods:
                        # 跳过finally
                        break
                else:
                    # Route was found but the methods didn't match
                    if route_found:
                        raise method_not_supported
                    raise NotFound(f"Requested URL {url} not found")

        # 将match.groups(1)与route.parameters进行打包为元组,https://www.runoob.com/python/python-func-zip.html
        # match.groups(1)代表是将match的结果分组返回比如match的结果是123abc!@#,第一组是123,二组是abc,三组是!@#
        kwargs = {
            p.name: p.cast(value)
            for value, p in zip(match.groups(1), route.parameters)
        }
        route_handler = route.handler
        if hasattr(route_handler, "handlers"):
            route_handler = route_handler.handlers[method]
        return route_handler, [], kwargs, route.uri, route.name