From 9c21457b58d95d2805662b6239c9b295442d7465 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=2E=20K=C3=A4rkk=C3=A4inen?= Date: Mon, 2 Mar 2020 16:34:21 +0200 Subject: [PATCH] Always try to consume request body, plus minor cleanup. --- sanic/http.py | 17 +++++++---------- sanic/server.py | 2 +- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/sanic/http.py b/sanic/http.py index b217200b..3009fdc9 100644 --- a/sanic/http.py +++ b/sanic/http.py @@ -78,11 +78,6 @@ class Http: raise ServerError("Handler produced no response") if self.stage is Stage.RESPONSE: await self.send(end_stream=True) - # Consume any remaining request body (TODO: or disconnect?) - if self.request_body: - logger.error(f"{self.request} body not consumed.") - async for _ in self: - pass except CancelledError: # Write an appropriate response before exiting e = self.exception or ServiceUnavailable(f"Cancelled") @@ -92,12 +87,14 @@ class Http: except Exception as e: # Write an error response await self.error_response(e) + # Try to consume any remaining request body + if self.request_body: + if self.response and 200 <= self.response.status < 300: + logger.error(f"{self.request} body not consumed.") + async for _ in self: + pass # Exit and disconnect if no more requests can be taken - if ( - self.stage is not Stage.IDLE - or self.request_body - or not self.keep_alive - ): + if self.stage is not Stage.IDLE or not self.keep_alive: break # Wait for next request if not self.recv_buffer: diff --git a/sanic/server.py b/sanic/server.py index 48ad79c9..c7f55f4a 100644 --- a/sanic/server.py +++ b/sanic/server.py @@ -228,7 +228,7 @@ class HttpProtocol(asyncio.Protocol): if self._task: self._task.cancel() if self._debug and self._http and self._http.response: - ip = self._http.request.ip + ip = self.transport.get_extra_info("peername") logger.error(f"Connection lost before response written @ {ip}") except Exception: logger.exception("protocol.connection_lost")