[][src]Trait roa::header::FriendlyHeaders

pub trait FriendlyHeaders {
    const GENERAL_ERROR_CODE: StatusCode;

    fn raw_header_map(&self) -> &HeaderMap<HeaderValue>;
fn raw_mut_header_map(&mut self) -> &mut HeaderMap<HeaderValue>; fn handle_to_str_error(err: ToStrError, value: &HeaderValue) -> Error { ... }
fn handle_none<K>(key: K) -> Error
    where
        K: AsHeaderName + AsRef<str>
, { ... }
fn get<K>(&self, key: K) -> Option<Result<&str>>
    where
        K: AsHeaderName + AsRef<str>
, { ... }
fn must_get<K>(&self, key: K) -> Result<&str>
    where
        K: AsHeaderName + AsRef<str>
, { ... }
fn get_all<K>(&self, key: K) -> Result<Vec<&str>>
    where
        K: AsHeaderName
, { ... }
fn insert<K, V>(&mut self, key: K, val: V) -> Result<Option<String>>
    where
        K: IntoHeaderName,
        V: AsRef<str>
, { ... }
fn append<K, V>(&mut self, key: K, val: V) -> Result<bool>
    where
        K: IntoHeaderName,
        V: AsRef<str>
, { ... } }

A Request/Response extension.

Associated Constants

const GENERAL_ERROR_CODE: StatusCode

General error code should be returned when some errors occur.

400 BAD REQUEST for Request, 500 INTERNAL SERVER ERROR for Response.

Loading content...

Required methods

fn raw_header_map(&self) -> &HeaderMap<HeaderValue>

Get immutable reference of raw header map.

fn raw_mut_header_map(&mut self) -> &mut HeaderMap<HeaderValue>

Get mutable reference of raw header map.

Loading content...

Provided methods

fn handle_to_str_error(err: ToStrError, value: &HeaderValue) -> Error

Deal with ToStrError, usually invoked when a header value is gotten, then fails to be transferred to string. Throw Self::GENERAL_ERROR_CODE.

fn handle_none<K>(key: K) -> Error where
    K: AsHeaderName + AsRef<str>, 

Deal with None, usually invoked when a header value is not gotten. Throw Self::GENERAL_ERROR_CODE.

fn get<K>(&self, key: K) -> Option<Result<&str>> where
    K: AsHeaderName + AsRef<str>, 

Try to get a header value, return None if not exists. Return Some(Err) if fails to string.

Example

use roa::core::{Context, Result, StatusCode};
use roa::core::header::{ORIGIN, CONTENT_TYPE};
use roa::header::FriendlyHeaders;

async fn get(ctx: Context<()>) -> Result {
    if let Some(value) = ctx.req().await.get(ORIGIN) {
        println!("origin: {}", value?);     
    }   
    Ok(())
}

fn must_get<K>(&self, key: K) -> Result<&str> where
    K: AsHeaderName + AsRef<str>, 

Get a header value. Return Err if not exists or fails to string.

Example

use roa::core::{Context, Result, StatusCode};
use roa::core::header::{ORIGIN, CONTENT_TYPE};
use roa::header::FriendlyHeaders;

async fn get(ctx: Context<()>) -> Result {
    println!("origin: {}", ctx.req().await.must_get(ORIGIN)?);     
    Ok(())
}

fn get_all<K>(&self, key: K) -> Result<Vec<&str>> where
    K: AsHeaderName

Get all header value with the same header name. Return Err if one of them fails to string.

Example

use roa::core::{Context, Result, StatusCode};
use roa::core::header::{ORIGIN, CONTENT_TYPE};
use roa::header::FriendlyHeaders;

async fn get(ctx: Context<()>) -> Result {
    for value in ctx.req().await.get_all(ORIGIN)?.into_iter() {
        println!("origin: {}", value);
    }
    Ok(())
}

fn insert<K, V>(&mut self, key: K, val: V) -> Result<Option<String>> where
    K: IntoHeaderName,
    V: AsRef<str>, 

Insert a header pair.

  • Return Err(500 INTERNAL SERVER ERROR) if value fails to header value.
  • Return Ok(Some(old_value)) if header name already exists.

Example

use roa::core::{Context, Result, StatusCode};
use roa::core::header::CONTENT_TYPE;
use roa::header::FriendlyHeaders;

async fn get(ctx: Context<()>) -> Result {
    ctx.resp_mut().await.insert(CONTENT_TYPE, "text/plain")?;   
    Ok(())
}

fn append<K, V>(&mut self, key: K, val: V) -> Result<bool> where
    K: IntoHeaderName,
    V: AsRef<str>, 

Append a header pair.

  • Return Err(500 INTERNAL SERVER ERROR) if value fails to header value.
  • Return Ok(true) if header name already exists.

Example

use roa::core::{Context, Result, StatusCode};
use roa::core::header::SET_COOKIE;
use roa::header::FriendlyHeaders;

async fn get(ctx: Context<()>) -> Result {
    ctx.resp_mut().await.append(SET_COOKIE, "this is a cookie")?;   
    Ok(())
}
Loading content...

Implementations on Foreign Types

impl FriendlyHeaders for Request[src]

impl FriendlyHeaders for Response[src]

Loading content...

Implementors

Loading content...