summaryrefslogtreecommitdiff
path: root/src/types/extract.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/types/extract.rs')
-rw-r--r--src/types/extract.rs26
1 files changed, 25 insertions, 1 deletions
diff --git a/src/types/extract.rs b/src/types/extract.rs
index 8292da7..6a01ad2 100644
--- a/src/types/extract.rs
+++ b/src/types/extract.rs
@@ -7,7 +7,7 @@ use axum::{
async_trait,
body::HttpBody,
extract::{ConnectInfo, FromRequest, FromRequestParts},
- http::{request::Parts, Request},
+ http::{request::Parts, Request, header::USER_AGENT},
response::Response,
BoxError, RequestExt,
};
@@ -236,6 +236,30 @@ pub trait Check {
}
}
+pub struct UserAgent(pub String);
+
+#[async_trait]
+impl<S> FromRequestParts<S> for UserAgent
+where
+ S: Send + Sync,
+{
+ type Rejection = Response;
+
+ async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self> {
+ let agent = parts.headers.get(USER_AGENT);
+
+ let Some(agent) = agent else {
+ return Err(ResponseCode::BadRequest.text("Bad Request"));
+ };
+
+ let Ok(agent) = agent.to_str() else {
+ return Err(ResponseCode::BadRequest.text("Bad Request"));
+ };
+
+ Ok(UserAgent(agent.to_string()))
+ }
+}
+
async fn read_body<S, B>(mut req: Request<B>, state: &S) -> Result<Vec<u8>>
where
B: HttpBody + Sync + Send + 'static,