Skip to content

Commit

Permalink
Merge branch 'master' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
gi0baro committed Jan 8, 2025
2 parents ff7f56b + 4e362c5 commit 9836f50
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Install uv
uses: astral-sh/setup-uv@v3
uses: astral-sh/setup-uv@v5
- name: Install dependencies
run: |
uv sync --dev
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
with:
python-version: 3.12
- name: Install uv
uses: astral-sh/setup-uv@v3
uses: astral-sh/setup-uv@v5
- name: Build distributions
run: |
uv build
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
with:
python-version: ${{ matrix.python-version }}
- name: Install uv
uses: astral-sh/setup-uv@v3
uses: astral-sh/setup-uv@v5
- name: Install dependencies
run: |
uv sync --dev
Expand All @@ -58,7 +58,7 @@ jobs:
with:
python-version: ${{ matrix.python-version }}
- name: Install uv
uses: astral-sh/setup-uv@v3
uses: astral-sh/setup-uv@v5
- name: Install dependencies
run: |
uv sync --dev
Expand All @@ -80,7 +80,7 @@ jobs:
with:
python-version: ${{ matrix.python-version }}
- name: Install uv
uses: astral-sh/setup-uv@v3
uses: astral-sh/setup-uv@v5
- name: Install dependencies
run: |
uv sync --dev
Expand Down
2 changes: 1 addition & 1 deletion emmett/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "2.6.2"
__version__ = "2.6.3"
52 changes: 49 additions & 3 deletions emmett/asgi/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
from importlib import resources
from typing import Awaitable, Callable

from emmett_core.http.response import HTTPBytesResponse, HTTPResponse
from emmett_core.protocols.asgi.handlers import HTTPHandler as _HTTPHandler, WSHandler as _WSHandler
from emmett_core.http.response import HTTPBytesResponse, HTTPResponse, HTTPStringResponse
from emmett_core.protocols.asgi.handlers import HTTPHandler as _HTTPHandler, RequestCancelled, WSHandler as _WSHandler
from emmett_core.protocols.asgi.typing import Receive, Scope, Send
from emmett_core.utils import cachedprop

from ..ctx import current
from ..ctx import RequestContext, WSContext, current
from ..debug import debug_handler, smart_traceback
from ..libs.contenttype import contenttype
from ..wrappers.response import Response
Expand Down Expand Up @@ -70,7 +70,53 @@ async def _debug_handler(self) -> str:
current.response.headers._data["content-type"] = "text/html; charset=utf-8"
return debug_handler(smart_traceback(self.app))

async def dynamic_handler(self, scope: Scope, receive: Receive, send: Send) -> HTTPResponse:
request = Request(
scope,
receive,
send,
max_content_length=self.app.config.request_max_content_length,
max_multipart_size=self.app.config.request_multipart_max_size,
body_timeout=self.app.config.request_body_timeout,
)
response = Response()
ctx = RequestContext(self.app, request, response)
ctx_token = current._init_(ctx)
try:
http = await self.router.dispatch(request, response)
except HTTPResponse as http_exception:
http = http_exception
#: render error with handlers if in app
error_handler = self.app.error_handlers.get(http.status_code)
if error_handler:
http = HTTPStringResponse(
http.status_code, await error_handler(), headers=response.headers, cookies=response.cookies
)
except RequestCancelled:
raise
except Exception:
self.app.log.exception("Application exception:")
http = HTTPStringResponse(500, await self.error_handler(), headers=response.headers)
finally:
current._close_(ctx_token)
return http

async def _exception_handler(self) -> str:
current.response.headers._data["content-type"] = "text/plain"
return "Internal error"


class WSHandler(_WSHandler):
__slots__ = []
wrapper_cls = Websocket

async def dynamic_handler(self, scope: Scope, send: Send):
ctx = WSContext(self.app, Websocket(scope, scope["emt.input"].get, send))
ctx_token = current._init_(ctx)
try:
await self.router.dispatch(ctx.websocket)
finally:
if not scope.get("emt._flow_cancel", False) and ctx.websocket._accepted:
await send({"type": "websocket.close", "code": 1000})
scope["emt._ws_closed"] = True
current._close_(ctx_token)
58 changes: 50 additions & 8 deletions emmett/rsgi/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,15 @@

from __future__ import annotations

import asyncio
import os
from typing import Awaitable, Callable

from emmett_core.http.response import HTTPResponse
from emmett_core.protocols.rsgi.handlers import HTTPHandler as _HTTPHandler, WSHandler as _WSHandler
from emmett_core.http.response import HTTPResponse, HTTPStringResponse
from emmett_core.protocols.rsgi.handlers import HTTPHandler as _HTTPHandler, WSHandler as _WSHandler, WSTransport
from emmett_core.utils import cachedprop
from granian.rsgi import (
HTTPProtocol,
Scope,
)

from ..ctx import current
from ..ctx import RequestContext, WSContext, current
from ..debug import debug_handler, smart_traceback
from ..wrappers.response import Response
from .wrappers import Request, Websocket
Expand All @@ -37,7 +34,7 @@ class HTTPHandler(_HTTPHandler):
def error_handler(self) -> Callable[[], Awaitable[str]]:
return self._debug_handler if self.app.debug else self.exception_handler

def _static_handler(self, scope: Scope, protocol: HTTPProtocol, path: str) -> Awaitable[HTTPResponse]:
def _static_handler(self, scope, protocol, path: str) -> Awaitable[HTTPResponse]:
#: handle internal assets
if path.startswith("/__emmett__"):
file_name = path[12:]
Expand All @@ -57,6 +54,51 @@ async def _debug_handler(self) -> str:
current.response.headers._data["content-type"] = "text/html; charset=utf-8"
return debug_handler(smart_traceback(self.app))

async def dynamic_handler(self, scope, protocol, path: str) -> HTTPResponse:
request = Request(
scope,
path,
protocol,
max_content_length=self.app.config.request_max_content_length,
max_multipart_size=self.app.config.request_multipart_max_size,
body_timeout=self.app.config.request_body_timeout,
)
response = Response()
ctx = RequestContext(self.app, request, response)
ctx_token = current._init_(ctx)
try:
http = await self.router.dispatch(request, response)
except HTTPResponse as http_exception:
http = http_exception
#: render error with handlers if in app
error_handler = self.app.error_handlers.get(http.status_code)
if error_handler:
http = HTTPStringResponse(
http.status_code, await error_handler(), headers=response.headers, cookies=response.cookies
)
except Exception:
self.app.log.exception("Application exception:")
http = HTTPStringResponse(500, await self.error_handler(), headers=response.headers)
finally:
current._close_(ctx_token)
return http


class WSHandler(_WSHandler):
wrapper_cls = Websocket

async def dynamic_handler(self, scope, transport: WSTransport, path: str):
ctx = WSContext(self.app, Websocket(scope, path, transport))
ctx_token = current._init_(ctx)
try:
await self.router.dispatch(ctx.websocket)
except HTTPResponse as http:
transport.status = http.status_code
except asyncio.CancelledError:
if not transport.interrupted:
self.app.log.exception("Application exception:")
except Exception:
transport.status = 500
self.app.log.exception("Application exception:")
finally:
current._close_(ctx_token)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "emmett"
version = "2.6.2"
version = "2.6.3"
description = "The web framework for inventors"
readme = "README.md"
license = "BSD-3-Clause"
Expand Down

0 comments on commit 9836f50

Please sign in to comment.