blob: 20799a3a7c2f7667bfda2853fe478c9c0c188126 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
#!/usr/bin/env python3
"""
Example for rewriting response headers by middleware.
"""
from aiohttp import web
async def handler(request):
return web.Response(text="Everything is fine")
@web.middleware
async def middleware(request, handler):
try:
response = await handler(request)
except web.HTTPException as exc:
raise exc
if not response.prepared:
response.headers["SERVER"] = "Secured Server Software"
return response
def init():
app = web.Application(middlewares=[middleware])
app.router.add_get("/", handler)
return app
web.run_app(init())
|