Skip to main content

Context

Struct Context 

Source
pub struct Context { /* private fields */ }
Expand description

Everything known about the current request.

The body is fully buffered before the handler runs (bounded by App::max_body_size), so the parsing helpers are synchronous.

Implementations§

Source§

impl Context

Source

pub fn path_prefix(&self) -> Option<&str>

The id at the front of the request’s path (42 in /42/projects), when IdPrefix took one off.

Source

pub fn method(&self) -> &Method

Source

pub fn uri(&self) -> &Uri

Source

pub fn path(&self) -> &str

Source

pub fn headers(&self) -> &HeaderMap

Source

pub fn header(&self, name: impl AsHeaderName) -> Option<&str>

Source

pub fn remote_ip(&self) -> Option<String>

The client’s IP address, as Rails’ request.remote_ip: the connection’s, unless it came through trusted proxies (loopback and private addresses, and the config’s trusted_proxies), in which case the nearest untrusted address in X-Forwarded-For. None when there was no connection (in tests).

Source

pub fn path_params(&self) -> &PathParams

Parameters captured by the route pattern, e.g. id in /posts/:id.

Source

pub fn set_access(&mut self, access: impl Access)

Sets who the request is for, from middleware (who’s signed in, and in which org): what access is in an action, and what Post::for(access) limits queries to.

Source

pub fn access(&self) -> Result<RequestAccess>

Who the request is for, as its middleware set it: access in a .glacier action. 401 Unauthorized when nothing set one (no one is signed in).

§Errors

401 Unauthorized without an access.

Source

pub fn params(&self) -> Params

All request parameters as text: path params, the query string and the body (form-encoded or a JSON object), later sources winning, as in Rails’ params. Pass them to Post::create(..) or post.update(..), which cast them into typed fields.

Source

pub fn param(&self, name: &str) -> Option<&str>

Source

pub fn id(&self) -> Result<i64>

The :id path parameter as an i64. A missing or malformed id is a 404 Not Found, as no record could have it.

Source

pub fn param_as<T: FromStr>(&self, name: &str) -> Result<T>

Parses a path parameter, responding 400 Bad Request if it is missing or malformed.

Source

pub fn query<T: DeserializeOwned>(&self) -> Result<T>

Deserializes the query string.

Source

pub fn json<T: DeserializeOwned>(&self) -> Result<T>

Deserializes a JSON request body.

Source

pub fn form<T: DeserializeOwned>(&self) -> Result<T>

Deserializes a application/x-www-form-urlencoded request body.

Source

pub fn files(&self) -> Vec<(String, Upload)>

The files of a multipart/form-data request, by field name, in the order sent.

Source

pub fn file(&self, name: &str) -> Option<Upload>

The file uploaded as name (comment[note_image]), as Rails’ params[:comment][:note_image].

Source

pub fn form_field(&self, name: &str) -> Option<String>

A single text field from a form’s body, URL-encoded or multipart/form-data (a form with a file input), without deserializing the whole thing: the CSRF token and _method are read this way, so upload forms pass them too.

Source

pub fn body(&self) -> &Bytes

Source

pub fn state<T: Send + Sync + 'static>(&self) -> Result<&T>

Application-wide state registered with App::state.

Source

pub fn cookies(&self) -> &Cookies

Source

pub fn session(&self) -> &Session

Source

pub fn flash(&self, kind: &str) -> Option<String>

A flash message set by an earlier request (e.g. "notice"), or by flash_now. Reading the flash spends it.

Source

pub fn flash_now(&self, kind: &str, message: impl Into<String>)

A flash message for the page this request renders, as Rails’ flash.now[:alert] = ….

Source

pub fn put_flash(&self, kind: &str, message: impl Into<String>)

Sets a flash message for the next request.

Source

pub fn base_url(&self) -> String

The URL the request came to, without its path: https://example.com, from X-Forwarded-Proto and Host behind a proxy.

Source

pub fn csrf_token(&self) -> String

The CSRF token for this session, for embedding in forms.

Source

pub fn extensions(&self) -> &Extensions

Per-request data, e.g. a current user set by an auth middleware.

Source

pub fn extensions_mut(&mut self) -> &mut Extensions

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> IntoField<Option<T>> for T

Source§

fn into_field(self) -> Option<T>

Source§

impl<T> IntoField<T> for T

Source§

fn into_field(self) -> T

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

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
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more