From 588cc3d50d5814caa5caa037be0077043aedd241 Mon Sep 17 00:00:00 2001 From: Joe Kaufeld Date: Sun, 13 Oct 2024 20:26:17 -0400 Subject: [PATCH] :construction: is this even going to work? --- poetry.lock | 31 ++++++++++++++++++++++++++++++- pyproject.toml | 1 + spiderweb/main.py | 22 ++++++++++++++++++++++ spiderweb/request.py | 21 ++++++++++++++------- 4 files changed, 67 insertions(+), 8 deletions(-) diff --git a/poetry.lock b/poetry.lock index eea4a67..fceefbc 100644 --- a/poetry.lock +++ b/poetry.lock @@ -367,6 +367,17 @@ setproctitle = ["setproctitle"] testing = ["coverage", "eventlet", "gevent", "pytest", "pytest-cov"] tornado = ["tornado (>=0.2)"] +[[package]] +name = "h11" +version = "0.14.0" +description = "A pure-Python, bring-your-own-I/O implementation of HTTP/1.1" +optional = false +python-versions = ">=3.7" +files = [ + {file = "h11-0.14.0-py3-none-any.whl", hash = "sha256:e3fe4ac4b851c468cc8363d500db52c2ead036020723024a109d37346efaa761"}, + {file = "h11-0.14.0.tar.gz", hash = "sha256:8f19fbbe99e72420ff35c00b27a34cb9937e902a8b810e2c88300c6f0a3b699d"}, +] + [[package]] name = "hypothesis" version = "6.111.2" @@ -784,7 +795,25 @@ files = [ {file = "typing_extensions-4.12.2.tar.gz", hash = "sha256:1a7ead55c7e559dd4dee8856e3a88b41225abfe1ce8df57b7c13915fe121ffb8"}, ] +[[package]] +name = "uvicorn" +version = "0.31.1" +description = "The lightning-fast ASGI server." +optional = false +python-versions = ">=3.8" +files = [ + {file = "uvicorn-0.31.1-py3-none-any.whl", hash = "sha256:adc42d9cac80cf3e51af97c1851648066841e7cfb6993a4ca8de29ac1548ed41"}, + {file = "uvicorn-0.31.1.tar.gz", hash = "sha256:f5167919867b161b7bcaf32646c6a94cdbd4c3aa2eb5c17d36bb9aa5cfd8c493"}, +] + +[package.dependencies] +click = ">=7.0" +h11 = ">=0.8" + +[package.extras] +standard = ["colorama (>=0.4)", "httptools (>=0.5.0)", "python-dotenv (>=0.13)", "pyyaml (>=5.1)", "uvloop (>=0.14.0,!=0.15.0,!=0.15.1)", "watchfiles (>=0.13)", "websockets (>=10.4)"] + [metadata] lock-version = "2.0" python-versions = "^3.11" -content-hash = "17f5dc4b157da57ad75a6f6aa3feb7adfa07500b805d5e79d1f09d640964949f" +content-hash = "2a986e86d4e694216b4585b8acb128a6f9f9958edb6236ac52fd9624be6c31cf" diff --git a/pyproject.toml b/pyproject.toml index d351fe6..d2225f2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -43,6 +43,7 @@ black = "^24.8.0" gunicorn = "^23.0.0" hypothesis = "^6.111.2" coverage = "^7.6.1" +uvicorn = "^0.31.1" [build-system] requires = ["poetry-core"] diff --git a/spiderweb/main.py b/spiderweb/main.py index b5a935c..6b93629 100644 --- a/spiderweb/main.py +++ b/spiderweb/main.py @@ -351,3 +351,25 @@ class SpiderwebRouter(LocalServerMixin, MiddlewareMixin, RoutesMixin, FernetMixi raise SpiderwebNetworkException(404) except SpiderwebNetworkException as e: return self.send_error_response(start_response, request, e) + + def asgi(self, scope): + async def inner(receive, send): + if scope['path'] == '/': + await send({ + 'type': 'http.response.start', + 'status': 200, + 'headers': [ + [b'content-type', b'text/html'] + ] + }) + await send({ + 'type': 'http.response.body', + 'body': bytes(f'

Index page

\n\n

{scope}

{receive}

{send}

', DEFAULT_ENCODING) + + }) + elif scope['path'] == '/hello': + ... + else: + ... + + return inner \ No newline at end of file diff --git a/spiderweb/request.py b/spiderweb/request.py index 90e0160..3731703 100644 --- a/spiderweb/request.py +++ b/spiderweb/request.py @@ -1,21 +1,28 @@ import json +from typing import Any, Optional, TYPE_CHECKING, Callable from urllib.parse import urlparse from spiderweb.constants import DEFAULT_ENCODING from spiderweb.utils import get_client_address, Headers +if TYPE_CHECKING: + from spiderweb import SpiderwebRouter + class Request: def __init__( self, - environ=None, - content=None, - headers=None, - path=None, - server=None, - handler=None, + environ: Optional[dict[str, Any]]=None, + content: Optional[str]=None, + headers: dict[str, str]=None, + path: str=None, + server: "SpiderwebRouter"=None, + handler: Callable=None, + scope: Optional[dict[str, Any]]=None, ): - self.environ = environ + self._data = environ or scope + + self.environ = environ or {} self.content: str = content self.method: str = environ["REQUEST_METHOD"] self.headers: dict[str, str] = headers if headers else {}