| 676 | await self.middleware_stack(scope, receive, send) |
| 677 | |
| 678 | async def app(self, scope: Scope, receive: Receive, send: Send) -> None: |
| 679 | assert scope["type"] in ("http", "websocket", "lifespan") |
| 680 | |
| 681 | if "router" not in scope: |
| 682 | scope["router"] = self |
| 683 | |
| 684 | if scope["type"] == "lifespan": |
| 685 | await self.lifespan(scope, receive, send) |
| 686 | return |
| 687 | |
| 688 | partial = None |
| 689 | |
| 690 | for route in self.routes: |
| 691 | # Determine if any route matches the incoming scope, |
| 692 | # and hand over to the matching route if found. |
| 693 | match, child_scope = route.matches(scope) |
| 694 | if match == Match.FULL: |
| 695 | scope["route"] = route |
| 696 | scope.update(child_scope) |
| 697 | await route.handle(scope, receive, send) |
| 698 | return |
| 699 | elif match == Match.PARTIAL and partial is None: |
| 700 | partial = route |
| 701 | partial_scope = child_scope |
| 702 | |
| 703 | if partial is not None: |
| 704 | # Handle partial matches. These are cases where an endpoint is |
| 705 | # able to handle the request, but is not a preferred option. |
| 706 | # We use this in particular to deal with "405 Method Not Allowed". |
| 707 | scope["route"] = partial |
| 708 | scope.update(partial_scope) |
| 709 | await partial.handle(scope, receive, send) |
| 710 | return |
| 711 | |
| 712 | route_path = get_route_path(scope) |
| 713 | if scope["type"] == "http" and self.redirect_slashes and route_path != "/": |
| 714 | redirect_scope = dict(scope) |
| 715 | if route_path.endswith("/"): |
| 716 | redirect_scope["path"] = redirect_scope["path"].rstrip("/") |
| 717 | else: |
| 718 | redirect_scope["path"] = redirect_scope["path"] + "/" |
| 719 | |
| 720 | for route in self.routes: |
| 721 | match, child_scope = route.matches(redirect_scope) |
| 722 | if match != Match.NONE: |
| 723 | redirect_url = URL(scope=redirect_scope) |
| 724 | response = RedirectResponse(url=str(redirect_url)) |
| 725 | await response(scope, receive, send) |
| 726 | return |
| 727 | |
| 728 | await self.default(scope, receive, send) |
| 729 | |
| 730 | def __eq__(self, other: Any) -> bool: |
| 731 | return isinstance(other, Router) and self.routes == other.routes |