[−][src]Struct roa::Context
A structure to share request, response and other data between middlewares.
Type of the first parameter in a middleware.
Example
use roa_core::App; use log::info; use async_std::fs::File; let mut app = App::new(()); app.gate_fn(|ctx, next| async move { info!("{} {}", ctx.method(), ctx.uri()); next.await }); app.end(|mut ctx| async move { ctx.resp_mut().write_reader(File::open("assets/welcome.html").await?); Ok(()) });
Methods
impl<S> Context<S>
[src]
pub fn req(&self) -> &Request
[src]
Get an immutable reference of request.
Example
use roa_core::App; use roa_core::http::Method; let mut app = App::new(()); app.end(|ctx| async move { assert_eq!(Method::GET, ctx.req().method); Ok(()) });
pub fn resp(&self) -> &Response
[src]
Get an immutable reference of response.
Example
use roa_core::App; use roa_core::http::StatusCode; let mut app = App::new(()); app.end(|ctx| async move { assert_eq!(StatusCode::OK, ctx.resp().status); Ok(()) });
pub fn req_mut(&mut self) -> &mut Request
[src]
Get a mutable reference of request.
Example
use roa_core::App; use roa_core::http::Method; let mut app = App::new(()); app.gate_fn(|mut ctx, next| async move { ctx.req_mut().method = Method::POST; next.await }); app.end(|ctx| async move { assert_eq!(Method::POST, ctx.req().method); Ok(()) });
pub fn resp_mut(&mut self) -> &mut Response
[src]
Get a mutable reference of response.
Example
use roa_core::App; let mut app = App::new(()); app.end(|mut ctx| async move { ctx.resp_mut().write("Hello, World!"); Ok(()) });
pub fn uri(&self) -> &Uri
[src]
Clone URI.
Example
use roa_core::App; let mut app = App::new(()); app.end(|ctx| async move { assert_eq!("/", ctx.uri().to_string()); Ok(()) });
pub fn method(&self) -> &Method
[src]
Clone request::method.
Example
use roa_core::App; use http::Method; let mut app = App::new(()); app.end(|ctx| async move { assert_eq!(Method::GET, ctx.method()); Ok(()) });
pub fn header(
&self,
name: impl AsHeaderName
) -> Option<Result<&str, ToStrError>>
[src]
&self,
name: impl AsHeaderName
) -> Option<Result<&str, ToStrError>>
Search for a header value and try to get its string copy.
Example
use roa_core::App; use roa_core::http::{StatusCode, header}; let mut app = App::new(()); app.end(|ctx| async move { assert_eq!( "text/plain", ctx.header(&header::CONTENT_TYPE).unwrap().unwrap() ); Ok(()) });
pub fn status(&self) -> StatusCode
[src]
Clone response::status.
Example
use roa_core::{App, http::StatusCode}; let mut app = App::new(()); app.end(|ctx| async move { assert_eq!(StatusCode::OK, ctx.status()); Ok(()) });
pub fn version(&self) -> Version
[src]
Clone request::version.
Example
use roa_core::App; use roa_core::http::{StatusCode, Version}; let mut app = App::new(()); app.end(|ctx| async move { assert_eq!(Version::HTTP_11, ctx.version()); Ok(()) });
Methods from Deref<Target = SyncContext<S>>
pub fn store_scoped<SC, T>(
&mut self,
_scope: SC,
name: &'a str,
value: T
) -> Option<Variable<'a, T>> where
SC: Any,
T: Any + Send + Sync,
[src]
&mut self,
_scope: SC,
name: &'a str,
value: T
) -> Option<Variable<'a, T>> where
SC: Any,
T: Any + Send + Sync,
Store key-value pair in specific scope.
Example
use roa_core::App; use roa_core::http::{StatusCode, Method}; struct Scope; struct AnotherScope; let mut app = App::new(()); app.gate_fn(|mut ctx, next| async move { ctx.store_scoped(Scope, "id", "1".to_string()); next.await }); app.end(|ctx| async move { assert_eq!(1, ctx.load_scoped::<Scope, String>("id").unwrap().parse::<i32>()?); assert!(ctx.load_scoped::<AnotherScope, String>("id").is_none()); Ok(()) });
pub fn store<T>(&mut self, name: &'a str, value: T) -> Option<Variable<'a, T>> where
T: Any + Send + Sync,
[src]
T: Any + Send + Sync,
Store key-value pair in public scope.
Example
use roa_core::App; use roa_core::http::{StatusCode, Method}; let mut app = App::new(()); app.gate_fn(|mut ctx, next| async move { ctx.store("id", "1".to_string()); next.await }); app.end(|ctx| async move { assert_eq!(1, ctx.load::<String>("id").unwrap().parse::<i32>()?); Ok(()) });
pub fn load_scoped<SC, T>(&self, name: &'a str) -> Option<Variable<'a, T>> where
SC: Any,
T: Any + Send + Sync,
[src]
SC: Any,
T: Any + Send + Sync,
Search for value by key in specific scope.
Example
use roa_core::App; struct Scope; let mut app = App::new(()); app.gate_fn(|mut ctx, next| async move { ctx.store_scoped(Scope, "id", "1".to_owned()); next.await }); app.end(|ctx| async move { assert_eq!(1, ctx.load_scoped::<Scope, String>("id").unwrap().parse::<i32>()?); Ok(()) });
pub fn load<T>(&self, name: &'a str) -> Option<Variable<'a, T>> where
T: Any + Send + Sync,
[src]
T: Any + Send + Sync,
Search for value by key in public scope.
Example
use roa_core::App; let mut app = App::new(()); app.gate_fn(|mut ctx, next| async move { ctx.store("id", "1".to_string()); next.await }); app.end(|ctx| async move { assert_eq!(1, ctx.load::<String>("id").unwrap().parse::<i32>()?); Ok(()) });
Trait Implementations
impl<S: State> CookieSetter for Context<S>
[src]
fn set_cookie(&mut self, cookie: Cookie) -> Result
[src]
impl<S> Deref for Context<S>
[src]
type Target = SyncContext<S>
The resulting type after dereferencing.
fn deref(&self) -> &<Context<S> as Deref>::Target
[src]
impl<S> DerefMut for Context<S>
[src]
impl<S: State> Forward for Context<S>
[src]
fn host(&self) -> Result<String>
[src]
fn client_ip(&self) -> IpAddr
[src]
fn forwarded_ips(&self) -> Vec<IpAddr>
[src]
fn forwarded_proto(&self) -> Option<Result<String>>
[src]
impl<S> PowerBody for Context<S> where
S: State,
S: State,
fn body<'life0, 'async_trait>(
&'life0 mut self
) -> Pin<Box<dyn Future<Output = Result<Bytes, Error>> + 'async_trait>> where
'life0: 'async_trait,
Context<S>: 'async_trait,
&'life0 mut self
) -> Pin<Box<dyn Future<Output = Result<Bytes, Error>> + 'async_trait>> where
'life0: 'async_trait,
Context<S>: 'async_trait,
fn read_json<'life0, 'async_trait, B>(
&'life0 mut self
) -> Pin<Box<dyn Future<Output = Result<B, Error>> + 'async_trait>> where
'life0: 'async_trait,
B: DeserializeOwned + 'async_trait,
Context<S>: 'async_trait,
&'life0 mut self
) -> Pin<Box<dyn Future<Output = Result<B, Error>> + 'async_trait>> where
'life0: 'async_trait,
B: DeserializeOwned + 'async_trait,
Context<S>: 'async_trait,
fn read_form<'life0, 'async_trait, B>(
&'life0 mut self
) -> Pin<Box<dyn Future<Output = Result<B, Error>> + 'async_trait>> where
'life0: 'async_trait,
B: DeserializeOwned + 'async_trait,
Context<S>: 'async_trait,
&'life0 mut self
) -> Pin<Box<dyn Future<Output = Result<B, Error>> + 'async_trait>> where
'life0: 'async_trait,
B: DeserializeOwned + 'async_trait,
Context<S>: 'async_trait,
fn write_json<B>(&mut self, data: &B) -> Result<(), Error> where
B: Serialize,
B: Serialize,
fn render<B>(&mut self, data: &B) -> Result<(), Error> where
B: Template,
B: Template,
fn write_text<B>(&mut self, data: B) -> Result<(), Error> where
B: Into<Bytes>,
B: Into<Bytes>,
fn write_octet<B>(&mut self, reader: B) -> Result<(), Error> where
B: 'static + Send + Sync + Unpin + AsyncRead,
B: 'static + Send + Sync + Unpin + AsyncRead,
fn write_file<'life0, 'async_trait, P>(
&'life0 mut self,
path: P,
typ: DispositionType
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + 'async_trait>> where
'life0: 'async_trait,
P: AsRef<Path> + 'async_trait,
Context<S>: 'async_trait,
&'life0 mut self,
path: P,
typ: DispositionType
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + 'async_trait>> where
'life0: 'async_trait,
P: AsRef<Path> + 'async_trait,
Context<S>: 'async_trait,
impl<S> RouterParam for Context<S> where
S: State,
S: State,
Auto Trait Implementations
impl<S> !RefUnwindSafe for Context<S>
impl<S> !Send for Context<S>
impl<S> !Sync for Context<S>
impl<S> Unpin for Context<S>
impl<S> !UnwindSafe for Context<S>
Blanket Implementations
impl<T, A, P> Access<T> for P where
A: Access<T>,
P: Deref<Target = A>,
[src]
A: Access<T>,
P: Deref<Target = A>,
type Guard = <A as Access<T>>::Guard
A guard object containing the value and keeping it alive. Read more
fn load(&self) -> <P as Access<T>>::Guard
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T, A> DynAccess<T> for A where
A: Access<T>,
<A as Access<T>>::Guard: 'static,
[src]
A: Access<T>,
<A as Access<T>>::Guard: 'static,
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> Same<T> for T
type Output = T
Should always be Self
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,