From 333c10b6270165c6f665a625a2740066d39c797e Mon Sep 17 00:00:00 2001 From: Fangdun Tsai Date: Fri, 5 Jan 2024 12:39:11 +0800 Subject: [PATCH] fix: clippy --- viz-core/src/request.rs | 7 +++---- viz-core/src/types/payload.rs | 38 ++++++++++++++++++++++++++--------- viz-test/tests/payload.rs | 9 +++++---- viz/src/server/listener.rs | 5 +++++ 4 files changed, 42 insertions(+), 17 deletions(-) diff --git a/viz-core/src/request.rs b/viz-core/src/request.rs index b8de371a..1f5a0105 100644 --- a/viz-core/src/request.rs +++ b/viz-core/src/request.rs @@ -284,7 +284,7 @@ impl RequestExt for Request { where T: serde::de::DeserializeOwned, { -
::check_header(self.content_type(), self.content_length(), None)?; + ::check_type(self.content_type())?; let bytes = self.bytes().await?; serde_urlencoded::from_reader(bytes::Buf::reader(bytes)).map_err(PayloadError::UrlDecode) } @@ -294,15 +294,14 @@ impl RequestExt for Request { where T: serde::de::DeserializeOwned, { - ::check_header(self.content_type(), self.content_length(), None)?; + ::check_type(self.content_type())?; let bytes = self.bytes().await?; serde_json::from_slice(&bytes).map_err(PayloadError::Json) } #[cfg(feature = "multipart")] async fn multipart(&mut self) -> Result { - let m = - ::check_header(self.content_type(), self.content_length(), None)?; + let m = ::check_type(self.content_type())?; let boundary = m .get_param(mime::BOUNDARY) diff --git a/viz-core/src/types/payload.rs b/viz-core/src/types/payload.rs index f67f30f1..f99f84bd 100644 --- a/viz-core/src/types/payload.rs +++ b/viz-core/src/types/payload.rs @@ -108,6 +108,32 @@ pub trait Payload { limit.unwrap_or(Self::LIMIT) } + /// Detects `Content-Type` + /// + /// # Errors + /// + /// Will return [`PayloadError::UnsupportedMediaType`] if the detected media type is not supported. + #[inline] + fn check_type(m: Option) -> Result { + m.filter(Self::detect) + .ok_or_else(|| PayloadError::UnsupportedMediaType(Self::mime())) + } + + /// Checks `Content-Length` + /// + /// # Errors + /// + /// Will return [`PayloadError::TooLarge`] if the detected content length is too large. + #[inline] + fn check_length(len: Option, limit: Option) -> Result<(), PayloadError> { + match len { + None => Err(PayloadError::LengthRequired), + Some(len) => (len <= Self::limit(limit)) + .then_some(()) + .ok_or_else(|| PayloadError::TooLarge), + } + } + /// Checks `Content-Type` & `Content-Length` /// /// # Errors @@ -121,14 +147,8 @@ pub trait Payload { len: Option, limit: Option, ) -> Result { - let m = m - .filter(Self::detect) - .ok_or_else(|| PayloadError::UnsupportedMediaType(Self::mime()))?; - - match len { - None => Err(PayloadError::LengthRequired), - Some(len) if len > Self::limit(limit) => Err(PayloadError::TooLarge), - Some(_) => Ok(m), - } + let m = Self::check_type(m)?; + Self::check_length(len, limit)?; + Ok(m) } } diff --git a/viz-test/tests/payload.rs b/viz-test/tests/payload.rs index a9350e51..f655742e 100644 --- a/viz-test/tests/payload.rs +++ b/viz-test/tests/payload.rs @@ -1,5 +1,6 @@ use std::collections::HashMap; -use viz::{types, Error, Request, RequestExt, Response, ResponseExt, Result}; + +use viz::{types, Error, Request, RequestLimitsExt, Response, ResponseExt, Result}; #[tokio::test] async fn payload() -> Result<()> { @@ -9,15 +10,15 @@ async fn payload() -> Result<()> { let router = Router::new() .post("/form", |mut req: Request| async move { - let data = req.form::>().await?; + let data = req.limited_form::>().await?; Ok(Response::json(data)) }) .post("/json", |mut req: Request| async move { - let data = req.json::>().await?; + let data = req.limited_json::>().await?; Ok(Response::json(data)) }) .post("/multipart", |mut req: Request| async move { - let _ = req.multipart().await?; + let _ = req.limited_multipart().await?; Ok(()) }) .with( diff --git a/viz/src/server/listener.rs b/viz/src/server/listener.rs index 4b72e1eb..8bb2cd49 100644 --- a/viz/src/server/listener.rs +++ b/viz/src/server/listener.rs @@ -13,5 +13,10 @@ pub trait Listener { fn accept(&self) -> impl Future> + Send; /// Returns the local address that this listener is bound to. + /// + /// # Errors + /// + /// An error will return if got the socket address of the local half of this connection is + /// failed. fn local_addr(&self) -> Result; }