Module ua

Source
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§

UserAgent
User Agent (UA) information.
UserAgentClassifier
A Service that classifies the UserAgent of incoming Requests.
UserAgentClassifierLayer
A Layer that wraps a Service with a UserAgentClassifier.
UserAgentInfo
Information about the UserAgent
UserAgentOverwrites
Information that can be used to overwrite the UserAgent of an http request.

Enums§

DeviceKind
Device on which the UserAgent operates.
HttpAgent
Http implementation used by the UserAgent
PlatformKind
Platform within the UserAgent operates.
TlsAgent
Tls implementation used by the UserAgent
UserAgentKind
The kind of UserAgent