Let black f*ckup the layout
This commit is contained in:
parent
29c6f3c49f
commit
6d8f5988d3
|
@ -46,11 +46,12 @@ class Signal:
|
||||||
|
|
||||||
|
|
||||||
class Status(enum.Enum):
|
class Status(enum.Enum):
|
||||||
IDLE = 0 # Waiting for request
|
IDLE = 0 # Waiting for request
|
||||||
REQUEST = 1 # Request headers being received
|
REQUEST = 1 # Request headers being received
|
||||||
EXPECT = 2 # Sender wants 100-continue
|
EXPECT = 2 # Sender wants 100-continue
|
||||||
HANDLER = 3 # Headers done, handler running
|
HANDLER = 3 # Headers done, handler running
|
||||||
RESPONSE = 4 # Response headers sent
|
RESPONSE = 4 # Response headers sent
|
||||||
|
|
||||||
|
|
||||||
class HttpProtocol(asyncio.Protocol):
|
class HttpProtocol(asyncio.Protocol):
|
||||||
"""
|
"""
|
||||||
|
@ -195,18 +196,20 @@ class HttpProtocol(asyncio.Protocol):
|
||||||
"""Runs itself once a second to enforce any expired timeouts."""
|
"""Runs itself once a second to enforce any expired timeouts."""
|
||||||
duration = current_time() - self._time
|
duration = current_time() - self._time
|
||||||
status = self._status
|
status = self._status
|
||||||
if (status == Status.IDLE and duration > self.keep_alive_timeout):
|
if status == Status.IDLE and duration > self.keep_alive_timeout:
|
||||||
logger.debug("KeepAlive Timeout. Closing connection.")
|
logger.debug("KeepAlive Timeout. Closing connection.")
|
||||||
elif (status == Status.REQUEST and duration > self.request_timeout):
|
elif status == Status.REQUEST and duration > self.request_timeout:
|
||||||
self._exception = RequestTimeout("Request Timeout")
|
self._exception = RequestTimeout("Request Timeout")
|
||||||
elif (status.value > Status.REQUEST.value and duration > self.response_timeout):
|
elif (
|
||||||
|
status.value > Status.REQUEST.value
|
||||||
|
and duration > self.response_timeout
|
||||||
|
):
|
||||||
self._exception = ServiceUnavailable("Response Timeout")
|
self._exception = ServiceUnavailable("Response Timeout")
|
||||||
else:
|
else:
|
||||||
self.loop.call_later(.1, self.check_timeouts)
|
self.loop.call_later(0.1, self.check_timeouts)
|
||||||
return
|
return
|
||||||
self._task.cancel()
|
self._task.cancel()
|
||||||
|
|
||||||
|
|
||||||
async def http1(self):
|
async def http1(self):
|
||||||
"""HTTP 1.1 connection handler"""
|
"""HTTP 1.1 connection handler"""
|
||||||
try:
|
try:
|
||||||
|
@ -256,9 +259,8 @@ class HttpProtocol(asyncio.Protocol):
|
||||||
if headers.get("connection", "").lower() == "close":
|
if headers.get("connection", "").lower() == "close":
|
||||||
self.keep_alive = False
|
self.keep_alive = False
|
||||||
# Prepare for request body
|
# Prepare for request body
|
||||||
body = (
|
body = headers.get("transfer-encoding") == "chunked" or int(
|
||||||
headers.get("transfer-encoding") == "chunked"
|
headers.get("content-length", 0)
|
||||||
or int(headers.get("content-length", 0))
|
|
||||||
)
|
)
|
||||||
self._request_chunked = False
|
self._request_chunked = False
|
||||||
self._request_bytes_left = 0
|
self._request_bytes_left = 0
|
||||||
|
@ -273,7 +275,7 @@ class HttpProtocol(asyncio.Protocol):
|
||||||
else:
|
else:
|
||||||
self._request_bytes_left = body
|
self._request_bytes_left = body
|
||||||
# Remove header and its trailing CRLF
|
# Remove header and its trailing CRLF
|
||||||
del buf[:pos + 4]
|
del buf[: pos + 4]
|
||||||
# Run handler
|
# Run handler
|
||||||
self._status, self._time = Status.HANDLER, current_time()
|
self._status, self._time = Status.HANDLER, current_time()
|
||||||
await self.request_handler(
|
await self.request_handler(
|
||||||
|
@ -281,13 +283,16 @@ class HttpProtocol(asyncio.Protocol):
|
||||||
)
|
)
|
||||||
# Consume any remaining request body
|
# Consume any remaining request body
|
||||||
if self._request_bytes_left or self._request_chunked:
|
if self._request_bytes_left or self._request_chunked:
|
||||||
logger.error(f"Handler of {method} {self.url} did not consume request body.")
|
logger.error(
|
||||||
while await self.stream_body(): pass
|
f"Handler of {method} {self.url} did not consume request body."
|
||||||
|
)
|
||||||
|
while await self.stream_body():
|
||||||
|
pass
|
||||||
self._status, self._time = Status.IDLE, current_time()
|
self._status, self._time = Status.IDLE, current_time()
|
||||||
except asyncio.CancelledError:
|
except asyncio.CancelledError:
|
||||||
self.write_error(
|
self.write_error(
|
||||||
self._exception or
|
self._exception
|
||||||
ServiceUnavailable("Request handler cancelled")
|
or ServiceUnavailable("Request handler cancelled")
|
||||||
)
|
)
|
||||||
except SanicException as e:
|
except SanicException as e:
|
||||||
self.write_error(e)
|
self.write_error(e)
|
||||||
|
@ -315,7 +320,7 @@ class HttpProtocol(asyncio.Protocol):
|
||||||
raise InvalidUsage("Bad chunked encoding")
|
raise InvalidUsage("Bad chunked encoding")
|
||||||
self._request_bytes_left = size
|
self._request_bytes_left = size
|
||||||
self._total_request_size += pos + 2
|
self._total_request_size += pos + 2
|
||||||
del buf[:pos + 2]
|
del buf[: pos + 2]
|
||||||
if self._request_bytes_left <= 0:
|
if self._request_bytes_left <= 0:
|
||||||
self._request_chunked = False
|
self._request_chunked = False
|
||||||
return None
|
return None
|
||||||
|
@ -323,7 +328,7 @@ class HttpProtocol(asyncio.Protocol):
|
||||||
if self._request_bytes_left:
|
if self._request_bytes_left:
|
||||||
if not buf:
|
if not buf:
|
||||||
await self.receive_more()
|
await self.receive_more()
|
||||||
data = bytes(buf[:self._request_bytes_left])
|
data = bytes(buf[: self._request_bytes_left])
|
||||||
size = len(data)
|
size = len(data)
|
||||||
del buf[:size]
|
del buf[:size]
|
||||||
self._request_bytes_left -= size
|
self._request_bytes_left -= size
|
||||||
|
@ -391,7 +396,9 @@ class HttpProtocol(asyncio.Protocol):
|
||||||
self._status, self._time = Status.RESPONSE, current_time()
|
self._status, self._time = Status.RESPONSE, current_time()
|
||||||
self._last_response_time = self._time
|
self._last_response_time = self._time
|
||||||
self.transport.write(
|
self.transport.write(
|
||||||
response.output("1.1", self.keep_alive, self.keep_alive_timeout)
|
response.output(
|
||||||
|
"1.1", self.keep_alive, self.keep_alive_timeout
|
||||||
|
)
|
||||||
)
|
)
|
||||||
self.log_response(response)
|
self.log_response(response)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
|
@ -436,7 +443,9 @@ class HttpProtocol(asyncio.Protocol):
|
||||||
try:
|
try:
|
||||||
self._status, self._time = Status.RESPONSE, current_time()
|
self._status, self._time = Status.RESPONSE, current_time()
|
||||||
response.protocol = self
|
response.protocol = self
|
||||||
await response.stream("1.1", self.keep_alive, self.keep_alive_timeout)
|
await response.stream(
|
||||||
|
"1.1", self.keep_alive, self.keep_alive_timeout
|
||||||
|
)
|
||||||
self.log_response(response)
|
self.log_response(response)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
logger.error(
|
logger.error(
|
||||||
|
|
Loading…
Reference in New Issue
Block a user