Middlewares¶
Warning
The current page still doesn't have a translation for this language.
But you can help translating it: Contributing.
All the Esmerald
native middlewares.
esmerald.middleware.CORSMiddleware
¶
CORSMiddleware(app, allow_origins=None, allow_methods=None, allow_headers=None, allow_credentials=False, allow_origin_regex=None, allow_private_networks=False, expose_headers=None, max_age=600)
Bases: MiddlewareProtocol
Middleware for handling Cross-Origin Resource Sharing (CORS) headers.
PARAMETER | DESCRIPTION |
---|---|
app
|
TYPE:
|
allow_origins
|
TYPE:
|
allow_methods
|
TYPE:
|
allow_headers
|
TYPE:
|
allow_credentials
|
TYPE:
|
allow_origin_regex
|
TYPE:
|
allow_private_networks
|
TYPE:
|
expose_headers
|
TYPE:
|
max_age
|
TYPE:
|
PARAMETER | DESCRIPTION |
---|---|
app
|
The ASGI application to wrap.
TYPE:
|
allow_origins
|
List of allowed origin patterns.
TYPE:
|
allow_methods
|
List of allowed HTTP methods.
TYPE:
|
allow_headers
|
List of allowed HTTP headers.
TYPE:
|
allow_credentials
|
Whether credentials such as cookies are allowed.
TYPE:
|
allow_origin_regex
|
Regular expression for allowed origins.
TYPE:
|
expose_headers
|
List of headers exposed to the browser.
TYPE:
|
max_age
|
Maximum age (in seconds) for caching preflight requests.
TYPE:
|
Source code in lilya/middleware/cors.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
|
preflight_explicit_allow_origin
instance-attribute
¶
preflight_explicit_allow_origin = preflight_explicit_allow_origin
validate_origin
¶
validate_origin(origin)
Validate if the origin is allowed.
PARAMETER | DESCRIPTION |
---|---|
origin
|
TYPE:
|
PARAMETER | DESCRIPTION |
---|---|
origin
|
Origin header value.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
bool
|
True if the origin is allowed, False otherwise.
TYPE:
|
Source code in lilya/middleware/cors.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
|
preflight_response
¶
preflight_response(request_headers)
Generate a preflight response.
PARAMETER | DESCRIPTION |
---|---|
request_headers
|
TYPE:
|
PARAMETER | DESCRIPTION |
---|---|
request_headers
|
Header from the incoming preflight request.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Response
|
Preflight response.
TYPE:
|
Source code in lilya/middleware/cors.py
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
|
preflight_private_network_response
¶
preflight_private_network_response(request_headers)
Process the preflight request for private network access. This feature is not part of the CORS specification but is useful for the new browsers that enforce the private network access policy.
By the time of this implementation, this specific functionality got inspired by great developers thinking about the future of the web and the security of the users.
More information about the private network access policy can be found at:
1. https://developer.chrome.com/blog/private-network-access-preflight/
2. https://documentation.alphasoftware.com/documentation/pages/Guides/Mobile%20and%20Web%20Components/UX/Properties/Advanced/CORS%20allow%20private%20network.xml#:~:text=Enables%20cross%20origin%20requests%20from,(e.g.%20behind%20a%20firewall).
This will be rolled out on Chromium based browsers such as Google Chrome, Microsoft Edge, Brave and others.
PARAMETER | DESCRIPTION |
---|---|
request_headers
|
TYPE:
|
PARAMETER | DESCRIPTION |
---|---|
request_headers
|
The headers of the request.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Response
|
The response to the preflight request.
TYPE:
|
Source code in lilya/middleware/cors.py
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
|
simple_response
async
¶
simple_response(scope, receive, send, request_headers)
Handle the simple response.
PARAMETER | DESCRIPTION |
---|---|
scope
|
TYPE:
|
receive
|
TYPE:
|
send
|
TYPE:
|
request_headers
|
TYPE:
|
PARAMETER | DESCRIPTION |
---|---|
scope
|
ASGI scope.
TYPE:
|
receive
|
ASGI receive channel.
TYPE:
|
send
|
ASGI send channel.
TYPE:
|
request_headers
|
Header from the incoming request.
TYPE:
|
Source code in lilya/middleware/cors.py
216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
|
send
async
¶
send(message, send, request_headers)
Send the message and apply CORS headers.
PARAMETER | DESCRIPTION |
---|---|
message
|
TYPE:
|
send
|
TYPE:
|
request_headers
|
TYPE:
|
PARAMETER | DESCRIPTION |
---|---|
message
|
ASGI message.
TYPE:
|
send
|
ASGI send channel.
TYPE:
|
request_headers
|
Header from the incoming request.
TYPE:
|
Source code in lilya/middleware/cors.py
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
|
set_explicit_origin
staticmethod
¶
set_explicit_origin(headers, origin)
Set explicit origin in headers.
PARAMETER | DESCRIPTION |
---|---|
headers
|
TYPE:
|
origin
|
TYPE:
|
PARAMETER | DESCRIPTION |
---|---|
headers
|
MutableHeaders instance.
TYPE:
|
origin
|
Origin header value.
TYPE:
|
Source code in lilya/middleware/cors.py
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
|
get_simple_headers
staticmethod
¶
get_simple_headers(allow_all_origins, allow_credentials, expose_headers, allow_private_networks)
Get headers for simple (non-preflight) responses.
PARAMETER | DESCRIPTION |
---|---|
allow_all_origins
|
TYPE:
|
allow_credentials
|
TYPE:
|
expose_headers
|
TYPE:
|
allow_private_networks
|
TYPE:
|
PARAMETER | DESCRIPTION |
---|---|
allow_all_origins
|
Whether all origins are allowed.
TYPE:
|
allow_credentials
|
Whether credentials such as cookies are allowed.
TYPE:
|
expose_headers
|
List of headers exposed to the browser.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
dict
|
Dictionary of simple response headers.
TYPE:
|
Source code in lilya/middleware/cors.py
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
|
get_preflight_headers
staticmethod
¶
get_preflight_headers(allow_all_origins, allow_methods, max_age, allow_all_headers, allow_headers, allow_credentials, allow_private_networks)
Get headers for preflight responses.
PARAMETER | DESCRIPTION |
---|---|
allow_all_origins
|
TYPE:
|
allow_methods
|
TYPE:
|
max_age
|
TYPE:
|
allow_all_headers
|
TYPE:
|
allow_headers
|
TYPE:
|
allow_credentials
|
TYPE:
|
allow_private_networks
|
TYPE:
|
PARAMETER | DESCRIPTION |
---|---|
allow_all_origins
|
Whether all origins are allowed.
TYPE:
|
allow_methods
|
List of allowed HTTP methods.
TYPE:
|
max_age
|
Maximum age (in seconds) for caching preflight requests.
TYPE:
|
allow_all_headers
|
Whether all headers are allowed.
TYPE:
|
allow_headers
|
List of allowed HTTP headers.
TYPE:
|
allow_credentials
|
Whether credentials such as cookies are allowed.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
dict
|
Dictionary of preflight response headers.
TYPE:
|
Source code in lilya/middleware/cors.py
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
|
esmerald.middleware.RequestSettingsMiddleware
¶
RequestSettingsMiddleware(app)
Bases: MiddlewareProtocol
Settings Middleware class.
PARAMETER | DESCRIPTION |
---|---|
app
|
TYPE:
|
PARAMETER | DESCRIPTION |
---|---|
app
|
The 'next' ASGI app to call.
TYPE:
|
Source code in esmerald/middleware/settings_middleware.py
8 9 10 11 12 13 14 15 |
|
esmerald.middleware.SessionMiddleware
¶
SessionMiddleware(app, secret_key, session_cookie='session', max_age=14 * 24 * 60 * 60, path='/', same_site='lax', https_only=False, domain=None)
Bases: MiddlewareProtocol
Middleware for handling session data in ASGI applications.
PARAMETER | DESCRIPTION |
---|---|
app
|
TYPE:
|
secret_key
|
TYPE:
|
session_cookie
|
TYPE:
|
max_age
|
TYPE:
|
path
|
TYPE:
|
same_site
|
TYPE:
|
https_only
|
TYPE:
|
domain
|
TYPE:
|
PARAMETER | DESCRIPTION |
---|---|
app
|
The ASGI application to wrap.
TYPE:
|
secret_key
|
The secret key used for signing session data.
TYPE:
|
session_cookie
|
The name of the session cookie.
TYPE:
|
max_age
|
The maximum age of the session in seconds (default is 14 days).
TYPE:
|
path
|
The path attribute for the session cookie.
TYPE:
|
same_site
|
The SameSite attribute for the session cookie.
TYPE:
|
https_only
|
If True, set the secure flag for the session cookie (HTTPS only).
TYPE:
|
domain
|
The domain attribute for the session cookie.
TYPE:
|
Source code in lilya/middleware/sessions.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
|
load_session_data
async
¶
load_session_data(scope, connection)
Load session data from the session cookie.
PARAMETER | DESCRIPTION |
---|---|
scope
|
TYPE:
|
connection
|
TYPE:
|
PARAMETER | DESCRIPTION |
---|---|
scope
|
ASGI scope.
TYPE:
|
connection
|
HTTP connection object.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
bool
|
True if the initial session was empty, False otherwise.
TYPE:
|
Source code in lilya/middleware/sessions.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
|
process_response
async
¶
process_response(message, scope, initial_session_was_empty, send)
Process the response and set the session cookie.
PARAMETER | DESCRIPTION |
---|---|
message
|
TYPE:
|
scope
|
TYPE:
|
initial_session_was_empty
|
TYPE:
|
send
|
TYPE:
|
PARAMETER | DESCRIPTION |
---|---|
message
|
ASGI message.
TYPE:
|
scope
|
ASGI scope.
TYPE:
|
initial_session_was_empty
|
True if the initial session was empty, False otherwise.
TYPE:
|
send
|
ASGI send channel.
TYPE:
|
Source code in lilya/middleware/sessions.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
|
set_session_cookie
async
¶
set_session_cookie(scope, message)
Set the session cookie in the response headers.
PARAMETER | DESCRIPTION |
---|---|
scope
|
TYPE:
|
message
|
TYPE:
|
PARAMETER | DESCRIPTION |
---|---|
scope
|
ASGI scope.
TYPE:
|
message
|
ASGI message
TYPE:
|
Source code in lilya/middleware/sessions.py
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
|
clear_session_cookie
async
¶
clear_session_cookie(scope, message)
Clear the session cookie in the response headers.
PARAMETER | DESCRIPTION |
---|---|
scope
|
TYPE:
|
message
|
TYPE:
|
PARAMETER | DESCRIPTION |
---|---|
scope
|
ASGI scope.
TYPE:
|
Source code in lilya/middleware/sessions.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
|
esmerald.middleware.HTTPSRedirectMiddleware
¶
HTTPSRedirectMiddleware(app)
esmerald.middleware.TrustedHostMiddleware
¶
TrustedHostMiddleware(app, allowed_hosts=None, www_redirect=True)
Bases: MiddlewareProtocol
Middleware for enforcing trusted host headers in incoming requests.
PARAMETER | DESCRIPTION |
---|---|
app
|
TYPE:
|
allowed_hosts
|
TYPE:
|
www_redirect
|
TYPE:
|
PARAMETER | DESCRIPTION |
---|---|
app
|
The ASGI application to wrap.
TYPE:
|
allowed_hosts
|
List of allowed host patterns. Defaults to ["*"].
TYPE:
|
www_redirect
|
Whether to redirect requests with missing 'www.' to 'www.' prefixed URLs.
TYPE:
|
Source code in lilya/middleware/trustedhost.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
|
validate_host
¶
validate_host(host)
Validate the host header against the allowed host patterns.
PARAMETER | DESCRIPTION |
---|---|
host
|
TYPE:
|
PARAMETER | DESCRIPTION |
---|---|
host
|
Host header value.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
tuple[bool, bool]
|
Tuple[bool, bool]: (is_valid_host, found_www_redirect). |
Source code in lilya/middleware/trustedhost.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
|
handle_invalid_host
async
¶
handle_invalid_host(scope, receive, send, found_www_redirect)
Handle requests with invalid host headers.
PARAMETER | DESCRIPTION |
---|---|
scope
|
TYPE:
|
receive
|
TYPE:
|
send
|
TYPE:
|
found_www_redirect
|
TYPE:
|
PARAMETER | DESCRIPTION |
---|---|
scope
|
ASGI scope.
TYPE:
|
receive
|
ASGI receive channel.
TYPE:
|
send
|
ASGI send channel.
TYPE:
|
found_www_redirect
|
Whether 'www.' redirect was found.
TYPE:
|
Source code in lilya/middleware/trustedhost.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
|
esmerald.middleware.GZipMiddleware
¶
GZipMiddleware(app, minimum_size=500, compresslevel=9)
Bases: MiddlewareProtocol
Middleware to compress responses with GZip.
PARAMETER | DESCRIPTION |
---|---|
app
|
The 'next' ASGI app to call.
TYPE:
|
minimum_size
|
Minimum response size to trigger compression.
TYPE:
|
compresslevel
|
GZip compression level (0 to 9).
TYPE:
|
Initialize GZipMiddleware.
PARAMETER | DESCRIPTION |
---|---|
app
|
TYPE:
|
minimum_size
|
TYPE:
|
compresslevel
|
TYPE:
|
PARAMETER | DESCRIPTION |
---|---|
app
|
The 'next' ASGI app to call.
TYPE:
|
minimum_size
|
Minimum response size to trigger compression.
TYPE:
|
compresslevel
|
GZip compression level (0 to 9).
TYPE:
|
Source code in lilya/middleware/compression.py
22 23 24 25 26 27 28 29 30 31 32 33 |
|
esmerald.middleware.wsgi.WSGIMiddleware
¶
WSGIMiddleware(app, workers=10)
Bases: WSGIMiddleware
PARAMETER | DESCRIPTION |
---|---|
app
|
TYPE:
|
workers
|
TYPE:
|
Source code in lilya/middleware/wsgi.py
18 19 20 21 |
|
executor
instance-attribute
¶
executor = ThreadPoolExecutor(thread_name_prefix='WSGI', max_workers=workers)