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