This repository has been archived on 2024-10-26. You can view files and clone it, but cannot push or open issues or pull requests.
staxman-old/src/http/accept.rs

28 lines
670 B
Rust
Raw Normal View History

2023-11-19 14:08:47 +00:00
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"))
}
}
}