Skip to main content

ankurah_websocket_server/
user_agent.rs

1use std::{convert::Infallible, future::Future};
2
3use axum::{extract::FromRequestParts, http::request::Parts};
4use axum_extra::{headers, TypedHeader};
5
6pub struct OptionalUserAgent(pub Option<String>);
7
8impl<S> FromRequestParts<S> for OptionalUserAgent
9where S: Send + Sync
10{
11    type Rejection = Infallible;
12
13    fn from_request_parts(parts: &mut Parts, state: &S) -> impl Future<Output = Result<Self, Self::Rejection>> + Send {
14        async move {
15            let user_agent = TypedHeader::<headers::UserAgent>::from_request_parts(parts, state)
16                .await
17                .ok()
18                .map(|TypedHeader(user_agent)| user_agent.to_string());
19
20            Ok(Self(user_agent))
21        }
22    }
23}