* assign app before handle_request so that request.app could be used in case of connection timeout

* gitignore pip-wheel-metadata/

* remove default app for request class and fix lint issue
This commit is contained in:
7 2019-04-12 05:48:32 -07:00 committed by Stephen Sadowski
parent d58151a0eb
commit 53f45810ff
4 changed files with 12 additions and 5 deletions

3
.gitignore vendored
View File

@ -15,4 +15,5 @@ docs/_build/
docs/_api/
build/*
.DS_Store
dist/*
dist/*
pip-wheel-metadata/

View File

@ -881,8 +881,6 @@ class Sanic:
# -------------------------------------------- #
# Request Middleware
# -------------------------------------------- #
request.app = self
response = await self._run_request_middleware(request)
# No middleware results
if not response:
@ -1288,6 +1286,7 @@ class Sanic:
"port": port,
"sock": sock,
"ssl": ssl,
"app": self,
"signal": Signal(),
"debug": debug,
"request_handler": self.handle_request,

View File

@ -95,11 +95,11 @@ class Request(dict):
"version",
)
def __init__(self, url_bytes, headers, version, method, transport):
def __init__(self, url_bytes, headers, version, method, transport, app):
self.raw_url = url_bytes
# TODO: Content-Encoding detection
self._parsed_url = parse_url(url_bytes)
self.app = None
self.app = app
self.headers = headers
self.version = version

View File

@ -44,6 +44,8 @@ class HttpProtocol(asyncio.Protocol):
"""
__slots__ = (
# app
"app",
# event loop, connection
"loop",
"transport",
@ -88,6 +90,7 @@ class HttpProtocol(asyncio.Protocol):
self,
*,
loop,
app,
request_handler,
error_handler,
signal=Signal(),
@ -107,6 +110,7 @@ class HttpProtocol(asyncio.Protocol):
**kwargs
):
self.loop = loop
self.app = app
self.transport = None
self.request = None
self.parser = None
@ -303,6 +307,7 @@ class HttpProtocol(asyncio.Protocol):
version=self.parser.get_http_version(),
method=self.parser.get_method().decode(),
transport=self.transport,
app=self.app,
)
# Remove any existing KeepAlive handler here,
# It will be recreated if required on the new request.
@ -607,6 +612,7 @@ def trigger_events(events, loop):
def serve(
host,
port,
app,
request_handler,
error_handler,
before_start=None,
@ -704,6 +710,7 @@ def serve(
loop=loop,
connections=connections,
signal=signal,
app=app,
request_handler=request_handler,
error_handler=error_handler,
request_timeout=request_timeout,