Always try to consume request body, plus minor cleanup.

This commit is contained in:
L. Kärkkäinen 2020-03-02 16:34:21 +02:00
parent 85d58d7b2b
commit 9c21457b58
2 changed files with 8 additions and 11 deletions

View File

@ -78,11 +78,6 @@ class Http:
raise ServerError("Handler produced no response") raise ServerError("Handler produced no response")
if self.stage is Stage.RESPONSE: if self.stage is Stage.RESPONSE:
await self.send(end_stream=True) 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: except CancelledError:
# Write an appropriate response before exiting # Write an appropriate response before exiting
e = self.exception or ServiceUnavailable(f"Cancelled") e = self.exception or ServiceUnavailable(f"Cancelled")
@ -92,12 +87,14 @@ class Http:
except Exception as e: except Exception as e:
# Write an error response # Write an error response
await self.error_response(e) 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 # Exit and disconnect if no more requests can be taken
if ( if self.stage is not Stage.IDLE or not self.keep_alive:
self.stage is not Stage.IDLE
or self.request_body
or not self.keep_alive
):
break break
# Wait for next request # Wait for next request
if not self.recv_buffer: if not self.recv_buffer:

View File

@ -228,7 +228,7 @@ class HttpProtocol(asyncio.Protocol):
if self._task: if self._task:
self._task.cancel() self._task.cancel()
if self._debug and self._http and self._http.response: 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}") logger.error(f"Connection lost before response written @ {ip}")
except Exception: except Exception:
logger.exception("protocol.connection_lost") logger.exception("protocol.connection_lost")