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
impl Context
Sourcepub fn path_prefix(&self) -> Option<&str>
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.
pub fn method(&self) -> &Method
pub fn uri(&self) -> &Uri
pub fn path(&self) -> &str
pub fn headers(&self) -> &HeaderMap
pub fn header(&self, name: impl AsHeaderName) -> Option<&str>
Sourcepub fn remote_ip(&self) -> Option<String>
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).
Sourcepub fn path_params(&self) -> &PathParams
pub fn path_params(&self) -> &PathParams
Parameters captured by the route pattern, e.g. id in /posts/:id.
Sourcepub fn set_access(&mut self, access: impl Access)
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.
Sourcepub fn access(&self) -> Result<RequestAccess>
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.
Sourcepub fn params(&self) -> Params
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.
pub fn param(&self, name: &str) -> Option<&str>
Sourcepub fn id(&self) -> Result<i64>
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.
Sourcepub fn param_as<T: FromStr>(&self, name: &str) -> Result<T>
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.
Sourcepub fn query<T: DeserializeOwned>(&self) -> Result<T>
pub fn query<T: DeserializeOwned>(&self) -> Result<T>
Deserializes the query string.
Sourcepub fn json<T: DeserializeOwned>(&self) -> Result<T>
pub fn json<T: DeserializeOwned>(&self) -> Result<T>
Deserializes a JSON request body.
Sourcepub fn form<T: DeserializeOwned>(&self) -> Result<T>
pub fn form<T: DeserializeOwned>(&self) -> Result<T>
Deserializes a application/x-www-form-urlencoded request body.
Sourcepub fn files(&self) -> Vec<(String, Upload)>
pub fn files(&self) -> Vec<(String, Upload)>
The files of a multipart/form-data request, by field name, in
the order sent.
Sourcepub fn file(&self, name: &str) -> Option<Upload>
pub fn file(&self, name: &str) -> Option<Upload>
The file uploaded as name (comment[note_image]), as Rails’
params[:comment][:note_image].
Sourcepub fn form_field(&self, name: &str) -> Option<String>
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.
pub fn body(&self) -> &Bytes
Sourcepub fn state<T: Send + Sync + 'static>(&self) -> Result<&T>
pub fn state<T: Send + Sync + 'static>(&self) -> Result<&T>
Application-wide state registered with App::state.
pub fn session(&self) -> &Session
Sourcepub fn flash(&self, kind: &str) -> Option<String>
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.
Sourcepub fn flash_now(&self, kind: &str, message: impl Into<String>)
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] = ….
Sourcepub fn put_flash(&self, kind: &str, message: impl Into<String>)
pub fn put_flash(&self, kind: &str, message: impl Into<String>)
Sets a flash message for the next request.
Sourcepub fn base_url(&self) -> String
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.
Sourcepub fn csrf_token(&self) -> String
pub fn csrf_token(&self) -> String
The CSRF token for this session, for embedding in forms.
Sourcepub fn extensions(&self) -> &Extensions
pub fn extensions(&self) -> &Extensions
Per-request data, e.g. a current user set by an auth middleware.
pub fn extensions_mut(&mut self) -> &mut Extensions
Auto Trait Implementations§
impl !Freeze for Context
impl !RefUnwindSafe for Context
impl !UnwindSafe for Context
impl Send for Context
impl Sync for Context
impl Unpin for Context
impl UnsafeUnpin for Context
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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