Middlewares¶
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, 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:
|
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 .venv/lib/python3.8/site-packages/lilya/middleware/cors.py
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 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 |
|
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 .venv/lib/python3.8/site-packages/lilya/middleware/cors.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
|
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 .venv/lib/python3.8/site-packages/lilya/middleware/cors.py
126 127 128 129 130 131 132 133 134 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 |
|
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 .venv/lib/python3.8/site-packages/lilya/middleware/cors.py
166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
|
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 .venv/lib/python3.8/site-packages/lilya/middleware/cors.py
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 |
|
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 .venv/lib/python3.8/site-packages/lilya/middleware/cors.py
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
|
get_simple_headers
staticmethod
¶
get_simple_headers(allow_all_origins, allow_credentials, expose_headers)
Get headers for simple (non-preflight) responses.
PARAMETER | DESCRIPTION |
---|---|
allow_all_origins |
TYPE:
|
allow_credentials |
TYPE:
|
expose_headers |
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 .venv/lib/python3.8/site-packages/lilya/middleware/cors.py
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
|
get_preflight_headers
staticmethod
¶
get_preflight_headers(allow_all_origins, allow_methods, max_age, allow_all_headers, allow_headers, allow_credentials)
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:
|
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 .venv/lib/python3.8/site-packages/lilya/middleware/cors.py
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
|
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 .venv/lib/python3.8/site-packages/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 .venv/lib/python3.8/site-packages/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 .venv/lib/python3.8/site-packages/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 .venv/lib/python3.8/site-packages/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 .venv/lib/python3.8/site-packages/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 .venv/lib/python3.8/site-packages/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 .venv/lib/python3.8/site-packages/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 .venv/lib/python3.8/site-packages/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 .venv/lib/python3.8/site-packages/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 .venv/lib/python3.8/site-packages/lilya/middleware/wsgi.py
18 19 20 21 |
|
executor
instance-attribute
¶
executor = ThreadPoolExecutor(thread_name_prefix='WSGI', max_workers=workers)