Skip to content

Commit

Permalink
fix: process LengthLimitError
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon committed Dec 22, 2023
1 parent 341850f commit bb7e87c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
9 changes: 3 additions & 6 deletions viz-core/src/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,9 @@ impl HttpBody for Body {
) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
match self.get_mut() {
Self::Empty => Poll::Ready(None),
Self::Full(inner) => Pin::new(inner).poll_frame(cx).map_err(Error::from),
Self::Incoming(inner) => Pin::new(inner).poll_frame(cx).map_err(Error::from),
Self::Boxed(inner) => Pin::new(inner)
.get_pin_mut()
.poll_frame(cx)
.map_err(Error::from),
Self::Full(inner) => Pin::new(inner).poll_frame(cx).map_err(Into::into),
Self::Incoming(inner) => Pin::new(inner).poll_frame(cx).map_err(Into::into),
Self::Boxed(inner) => Pin::new(inner).get_pin_mut().poll_frame(cx),
}
}

Expand Down
19 changes: 15 additions & 4 deletions viz-core/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,10 @@ impl RequestExt for Request {
{
self.headers()
.get(key)
.and_then(|v| v.to_str().ok())
.and_then(|v| v.parse::<T>().ok())
.map(header::HeaderValue::to_str)
.and_then(Result::ok)
.map(str::parse)
.and_then(Result::ok)
}

fn header_typed<H>(&self) -> Option<H>
Expand Down Expand Up @@ -271,8 +273,16 @@ impl RequestExt for Request {
self.incoming()?
.collect()
.await
.map_err(|err| {
if err.downcast_ref::<LengthLimitError>().is_some() {
return PayloadError::TooLarge;
}
if let Ok(err) = err.downcast::<hyper::Error>() {
return PayloadError::Hyper(err);
}
PayloadError::Read
})
.map(Collected::to_bytes)
.map_err(|_| PayloadError::Read)
}

#[cfg(feature = "limits")]
Expand Down Expand Up @@ -369,7 +379,8 @@ impl RequestExt for Request {
boundary,
self.extensions()
.get::<std::sync::Arc<MultipartLimits>>()
.map(|ml| ml.as_ref().clone())
.map(AsRef::as_ref)
.cloned()
.unwrap_or_default(),
))
}
Expand Down
2 changes: 1 addition & 1 deletion viz-core/src/types/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ pub enum PayloadError {
#[error("JSON serialize or deserialize faild, {0}")]
Json(#[from] serde_json::Error),

#[cfg(any(feature = "form", feature = "query"))]
/// 400
#[cfg(any(feature = "form", feature = "query"))]
#[error("url decode failed, {0}")]
UrlDecode(#[from] serde_urlencoded::de::Error),

Expand Down

0 comments on commit bb7e87c

Please sign in to comment.