Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: inside page[WIP] #40

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added asgi_webdav/page/__init__.py
Empty file.
73 changes: 73 additions & 0 deletions asgi_webdav/page/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from collections.abc import Callable
from dataclasses import dataclass

from asgi_webdav.request import DAVRequest
from asgi_webdav.response import DAVResponse


@dataclass
class PageURL:
path: str
view: Callable
require_user: bool = True
require_admin: bool = False


class PageResponseBase:
pass


class PageResponseHTML(PageResponseBase):
pass


class PageResponseJSON(PageResponseBase):
pass


class PageResponseRedirect(PageResponseBase):
pass


"""
HttpResponseBadRequest
HttpResponseNotAllowed
HttpResponseNotFound
HttpResponseForbidden
HttpResponseServerError
"""


class PageView:
async def __call__(self, request: DAVRequest) -> DAVResponse:
status, content = self._get_response(request)
return DAVResponse(status=status, content=content.encode("utf-8"))

Check warning on line 44 in asgi_webdav/page/base.py

View check run for this annotation

Codecov / codecov/patch

asgi_webdav/page/base.py#L43-L44

Added lines #L43 - L44 were not covered by tests

def _get_response(self, request: DAVRequest) -> (int, str):
raise NotImplementedError()

Check warning on line 47 in asgi_webdav/page/base.py

View check run for this annotation

Codecov / codecov/patch

asgi_webdav/page/base.py#L47

Added line #L47 was not covered by tests


class PageTemplateView(PageView):
pass


class PageEntry:
urls: list[PageURL]

def __init__(self):
self._data = dict()
for url_obj in self.urls:
self._data[url_obj.path] = url_obj

async def enter(self, request: DAVRequest) -> DAVResponse:
url_obj = self._data.get(request.path.raw)
if url_obj is None:
return DAVResponse(404)

Check warning on line 65 in asgi_webdav/page/base.py

View check run for this annotation

Codecov / codecov/patch

asgi_webdav/page/base.py#L63-L65

Added lines #L63 - L65 were not covered by tests

if url_obj.require_user and request.user is None:
return DAVResponse(403, content=b"Requires user privileges")

Check warning on line 68 in asgi_webdav/page/base.py

View check run for this annotation

Codecov / codecov/patch

asgi_webdav/page/base.py#L67-L68

Added lines #L67 - L68 were not covered by tests

if url_obj.require_admin and not request.user.admin:
return DAVResponse(403, content=b"Requires administrator privileges")

Check warning on line 71 in asgi_webdav/page/base.py

View check run for this annotation

Codecov / codecov/patch

asgi_webdav/page/base.py#L70-L71

Added lines #L70 - L71 were not covered by tests

return await url_obj.view(request)

Check warning on line 73 in asgi_webdav/page/base.py

View check run for this annotation

Codecov / codecov/patch

asgi_webdav/page/base.py#L73

Added line #L73 was not covered by tests
42 changes: 42 additions & 0 deletions asgi_webdav/page/inside.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from jinja2 import DictLoader, Environment

from asgi_webdav.log import get_log_messages
from asgi_webdav.page.base import PageEntry, PageURL, PageView
from asgi_webdav.request import DAVRequest

jinja_template_mapping = {
"info.html": """System Info:
Session Info:
User: {{ username }}
"""
}
env = Environment(loader=DictLoader(jinja_template_mapping))


class IndexView(PageView):
def _get_response(self, request: DAVRequest) -> (int, str):
return 200, '<a href="/_/admin/logging">Logging page</a>'

Check warning on line 18 in asgi_webdav/page/inside.py

View check run for this annotation

Codecov / codecov/patch

asgi_webdav/page/inside.py#L18

Added line #L18 was not covered by tests


class InfoView(PageView):
def _get_response(self, request: DAVRequest) -> (int, str):
return 200, env.get_template("info.html").render(username=request.user.username)

Check warning on line 23 in asgi_webdav/page/inside.py

View check run for this annotation

Codecov / codecov/patch

asgi_webdav/page/inside.py#L23

Added line #L23 was not covered by tests


class LoggingView(PageView):
def _get_response(self, request: DAVRequest) -> (int, str):
data = ""
for message in get_log_messages():
data += message + "<br>"
return 200, data

Check warning on line 31 in asgi_webdav/page/inside.py

View check run for this annotation

Codecov / codecov/patch

asgi_webdav/page/inside.py#L28-L31

Added lines #L28 - L31 were not covered by tests


urls = [
PageURL("/_", IndexView()),
PageURL("/_/info", InfoView(), require_admin=True),
PageURL("/_/admin/logging", LoggingView()),
]


class InsidePage(PageEntry):
urls = urls
21 changes: 7 additions & 14 deletions asgi_webdav/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
init_config_from_file,
init_config_from_obj,
)
from asgi_webdav.constants import AppEntryParameters, ASGIScope, DAVMethod, DevMode
from asgi_webdav.constants import AppEntryParameters, ASGIScope, DevMode
from asgi_webdav.exception import DAVExceptionProviderInitFailed
from asgi_webdav.log import get_dav_logging_config
from asgi_webdav.middleware.cors import ASGIMiddlewareCORS
from asgi_webdav.page.inside import InsidePage
from asgi_webdav.request import DAVRequest
from asgi_webdav.response import DAVResponse
from asgi_webdav.web_dav import WebDAV
from asgi_webdav.web_page import WebPage

logger = getLogger(__name__)

Expand All @@ -41,7 +41,7 @@
logger.info(_service_abnormal_exit_message)
sys.exit(1)

self.web_page = WebPage()
self.inside_page = InsidePage()

async def __call__(self, scope: ASGIScope, receive, send) -> None:
request, response = await self.handle(scope, receive, send)
Expand Down Expand Up @@ -70,18 +70,11 @@
logger.debug(request)
return request, self.dav_auth.create_response_401(request, message)

# process Admin request
if (
request.method == DAVMethod.GET
and request.src_path.count >= 1
and request.src_path.parts[0] == "_"
):
# route to inside page
if request.src_path.count >= 1 and request.src_path.parts[0] == "_":
# route /_
status, data = await self.web_page.enter(request)
return request, DAVResponse(
status=status,
content=data.encode("utf-8"),
)
response = await self.inside_page.enter(request)
return request, response

Check warning on line 77 in asgi_webdav/server.py

View check run for this annotation

Codecov / codecov/patch

asgi_webdav/server.py#L76-L77

Added lines #L76 - L77 were not covered by tests

# process WebDAV request
try:
Expand Down
41 changes: 0 additions & 41 deletions asgi_webdav/web_page.py

This file was deleted.

1 change: 1 addition & 0 deletions build_docker_and_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ docker run -dit --restart unless-stopped \
-e DEBUG=true \
-e WEBDAV_LOGGING_LEVEL=DEBUG \
--name asgi-webdav cr.h.rexzhang.com/ray1ex/asgi-webdav

docker image prune -f
docker container logs -f asgi-webdav
3 changes: 3 additions & 0 deletions requirements/standalone.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ httptools

# response compress
brotli # c runtime

# inside page
jinja2
Loading