rama::http::layer

Module ua

Expand description

User-Agent (see also rama-ua) http layer support

§Example

use rama_http::{
    service::client::HttpClientExt, IntoResponse, Request, Response, StatusCode,
    layer::ua::{PlatformKind, UserAgent, UserAgentClassifierLayer, UserAgentKind, UserAgentInfo},
};
use rama_core::{Context, Layer, service::service_fn};
use std::convert::Infallible;

const UA: &str = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.2478.67";

async fn handle<S>(ctx: Context<S>, _req: Request) -> Result<Response, Infallible> {
    let ua: &UserAgent = ctx.get().unwrap();

    assert_eq!(ua.header_str(), UA);
    assert_eq!(ua.info(), Some(UserAgentInfo{ kind: UserAgentKind::Chromium, version: Some(124) }));
    assert_eq!(ua.platform(), Some(PlatformKind::Windows));

    Ok(StatusCode::OK.into_response())
}

let service = UserAgentClassifierLayer::new().layer(service_fn(handle));

let _ = service
    .get("http://www.example.com")
    .typed_header(headers::UserAgent::from_static(UA))
    .send(Context::default())
    .await
    .unwrap();

Structs§

Enums§