From 3add40625dc1b195c3129c34b88724e1a1869638 Mon Sep 17 00:00:00 2001 From: cr0hn Date: Fri, 23 Dec 2016 16:07:59 +0100 Subject: [PATCH 1/2] Explain how to chain two (or more) middlewares A funny and useful examples about how to chain middlewares. --- docs/middleware.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/docs/middleware.md b/docs/middleware.md index 0b27443c..88d4e535 100644 --- a/docs/middleware.md +++ b/docs/middleware.md @@ -27,3 +27,23 @@ async def handler(request): app.run(host="0.0.0.0", port=8000) ``` + +## Middleware chain + +If you want to apply the middleware as a chain, applying more than one, is so easy. You only have to be aware that **no return** any response in your middleware: + +```python +app = Sanic(__name__) + +@app.middleware('response') +async def custom_banner(request, response): + response.headers["Server"] = "Fake-Server" + +@app.middleware('response') +async def prevent_xss(request, response): + response.headers["x-xss-protection"] = "1; mode=block" + +app.run(host="0.0.0.0", port=8000) +``` + +The above code will apply the two middlewares in order. First the middleware **custom_banner** will change the HTTP Response headers *Server* by *Fake-Server*, and the second middleware **prevent_xss** will add the HTTP Headers for prevent Cross-Site-Scripting (XSS) attacks. From cd17a42234c6465f70795f78436e0589badc5a25 Mon Sep 17 00:00:00 2001 From: Eli Uriegas Date: Fri, 23 Dec 2016 09:59:28 -0800 Subject: [PATCH 2/2] Fix some verbage --- docs/middleware.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/middleware.md b/docs/middleware.md index 88d4e535..39930e3e 100644 --- a/docs/middleware.md +++ b/docs/middleware.md @@ -30,7 +30,7 @@ app.run(host="0.0.0.0", port=8000) ## Middleware chain -If you want to apply the middleware as a chain, applying more than one, is so easy. You only have to be aware that **no return** any response in your middleware: +If you want to apply the middleware as a chain, applying more than one, is so easy. You only have to be aware that you do **not return** any response in your middleware: ```python app = Sanic(__name__)