From 427fd201179330b171cf92461b5af1ed14eeebda Mon Sep 17 00:00:00 2001 From: StellarisW Date: Tue, 22 Oct 2024 21:06:45 +0800 Subject: [PATCH] feat(http): add multipart for server --- volo-http/src/server/layer/body_limit.rs | 15 +++++++++------ volo-http/src/server/utils/multipart.rs | 8 ++++---- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/volo-http/src/server/layer/body_limit.rs b/volo-http/src/server/layer/body_limit.rs index c32074d0..e34d110d 100644 --- a/volo-http/src/server/layer/body_limit.rs +++ b/volo-http/src/server/layer/body_limit.rs @@ -2,9 +2,9 @@ use http::StatusCode; use http_body::Body; use motore::{layer::Layer, Service}; -use crate::{context::ServerContext, request::ServerRequest}; -use crate::response::ServerResponse; -use crate::server::IntoResponse; +use crate::{ + context::ServerContext, request::ServerRequest, response::ServerResponse, server::IntoResponse, +}; #[derive(Debug, Clone, Copy)] pub(crate) enum BodyLimitKind { @@ -67,8 +67,7 @@ impl BodyLimitLayer { } } -impl Layer for BodyLimitLayer -{ +impl Layer for BodyLimitLayer { type Service = BodyLimitService; fn layer(self, inner: S) -> Self::Service { @@ -104,7 +103,11 @@ where let (parts, body) = req.into_parts(); if let BodyLimitKind::Block(limit) = self.kind { // get body size from content length - if let Some(size) = parts.headers.get(http::header::CONTENT_LENGTH).and_then(|v| v.to_str().ok().and_then(|s| s.parse::().ok())) { + if let Some(size) = parts + .headers + .get(http::header::CONTENT_LENGTH) + .and_then(|v| v.to_str().ok().and_then(|s| s.parse::().ok())) + { if size > limit { return Ok(StatusCode::PAYLOAD_TOO_LARGE.into_response()); } diff --git a/volo-http/src/server/utils/multipart.rs b/volo-http/src/server/utils/multipart.rs index 357021a6..d861f36e 100644 --- a/volo-http/src/server/utils/multipart.rs +++ b/volo-http/src/server/utils/multipart.rs @@ -256,10 +256,10 @@ mod multipart_tests { async fn run_handler(service: S, port: u16) where - S: Service - + Send - + Sync - + 'static, + S: Service + + Send + + Sync + + 'static, { let addr = Address::Ip(SocketAddr::new( IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),