nativecluster 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,139 @@
1
+ """NativeCluster's kit for Python apps: the app's half of the host API
2
+ (spec/hostapi.v1.md), as sdk/go/app is for Go, held to the same ticket
3
+ vectors (spec/ticket-vectors.v1.json) and the same `ncdev check`.
4
+
5
+ A WSGI app (Flask, Django, a plain function) or an ASGI one (FastAPI,
6
+ Starlette) runs behind NativeCluster with one line, its main:
7
+
8
+ nativecluster.run_wsgi(<nc-app.json's path>, app, on_wake=on_firing)
9
+
10
+ It answers `<program> manifest`, reads what the host handed the app, puts
11
+ one ticket check in front of every route, answers the host's readiness
12
+ check, its firings and the conformance probe, serves on the app's socket,
13
+ and leaves when the host does. A handler takes the person from the ticket
14
+ (person_of) and nowhere else, keeps its data in data_dir(), and reaches
15
+ the models only through the host: models().chat(<task>, messages), or
16
+ OpenAI's own library with transport.http_client().
17
+ """
18
+
19
+ from __future__ import annotations
20
+
21
+ import threading
22
+ from typing import Optional
23
+
24
+ from ._firings import TICK_PATH, WAKE_PATH, Firing, WakeAnswer, answer_wake, run_firing
25
+ from ._gate import SCOPE_KEY, Gate, WSGIGate, from_host, person_of, ticket_of
26
+ from ._host import (
27
+ FIRING_DONE_PATH,
28
+ FIRING_RENEW_PATH,
29
+ HAND_MIN_HOST,
30
+ HAND_PATH,
31
+ MAX_SECRET_VALUE_BYTES,
32
+ MAX_SECRETS_STORE_BYTES,
33
+ SECRETS_MIN_HOST,
34
+ SECRETS_PATH,
35
+ Away,
36
+ HostClient,
37
+ NoMain,
38
+ SecretsFull,
39
+ SecretTooLarge,
40
+ SecretUnreadable,
41
+ StopFiring,
42
+ at,
43
+ cron,
44
+ every,
45
+ on_event,
46
+ schedule,
47
+ weekly,
48
+ )
49
+ from ._hostsock import BASE_URL, HostError, HostSocket
50
+ from ._launch import (
51
+ APP_SOCKET_ENV,
52
+ BASE_ENV,
53
+ DATA_ENV,
54
+ EXIT_WITH_PARENT_ENV,
55
+ HEALTH_PATH,
56
+ HOST_SOCKET_ENV,
57
+ MANIFEST_ARG,
58
+ PERSON_ENV,
59
+ SECRETS_STDIN_ENV,
60
+ SOCKETS_ENV,
61
+ WHOAMI_PATH,
62
+ Launch,
63
+ NotBehindHost,
64
+ answer_manifest,
65
+ base_path,
66
+ behind_host,
67
+ data_dir,
68
+ launch,
69
+ process_person,
70
+ read_launch,
71
+ sockets_dir,
72
+ watch_parent,
73
+ )
74
+ from ._models import (
75
+ CONTEXT_LENGTH_EXCEEDED,
76
+ KIND_CHAT,
77
+ KIND_EMBEDDING,
78
+ KIND_IMAGE,
79
+ KIND_TRANSCRIPTION,
80
+ KIND_TTS,
81
+ Answer,
82
+ ContextTooLong,
83
+ Grant,
84
+ ModelError,
85
+ Models,
86
+ NoModel,
87
+ NoOrigin,
88
+ Refused,
89
+ Stopped,
90
+ )
91
+ from ._origin import (
92
+ Origin,
93
+ acting_for,
94
+ bind,
95
+ carry_origin_into_threads,
96
+ conversation,
97
+ current_conversation,
98
+ current_origin,
99
+ for_firing,
100
+ for_job,
101
+ for_request,
102
+ otherwise_acting_for,
103
+ when_away,
104
+ )
105
+ from ._serve import listen, run_asgi, run_wsgi, serve_asgi, serve_wsgi
106
+ from ._ticket import TICKET_HEADER, TICKET_TTL_MS, NoTicket, Ticket, TicketKey
107
+
108
+ __version__ = "0.1.0"
109
+
110
+ # MIN_HOST is the oldest NativeCluster whose host answers every route this
111
+ # kit calls (the schedules' put and remove, a firing's lease, loops): an
112
+ # app's manifest that uses them sets "min_host" at least this.
113
+ MIN_HOST = "0.3.5"
114
+
115
+ # The process's clients (not _models or _host: those are this package's
116
+ # modules).
117
+ _lock = threading.Lock()
118
+ _the_models: Optional[Models] = None
119
+ _the_host: Optional[HostClient] = None
120
+
121
+
122
+ def models() -> Models:
123
+ """The models client for this process's launch."""
124
+ global _the_models
125
+ with _lock:
126
+ if _the_models is None:
127
+ l = launch()
128
+ _the_models = Models(HostSocket(l.host_socket, l.credential), test=l.test)
129
+ return _the_models
130
+
131
+
132
+ def host() -> HostClient:
133
+ """The host's other routes for this process's launch."""
134
+ global _the_host
135
+ with _lock:
136
+ if _the_host is None:
137
+ l = launch()
138
+ _the_host = HostClient(HostSocket(l.host_socket, l.credential))
139
+ return _the_host
@@ -0,0 +1,185 @@
1
+ """Firings, an app's side (spec/hostapi.v1.md, "The host's calls to an app"
2
+ and "The trigger clock"; the Go kit's sdk/go/app/firings.go).
3
+
4
+ The host's clock hands the app each firing of its schedules at
5
+ POST /nc/v1/wake, on the host's own ticket. The app answers at once, with the
6
+ jobs it started, or with running when it takes the work on: it then renews
7
+ its lease meanwhile, says when it is done, and stops when the host says so
8
+ (the firing's person turned off, its work paused).
9
+ """
10
+
11
+ from __future__ import annotations
12
+
13
+ import inspect
14
+ import json
15
+ import threading
16
+ from dataclasses import dataclass, field
17
+ from typing import Any, Callable, List, Optional, Tuple, Union
18
+
19
+ from ._host import HostClient, StopFiring
20
+ from ._origin import acting_for, bind, for_firing
21
+ from ._ticket import Ticket
22
+
23
+ WAKE_PATH = "/nc/v1/wake"
24
+ TICK_PATH = "/nc/v1/tick"
25
+
26
+ DEFAULT_LEASE_SECS = 600
27
+ _MAX_FIRING = 64 << 10
28
+
29
+
30
+ @dataclass(frozen=True)
31
+ class Firing:
32
+ """One firing as the host hands it to its app. attempt counts the
33
+ handovers before this one that did not land; kind is its trigger's kind
34
+ (time, event, loop); iteration a loop's round, from 1."""
35
+
36
+ id: str
37
+ app: str = ""
38
+ trigger_id: str = ""
39
+ person: str = ""
40
+ trigger: str = ""
41
+ due: str = ""
42
+ attempt: int = 0
43
+ kind: str = ""
44
+ iteration: int = 0
45
+
46
+ @classmethod
47
+ def from_wire(cls, d: Any) -> "Firing":
48
+ if not isinstance(d, dict) or not isinstance(d.get("id"), str) or not d["id"]:
49
+ raise ValueError("a firing")
50
+
51
+ def s(k: str) -> str:
52
+ v = d.get(k)
53
+ return v if isinstance(v, str) else ""
54
+
55
+ def n(k: str) -> int:
56
+ v = d.get(k)
57
+ return v if isinstance(v, int) and not isinstance(v, bool) else 0
58
+
59
+ return cls(id=d["id"], app=s("app"), trigger_id=s("trigger_id"), person=s("person"), trigger=s("trigger"),
60
+ due=s("due"), attempt=n("attempt"), kind=s("kind"), iteration=n("iteration"))
61
+
62
+
63
+ @dataclass
64
+ class WakeAnswer:
65
+ """An app's answer to a firing: the jobs it started, or running, with
66
+ the lease it asks for, when it took the work on and will say when it is
67
+ done. outcome answers a loop's round at once: "continue" for another
68
+ round, "done" or "failed" to end the loop (left out, the loop ends)."""
69
+
70
+ jobs: List[str] = field(default_factory=list)
71
+ running: bool = False
72
+ lease_secs: int = 0
73
+ outcome: str = ""
74
+
75
+ def to_wire(self) -> dict:
76
+ out: dict = {"jobs": list(self.jobs)}
77
+ if self.running:
78
+ out["running"] = True
79
+ if self.lease_secs:
80
+ out["lease_secs"] = self.lease_secs
81
+ if self.outcome:
82
+ out["outcome"] = self.outcome
83
+ return out
84
+
85
+
86
+ WakeResult = Union[WakeAnswer, List[str], dict, None]
87
+
88
+
89
+ def _answer(result: WakeResult) -> dict:
90
+ if isinstance(result, WakeAnswer):
91
+ return result.to_wire()
92
+ if isinstance(result, dict):
93
+ return WakeAnswer(jobs=list(result.get("jobs") or []), running=bool(result.get("running")),
94
+ lease_secs=int(result.get("lease_secs") or 0), outcome=result.get("outcome") or "").to_wire()
95
+ return WakeAnswer(jobs=list(result or [])).to_wire()
96
+
97
+
98
+ def read_firing(ticket: Optional[Ticket], body: bytes) -> Tuple[int, Optional[Firing], bytes]:
99
+ """A wake's firing: (0, firing, b"") to go on, else the status and body
100
+ to answer. Anyone but the host is answered as a route that does not
101
+ exist."""
102
+ if ticket is None or not ticket.host:
103
+ return 404, None, b"404 page not found\n"
104
+ if len(body) > _MAX_FIRING:
105
+ return 400, None, b"a firing\n"
106
+ try:
107
+ return 0, Firing.from_wire(json.loads(body)), b""
108
+ except ValueError:
109
+ return 400, None, b"a firing\n"
110
+
111
+
112
+ def answer_wake(ticket: Optional[Ticket], body: bytes, fn: Callable[[Firing], WakeResult]) -> Tuple[int, bytes]:
113
+ """Answers POST /nc/v1/wake: fn is handed the firing, with its model calls
114
+ waiting as that firing, and answers at once (the jobs it started, or
115
+ running). Returns the status and the JSON body."""
116
+ status, f, msg = read_firing(ticket, body)
117
+ if f is None:
118
+ return status, msg
119
+ with acting_for(for_firing(f)):
120
+ result = fn(f)
121
+ return 200, json.dumps(_answer(result)).encode("utf-8")
122
+
123
+
124
+ async def answer_wake_async(ticket: Optional[Ticket], body: bytes, fn: Callable[[Firing], Any]) -> Tuple[int, bytes]:
125
+ """answer_wake for an ASGI app: fn may be a coroutine function, else it
126
+ runs on a thread, so a slow start never holds the event loop."""
127
+ import asyncio
128
+
129
+ status, f, msg = read_firing(ticket, body)
130
+ if f is None:
131
+ return status, msg
132
+ with acting_for(for_firing(f)):
133
+ if inspect.iscoroutinefunction(fn):
134
+ result = await fn(f)
135
+ else:
136
+ result = await asyncio.get_running_loop().run_in_executor(None, bind(fn), f)
137
+ return 200, json.dumps(_answer(result)).encode("utf-8")
138
+
139
+
140
+ def run_firing(host: HostClient, firing: Firing, work: Callable[[threading.Event], Optional[str]],
141
+ lease_secs: int = DEFAULT_LEASE_SECS) -> threading.Thread:
142
+ """Works on a firing the app answered running with lease_secs, on a
143
+ thread of its own: runs work, with model calls waiting as the firing,
144
+ renews the lease every third of it, sets work's stop event when the host
145
+ says stop, and tells the host how it ended: work's outcome ("done",
146
+ "failed", a loop round's "continue"), "done" for none, "failed" for an
147
+ exception. Call it after answering the wake; work keeps its progress in
148
+ NC_APP_DATA, since the firing comes again if the app stops."""
149
+ if lease_secs <= 0:
150
+ lease_secs = DEFAULT_LEASE_SECS
151
+ stop = threading.Event()
152
+ result: dict = {}
153
+
154
+ def run() -> None:
155
+ try:
156
+ with acting_for(for_firing(firing)):
157
+ result["outcome"] = work(stop) or "done"
158
+ except BaseException:
159
+ result["outcome"] = "failed"
160
+
161
+ def watch() -> None:
162
+ worker = threading.Thread(target=run, name="nativecluster-firing-" + firing.id, daemon=True)
163
+ worker.start()
164
+ while True:
165
+ worker.join(lease_secs / 3)
166
+ if not worker.is_alive():
167
+ break
168
+ try:
169
+ host.renew_firing(firing.id)
170
+ except StopFiring:
171
+ stop.set()
172
+ worker.join()
173
+ return
174
+ except Exception:
175
+ pass # renewed at the next third, or handed over again once the lease lapses
176
+ if stop.is_set():
177
+ return
178
+ try:
179
+ host.firing_done(firing.id, result.get("outcome", "failed"))
180
+ except Exception:
181
+ pass # unsaid, the lease lapses and the host hands the firing over again
182
+
183
+ t = threading.Thread(target=watch, name="nativecluster-lease-" + firing.id, daemon=True)
184
+ t.start()
185
+ return t
nativecluster/_gate.py ADDED
@@ -0,0 +1,312 @@
1
+ """The one check in front of every route (spec/hostapi.v1.md, "Requests to
2
+ the app"; the Go kit's Gate).
3
+
4
+ The gate admits only requests carrying a ticket the launch's key signed,
5
+ and refuses everything else with a 403, before any route runs. It rebuilds
6
+ what the host saw (the client's address, TLS) onto the request, so every
7
+ handler behind it judges the client exactly as it would on a listener of its
8
+ own, and drops the ticket header. Nothing but the host can mint a ticket, so
9
+ this is not trusting a forwarded header. Behind it, it answers the host's
10
+ readiness check and the conformance probe, and, when the app hands it the
11
+ functions, the host's calls for its schedules; every request it passes on
12
+ carries its origin, so a model call made while handling it waits as that
13
+ person's.
14
+
15
+ Gate wraps an ASGI app (FastAPI, Starlette, an agent's gateway), WSGIGate a
16
+ WSGI one (Flask, Django, a plain function).
17
+ """
18
+
19
+ from __future__ import annotations
20
+
21
+ import asyncio
22
+ import inspect
23
+ import json
24
+ import sys
25
+ import threading
26
+ import time
27
+ from typing import Any, Awaitable, Callable, Iterable, Iterator, List, Optional, Tuple
28
+
29
+ from ._firings import TICK_PATH, WAKE_PATH, Firing, WakeResult, answer_wake, answer_wake_async
30
+ from ._launch import HEALTH_PATH, WHOAMI_PATH, launch
31
+ from ._origin import acting_for, bind, for_request
32
+ from ._ticket import NoTicket, Ticket, TicketKey
33
+
34
+ # SCOPE_KEY is where the gate puts the verified ticket: an ASGI scope's key,
35
+ # a WSGI environ's.
36
+ SCOPE_KEY = "nativecluster.ticket"
37
+
38
+ _REFUSED = b"this app answers only NativeCluster\n"
39
+ _MAX_BODY = 64 << 10
40
+
41
+ # What the gate refuses is written down in the app's own log, which the host
42
+ # keeps as the app's: one line on stderr for each request without
43
+ # the host's ticket, its path and why, never the ticket, a query, a header or
44
+ # a body, at most REFUSALS_LOGGED a minute, the rest counted and the count
45
+ # written as the minute ends. A refused ticket means a caller reached the
46
+ # app's socket without the host, which never mints a bad one.
47
+ REFUSALS_LOGGED = 10
48
+ _MAX_REFUSED_PATH = 128
49
+
50
+
51
+ class _RefusedTickets:
52
+ def __init__(self, window: float = 60.0) -> None:
53
+ self.window = window
54
+ self._lock = threading.Lock()
55
+ self._since = 0.0
56
+ self._n = 0
57
+ self._dropped = 0
58
+ self._ending = False
59
+
60
+ def refused(self, path: str, reason: str) -> None:
61
+ now = time.monotonic()
62
+ with self._lock:
63
+ if self._n == 0 or now - self._since >= self.window:
64
+ self._since, self._n = now, 0
65
+ self._n += 1
66
+ if self._n > REFUSALS_LOGGED:
67
+ self._dropped += 1
68
+ if not self._ending:
69
+ self._ending = True
70
+ t = threading.Timer(max(0.0, self._since + self.window - now), self._end_minute)
71
+ t.daemon = True
72
+ t.start()
73
+ return
74
+ print("nativecluster: refused a request without the host's ticket path=%s reason=%s"
75
+ % (json.dumps(path[:_MAX_REFUSED_PATH]), reason), file=sys.stderr, flush=True)
76
+
77
+ def _end_minute(self) -> None:
78
+ with self._lock:
79
+ n, self._dropped, self._ending = self._dropped, 0, False
80
+ if n:
81
+ print("nativecluster: more refused tickets in a minute than the log writes one by one dropped=%d" % n,
82
+ file=sys.stderr, flush=True)
83
+
84
+
85
+ _ticket_refusals = _RefusedTickets()
86
+
87
+
88
+ def _refused_ticket(path: Any, header: Any) -> None:
89
+ if isinstance(path, bytes):
90
+ path = path.decode("latin-1")
91
+ _ticket_refusals.refused(str(path or ""), "no_ticket" if not header else "bad_ticket")
92
+
93
+ WakeFn = Callable[[Firing], Any]
94
+ TickFn = Callable[[], Any]
95
+
96
+
97
+ def _client(remote: str) -> Tuple[str, int]:
98
+ host, sep, port = remote.rpartition(":")
99
+ if sep and port.isdigit():
100
+ return host.strip("[]"), int(port)
101
+ return remote, 0
102
+
103
+
104
+ def ticket_of(where: Any) -> Optional[Ticket]:
105
+ """The ticket the gate verified for a request: its ASGI scope or WSGI
106
+ environ, or a framework's request object holding one (Starlette's
107
+ .scope, Werkzeug's .environ)."""
108
+ for d in (where, getattr(where, "scope", None), getattr(where, "environ", None)):
109
+ if isinstance(d, dict) and isinstance(d.get(SCOPE_KEY), Ticket):
110
+ return d[SCOPE_KEY]
111
+ return None
112
+
113
+
114
+ def person_of(where: Any) -> str:
115
+ """The person the host signed in for a request ("" for nobody, and for
116
+ the host's own call). It reads the host's ticket and nothing else: no
117
+ header, cookie or parameter of the request can name a person."""
118
+ t = ticket_of(where)
119
+ return t.person if t is not None and not t.host else ""
120
+
121
+
122
+ def from_host(where: Any) -> bool:
123
+ """Whether a request is the host's own call to its app (a firing, the
124
+ clock's beat): every route only the host may call checks it, and
125
+ answers anyone else as missing."""
126
+ t = ticket_of(where)
127
+ return t is not None and t.host
128
+
129
+
130
+ class Gate:
131
+ """ASGI middleware: Gate(app). on_wake answers the host's firings
132
+ (POST /nc/v1/wake) and on_tick its beat (POST /nc/v1/tick); each may be
133
+ a coroutine function, else it runs on a thread. key defaults to the
134
+ launch's, read at the first request."""
135
+
136
+ def __init__(self, app: Callable[..., Awaitable[None]], key: Optional[TicketKey] = None, *,
137
+ on_wake: Optional[WakeFn] = None, on_tick: Optional[TickFn] = None) -> None:
138
+ self.app = app
139
+ self._key = key
140
+ self.on_wake = on_wake
141
+ self.on_tick = on_tick
142
+
143
+ def _ticket_key(self) -> TicketKey:
144
+ if self._key is None:
145
+ self._key = launch().ticket_key
146
+ return self._key
147
+
148
+ async def __call__(self, scope: dict, receive: Callable, send: Callable) -> None:
149
+ kind = scope.get("type")
150
+ if kind not in ("http", "websocket"):
151
+ await self.app(scope, receive, send)
152
+ return
153
+ header = next((v for k, v in scope.get("headers") or [] if k.lower() == b"x-nc-ticket"), None)
154
+ try:
155
+ ticket = self._ticket_key().verify(header)
156
+ except NoTicket:
157
+ _refused_ticket(scope.get("path", ""), header)
158
+ if kind == "http":
159
+ await _respond(send, 403, _REFUSED, b"text/plain; charset=utf-8")
160
+ else:
161
+ await send({"type": "websocket.close", "code": 1008})
162
+ return
163
+ scope = dict(scope)
164
+ scope["headers"] = [(k, v) for k, v in scope.get("headers") or [] if k.lower() != b"x-nc-ticket"]
165
+ scope["client"] = _client(ticket.remote)
166
+ if kind == "http":
167
+ scope["scheme"] = "https" if ticket.tls else "http"
168
+ else:
169
+ scope["scheme"] = "wss" if ticket.tls else "ws"
170
+ scope[SCOPE_KEY] = ticket
171
+ if kind == "http" and await self._own_route(scope, receive, send, ticket):
172
+ return
173
+ with acting_for(None if ticket.host else for_request(ticket)):
174
+ await self.app(scope, receive, send)
175
+
176
+ async def _own_route(self, scope: dict, receive: Callable, send: Callable, ticket: Ticket) -> bool:
177
+ path, method = scope.get("path", ""), scope.get("method", "")
178
+ if path == HEALTH_PATH and method == "GET":
179
+ await _respond(send, 200, b'{"status":"ok"}', b"application/json")
180
+ elif path == WHOAMI_PATH and method == "GET":
181
+ await _respond(send, 200, json.dumps({"person": ticket.person}).encode("utf-8"), b"application/json")
182
+ elif path == WAKE_PATH and method == "POST" and self.on_wake is not None:
183
+ status, out = await answer_wake_async(ticket, await _read(receive), self.on_wake)
184
+ await _respond(send, status, out, b"application/json" if status == 200 else b"text/plain; charset=utf-8")
185
+ elif path == TICK_PATH and method == "POST" and self.on_tick is not None:
186
+ if not ticket.host:
187
+ await _respond(send, 404, b"404 page not found\n", b"text/plain; charset=utf-8")
188
+ else:
189
+ if inspect.iscoroutinefunction(self.on_tick):
190
+ await self.on_tick()
191
+ else:
192
+ await asyncio.get_running_loop().run_in_executor(None, bind(self.on_tick))
193
+ await _respond(send, 204, b"", None)
194
+ else:
195
+ return False
196
+ return True
197
+
198
+
199
+ async def _read(receive: Callable) -> bytes:
200
+ body = b""
201
+ while len(body) <= _MAX_BODY:
202
+ msg = await receive()
203
+ if msg.get("type") != "http.request":
204
+ break
205
+ body += msg.get("body", b"")
206
+ if not msg.get("more_body"):
207
+ break
208
+ return body
209
+
210
+
211
+ async def _respond(send: Callable, status: int, body: bytes, content_type: Optional[bytes]) -> None:
212
+ headers: List[Tuple[bytes, bytes]] = [(b"content-length", str(len(body)).encode("ascii"))]
213
+ if content_type:
214
+ headers.append((b"content-type", content_type))
215
+ await send({"type": "http.response.start", "status": status, "headers": headers})
216
+ await send({"type": "http.response.body", "body": body})
217
+
218
+
219
+ class WSGIGate:
220
+ """WSGI middleware: WSGIGate(app), with on_wake and on_tick as Gate's.
221
+ key defaults to the launch's, read at the first request."""
222
+
223
+ def __init__(self, app: Callable, key: Optional[TicketKey] = None, *,
224
+ on_wake: Optional[Callable[[Firing], WakeResult]] = None, on_tick: Optional[TickFn] = None) -> None:
225
+ self.app = app
226
+ self._key = key
227
+ self.on_wake = on_wake
228
+ self.on_tick = on_tick
229
+
230
+ def _ticket_key(self) -> TicketKey:
231
+ if self._key is None:
232
+ self._key = launch().ticket_key
233
+ return self._key
234
+
235
+ def __call__(self, environ: dict, start_response: Callable) -> Iterable[bytes]:
236
+ try:
237
+ ticket = self._ticket_key().verify(environ.get("HTTP_X_NC_TICKET"))
238
+ except NoTicket:
239
+ _refused_ticket(environ.get("PATH_INFO", ""), environ.get("HTTP_X_NC_TICKET"))
240
+ return _plain(start_response, "403 Forbidden", _REFUSED)
241
+ environ = dict(environ)
242
+ environ.pop("HTTP_X_NC_TICKET", None)
243
+ host, port = _client(ticket.remote)
244
+ environ["REMOTE_ADDR"], environ["REMOTE_PORT"] = host, str(port)
245
+ environ["wsgi.url_scheme"] = "https" if ticket.tls else "http"
246
+ if ticket.tls:
247
+ environ["HTTPS"] = "on"
248
+ else:
249
+ environ.pop("HTTPS", None)
250
+ environ[SCOPE_KEY] = ticket
251
+ path, method = environ.get("PATH_INFO", ""), environ.get("REQUEST_METHOD", "")
252
+ if path == HEALTH_PATH and method == "GET":
253
+ return _plain(start_response, "200 OK", b'{"status":"ok"}', "application/json")
254
+ if path == WHOAMI_PATH and method == "GET":
255
+ return _plain(start_response, "200 OK", json.dumps({"person": ticket.person}).encode("utf-8"), "application/json")
256
+ if path == WAKE_PATH and method == "POST" and self.on_wake is not None:
257
+ status, out = answer_wake(ticket, _wsgi_body(environ), self.on_wake)
258
+ if status == 200:
259
+ return _plain(start_response, "200 OK", out, "application/json")
260
+ return _plain(start_response, "%d %s" % (status, "Not Found" if status == 404 else "Bad Request"), out)
261
+ if path == TICK_PATH and method == "POST" and self.on_tick is not None:
262
+ if not ticket.host:
263
+ return _plain(start_response, "404 Not Found", b"404 page not found\n")
264
+ self.on_tick()
265
+ start_response("204 No Content", [])
266
+ return []
267
+ origin = None if ticket.host else for_request(ticket)
268
+ with acting_for(origin):
269
+ result = self.app(environ, start_response)
270
+ return _InOrigin(result, origin)
271
+
272
+
273
+ def _plain(start_response: Callable, status: str, body: bytes, content_type: str = "text/plain; charset=utf-8") -> List[bytes]:
274
+ start_response(status, [("Content-Type", content_type), ("Content-Length", str(len(body)))])
275
+ return [body]
276
+
277
+
278
+ def _wsgi_body(environ: dict) -> bytes:
279
+ stream = environ.get("wsgi.input")
280
+ if stream is None:
281
+ return b""
282
+ try:
283
+ n = int(environ.get("CONTENT_LENGTH") or -1)
284
+ except ValueError:
285
+ n = -1
286
+ if n >= 0:
287
+ return stream.read(min(n, _MAX_BODY + 1))
288
+ return stream.read(_MAX_BODY + 1)
289
+
290
+
291
+ class _InOrigin:
292
+ """A WSGI answer whose body is made with the request's origin: a model
293
+ call made while it streams waits as the request's person's."""
294
+
295
+ def __init__(self, result: Iterable[bytes], origin: Any) -> None:
296
+ self._result = result
297
+ self._origin = origin
298
+
299
+ def __iter__(self) -> Iterator[bytes]:
300
+ it = iter(self._result)
301
+ while True:
302
+ with acting_for(self._origin):
303
+ try:
304
+ chunk = next(it)
305
+ except StopIteration:
306
+ return
307
+ yield chunk
308
+
309
+ def close(self) -> None:
310
+ close = getattr(self._result, "close", None)
311
+ if close is not None:
312
+ close()