aliasgate 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.
aliasgate/proxy/app.py ADDED
@@ -0,0 +1,602 @@
1
+ """The gateway's ASGI application: routing, the dialect request flow and logging (architecture §7).
2
+
3
+ Fail closed: every failure before forwarding produces an error response and no upstream call,
4
+ and the leak audit checks the outbound body before it is sent. Logs and metrics carry ids,
5
+ counts, entity types, durations and status codes, never text.
6
+ """
7
+
8
+ import json
9
+ import logging
10
+ import sys
11
+ import time
12
+ import uuid
13
+ from collections import Counter
14
+ from collections.abc import AsyncGenerator, AsyncIterator, Iterable
15
+ from contextlib import aclosing, asynccontextmanager
16
+ from dataclasses import dataclass, field
17
+ from datetime import UTC, datetime
18
+ from functools import partial
19
+ from typing import Any, Final
20
+
21
+ import anyio
22
+ import anyio.to_thread
23
+ import httpx
24
+ from starlette.applications import Starlette
25
+ from starlette.requests import ClientDisconnect, Request
26
+ from starlette.responses import JSONResponse, Response, StreamingResponse
27
+ from starlette.routing import Route
28
+ from starlette.types import Receive, Scope, Send
29
+
30
+ from aliasgate.audit import AuditResult, audit_outbound
31
+ from aliasgate.config import PolicySettings, Settings
32
+ from aliasgate.detectors.cache import CachedDetector
33
+ from aliasgate.dialects.anthropic_messages import AnthropicMessagesDialect
34
+ from aliasgate.dialects.base import (
35
+ Dialect,
36
+ MalformedRequestError,
37
+ Policy,
38
+ StreamTransformer,
39
+ UnsupportedContentError,
40
+ restore_all_strings,
41
+ slots_for_json_strings,
42
+ )
43
+ from aliasgate.dialects.openai_chat import OpenAIChatDialect
44
+ from aliasgate.dialects.sse import SSEEvent, SSEParser
45
+ from aliasgate.engine import BatchStats, DetectionError, Pseudonymizer, count_unresolved
46
+ from aliasgate.factory import build_pseudonymizer
47
+ from aliasgate.mapping import Mapping
48
+ from aliasgate.metrics import CONTENT_TYPE, GatewayMetrics, Outcome, Phase
49
+ from aliasgate.proxy import errors
50
+ from aliasgate.proxy.errors import GatewayError
51
+ from aliasgate.proxy.headers import filter_request_headers, filter_response_headers
52
+
53
+ REQUEST_ID_HEADER: Final = "x-aliasgate-request-id"
54
+ ENTITIES_HEADER: Final = "x-aliasgate-entities"
55
+
56
+ _logger: Final = logging.getLogger("aliasgate.proxy")
57
+ # Every method reaches the gateway's own dispatch; Starlette answers any other with 405, which
58
+ # is mapped to the same 404 as an unknown path.
59
+ _METHODS: Final = ["GET", "HEAD", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"]
60
+ _HEALTH: Final = ("GET", "/healthz")
61
+ _METRICS: Final = ("GET", "/metrics")
62
+ _CHAT_COMPLETIONS: Final = "/v1/chat/completions"
63
+ _MESSAGES: Final = "/v1/messages"
64
+ _COUNT_TOKENS: Final = "/v1/messages/count_tokens"
65
+ # A request carrying one of these headers comes from an Anthropic client: errors raised before
66
+ # a route is chosen (an unknown endpoint) use the Anthropic error shape.
67
+ _ANTHROPIC_HEADERS: Final = ("anthropic-version", "x-api-key")
68
+ _UNKNOWN_ROUTE: Final = "unknown"
69
+ # httpx logs every request URL at INFO, query string included; httpcore logs headers at DEBUG.
70
+ _QUIET_LOGGERS: Final = ("httpx", "httpcore")
71
+ _NOT_JSON: Final = object()
72
+ _EVENT_STREAM: Final = "text/event-stream"
73
+
74
+
75
+ class JsonFormatter(logging.Formatter):
76
+ """One JSON object per record: time, level, logger, message, then the record's `fields`.
77
+
78
+ Tracebacks and stack information are never rendered, only an exception's class name,
79
+ because a traceback can show request text.
80
+ """
81
+
82
+ def format(self, record: logging.LogRecord) -> str:
83
+ entry: dict[str, Any] = {
84
+ "time": datetime.fromtimestamp(record.created, UTC).isoformat(timespec="milliseconds"),
85
+ "level": record.levelname,
86
+ "logger": record.name,
87
+ "message": record.getMessage(),
88
+ }
89
+ fields = getattr(record, "fields", None)
90
+ if isinstance(fields, dict):
91
+ for key, value in fields.items():
92
+ entry.setdefault(str(key), value)
93
+ if record.exc_info and record.exc_info[0] is not None:
94
+ entry["exception"] = record.exc_info[0].__name__
95
+ return json.dumps(entry, ensure_ascii=False, default=str)
96
+
97
+
98
+ def configure_logging(level: str) -> None:
99
+ """Send every log record to stderr as one JSON line, at `level`. The httpx and httpcore
100
+ loggers are kept at WARNING or above, because they log URLs and headers."""
101
+ handler = logging.StreamHandler(sys.stderr)
102
+ handler.setFormatter(JsonFormatter())
103
+ root = logging.getLogger()
104
+ root.handlers[:] = [handler]
105
+ root.setLevel(level)
106
+ for name in _QUIET_LOGGERS:
107
+ logging.getLogger(name).setLevel(max(logging.WARNING, root.level))
108
+
109
+
110
+ @dataclass(slots=True)
111
+ class _RequestLog:
112
+ """What the logs and metrics may say about one request: its id, route, outcome, counts,
113
+ entity types and timings. Written once, when the request ends."""
114
+
115
+ metrics: GatewayMetrics | None = None
116
+ request_id: str = field(default_factory=lambda: uuid.uuid4().hex)
117
+ route: str = _UNKNOWN_ROUTE
118
+ started: float = field(default_factory=time.perf_counter)
119
+ slots: int = 0
120
+ entities: dict[str, int] = field(default_factory=dict)
121
+ entity_count: int = 0
122
+ unresolved_placeholders: int = 0
123
+ outcome: Outcome = "ok" # an error answered by the upstream becomes upstream_error at emit
124
+ code: str | None = None # the code of an error response the gateway produced
125
+ stats: BatchStats | None = None
126
+ leaked_types: tuple[str, ...] = ()
127
+ overhead: dict[Phase, float] = field(default_factory=dict)
128
+ deferred: bool = False # a streamed response writes the line itself, when the stream ends
129
+
130
+ def emit(self, status: int) -> None:
131
+ fields = {
132
+ "request_id": self.request_id,
133
+ "route": self.route,
134
+ "status": status,
135
+ "duration_ms": round((time.perf_counter() - self.started) * 1000, 3),
136
+ "slots": self.slots,
137
+ "entities": self.entities,
138
+ "unresolved_placeholders": self.unresolved_placeholders,
139
+ }
140
+ _logger.info("request", extra={"fields": fields})
141
+ if self.metrics is not None:
142
+ self._record(self.metrics, status)
143
+
144
+ def _record(self, metrics: GatewayMetrics, status: int) -> None:
145
+ outcome: Outcome = (
146
+ "upstream_error" if self.outcome == "ok" and status >= 400 else self.outcome
147
+ )
148
+ metrics.request(self.route, status, outcome)
149
+ if self.code is not None:
150
+ metrics.rejection(self.route, self.code)
151
+ if self.stats is not None:
152
+ metrics.batch(self.stats)
153
+ for phase, seconds in self.overhead.items():
154
+ metrics.overhead(self.route, phase, seconds)
155
+ if self.unresolved_placeholders:
156
+ metrics.unresolved(self.route, self.unresolved_placeholders)
157
+ if self.leaked_types:
158
+ metrics.leak(self.route, self.leaked_types)
159
+
160
+ def leak(self, result: AuditResult) -> None:
161
+ """Log a leak audit failure by count and entity types only."""
162
+ self.leaked_types = result.leaked_types
163
+ fields = {
164
+ "request_id": self.request_id,
165
+ "leaked_entities": result.leaked_entities,
166
+ "leaked_types": list(result.leaked_types),
167
+ }
168
+ _logger.error("leak audit failed", extra={"fields": fields})
169
+
170
+ def failure(self, stage: str, exc: BaseException) -> None:
171
+ """Log a failure by exception class name only: its message and cause may hold text."""
172
+ fields = {"request_id": self.request_id, "stage": stage, "error": type(exc).__name__}
173
+ if isinstance(exc, DetectionError):
174
+ fields["detector"] = exc.detector_name
175
+ _logger.error("request failed", extra={"fields": fields}, exc_info=False)
176
+
177
+
178
+ @dataclass(frozen=True, slots=True)
179
+ class _DialectRoute:
180
+ """A route the gateway pseudonymises. With `request_only`, the body's `stream` is not
181
+ honoured and a successful answer is returned unchanged (it holds no text)."""
182
+
183
+ dialect: Dialect
184
+ base_url: str
185
+ path: str
186
+ request_only: bool = False
187
+
188
+
189
+ @dataclass(frozen=True, slots=True)
190
+ class _PassthroughRoute:
191
+ """A route forwarded unchanged to one upstream; its errors use that upstream's dialect."""
192
+
193
+ dialect: Dialect
194
+ base_url: str
195
+
196
+
197
+ def _policy(settings: PolicySettings) -> Policy:
198
+ return Policy(
199
+ unsupported_content=settings.unsupported_content,
200
+ strip_user_identifiers=settings.strip_user_identifiers,
201
+ tool_definitions=settings.tool_definitions,
202
+ )
203
+
204
+
205
+ def _parse_object(raw: bytes) -> dict[str, Any]:
206
+ try:
207
+ body = json.loads(raw)
208
+ except (ValueError, RecursionError):
209
+ raise errors.invalid_json() from None
210
+ if not isinstance(body, dict):
211
+ raise errors.invalid_json()
212
+ return body
213
+
214
+
215
+ def _encode(body: dict[str, Any]) -> bytes:
216
+ try:
217
+ return json.dumps(body, ensure_ascii=False).encode("utf-8")
218
+ except UnicodeEncodeError:
219
+ # An escaped lone surrogate ("\ud800") parses into a str that UTF-8 cannot encode.
220
+ raise errors.invalid_json() from None
221
+
222
+
223
+ def _loads(content: bytes) -> object:
224
+ try:
225
+ return json.loads(content)
226
+ except (ValueError, RecursionError):
227
+ return _NOT_JSON
228
+
229
+
230
+ def _media_type(content_type: str | None) -> str:
231
+ return (content_type or "").split(";", 1)[0].strip().lower()
232
+
233
+
234
+ def _is_json(content_type: str | None) -> bool:
235
+ media_type = _media_type(content_type)
236
+ return media_type == "application/json" or media_type.endswith("+json")
237
+
238
+
239
+ def _strings(value: object) -> list[str]:
240
+ if isinstance(value, str):
241
+ return [value]
242
+ if isinstance(value, dict | list):
243
+ return [slot.get() for slot in slots_for_json_strings(value)]
244
+ return []
245
+
246
+
247
+ def _decoded(raw: Iterable[tuple[bytes, bytes]]) -> list[tuple[str, str]]:
248
+ return [(name.decode("latin-1"), value.decode("latin-1")) for name, value in raw]
249
+
250
+
251
+ def _encoded(headers: Iterable[tuple[str, str]]) -> list[tuple[bytes, bytes]]:
252
+ return [(name.lower().encode("latin-1"), value.encode("latin-1")) for name, value in headers]
253
+
254
+
255
+ def _upstream_response(upstream: httpx.Response, content: bytes) -> Response:
256
+ response = Response(content=content, status_code=upstream.status_code)
257
+ response.raw_headers.extend(_encoded(filter_response_headers(_decoded(upstream.headers.raw))))
258
+ return response
259
+
260
+
261
+ class _RelayResponse(StreamingResponse):
262
+ """Relays an upstream event stream through a stream transformer.
263
+
264
+ However the response ends (completion, a failure, a client disconnect), the body generator
265
+ and the upstream response are closed and the request line is written once. On a failure the
266
+ stream stops: the client sees a truncated stream, and text the transformer holds back is
267
+ never sent.
268
+ """
269
+
270
+ def __init__(
271
+ self, upstream: httpx.Response, transformer: StreamTransformer, log: _RequestLog
272
+ ) -> None:
273
+ self._upstream = upstream
274
+ self._transformer = transformer
275
+ self._log = log
276
+ self._restoring = 0.0
277
+ self._events = self._relay()
278
+ log.deferred = True
279
+ super().__init__(self._events, status_code=upstream.status_code, media_type=_EVENT_STREAM)
280
+ kept = [
281
+ (name, value)
282
+ for name, value in filter_response_headers(_decoded(upstream.headers.raw))
283
+ if name.lower() not in ("content-type", "cache-control")
284
+ ]
285
+ self.raw_headers.extend(_encoded([*kept, ("cache-control", "no-cache")]))
286
+
287
+ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
288
+ try:
289
+ await super().__call__(scope, receive, send)
290
+ except ClientDisconnect:
291
+ pass # how servers implementing ASGI 2.4 report a client that went away
292
+ except Exception as exc:
293
+ self._log.outcome = "gateway_error"
294
+ self._log.failure("stream", exc)
295
+ finally:
296
+ # Shielded: after a disconnect the task is being cancelled, and the provider must
297
+ # still be told to stop generating.
298
+ with anyio.CancelScope(shield=True):
299
+ await self._events.aclose()
300
+ await self._upstream.aclose()
301
+ self._log.unresolved_placeholders = self._transformer.unresolved
302
+ self._log.overhead["restore"] = self._restoring
303
+ self._log.emit(self.status_code)
304
+
305
+ def _transform(self, event: SSEEvent | None) -> list[SSEEvent]:
306
+ """The transformer's output for one event, or for the end of the stream (None), timed."""
307
+ started = time.perf_counter()
308
+ try:
309
+ return self._transformer.end() if event is None else self._transformer.feed(event)
310
+ finally:
311
+ self._restoring += time.perf_counter() - started
312
+
313
+ async def _relay(self) -> AsyncGenerator[bytes, None]:
314
+ parser = SSEParser()
315
+ try:
316
+ async for chunk in self._upstream.aiter_bytes():
317
+ for event in parser.feed(chunk):
318
+ for output in self._transform(event):
319
+ yield output.encode()
320
+ for event in parser.end():
321
+ for output in self._transform(event):
322
+ yield output.encode()
323
+ for output in self._transform(None):
324
+ yield output.encode()
325
+ except Exception as exc:
326
+ self._log.outcome = (
327
+ "upstream_error" if isinstance(exc, httpx.HTTPError) else "gateway_error"
328
+ )
329
+ self._log.failure("stream", exc)
330
+
331
+
332
+ class _Gateway:
333
+ """Routes requests and runs the request flow; one instance per application."""
334
+
335
+ def __init__(
336
+ self,
337
+ settings: Settings,
338
+ pseudonymizer: Pseudonymizer,
339
+ http_client: httpx.AsyncClient | None,
340
+ metrics: GatewayMetrics | None,
341
+ ) -> None:
342
+ self.client = http_client
343
+ self._settings = settings
344
+ self._pseudonymizer = pseudonymizer
345
+ self._metrics = metrics
346
+ self._policy = _policy(settings.policy)
347
+ self._openai = OpenAIChatDialect()
348
+ self._anthropic = AnthropicMessagesDialect()
349
+ upstreams = settings.upstreams
350
+ openai_url, anthropic_url = upstreams.openai.base_url, upstreams.anthropic.base_url
351
+ self._routes: dict[tuple[str, str], _DialectRoute] = {
352
+ ("POST", _CHAT_COMPLETIONS): _DialectRoute(self._openai, openai_url, _CHAT_COMPLETIONS),
353
+ ("POST", _MESSAGES): _DialectRoute(self._anthropic, anthropic_url, _MESSAGES),
354
+ ("POST", _COUNT_TOKENS): _DialectRoute(
355
+ self._anthropic, anthropic_url, _COUNT_TOKENS, request_only=True
356
+ ),
357
+ }
358
+ by_upstream = {
359
+ "openai": _PassthroughRoute(self._openai, openai_url),
360
+ "anthropic": _PassthroughRoute(self._anthropic, anthropic_url),
361
+ }
362
+ self._passthrough: dict[tuple[str, str], _PassthroughRoute] = {}
363
+ for entry in upstreams.passthrough_paths:
364
+ upstream, method, path = entry.split(" ", 2)
365
+ self._passthrough[(method, path)] = by_upstream[upstream]
366
+
367
+ async def dispatch(self, request: Request) -> Response:
368
+ """Answer one request. Never raises; every response carries the request id."""
369
+ log = _RequestLog(self._metrics)
370
+ key = (request.method, request.scope["path"])
371
+ anthropic_client = any(name in request.headers for name in _ANTHROPIC_HEADERS)
372
+ dialect: Dialect = self._anthropic if anthropic_client else self._openai
373
+ try:
374
+ if key == _HEALTH:
375
+ log.route = " ".join(key)
376
+ response: Response = JSONResponse({"status": "ok"})
377
+ elif key == _METRICS and self._metrics is not None:
378
+ log.route = " ".join(key)
379
+ response = Response(self._metrics.render(), media_type=CONTENT_TYPE)
380
+ elif (route := self._routes.get(key)) is not None:
381
+ log.route = " ".join(key)
382
+ dialect = route.dialect
383
+ response = await self._dialect_flow(request, route, log)
384
+ elif (passthrough := self._passthrough.get(key)) is not None:
385
+ log.route = " ".join(key)
386
+ dialect = passthrough.dialect
387
+ response = await self._passthrough_flow(request, passthrough, key[0], log)
388
+ else:
389
+ raise errors.unknown_endpoint()
390
+ except GatewayError as error:
391
+ response = self._error(dialect, error, log)
392
+ except Exception as exc:
393
+ log.failure("internal", exc)
394
+ response = self._error(dialect, errors.internal_error(), log)
395
+ response.headers[REQUEST_ID_HEADER] = log.request_id
396
+ response.headers[ENTITIES_HEADER] = str(log.entity_count)
397
+ if not log.deferred:
398
+ log.emit(response.status_code)
399
+ return response
400
+
401
+ @staticmethod
402
+ def _error(dialect: Dialect, error: GatewayError, log: _RequestLog) -> Response:
403
+ log.outcome = error.outcome
404
+ log.code = error.code
405
+ return errors.error_response(dialect, error)
406
+
407
+ async def not_routed(self, request: Request, exc: Exception) -> Response:
408
+ """Starlette's 404 and 405 go through the same dispatch, which answers 404."""
409
+ return await self.dispatch(request)
410
+
411
+ async def _dialect_flow(
412
+ self, request: Request, route: _DialectRoute, log: _RequestLog
413
+ ) -> Response:
414
+ body = _parse_object(await self._read_body(request))
415
+ streaming = not route.request_only and bool(body.get("stream"))
416
+ started = time.perf_counter()
417
+ try:
418
+ prepared = route.dialect.prepare_request(body, self._policy)
419
+ except UnsupportedContentError as exc:
420
+ raise errors.unsupported_content(exc.kind) from None
421
+ except MalformedRequestError as exc:
422
+ raise errors.malformed_request(str(exc)) from None
423
+ texts = prepared.texts()
424
+ log.slots = len(texts)
425
+ try:
426
+ batch = await anyio.to_thread.run_sync(self._pseudonymizer.pseudonymize, texts)
427
+ prepared.apply(batch.texts)
428
+ except Exception as exc:
429
+ log.failure("pseudonymize", exc)
430
+ raise errors.pseudonymization_failed() from None
431
+ log.entities = dict(Counter(entity.entity_type for entity in batch.mapping.entities()))
432
+ log.entity_count = len(batch.mapping)
433
+ log.stats = batch.stats
434
+ if self._settings.policy.leak_audit:
435
+ await self._audit(body, batch.mapping, log)
436
+ log.overhead["pseudonymize"] = time.perf_counter() - started
437
+ url = route.base_url + route.path
438
+ upstream = await self._forward(
439
+ request, "POST", url, _encode(body), log, json_content=True, stream=streaming
440
+ )
441
+ if not streaming:
442
+ if route.request_only and upstream.status_code < 400:
443
+ return _upstream_response(upstream, upstream.content)
444
+ return self._restored(upstream, route.dialect, batch.mapping, log)
445
+ if (
446
+ upstream.status_code < 400
447
+ and _media_type(upstream.headers.get("content-type")) == _EVENT_STREAM
448
+ ):
449
+ try:
450
+ transformer = route.dialect.stream_transformer(batch.mapping)
451
+ return _RelayResponse(upstream, transformer, log)
452
+ except Exception:
453
+ await upstream.aclose()
454
+ raise
455
+ # An error, or an answer that is not a stream: handled like a non-streamed answer.
456
+ try:
457
+ await self._read(upstream, log)
458
+ finally:
459
+ await upstream.aclose()
460
+ return self._restored(upstream, route.dialect, batch.mapping, log)
461
+
462
+ async def _audit(self, body: dict[str, Any], mapping: Mapping, log: _RequestLog) -> None:
463
+ """Reject the request if a detected value is still anywhere in the outbound body."""
464
+ audit = partial(
465
+ audit_outbound,
466
+ body,
467
+ mapping,
468
+ min_length=self._settings.detection.min_propagation_length,
469
+ )
470
+ try:
471
+ result = await anyio.to_thread.run_sync(audit)
472
+ except Exception as exc:
473
+ log.failure("audit", exc)
474
+ raise errors.internal_error() from None
475
+ if result.leaked_entities:
476
+ log.leak(result)
477
+ raise errors.leak_audit_failed()
478
+
479
+ async def _passthrough_flow(
480
+ self, request: Request, route: _PassthroughRoute, method: str, log: _RequestLog
481
+ ) -> Response:
482
+ raw = await self._read_body(request)
483
+ url = route.base_url + request.scope["path"]
484
+ upstream = await self._forward(request, method, url, raw or None, log, json_content=False)
485
+ return _upstream_response(upstream, upstream.content)
486
+
487
+ async def _read_body(self, request: Request) -> bytes:
488
+ """The whole body, refusing to read past `max_request_bytes`."""
489
+ limit = self._settings.listen.max_request_bytes
490
+ declared = request.headers.get("content-length", "")
491
+ if declared.isascii() and declared.isdigit() and int(declared) > limit:
492
+ raise errors.request_too_large()
493
+ chunks: list[bytes] = []
494
+ size = 0
495
+ async with aclosing(request.stream()) as stream:
496
+ async for chunk in stream:
497
+ size += len(chunk)
498
+ if size > limit:
499
+ raise errors.request_too_large()
500
+ chunks.append(chunk)
501
+ return b"".join(chunks)
502
+
503
+ async def _forward(
504
+ self,
505
+ request: Request,
506
+ method: str,
507
+ url: str,
508
+ content: bytes | None,
509
+ log: _RequestLog,
510
+ *,
511
+ json_content: bool,
512
+ stream: bool = False,
513
+ ) -> httpx.Response:
514
+ if self.client is None:
515
+ raise RuntimeError("no upstream client: the application lifespan has not started")
516
+ headers = filter_request_headers(_decoded(request.headers.raw))
517
+ if json_content and not any(name.lower() == "content-type" for name, _ in headers):
518
+ headers.append(("content-type", "application/json"))
519
+ query: bytes = request.scope.get("query_string", b"")
520
+ target = httpx.URL(url, query=query) if query else httpx.URL(url)
521
+ outgoing = self.client.build_request(
522
+ method, target, content=content, headers=_encoded(headers)
523
+ )
524
+ try:
525
+ return await self.client.send(outgoing, stream=stream, follow_redirects=False)
526
+ except httpx.TimeoutException as exc:
527
+ log.failure("forward", exc)
528
+ raise errors.upstream_timeout() from None
529
+ except httpx.HTTPError as exc:
530
+ log.failure("forward", exc)
531
+ raise errors.upstream_unreachable() from None
532
+
533
+ async def _read(self, upstream: httpx.Response, log: _RequestLog) -> None:
534
+ """Read a streamed upstream body to the end, mapping transport errors like `_forward`."""
535
+ try:
536
+ await upstream.aread()
537
+ except httpx.TimeoutException as exc:
538
+ log.failure("forward", exc)
539
+ raise errors.upstream_timeout() from None
540
+ except httpx.HTTPError as exc:
541
+ log.failure("forward", exc)
542
+ raise errors.upstream_unreachable() from None
543
+
544
+ def _restored(
545
+ self, upstream: httpx.Response, dialect: Dialect, mapping: Mapping, log: _RequestLog
546
+ ) -> Response:
547
+ """The upstream response with placeholders restored; non-JSON bodies are unchanged."""
548
+ started = time.perf_counter()
549
+ content = upstream.content
550
+ parsed = _loads(content) if _is_json(upstream.headers.get("content-type")) else _NOT_JSON
551
+ if parsed is not _NOT_JSON:
552
+ try:
553
+ log.unresolved_placeholders = sum(
554
+ count_unresolved(text, mapping) for text in _strings(parsed)
555
+ )
556
+ if upstream.status_code < 400 and isinstance(parsed, dict):
557
+ dialect.restore_response(parsed, mapping)
558
+ else:
559
+ parsed = restore_all_strings(parsed, mapping)
560
+ content = json.dumps(parsed, ensure_ascii=False).encode("utf-8")
561
+ except Exception as exc:
562
+ log.failure("restore", exc)
563
+ raise errors.restoration_failed() from None
564
+ log.overhead["restore"] = time.perf_counter() - started
565
+ return _upstream_response(upstream, content)
566
+
567
+
568
+ def create_app(
569
+ settings: Settings,
570
+ *,
571
+ pseudonymizer: Pseudonymizer | None = None,
572
+ http_client: httpx.AsyncClient | None = None,
573
+ ) -> Starlette:
574
+ """Build the ASGI app. `pseudonymizer` defaults to one built from the settings;
575
+ `http_client` exists for tests."""
576
+ if pseudonymizer is None:
577
+ pseudonymizer = build_pseudonymizer(settings)
578
+ metrics = None
579
+ if settings.metrics.enabled:
580
+ caches = [d for d in pseudonymizer.detectors if isinstance(d, CachedDetector)]
581
+ metrics = GatewayMetrics([cache.stats for cache in caches])
582
+ gateway = _Gateway(settings, pseudonymizer, http_client, metrics)
583
+
584
+ @asynccontextmanager
585
+ async def lifespan(app: Starlette) -> AsyncIterator[None]:
586
+ if http_client is not None:
587
+ yield
588
+ return
589
+ async with httpx.AsyncClient(
590
+ timeout=settings.upstreams.timeout_seconds, follow_redirects=False
591
+ ) as client:
592
+ gateway.client = client
593
+ try:
594
+ yield
595
+ finally:
596
+ gateway.client = None
597
+
598
+ return Starlette(
599
+ routes=[Route("/{path:path}", gateway.dispatch, methods=_METHODS)],
600
+ exception_handlers={404: gateway.not_routed, 405: gateway.not_routed},
601
+ lifespan=lifespan,
602
+ )