[−][src]Struct roa::Context
A structure to share request, response and other data between middlewares.
Example
use roa_core::{App, Context, Next, Result}; use log::info; use async_std::fs::File; let app = App::new().gate(gate).end(end); async fn gate(ctx: &mut Context, next: Next<'_>) -> Result { info!("{} {}", ctx.method(), ctx.uri()); next.await } async fn end(ctx: &mut Context) -> Result { ctx.resp.write_reader(File::open("assets/welcome.html").await?); Ok(()) }
Fields
req: Request
The request, to read http method, uri, version, headers and body.
resp: Response
The response, to set http status, version, headers and body.
exec: Executor
The executor, to spawn futures or blocking works.
remote_addr: SocketAddr
Socket addr of last client or proxy.
Methods
impl<S> Context<S>
[src]
pub fn uri(&self) -> &Uri
[src]
Clone URI.
Example
use roa_core::{App, Context, Result}; let app = App::new().end(get); async fn get(ctx: &mut Context) -> Result { assert_eq!("/", ctx.uri().to_string()); Ok(()) }
pub fn method(&self) -> &Method
[src]
Clone request::method.
Example
use roa_core::{App, Context, Result}; use roa_core::http::Method; let app = App::new().end(get); async fn get(ctx: &mut Context) -> Result { assert_eq!(Method::GET, ctx.method()); Ok(()) }
pub fn get(&self, name: impl AsHeaderName) -> Option<&str>
[src]
Search for a header value and try to get its string reference.
Example
use roa_core::{App, Context, Result}; use roa_core::http::header::CONTENT_TYPE; let app = App::new().end(get); async fn get(ctx: &mut Context) -> Result { assert_eq!( Some("text/plain"), ctx.get(CONTENT_TYPE), ); Ok(()) }
pub fn must_get(&self, name: impl AsHeaderName) -> Result<&str, Status>
[src]
Search for a header value and get its string reference.
Otherwise return a 400 BAD REQUEST.
Example
use roa_core::{App, Context, Result}; use roa_core::http::header::CONTENT_TYPE; let app = App::new().end(get); async fn get(ctx: &mut Context) -> Result { assert_eq!( "text/plain", ctx.must_get(CONTENT_TYPE)?, ); Ok(()) }
pub fn status(&self) -> StatusCode
[src]
Clone response::status.
Example
use roa_core::{App, Context, Result}; use roa_core::http::StatusCode; let app = App::new().end(get); async fn get(ctx: &mut Context) -> Result { assert_eq!(StatusCode::OK, ctx.status()); Ok(()) }
pub fn version(&self) -> Version
[src]
Clone request::version.
Example
use roa_core::{App, Context, Result}; use roa_core::http::Version; let app = App::new().end(get); async fn get(ctx: &mut Context) -> Result { assert_eq!(Version::HTTP_11, ctx.version()); Ok(()) }
pub fn store_scoped<SC, K, V>(
&mut self,
scope: SC,
key: K,
value: V
) -> Option<Arc<V>> where
K: Into<Cow<'static, str>>,
SC: Any,
V: Value,
[src]
&mut self,
scope: SC,
key: K,
value: V
) -> Option<Arc<V>> where
K: Into<Cow<'static, str>>,
SC: Any,
V: Value,
Store key-value pair in specific scope.
Example
use roa_core::{App, Context, Result, Next}; struct Scope; struct AnotherScope; async fn gate(ctx: &mut Context, next: Next<'_>) -> Result { ctx.store_scoped(Scope, "id", "1".to_string()); next.await } async fn end(ctx: &mut Context) -> Result { assert_eq!(1, ctx.load_scoped::<Scope, String>("id").unwrap().parse::<i32>()?); assert!(ctx.load_scoped::<AnotherScope, String>("id").is_none()); Ok(()) } let app = App::new().gate(gate).end(end);
pub fn store<K, V>(&mut self, key: K, value: V) -> Option<Arc<V>> where
K: Into<Cow<'static, str>>,
V: Value,
[src]
K: Into<Cow<'static, str>>,
V: Value,
Store key-value pair in public scope.
Example
use roa_core::{App, Context, Result, Next}; async fn gate(ctx: &mut Context, next: Next<'_>) -> Result { ctx.store("id", "1".to_string()); next.await } async fn end(ctx: &mut Context) -> Result { assert_eq!(1, ctx.load::<String>("id").unwrap().parse::<i32>()?); Ok(()) } let app = App::new().gate(gate).end(end);
pub fn load_scoped<SC, V>(&self, key: &'a str) -> Option<Variable<'a, V>> where
SC: Any,
V: Value,
[src]
SC: Any,
V: Value,
Search for value by key in specific scope.
Example
use roa_core::{App, Context, Result, Next}; struct Scope; async fn gate(ctx: &mut Context, next: Next<'_>) -> Result { ctx.store_scoped(Scope, "id", "1".to_owned()); next.await } async fn end(ctx: &mut Context) -> Result { assert_eq!(1, ctx.load_scoped::<Scope, String>("id").unwrap().parse::<i32>()?); Ok(()) } let app = App::new().gate(gate).end(end);
pub fn load<V>(&self, key: &'a str) -> Option<Variable<'a, V>> where
V: Value,
[src]
V: Value,
Search for value by key in public scope.
Example
use roa_core::{App, Context, Result, Next}; async fn gate(ctx: &mut Context, next: Next<'_>) -> Result { ctx.store("id", "1".to_string()); next.await } async fn end(ctx: &mut Context) -> Result { assert_eq!(1, ctx.load::<String>("id").unwrap().parse::<i32>()?); Ok(()) } let app = App::new().gate(gate).end(end);
Trait Implementations
impl<S> Clone for Context<S> where
S: Clone,
[src]
S: Clone,
impl<S> CookieGetter for Context<S>
[src]
fn must_cookie(&mut self, name: &str) -> Result<Arc<Cookie<'static>>>
[src]
fn cookie(&self, name: &str) -> Option<Arc<Cookie<'static>>>
[src]
impl<S> CookieSetter for Context<S>
[src]
fn set_cookie(&mut self, cookie: Cookie) -> Result
[src]
impl<S> Deref for Context<S>
[src]
type Target = 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) -> Option<&str>
[src]
fn client_ip(&self) -> IpAddr
[src]
fn forwarded_ips(&self) -> Vec<IpAddr>
[src]
fn forwarded_proto(&self) -> Option<&str>
[src]
impl<S> JwtVerifier<S> for Context<S>
[src]
fn claims<C>(&self) -> Result<C> where
C: 'static + DeserializeOwned,
[src]
C: 'static + DeserializeOwned,
fn verify<C>(&mut self, validation: &Validation) -> Result<C> where
C: 'static + DeserializeOwned,
[src]
C: 'static + DeserializeOwned,
impl<S: State> PowerBody for Context<S>
[src]
fn read<'life0, 'async_trait>(
&'life0 mut self
) -> Pin<Box<dyn Future<Output = Result<Vec<u8>>> + Send + 'async_trait>> where
'life0: 'async_trait,
Self: 'async_trait,
[src]
&'life0 mut self
) -> Pin<Box<dyn Future<Output = Result<Vec<u8>>> + Send + 'async_trait>> where
'life0: 'async_trait,
Self: 'async_trait,
fn read_json<'life0, 'async_trait, B>(
&'life0 mut self
) -> Pin<Box<dyn Future<Output = Result<B>> + Send + 'async_trait>> where
B: DeserializeOwned,
B: 'async_trait,
'life0: 'async_trait,
Self: 'async_trait,
[src]
&'life0 mut self
) -> Pin<Box<dyn Future<Output = Result<B>> + Send + 'async_trait>> where
B: DeserializeOwned,
B: 'async_trait,
'life0: 'async_trait,
Self: 'async_trait,
fn read_form<'life0, 'async_trait, B>(
&'life0 mut self
) -> Pin<Box<dyn Future<Output = Result<B>> + Send + 'async_trait>> where
B: DeserializeOwned,
B: 'async_trait,
'life0: 'async_trait,
Self: 'async_trait,
[src]
&'life0 mut self
) -> Pin<Box<dyn Future<Output = Result<B>> + Send + 'async_trait>> where
B: DeserializeOwned,
B: 'async_trait,
'life0: 'async_trait,
Self: 'async_trait,
fn write_json<B>(&mut self, data: &B) -> Result where
B: Serialize,
[src]
B: Serialize,
fn render<B>(&mut self, data: &B) -> Result where
B: Template,
[src]
B: Template,
fn write<B>(&mut self, data: B) where
B: Into<Bytes>,
[src]
B: Into<Bytes>,
fn write_reader<B>(&mut self, reader: B) where
B: 'static + AsyncRead + Unpin + Sync + Send,
[src]
B: 'static + AsyncRead + Unpin + Sync + Send,
fn write_file<'life0, 'async_trait, P>(
&'life0 mut self,
path: P,
typ: DispositionType
) -> Pin<Box<dyn Future<Output = Result> + Send + 'async_trait>> where
P: Send + AsRef<Path>,
P: 'async_trait,
'life0: 'async_trait,
Self: 'async_trait,
[src]
&'life0 mut self,
path: P,
typ: DispositionType
) -> Pin<Box<dyn Future<Output = Result> + Send + 'async_trait>> where
P: Send + AsRef<Path>,
P: 'async_trait,
'life0: 'async_trait,
Self: 'async_trait,
impl<S> Query for Context<S>
[src]
fn must_query<'a>(&self, name: &'a str) -> Result<Variable<'a, String>>
[src]
fn query<'a>(&self, name: &'a str) -> Option<Variable<'a, String>>
[src]
impl<S> RouterParam for Context<S>
[src]
Auto Trait Implementations
impl<S = ()> !RefUnwindSafe for Context<S>
impl<S> Send for Context<S> where
S: Send,
S: Send,
impl<S> Sync for Context<S> where
S: Sync,
S: Sync,
impl<S> Unpin for Context<S> where
S: Unpin,
S: Unpin,
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> State for T where
T: 'static + Clone + Send + Sync,
[src]
T: 'static + Clone + Send + Sync,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
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>,