|
| 1 | +"""The driver's ``/svc/<name>/`` route to each app on its computer: a request sent on to the app's server with its body |
| 2 | +and its answer streamed, and a WebSocket relayed to it both ways. It needs the ``computer`` extra's ``websockets``, so |
| 3 | +only the driver imports it; an app's venv, with the ``app`` extra, imports ``apps`` and never this.""" |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import asyncio |
| 7 | +import contextlib |
| 8 | +import re |
| 9 | +from collections.abc import AsyncIterator, Iterable |
| 10 | +from pathlib import Path |
| 11 | +from typing import Any, Callable |
| 12 | +from urllib.parse import quote, unquote |
| 13 | + |
| 14 | +import httpx |
| 15 | +import websockets |
| 16 | +from starlette.background import BackgroundTask |
| 17 | +from starlette.requests import Request |
| 18 | +from starlette.responses import Response, StreamingResponse |
| 19 | +from starlette.routing import WebSocketRoute |
| 20 | +from starlette.websockets import WebSocket, WebSocketDisconnect |
| 21 | +from websockets.asyncio.client import ClientConnection, unix_connect |
| 22 | + |
| 23 | +from ..agent_env_environment import _error_response, control_check |
| 24 | +from .app import SOCKET |
| 25 | +from .errors import DeliveryFailed |
| 26 | + |
| 27 | +#: The methods the route forwards. |
| 28 | +METHODS = ["GET", "HEAD", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"] |
| 29 | +#: The headers that belong to one connection, which a proxy doesn't pass on. |
| 30 | +HOP_HEADERS = frozenset({"connection", "keep-alive", "proxy-authenticate", "proxy-authorization", "proxy-connection", |
| 31 | + "te", "trailer", "transfer-encoding", "upgrade"}) |
| 32 | +#: The address a request reached the driver at, which the driver sets for the app in place of any the caller sent. |
| 33 | +FORWARDED = ("x-forwarded-host", "x-forwarded-proto", "x-forwarded-prefix") |
| 34 | +#: A WebSocket close code that means the connection ended without one, which a close can't send on. |
| 35 | +UNSENDABLE_CLOSES = (1005, 1006, 1015) |
| 36 | +#: A Host header that names a host, and its port or not, and nothing else. |
| 37 | +HOST = re.compile(r"(?:[A-Za-z0-9.-]+|\[[0-9A-Fa-f:.]+\])(?::[0-9]{1,5})?") |
| 38 | +#: What a path's segments may hold unescaped, besides letters, digits and ``_.-~``. |
| 39 | +PATH_SAFE = "/:@!$&'()*+,;=" |
| 40 | + |
| 41 | + |
| 42 | +def passed_on(headers: Iterable[tuple[str, str]], *dropped: str) -> list[tuple[str, str]]: |
| 43 | + """``headers`` as a proxy passes them on: without the hop-by-hop ones, those a ``Connection`` header names, and |
| 44 | + ``dropped``.""" |
| 45 | + headers = list(headers) |
| 46 | + named = {token.strip().lower() for name, value in headers if name.lower() == "connection" |
| 47 | + for token in value.split(",")} |
| 48 | + skipped = HOP_HEADERS | named | set(dropped) |
| 49 | + return [(name, value) for name, value in headers if name.lower() not in skipped] |
| 50 | + |
| 51 | + |
| 52 | +def to_app(headers: Iterable[tuple[str, str]], host: str, scheme: str, prefix: str) -> list[tuple[str, str]]: |
| 53 | + """A caller's headers as the app gets them: passed on without the control token's ``Authorization`` or any address |
| 54 | + the caller forwarded, and with the one it reached the driver at, its ``host``, ``scheme`` and the app's |
| 55 | + ``prefix``.""" |
| 56 | + return passed_on(headers, "authorization", *FORWARDED) + [ |
| 57 | + ("x-forwarded-host", host), ("x-forwarded-proto", scheme), ("x-forwarded-prefix", prefix)] |
| 58 | + |
| 59 | + |
| 60 | +def target(connection: Request | WebSocket) -> str: |
| 61 | + """The path and query a request under ``/svc/<name>/`` asks its app for, as it came: still percent-encoded, so an |
| 62 | + escaped ``?``, ``#`` or ``/`` stays one. The raw path's segments past any the driver is mounted under must be |
| 63 | + ``svc`` and the app's name, else the routed path is escaped again.""" |
| 64 | + mounted = len([segment for segment in connection.scope.get("root_path", "").split("/") if segment]) |
| 65 | + parts = (connection.scope.get("raw_path") or b"").decode("latin-1").split("/")[1 + mounted:] |
| 66 | + if len(parts) > 2 and [unquote(part) for part in parts[:2]] == ["svc", connection.path_params["name"]]: |
| 67 | + tail = "/".join(parts[2:]) |
| 68 | + else: |
| 69 | + tail = quote(connection.path_params["path"], safe=PATH_SAFE) |
| 70 | + query = connection.scope.get("query_string", b"").decode("latin-1") |
| 71 | + return f"/{tail}" + (f"?{query}" if query else "") |
| 72 | + |
| 73 | + |
| 74 | +def _bytes(headers: Iterable[tuple[str, str]]) -> list[tuple[bytes, bytes]]: |
| 75 | + """Headers decoded as Latin-1, as Starlette and the helpers here hold them, as the bytes they came as.""" |
| 76 | + return [(name.encode("latin-1"), value.encode("latin-1")) for name, value in headers] |
| 77 | + |
| 78 | + |
| 79 | +def websocket_route(app: Any, path: str, endpoint: Callable) -> None: |
| 80 | + """``endpoint`` served at ``path`` as a WebSocket route of ``app``, an mcp or a standalone fastmcp FastMCP, which |
| 81 | + have no API for one.""" |
| 82 | + for routes in ("_custom_starlette_routes", "_additional_http_routes"): |
| 83 | + if isinstance(getattr(app, routes, None), list): |
| 84 | + getattr(app, routes).append(WebSocketRoute(path, endpoint)) |
| 85 | + return |
| 86 | + raise TypeError(f"a driver's app takes WebSocket routes on an mcp or a fastmcp FastMCP, not a {type(app).__name__}") |
| 87 | + |
| 88 | + |
| 89 | +async def forward(folder: Path, name: str, request: Request) -> Response: |
| 90 | + """``request`` sent on to the app ``name``'s server in ``folder`` with the caller's headers and none of the |
| 91 | + client's own, its body and its answer streamed; it takes as long as the app does.""" |
| 92 | + headers = to_app(request.headers.items(), request.headers.get("host", ""), request.url.scheme, f"/svc/{name}") |
| 93 | + has_body = request.headers.get("content-length", "0") != "0" or "transfer-encoding" in request.headers |
| 94 | + http = httpx.AsyncClient(transport=httpx.AsyncHTTPTransport(uds=str(folder / SOCKET)), base_url="http://app", |
| 95 | + timeout=None) |
| 96 | + http.headers.clear() |
| 97 | + try: |
| 98 | + answer = await http.send(http.build_request(request.method, target(request), headers=_bytes(headers), |
| 99 | + content=request.stream() if has_body else None), stream=True) |
| 100 | + except (httpx.HTTPError, OSError) as e: |
| 101 | + await http.aclose() |
| 102 | + return _error_response(DeliveryFailed(f"app {name}'s server isn't answering: {type(e).__name__}: {e}")) |
| 103 | + except BaseException: |
| 104 | + await http.aclose() |
| 105 | + raise |
| 106 | + |
| 107 | + async def close() -> None: |
| 108 | + await answer.aclose() |
| 109 | + await http.aclose() |
| 110 | + |
| 111 | + async def streamed() -> AsyncIterator[bytes]: |
| 112 | + try: |
| 113 | + async for chunk in answer.aiter_raw(): |
| 114 | + yield chunk |
| 115 | + finally: |
| 116 | + await close() |
| 117 | + |
| 118 | + response = StreamingResponse(streamed(), status_code=answer.status_code, background=BackgroundTask(close)) |
| 119 | + response.raw_headers = _bytes(passed_on((name.decode("latin-1"), value.decode("latin-1")) |
| 120 | + for name, value in answer.headers.raw)) |
| 121 | + return response |
| 122 | + |
| 123 | + |
| 124 | +async def relay(folder: Path, name: str, websocket: WebSocket) -> None: |
| 125 | + """``websocket`` relayed both ways to the app ``name``'s server in ``folder`` until either side closes; refused, |
| 126 | + before it opens, without the control token, with a Host that isn't a host and port, or with the app's server not |
| 127 | + answering.""" |
| 128 | + host = websocket.headers.get("host", "") |
| 129 | + if not control_check()(websocket.headers) or (host and not HOST.fullmatch(host)): |
| 130 | + await websocket.close(code=1008) |
| 131 | + return |
| 132 | + headers = [(key, _utf8(value)) for key, value in to_app( |
| 133 | + websocket.headers.items(), host, "https" if websocket.url.scheme == "wss" else "http", f"/svc/{name}") |
| 134 | + if key.lower() != "host" and not key.lower().startswith("sec-websocket-")] |
| 135 | + offered = [p.strip() for p in websocket.headers.get("sec-websocket-protocol", "").split(",") if p.strip()] |
| 136 | + try: |
| 137 | + upstream = await unix_connect(str(folder / SOCKET), f"ws://{host or 'app'}{target(websocket)}", |
| 138 | + additional_headers=headers, subprotocols=offered or None, max_size=None, |
| 139 | + compression=None, ping_interval=None, user_agent_header=None) |
| 140 | + except (OSError, ValueError, asyncio.TimeoutError, websockets.WebSocketException): |
| 141 | + await websocket.close(code=1011) |
| 142 | + return |
| 143 | + async with upstream: |
| 144 | + await websocket.accept(subprotocol=upstream.subprotocol) |
| 145 | + await _both_ways(websocket, upstream) |
| 146 | + |
| 147 | + |
| 148 | +def _utf8(value: str) -> str: |
| 149 | + """A header value decoded as Latin-1 as the string ``websockets`` sends as the same bytes, which it encodes as |
| 150 | + UTF-8; bytes that aren't UTF-8 become replacement characters.""" |
| 151 | + return value.encode("latin-1").decode("utf-8", "replace") |
| 152 | + |
| 153 | + |
| 154 | +async def _both_ways(client: WebSocket, app: ClientConnection) -> None: |
| 155 | + """Each message from either side sent to the other until one side closes, then the other closed with its code.""" |
| 156 | + async def inbound() -> None: |
| 157 | + while True: |
| 158 | + message = await client.receive() |
| 159 | + if message["type"] == "websocket.disconnect": |
| 160 | + code = message.get("code") or 1000 |
| 161 | + await app.close(1000 if code in UNSENDABLE_CLOSES else code) |
| 162 | + return |
| 163 | + await app.send(message["text"] if message.get("text") is not None else message["bytes"]) |
| 164 | + |
| 165 | + async def outbound() -> None: |
| 166 | + with contextlib.suppress(websockets.ConnectionClosed): |
| 167 | + async for message in app: |
| 168 | + if isinstance(message, str): |
| 169 | + await client.send_text(message) |
| 170 | + else: |
| 171 | + await client.send_bytes(message) |
| 172 | + code = app.close_code or 1000 |
| 173 | + with contextlib.suppress(RuntimeError, WebSocketDisconnect): |
| 174 | + await client.close(1000 if code in UNSENDABLE_CLOSES else code) |
| 175 | + |
| 176 | + tasks = [asyncio.ensure_future(inbound()), asyncio.ensure_future(outbound())] |
| 177 | + try: |
| 178 | + await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED) |
| 179 | + finally: |
| 180 | + for task in tasks: |
| 181 | + task.cancel() |
| 182 | + await asyncio.gather(*tasks, return_exceptions=True) |
0 commit comments