| 326 | return {"type": "http.request", "body": body_bytes} |
| 327 | |
| 328 | async def send(message: Message) -> None: |
| 329 | nonlocal raw_kwargs, response_started, template, context |
| 330 | |
| 331 | if message["type"] == "http.response.start": |
| 332 | assert not response_started, 'Received multiple "http.response.start" messages.' |
| 333 | raw_kwargs["status_code"] = message["status"] |
| 334 | raw_kwargs["headers"] = [(key.decode(), value.decode()) for key, value in message.get("headers", [])] |
| 335 | response_started = True |
| 336 | elif message["type"] == "http.response.body": |
| 337 | assert response_started, 'Received "http.response.body" without "http.response.start".' |
| 338 | assert not response_complete.is_set(), 'Received "http.response.body" after response completed.' |
| 339 | body = message.get("body", b"") |
| 340 | more_body = message.get("more_body", False) |
| 341 | if request.method != "HEAD": |
| 342 | raw_kwargs["stream"].write(body) |
| 343 | if not more_body: |
| 344 | raw_kwargs["stream"].seek(0) |
| 345 | response_complete.set() |
| 346 | elif message["type"] == "http.response.debug": |
| 347 | template = message["info"]["template"] |
| 348 | context = message["info"]["context"] |
| 349 | |
| 350 | try: |
| 351 | with self.portal_factory() as portal: |