|
77 | 77 | logger = logging.getLogger(__name__) |
78 | 78 |
|
79 | 79 |
|
80 | | -_OK_STATUS = 200 |
81 | | - |
82 | | - |
83 | 80 | @dataclass |
84 | 81 | class _SingleExchangeDispatchContext: |
85 | 82 | """`DispatchContext` for one inbound HTTP request. |
@@ -146,13 +143,6 @@ async def _to_jsonrpc_response( |
146 | 143 | _SSE_PING_INTERVAL: float = 15.0 |
147 | 144 | """Seconds between SSE comment-line keepalives once `text/event-stream` has committed.""" |
148 | 145 |
|
149 | | -_SSE_HEADERS: Final[list[tuple[bytes, bytes]]] = [ |
150 | | - (b"content-type", b"text/event-stream"), |
151 | | - (b"cache-control", b"no-cache, no-transform"), |
152 | | - (b"connection", b"keep-alive"), |
153 | | - (b"x-accel-buffering", b"no"), |
154 | | -] |
155 | | - |
156 | 146 |
|
157 | 147 | def _sse_event(msg: JSONRPCResponse | JSONRPCError | JSONRPCNotification) -> bytes: |
158 | 148 | """Serialise a JSON-RPC message as one SSE `event: message` frame. |
@@ -188,7 +178,7 @@ async def _write( |
188 | 178 | send: Send, |
189 | 179 | ) -> None: |
190 | 180 | """Serialise a JSON-RPC reply with the table-mapped HTTP status.""" |
191 | | - status = ERROR_CODE_HTTP_STATUS.get(msg.error.code, _OK_STATUS) if isinstance(msg, JSONRPCError) else _OK_STATUS |
| 181 | + status = ERROR_CODE_HTTP_STATUS.get(msg.error.code, 200) if isinstance(msg, JSONRPCError) else 200 |
192 | 182 | body = msg.model_dump(mode="json", by_alias=True, exclude_none=True) |
193 | 183 | if isinstance(msg, JSONRPCError) and msg.id is None: |
194 | 184 | # JSON-RPC requires `id: null` to appear on the wire when the request |
@@ -512,7 +502,18 @@ async def watch_disconnect(cancel_scope: anyio.CancelScope) -> None: |
512 | 502 | # First notification arrived, or the deferral window elapsed: commit |
513 | 503 | # `text/event-stream` and start pinging so a proxy idle-read timeout |
514 | 504 | # cannot close the stream (which on this path cancels the handler). |
515 | | - await send({"type": "http.response.start", "status": _OK_STATUS, "headers": _SSE_HEADERS}) |
| 505 | + await send( |
| 506 | + { |
| 507 | + "type": "http.response.start", |
| 508 | + "status": 200, |
| 509 | + "headers": [ |
| 510 | + (b"content-type", b"text/event-stream"), |
| 511 | + (b"cache-control", b"no-cache, no-transform"), |
| 512 | + (b"connection", b"keep-alive"), |
| 513 | + (b"x-accel-buffering", b"no"), |
| 514 | + ], |
| 515 | + } |
| 516 | + ) |
516 | 517 | while not done: |
517 | 518 | await send({"type": "http.response.body", "body": event or b": ping\r\n\r\n", "more_body": True}) |
518 | 519 | event = None |
|
0 commit comments