From 8e37e58a99d372691fe0201c2b9572682e6faa4c Mon Sep 17 00:00:00 2001 From: Joe Kaufeld Date: Fri, 9 Aug 2024 14:15:32 -0400 Subject: [PATCH] :bug: fix handling of UnusedMiddleware --- example.py | 8 +++++++- example_middleware.py | 6 ++++++ pyproject.toml | 2 +- spiderweb/main.py | 16 ++++++++++++---- 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/example.py b/example.py index ad1f75f..c06663d 100644 --- a/example.py +++ b/example.py @@ -6,7 +6,8 @@ app = WebServer( templates_dirs=["templates"], middleware=[ "example_middleware.TestMiddleware", - "example_middleware.RedirectMiddleware" + "example_middleware.RedirectMiddleware", + "example_middleware.ExplodingMiddleware", ], append_slash=False # default ) @@ -39,6 +40,11 @@ def middleware(request): ) +@app.route("/example/") +def example(request, id): + return HttpResponse(body=f"Example with id {id}") + + if __name__ == "__main__": # can also add routes like this: # app.add_route("/", index) diff --git a/example_middleware.py b/example_middleware.py index 09f38d7..be2d105 100644 --- a/example_middleware.py +++ b/example_middleware.py @@ -1,3 +1,4 @@ +from spiderweb.exceptions import UnusedMiddleware from spiderweb.middleware import SpiderwebMiddleware from spiderweb.request import Request from spiderweb.response import HttpResponse, RedirectResponse @@ -20,3 +21,8 @@ class RedirectMiddleware(SpiderwebMiddleware): def process_request(self, request: Request) -> HttpResponse | None: if request.path == "/middleware": return RedirectResponse("/") + + +class ExplodingMiddleware(SpiderwebMiddleware): + def process_request(self, request: Request) -> HttpResponse | None: + raise UnusedMiddleware("Unfinished!") diff --git a/pyproject.toml b/pyproject.toml index 04c4e42..c5323d9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "spiderweb" -version = "0.1.0" +version = "0.4.0" description = "A small web framework, just big enough to hold your average spider." authors = ["Joe Kaufeld "] readme = "README.md" diff --git a/spiderweb/main.py b/spiderweb/main.py index 91f748e..7d89ad5 100644 --- a/spiderweb/main.py +++ b/spiderweb/main.py @@ -23,7 +23,7 @@ from spiderweb.exceptions import ( ConfigError, ParseError, GeneralException, - NoResponseError, + NoResponseError, UnusedMiddleware, ) from spiderweb.request import Request from spiderweb.response import HttpResponse, JsonResponse, TemplateResponse, RedirectResponse @@ -301,7 +301,11 @@ class RequestHandler(BaseHTTPRequestHandler): def process_request_middleware(self, request: Request) -> None | bool: for middleware in self.middleware: - resp = middleware.process_request(request) + try: + resp = middleware.process_request(request) + except UnusedMiddleware: + self.middleware.remove(middleware) + continue if resp: self.process_response_middleware(request, resp) self.fire_response(request, resp) @@ -309,7 +313,11 @@ class RequestHandler(BaseHTTPRequestHandler): def process_response_middleware(self, request: Request, response: HttpResponse) -> None: for middleware in self.middleware: - middleware.process_response(request, response) + try: + middleware.process_response(request, response) + except UnusedMiddleware: + self.middleware.remove(middleware) + continue def prepare_response(self, request, resp) -> HttpResponse: try: @@ -322,7 +330,7 @@ class RequestHandler(BaseHTTPRequestHandler): for middleware in self.middleware: middleware.process_response(request, resp) - self.fire_response(resp) + self.fire_response(request, resp) except APIError: raise