Skip to content
Merged
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
47 changes: 39 additions & 8 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,60 @@ env:
PYTHONUNBUFFERED: "1"
FORCE_COLOR: "1"
PYTHONIOENCODING: "utf8"
PYTHONDEVMODE: "1"
HATCH_VERBOSE: "1"

jobs:
run:
changes:
name: Check for changed files
runs-on: ubuntu-latest
outputs:
source: ${{ steps.filter.outputs.source }}
tests: ${{ steps.filter.outputs.tests }}
steps:
- uses: actions/checkout@v2
- uses: dorny/paths-filter@v3
id: filter
with:
filters: |
source:
- 'src/**'
tests:
- 'tests/**'

run-tests:
needs: changes
if: ${{ needs.changes.outputs.source == 'true' || needs.changes.outputs.tests == 'true' }}
name: Python ${{ matrix.python-version }} on ${{ startsWith(matrix.os, 'macos-') && 'macOS' || startsWith(matrix.os, 'windows-') && 'Windows' || 'Linux' }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
fail-fast: true
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"]

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- name: Install Hatch
run: pip install hatch
uses: pypa/hatch@install

- name: Run tests
run: hatch test

tests-pass:
runs-on: ubuntu-latest
name: All tests passed
if: always()

needs:
- run-tests

steps:
- name: Check whether all tests passed
uses: re-actors/alls-green@release/v1
with:
jobs: ${{ toJSON(needs) }}
allowed-skips: ${{ toJSON(needs) }}
67 changes: 67 additions & 0 deletions .github/workflows/triage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: Triage
on:
pull_request:
types:
- "opened"
- "reopened"
- "synchronize"
- "labeled"
- "unlabeled"

jobs:
changelog_check:
runs-on: ubuntu-latest
name: Check for changelog updates
steps:
- name: "Check if the source directory was changed"
uses: dorny/paths-filter@v3
id: changes
with:
filters: |
src:
- 'src/**'

- name: "Check for changelog updates"
if: steps.changes.outputs.src == 'true'
uses: brettcannon/check-for-changed-files@v1
with:
file-pattern: |
CHANGELOG.md
skip-label: "skip changelog"
failure-message: "Missing a CHANGELOG.md update; please add one or apply the ${skip-label} label to the pull request"

tests_check:
runs-on: ubuntu-latest
name: Check for updated tests
steps:
- name: "Check if the source directory was changed"
uses: dorny/paths-filter@v3
id: changes
with:
filters: |
src:
- 'src/**'

- name: "Check for test updates"
if: steps.changes.outputs.src == 'true'
uses: brettcannon/check-for-changed-files@v1
with:
file-pattern: |
tests/*
skip-label: "skip tests"
failure-message: "Missing unit tests; please add some or apply the ${skip-label} label to the pull request"

all_green:
runs-on: ubuntu-latest
name: PR has no missing information
if: always()

needs:
- changelog_check
- tests_check

steps:
- name: Check whether jobs passed
uses: re-actors/alls-green@release/v1
with:
jobs: ${{ toJSON(needs) }}
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ classifiers = [
"Programming Language :: Python :: 3.14",
"Programming Language :: Python :: Implementation :: CPython",
]
dependencies = ["multidict~=6.5", "loguru~=0.7", "aiofiles~=24.1", "typing_extensions>=4"]
dependencies = ["loguru~=0.7", "aiofiles~=24.1", "typing_extensions>=4"]
dynamic = ["version", "license"]

[project.optional-dependencies]
Expand Down
4 changes: 2 additions & 2 deletions src/view/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
if TYPE_CHECKING:
from collections.abc import Callable

from multidict import CIMultiDict
from view.core.headers import HTTPHeaders

from view.core.response import (
Response,
Expand Down Expand Up @@ -47,7 +47,7 @@ async def __call__(
@dataclass(slots=True, frozen=True)
class _CachedResponse:
body: bytes
headers: CIMultiDict[str]
headers: HTTPHeaders
status: int
last_reset: float

Expand Down
4 changes: 2 additions & 2 deletions src/view/core/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from view.run.asgi import ASGIProtocol
from view.run.wsgi import WSGIProtocol

__all__ = "BaseApp", "as_app", "App"
__all__ = "App", "BaseApp", "as_app"

T = TypeVar("T")
P = ParamSpec("P")
Expand Down Expand Up @@ -143,7 +143,7 @@ def run(
settings.run_app_on_any_server()
except KeyboardInterrupt:
logger.info("CTRL^C received, shutting down")
except Exception: # noqa
except Exception: # noqa: BLE001
logger.exception("Error in server lifecycle")
finally:
logger.info("Server finished")
Expand Down
129 changes: 99 additions & 30 deletions src/view/core/headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,88 +3,157 @@
from collections.abc import Mapping
from typing import TYPE_CHECKING, Any, TypeAlias

from multidict import CIMultiDict
from typing_extensions import Self

from view.core.multi_map import MultiMap
from view.exceptions import InvalidTypeError

if TYPE_CHECKING:
from view.run.asgi import ASGIHeaders
from view.run.wsgi import WSGIHeaders

__all__ = (
"RequestHeaders",
"HTTPHeaders",
"HeadersLike",
"as_multidict",
"asgi_as_multidict",
"multidict_as_asgi",
"wsgi_as_multidict",
"as_real_headers",
"asgi_to_headers",
"headers_to_asgi",
"wsgi_to_headers",
)

RequestHeaders: TypeAlias = CIMultiDict[str]

class LowerStr(str):
"""
A string that always acts in lowercase. This is useful for case-insensitive
comparisons.
"""

__slots__ = ()

def __new__(cls, data: object) -> Self:
return super().__new__(cls, cls._to_lower(data))

@staticmethod
def _to_lower(data: object) -> object:
if isinstance(data, str):
data = data.lower()

return data

def __contains__(self, key: str, /) -> bool:
return super().__contains__(key.lower())

def __eq__(self, string: object) -> bool:
return super().__eq__(self._to_lower(string))

def __ne__(self, value: object, /) -> bool:
return super().__ne__(self._to_lower(value))

def __hash__(self) -> int:
return hash(str(self))


class HTTPHeaders(MultiMap[str, str]):
"""
Case-insensitive multi-map of HTTP headers.
"""

def __getitem__(self, key: str, /) -> str:
return super().__getitem__(LowerStr(key))

def __contains__(self, key: object, /) -> bool:
return super().__contains__(LowerStr(key))

def __repr__(self) -> str:
return f"HTTPHeaders({self.as_sequence()})"

def get_exactly_one(self, key: str) -> str:
return super().get_exactly_one(LowerStr(key))

def with_new_value(self, key: str, value: str) -> HTTPHeaders:
new_sequence = [*list(self.as_sequence()), (LowerStr(key), value)]
return type(self)(new_sequence)


HeadersLike: TypeAlias = (
RequestHeaders | Mapping[str, str] | Mapping[bytes, bytes]
HTTPHeaders | Mapping[str, str] | Mapping[bytes, bytes]
)


def as_multidict(headers: HeadersLike | None, /) -> RequestHeaders:
def as_real_headers(headers: HeadersLike | None, /) -> HTTPHeaders:
"""
Convenience function for casting a "header-like object" (or `None`)
to a `CIMultiDict`.
to a `MultiMap`.
"""
if headers is None:
return CIMultiDict[str]()
return HTTPHeaders()

if isinstance(headers, CIMultiDict):
if isinstance(headers, HTTPHeaders):
return headers

if __debug__ and not isinstance(headers, Mapping):
raise InvalidTypeError(Mapping, headers)

assert isinstance(headers, dict)
multidict = CIMultiDict[str]()
all_values: list[tuple[LowerStr, str]] = []

for key, value in headers.items():
if isinstance(key, bytes):
key = key.decode("utf-8") # noqa
key = key.decode("utf-8") # noqa: PLW2901

if isinstance(value, bytes):
value = value.decode("utf-8") # noqa
value = value.decode("utf-8") # noqa: PLW2901

multidict[key] = value
all_values.append((LowerStr(key), value))

return multidict
return HTTPHeaders(all_values)


def wsgi_as_multidict(environ: Mapping[str, Any]) -> RequestHeaders:
def wsgi_to_headers(environ: Mapping[str, Any]) -> HTTPHeaders:
"""
Convert WSGI headers (from the `environ`) to a case-insensitive multidict.
Convert WSGI headers (from the `environ`) to a case-insensitive multi-map.
"""
headers = CIMultiDict[str]()
values: list[tuple[LowerStr, str]] = []

for key, value in environ.items():
if not key.startswith("HTTP_"):
continue

assert isinstance(value, str)
key = key.removeprefix("HTTP_").replace("_", "-").lower() # noqa
headers[key] = value
key = key.removeprefix("HTTP_").replace("_", "-").lower() # noqa: PLW2901
values.append((LowerStr(key), value))

return HTTPHeaders(values)


def headers_to_wsgi(headers: HTTPHeaders) -> WSGIHeaders:
"""
Convert a case-insensitive multi-map to a WSGI header iterable.
"""

wsgi_headers: WSGIHeaders = []
for key, value in headers.items():
wsgi_headers.append((str(key), value))

return headers
return wsgi_headers


def asgi_as_multidict(headers: ASGIHeaders, /) -> RequestHeaders:
def asgi_to_headers(headers: ASGIHeaders, /) -> HTTPHeaders:
"""
Convert ASGI headers to a case-insensitive multidict.
Convert ASGI headers to a case-insensitive multi-map.
"""
multidict = CIMultiDict[str]()
values: list[tuple[LowerStr, str]] = []

for key, value in headers:
multidict[key.decode("utf-8")] = value.decode("utf-8")
lower_str = LowerStr(key.decode("utf-8"))
values.append((lower_str, value.decode("utf-8")))

return multidict
return MultiMap(values)


def multidict_as_asgi(headers: RequestHeaders, /) -> ASGIHeaders:
def headers_to_asgi(headers: HTTPHeaders, /) -> ASGIHeaders:
"""
Convert a case-insensitive multidict to an ASGI header iterable.
Convert a case-insensitive multi-map to an ASGI header iterable.
"""
asgi_headers: ASGIHeaders = []

Expand Down
Loading