diff --git a/spiderweb/__init__.py b/spiderweb/__init__.py index 5edb991..d5bf1b4 100644 --- a/spiderweb/__init__.py +++ b/spiderweb/__init__.py @@ -1,3 +1,3 @@ from spiderweb.main import SpiderwebRouter # noqa: F401 from spiderweb.middleware import * # noqa: F401, F403 -from spiderweb.constants import __version__ +from spiderweb.constants import __version__ as __version__ diff --git a/spiderweb/local_server.py b/spiderweb/local_server.py index cfca6e9..b1ad396 100644 --- a/spiderweb/local_server.py +++ b/spiderweb/local_server.py @@ -3,7 +3,7 @@ import threading import time from logging import Logger from threading import Thread -from typing import NoReturn, Callable, Any +from typing import NoReturn from wsgiref.simple_server import WSGIServer, WSGIRequestHandler from spiderweb.constants import __version__ diff --git a/spiderweb/main.py b/spiderweb/main.py index 0cfd83a..daca021 100644 --- a/spiderweb/main.py +++ b/spiderweb/main.py @@ -21,7 +21,7 @@ from spiderweb.exceptions import ( ) from spiderweb.local_server import LocalServerMiddleware from spiderweb.request import Request -from spiderweb.response import HttpResponse, TemplateResponse +from spiderweb.response import HttpResponse, TemplateResponse, JsonResponse from spiderweb.routes import RoutesMiddleware from spiderweb.secrets import FernetMiddleware from spiderweb.utils import get_http_status_by_code @@ -87,15 +87,15 @@ class SpiderwebRouter( f"Static files directory '{str(static_dir)}' does not exist." ) raise ConfigError - self.add_route(r"/static/", send_file) + self.add_route(r"/static/", send_file) # noqa: F405 def fire_response(self, start_response, request: Request, resp: HttpResponse): try: status = get_http_status_by_code(resp.status_code) cookies = [] if "Set-Cookie" in resp.headers: - cookies = resp.headers['Set-Cookie'] - del resp.headers['Set-Cookie'] + cookies = resp.headers["Set-Cookie"] + del resp.headers["Set-Cookie"] headers = list(resp.headers.items()) for c in cookies: headers.append(("Set-Cookie", c)) @@ -157,7 +157,7 @@ class SpiderwebRouter( def prepare_and_fire_response(self, start_response, request, resp) -> list[bytes]: try: if isinstance(resp, dict): - self.fire_response(request, JsonResponse(data=resp)) + self.fire_response(start_response, request, JsonResponse(data=resp)) if isinstance(resp, TemplateResponse): resp.set_template_loader(self.env) @@ -200,9 +200,11 @@ class SpiderwebRouter( try: if handler: - abort = self.process_request_middleware(request) - if abort: - return + abort_view = self.process_request_middleware(request) + if abort_view: + return self.prepare_and_fire_response( + start_response, request, abort_view + ) resp = handler(request, **additional_args) if resp is None: raise NoResponseError(f"View {handler} returned None.") diff --git a/spiderweb/middleware/__init__.py b/spiderweb/middleware/__init__.py index 89a8ac2..f3112e0 100644 --- a/spiderweb/middleware/__init__.py +++ b/spiderweb/middleware/__init__.py @@ -1,7 +1,7 @@ from typing import Callable, ClassVar -from .base import SpiderwebMiddleware -from .csrf import CSRFMiddleware +from .base import SpiderwebMiddleware as SpiderwebMiddleware +from .csrf import CSRFMiddleware as CSRFMiddleware from ..exceptions import ConfigError, UnusedMiddleware from ..request import Request from ..response import HttpResponse @@ -33,8 +33,7 @@ class MiddlewareMiddleware: continue if resp: self.process_response_middleware(request, resp) - self.fire_response(request, resp) - return True # abort further processing + return resp # abort further processing def process_response_middleware( self, request: Request, response: HttpResponse diff --git a/spiderweb/response.py b/spiderweb/response.py index 9d1e846..b0ded65 100644 --- a/spiderweb/response.py +++ b/spiderweb/response.py @@ -39,17 +39,17 @@ class HttpResponse: return self.body def set_cookie( - self, - name: str, - value: str, - domain: str=None, - expires: datetime.datetime = None, - http_only: bool=None, - max_age: int=None, - partitioned: bool=None, - path: str=None, - secure: bool=False, - same_site: str=None + self, + name: str, + value: str, + domain: str = None, + expires: datetime.datetime = None, + http_only: bool = None, + max_age: int = None, + partitioned: bool = None, + path: str = None, + secure: bool = False, + same_site: str = None, ): if not bool(re.match(REGEX_COOKIE_NAME, name)): url = "https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#attributes" @@ -94,7 +94,6 @@ class HttpResponse: else: self.headers["Set-Cookie"] = [cookie] - def render(self) -> str: return str(self.body) diff --git a/spiderweb/routes.py b/spiderweb/routes.py index 407eadc..0a608b7 100644 --- a/spiderweb/routes.py +++ b/spiderweb/routes.py @@ -85,7 +85,7 @@ class RoutesMiddleware: def get_error_route(self, code: int) -> Callable: view = self._error_routes.get(code) or globals().get(f"http{code}") if not view: - return http500 + return http500 # noqa: F405 return view def check_for_route_duplicates(self, path: str): diff --git a/spiderweb/utils.py b/spiderweb/utils.py index 7ddc47a..4ae033f 100644 --- a/spiderweb/utils.py +++ b/spiderweb/utils.py @@ -15,7 +15,7 @@ def import_by_string(name): def is_safe_path(path: str) -> bool: # this cannot possibly catch all issues - return not ".." in str(path) + return ".." not in str(path) def get_http_status_by_code(code: int) -> Optional[str]: