Struct gotham::middleware::session::NewSessionMiddleware
source · [−]pub struct NewSessionMiddleware<B, T> where
B: NewBackend,
T: Default + Serialize + for<'de> Deserialize<'de> + Send + 'static, { /* private fields */ }
Expand description
Added to a Pipeline
, this spawns the per-request SessionMiddleware
There are two ways to construct the NewSessionMiddleware
, but with_session_type
must be
called before the middleware is useful:
-
Using the
Default
implementation, which sets up an in-memory session store. When constructed this way, sessions are unable to be shared between multiple application servers, and are lost on restart:NewSessionMiddleware::default()
-
Using the
NewSessionMiddleware::new
function, and providing a backend. TheDefault
implementation usesMemoryBackend
, but this can be changed by providing your own:NewSessionMiddleware::new(MemoryBackend::default())
Before the middleware can be used, it must be associated with a session type provided by the application. This gives type-safe storage for all session data:
#[derive(Default, Serialize, Deserialize)]
struct MySessionType {
items: Vec<String>,
}
NewSessionMiddleware::default().with_session_type::<MySessionType>()
For plaintext HTTP servers, the insecure
method must also be called to instruct the
middleware not to set the secure
flag on the cookie.
Implementations
sourceimpl<B> NewSessionMiddleware<B, ()> where
B: NewBackend,
impl<B> NewSessionMiddleware<B, ()> where
B: NewBackend,
sourcepub fn new(b: B) -> NewSessionMiddleware<B, ()>
pub fn new(b: B) -> NewSessionMiddleware<B, ()>
Create a NewSessionMiddleware
value for the provided backend and with a blank session
type. with_session_type
must be called before the result is useful.
sourceimpl<B, T> NewSessionMiddleware<B, T> where
B: NewBackend,
T: Default + Serialize + for<'de> Deserialize<'de> + Send + 'static,
impl<B, T> NewSessionMiddleware<B, T> where
B: NewBackend,
T: Default + Serialize + for<'de> Deserialize<'de> + Send + 'static,
Configures the session cookie to be set at a more restrictive path.
NewSessionMiddleware::default()
.with_session_type::<MySessionType>()
.with_cookie_path("/app".to_string())
sourcepub fn insecure(self) -> NewSessionMiddleware<B, T>
pub fn insecure(self) -> NewSessionMiddleware<B, T>
Configures the NewSessionMiddleware
not to send the secure
flag along with the cookie.
This is required for plaintext HTTP connections.
NewSessionMiddleware::default()
.with_session_type::<MySessionType>()
.insecure()
Configures the NewSessionMiddleware
to use an alternate cookie name. The default cookie
name is _gotham_session
.
When a cookie name with a cookie prefix is used, the other options are forced to be correct (ignoring overridden settings from the application). i.e.:
- For a cookie prefix of
__Secure-
, the cookie attributes will includeSecure
- For a cookie prefix of
__Host-
, the cookie attributes will includeSecure; Path=/
and not includeDomain=
If the session cookie configuration set by the application does not match the prefix, a warning will be logged upon startup and the cookie prefix options will override what was provided by the application.
NewSessionMiddleware::default()
.with_session_type::<MySessionType>()
.with_cookie_name("_myapp_session")
Configures the NewSessionMiddleware
to use a Domain
attribute with the provided value.
NewSessionMiddleware::default()
.with_session_type::<MySessionType>()
.with_cookie_domain("example.com")
sourcepub fn allow_cross_site_usage(self) -> NewSessionMiddleware<B, T>
pub fn allow_cross_site_usage(self) -> NewSessionMiddleware<B, T>
Removes the SameSite
cookie attribute, allowing cross-site requests to include the cookie.
By default, the session cookie will be set with SameSite=lax
, which ensures cross-site
requests will include the cookie if and only if they are top-level navigations which use a
“safe” (in the RFC7231 sense) HTTP
method.
See: https://tools.ietf.org/html/draft-ietf-httpbis-cookie-same-site-00#section-4.1
NewSessionMiddleware::default()
.with_session_type::<MySessionType>()
.allow_cross_site_usage()
sourcepub fn with_strict_same_site_enforcement(self) -> NewSessionMiddleware<B, T>
pub fn with_strict_same_site_enforcement(self) -> NewSessionMiddleware<B, T>
Sets the “SameSite” cookie attribute value to “strict”.
This will ensure that the cookie is never sent for cross-site requests (including top-level navigations).
By default, the session cookie will be set with “SameSite=lax”, which ensures cross-site requests will include the cookie if and only if they are top-level navigations which use a “safe” (in the RFC7231 sense) HTTP method.
NewSessionMiddleware::default()
.with_session_type::<MySessionType>()
.with_strict_same_site_enforcement()
sourcepub fn with_session_type<U>(self) -> NewSessionMiddleware<B, U> where
U: Default + Serialize + for<'de> Deserialize<'de> + Send + 'static,
pub fn with_session_type<U>(self) -> NewSessionMiddleware<B, U> where
U: Default + Serialize + for<'de> Deserialize<'de> + Send + 'static,
Changes the session type to the provided type parameter. This is required to override the
default (unusable) session type of ()
.
#[derive(Default, Serialize, Deserialize)]
struct MySessionType {
items: Vec<String>,
}
NewSessionMiddleware::default().with_session_type::<MySessionType>()
Trait Implementations
sourceimpl<B, T> Clone for NewSessionMiddleware<B, T> where
B: NewBackend,
T: Default + Serialize + for<'de> Deserialize<'de> + Send + 'static,
impl<B, T> Clone for NewSessionMiddleware<B, T> where
B: NewBackend,
T: Default + Serialize + for<'de> Deserialize<'de> + Send + 'static,
sourceimpl Default for NewSessionMiddleware<MemoryBackend, ()>
impl Default for NewSessionMiddleware<MemoryBackend, ()>
sourcefn default() -> NewSessionMiddleware<MemoryBackend, ()>
fn default() -> NewSessionMiddleware<MemoryBackend, ()>
Returns the “default value” for a type. Read more
sourceimpl<B, T> NewMiddleware for NewSessionMiddleware<B, T> where
B: NewBackend,
T: Default + Serialize + for<'de> Deserialize<'de> + Send + 'static,
impl<B, T> NewMiddleware for NewSessionMiddleware<B, T> where
B: NewBackend,
T: Default + Serialize + for<'de> Deserialize<'de> + Send + 'static,
type Instance = SessionMiddleware<B::Instance, T>
type Instance = SessionMiddleware<B::Instance, T>
The type of Middleware
created by the NewMiddleware
.
sourcefn new_middleware(&self) -> Result<Self::Instance>
fn new_middleware(&self) -> Result<Self::Instance>
Create and return a new Middleware
value.
Auto Trait Implementations
impl<B, T> RefUnwindSafe for NewSessionMiddleware<B, T>
impl<B, T> Send for NewSessionMiddleware<B, T> where
B: Send,
impl<B, T> Sync for NewSessionMiddleware<B, T>
impl<B, T> !Unpin for NewSessionMiddleware<B, T>
impl<B, T> !UnwindSafe for NewSessionMiddleware<B, T>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcepub fn to_owned(&self) -> T
pub fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
sourcepub fn clone_into(&self, target: &mut T)
pub fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more