From d81096fdc0b8a99c8cff17724839ce8ad13af293 Mon Sep 17 00:00:00 2001 From: Kiril Yershov Date: Sun, 28 Jun 2020 09:29:48 +0300 Subject: [PATCH] Clarified response middleware execution order in the documentation (#1846) Co-authored-by: Adam Hopkins --- docs/sanic/middleware.rst | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/sanic/middleware.rst b/docs/sanic/middleware.rst index 22a97192..e7d1d01c 100644 --- a/docs/sanic/middleware.rst +++ b/docs/sanic/middleware.rst @@ -14,8 +14,8 @@ There are two types of middleware: request and response. Both are declared using the `@app.middleware` decorator, with the decorator's parameter being a string representing its type: `'request'` or `'response'`. -* Request middleware receives only the `request` as argument. -* Response middleware receives both the `request` and `response`. +* Request middleware receives only the `request` as an argument and are executed in the order they were added. +* Response middleware receives both the `request` and `response` and are executed in *reverse* order. The simplest middleware doesn't modify the request or response at all: @@ -64,12 +64,12 @@ this. app.run(host="0.0.0.0", port=8000) -The three middlewares are executed in order: +The three middlewares are executed in the following order: 1. The first request middleware **add_key** adds a new key `foo` into request context. 2. Request is routed to handler **index**, which gets the key from context and returns a text response. -3. The first response middleware **custom_banner** changes the HTTP response header *Server* to say *Fake-Server* -4. The second response middleware **prevent_xss** adds the HTTP header for preventing Cross-Site-Scripting (XSS) attacks. +3. The second response middleware **prevent_xss** adds the HTTP header for preventing Cross-Site-Scripting (XSS) attacks. +4. The first response middleware **custom_banner** changes the HTTP response header *Server* to say *Fake-Server* Responding early ----------------