28 lines
670 B
Rust
28 lines
670 B
Rust
|
use axum::{
|
||
|
async_trait,
|
||
|
extract::FromRequestParts,
|
||
|
http::{
|
||
|
header::{HeaderValue, ACCEPT},
|
||
|
request::Parts,
|
||
|
StatusCode,
|
||
|
},
|
||
|
};
|
||
|
|
||
|
pub struct ExtractAccept(pub HeaderValue);
|
||
|
|
||
|
#[async_trait]
|
||
|
impl<S> FromRequestParts<S> for ExtractAccept
|
||
|
where
|
||
|
S: Send + Sync,
|
||
|
{
|
||
|
type Rejection = (StatusCode, &'static str);
|
||
|
|
||
|
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
|
||
|
if let Some(user_agent) = parts.headers.get(ACCEPT) {
|
||
|
Ok(ExtractAccept(user_agent.clone()))
|
||
|
} else {
|
||
|
Err((StatusCode::BAD_REQUEST, "`User-Agent` header is missing"))
|
||
|
}
|
||
|
}
|
||
|
}
|