swadhinbiswas WIP save-point: production hardening, H3 removal, fmt gate, and accumulated phase work · 7/15/2026
2080d40
217 lines · 6.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import json
import os
import mimetypes
import urllib.parse
from datetime import datetime, timezone
from typing import Any, Mapping, Optional, Union
from ._justapi import TokenStreamResponse


class Response:
    """HTTP response.

    The body/headers/status are plain Python attributes; the Rust side reads
    them directly (via a ``_justapi_response`` marker) to avoid a ``to_dict``
    round-trip on the hot path.
    """

    _justapi_response = True
    media_type = None
    charset = "utf-8"

    def __init__(
        self,
        content: Any = None,
        status_code: int = 200,
        headers: Optional[Mapping[str, str]] = None,
        media_type: Optional[str] = None,
        background: Any = None,
    ) -> None:
        self.status_code = status_code
        if media_type is not None:
            self.media_type = media_type
        self.background = background
        self.body = self.render(content)
        self.headers = self.init_headers(headers)

    def render(self, content: Any) -> bytes:
        if content is None:
            return b""
        if isinstance(content, bytes):
            return content
        return str(content).encode(self.charset)

    def init_headers(self, headers: Optional[Mapping[str, str]] = None) -> list:
        raw_headers = []
        if headers:
            for k, v in headers.items():
                raw_headers.append((k.lower().encode("latin-1"), v.encode("latin-1")))
        if self.media_type is not None:
            raw_headers.append((b"content-type", self.media_type.encode("latin-1")))
        return raw_headers

    def append_header(self, name: bytes, value: bytes) -> None:
        """Append a raw ``(name, value)`` header tuple (used by ``set_cookie``)."""
        self.headers.append((name, value))

    def set_cookie(
        self,
        name: str,
        value: str = "",
        max_age: Optional[int] = None,
        expires: Optional[Union[int, datetime, str]] = None,
        path: str = "/",
        domain: Optional[str] = None,
        secure: bool = False,
        httponly: bool = False,
        samesite: Optional[str] = None,
    ) -> None:
        """Set a ``Set-Cookie`` response header (Starlette-compatible)."""
        cookie = f"{name}={value}"
        if expires is not None:
            if isinstance(expires, datetime):
                expires = expires.astimezone(timezone.utc).strftime("%a, %d %b %Y %H:%M:%S GMT")
            elif isinstance(expires, int):
                expires = datetime.fromtimestamp(expires, tz=timezone.utc).strftime(
                    "%a, %d %b %Y %H:%M:%S GMT"
                )
            cookie += f"; expires={expires}"
        if max_age is not None:
            cookie += f"; Max-Age={max_age}"
        if path is not None:
            cookie += f"; path={path}"
        if domain is not None:
            cookie += f"; domain={domain}"
        if secure:
            cookie += "; secure"
        if httponly:
            cookie += "; httponly"
        if samesite is not None:
            cookie += f"; samesite={samesite}"
        self.append_header(b"set-cookie", cookie.encode("latin-1"))

    def delete_cookie(self, name: str, path: str = "/", domain: Optional[str] = None) -> None:
        """Expire and delete a cookie by name (sets an already-expired cookie)."""
        self.set_cookie(
            name=name,
            value="",
            expires="Thu, 01 Jan 1970 00:00:00 GMT",
            path=path,
            domain=domain,
            max_age=0,
        )

    def to_dict(self):
        return {
            "status": self.status_code,
            "headers": self.headers,
            "body": self.body,
        }


class HTMLResponse(Response):
    media_type = "text/html"


class PlainTextResponse(Response):
    media_type = "text/plain"


class JSONResponse(Response):
    media_type = "application/json"

    def render(self, content: Any) -> bytes:
        return json.dumps(content, default=str).encode(self.charset)


class RedirectResponse(Response):
    def __init__(
        self,
        url: str,
        status_code: int = 307,
        headers: Optional[Mapping[str, str]] = None,
        background: Any = None,
    ) -> None:
        super().__init__(
            content=b"",
            status_code=status_code,
            headers=headers,
            background=background,
        )
        self.headers.append(
            (
                b"location",
                urllib.parse.quote(url, safe=":/%#?=@[]!$&'()*+,;").encode("latin-1"),
            )
        )


class StreamingResponse(TokenStreamResponse):
    def __init__(
        self,
        content: Any,
        status_code: int = 200,
        headers: Optional[Mapping[str, str]] = None,
        media_type: Optional[str] = None,
        background: Any = None,
    ) -> None:
        raw_headers = []
        if headers:
            for k, v in headers.items():
                raw_headers.append((k.lower().encode("latin-1"), v.encode("latin-1")))
        if media_type is not None:
            raw_headers.append((b"content-type", media_type.encode("latin-1")))

        super().__init__(content, status_code, raw_headers)
        self.background = background


class FileResponse(Response):
    """Serve a file from disk (Starlette-compatible).

    The file is read eagerly into memory and returned as a single body; the
    media type is inferred from the file extension unless ``media_type`` is
    given explicitly. A ``Content-Disposition`` header is added when a
    ``filename`` is supplied.
    """

    def __init__(
        self,
        path: Union[str, os.PathLike],
        headers: Optional[Mapping[str, str]] = None,
        media_type: Optional[str] = None,
        filename: Optional[str] = None,
        stat_result: Optional[os.stat_result] = None,
        content_disposition_type: str = "attachment",
        status_code: int = 200,
        background: Any = None,
    ) -> None:
        self.path = os.fspath(path)
        if filename is None:
            filename = os.path.basename(self.path)
        if media_type is None:
            media_type, _ = mimetypes.guess_type(filename)
            if media_type is None:
                media_type = "application/octet-stream"

        content = b""
        try:
            with open(self.path, "rb") as fh:
                content = fh.read()
        except OSError as exc:
            raise RuntimeError(f"File at path {self.path!r} does not exist or is unreadable: {exc}")

        content_headers = dict(headers or {})
        content_headers.setdefault(
            "content-disposition",
            f'{content_disposition_type}; filename="{filename}"',
        )

        super().__init__(
            content=content,
            status_code=status_code,
            headers=content_headers,
            media_type=media_type,
            background=background,
        )