Function warp::filters::host::optional

source ·
pub fn optional(
) -> impl Filter<Extract = (Option<Authority>,), Error = Rejection> + Copy
Expand description

Creates a Filter that looks for an authority (target server’s host and port) in the request.

Authority is specified either in the Host header or in the target URI.

If found, extracts the Authority, otherwise continues the request, extracting None.

Rejects with 400 Bad Request if the Host header is malformed or if there is a mismatch between the Host header and the target URI.

§Example

use warp::{Filter, host::Authority};

let host = warp::host::optional()
    .map(|authority: Option<Authority>| {
        if let Some(a) = authority {
            format!("{} is currently not at home", a.host())
        } else {
            "please state who you're trying to reach".to_owned()
        }
    });