mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-28 00:00:20 +02:00
refactor(axum): Refactor json module (#3332)
This commit is contained in:
+8
-14
@@ -104,12 +104,12 @@ where
|
|||||||
type Rejection = JsonRejection;
|
type Rejection = JsonRejection;
|
||||||
|
|
||||||
async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> {
|
async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> {
|
||||||
if json_content_type(req.headers()) {
|
if !json_content_type(req.headers()) {
|
||||||
let bytes = Bytes::from_request(req, state).await?;
|
return Err(MissingJsonContentType.into());
|
||||||
Self::from_bytes(&bytes)
|
|
||||||
} else {
|
|
||||||
Err(MissingJsonContentType.into())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let bytes = Bytes::from_request(req, state).await?;
|
||||||
|
Self::from_bytes(&bytes)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -136,21 +136,15 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn json_content_type(headers: &HeaderMap) -> bool {
|
fn json_content_type(headers: &HeaderMap) -> bool {
|
||||||
let content_type = if let Some(content_type) = headers.get(header::CONTENT_TYPE) {
|
let Some(content_type) = headers.get(header::CONTENT_TYPE) else {
|
||||||
content_type
|
|
||||||
} else {
|
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
let content_type = if let Ok(content_type) = content_type.to_str() {
|
let Ok(content_type) = content_type.to_str() else {
|
||||||
content_type
|
|
||||||
} else {
|
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
let mime = if let Ok(mime) = content_type.parse::<mime::Mime>() {
|
let Ok(mime) = content_type.parse::<mime::Mime>() else {
|
||||||
mime
|
|
||||||
} else {
|
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user