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

pub trait FriendlyHeaders {
    const GENERAL_ERROR_CODE: StatusCode;
    const GENERAL_ERROR_EXPOSE: bool;

    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: Display
, { ... }
fn get<K>(&self, key: K) -> Option<Result<&str>>
    where
        K: AsHeaderName
, { ... }
fn must_get<K>(&self, key: K) -> Result<&str>
    where
        K: 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: TryInto<HeaderValue>,
        V::Error: Display
, { ... }
fn append<K, V>(&mut self, key: K, val: V) -> Result<bool>
    where
        K: IntoHeaderName,
        V: TryInto<HeaderValue>,
        V::Error: Display
, { ... } }

A Request/Response extension.

Associated Constants

const GENERAL_ERROR_CODE: StatusCode

StatusCode of general error.

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

const GENERAL_ERROR_EXPOSE: bool

If general errors should be exposed.

true for Request, false 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: Display

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

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

Example

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

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

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

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

Example

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

async fn get(ctx: Context<()>) -> Result {
    println!("origin: {}", ctx.req().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::{Context, Result};
use roa::http::header::{ORIGIN, CONTENT_TYPE};
use roa::http::StatusCode;
use roa::header::FriendlyHeaders;

async fn get(ctx: Context<()>) -> Result {
    for value in ctx.req().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: TryInto<HeaderValue>,
    V::Error: Display

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::{Context, Result};
use roa::http::header::{ORIGIN, CONTENT_TYPE};
use roa::http::StatusCode;
use roa::header::FriendlyHeaders;

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

fn append<K, V>(&mut self, key: K, val: V) -> Result<bool> where
    K: IntoHeaderName,
    V: TryInto<HeaderValue>,
    V::Error: Display

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::{Context, Result};
use roa::http::header::SET_COOKIE;
use roa::http::StatusCode;
use roa::header::FriendlyHeaders;

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

Implementors

Loading content...