server_side¶
- members:
- class litestar.middleware.session.server_side.ServerSideSessionBackend[原始碼]¶
基底類別:
BaseSessionBackend[ServerSideSessionConfig]Base class for server-side backends.
Implements
BaseSessionBackendand defines and interface which subclasses can implement to facilitate the storage of session data.- __init__(config: ServerSideSessionConfig) None[原始碼]¶
初始化
ServerSideSessionBackend- 參數:
config¶ -- A subclass of
ServerSideSessionConfig
- async get(session_id: str, store: Store) bytes | None[原始碼]¶
Retrieve data associated with
session_id.
- async set(session_id: str, data: bytes, store: Store) None[原始碼]¶
Store
dataunder thesession_idfor later retrieval.If there is already data associated with
session_id, replace it withdataand reset its expiry time
- async delete(session_id: str, store: Store) None[原始碼]¶
Delete the data associated with
session_id. Fails silently if no such session-ID exists.
- get_session_id(connection: ASGIConnection) str[原始碼]¶
Try to fetch session id from the connection. If one does not exist, generate one.
If a session ID already exists in the cookies, it is returned. If there is no ID in the cookies but one in the connection state, then the session exists but has not yet been returned to the user. Otherwise, a new session must be created.
- 參數:
connection¶ -- Originating ASGIConnection containing the scope
- 回傳:
Session id str or None if the concept of a session id does not apply.
- generate_session_id() str[原始碼]¶
Generate a new session-ID, with n=:attr:session_id_bytes <ServerSideSessionConfig.session_id_bytes> random bytes.
- 回傳:
A session-ID
- async store_in_message(scope_session: ScopeSession, message: Message, connection: ASGIConnection) None[原始碼]¶
Store the necessary information in the outgoing
Messageby setting a cookie containing the session-ID.If the session is empty, a null-cookie will be set. Otherwise, the serialised data will be stored using
set, under the current session-id. If no session-ID exists, a new ID will be generated usinggenerate_session_id.
- async load_from_connection(connection: ASGIConnection) dict[str, Any][原始碼]¶
Load session data from a connection and return it as a dictionary to be used in the current application scope.
The session-ID will be gathered from a cookie with the key set in
BaseBackendConfig.key. If a cookie is found, its value will be used as the session-ID and data associated with this ID will be loaded usingget. If no cookie was found or no data was loaded from the store, this will return an empty dictionary.- 參數:
connection¶ -- An ASGIConnection instance
- 回傳:
The current session data
- class litestar.middleware.session.server_side.ServerSideSessionConfig[原始碼]¶
基底類別:
BaseBackendConfig[ServerSideSessionBackend]Base configuration for server side backends.
- key: str = 'session'¶
Key to use for the cookie inside the header, e.g.
session=<data>wheresessionis the cookie key and<data>is the session data.備註
If a session cookie exceeds 4KB in size it is split. In this case the key will be of the format
session-{segment number}.
- path: str = '/'¶
Path fragment that must exist in the request url for the cookie to be valid.
Defaults to
'/'.
- __init__(session_id_bytes: int = 32, renew_on_access: bool = False, key: str = 'session', max_age: int = 1209600, scopes: set[~typing.Literal[ScopeType.HTTP, ScopeType.WEBSOCKET]] = <factory>, path: str = '/', domain: str | None = None, secure: bool = False, httponly: bool = True, samesite: ~typing.Literal['lax', 'strict', 'none'] = 'lax', exclude: str | list[str] | None = None, exclude_opt_key: str = 'skip_session', store: str = 'sessions') None¶
- samesite: Literal['lax', 'strict', 'none'] = 'lax'¶
Controls whether or not a cookie is sent with cross-site requests. Defaults to
lax.
- exclude: str | list[str] | None = None¶
A pattern or list of patterns to skip in the session middleware.