Apply response Middleware always

Response middleware are useful to apply some post-process information, just before send to the user. For example: Add some HTTP headers (security headers, for example), remove "Server" banner (for security reasons) or cookie management. 

The change is very very simple: although an "request" middleware has produced any response, we'll even apply the response middlewares.
This commit is contained in:
cr0hn 2016-12-23 15:59:04 +01:00 committed by GitHub
parent ef9d8710f5
commit 5afae986a0

View File

@ -193,18 +193,18 @@ class Sanic:
if isawaitable(response): if isawaitable(response):
response = await response response = await response
# -------------------------------------------- # # -------------------------------------------- #
# Response Middleware # Response Middleware
# -------------------------------------------- # # -------------------------------------------- #
if self.response_middleware: if self.response_middleware:
for middleware in self.response_middleware: for middleware in self.response_middleware:
_response = middleware(request, response) _response = middleware(request, response)
if isawaitable(_response): if isawaitable(_response):
_response = await _response _response = await _response
if _response: if _response:
response = _response response = _response
break break
except Exception as e: except Exception as e:
# -------------------------------------------- # # -------------------------------------------- #