27 lines
671 B
Rust
27 lines
671 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"))
|
|
}
|
|
}
|
|
}
|