From f0351e61a494fc6d828f00a95a5234d05bc19696 Mon Sep 17 00:00:00 2001 From: StellarisW Date: Fri, 25 Oct 2024 14:35:15 +0800 Subject: [PATCH] feat(http): add multipart for server --- Cargo.lock | 1 - volo-http/Cargo.toml | 1 - volo-http/src/server/layer/body_limit.rs | 31 +++--------------------- volo-http/src/server/utils/multipart.rs | 27 +++++++++------------ 4 files changed, 15 insertions(+), 45 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e8349c72..744cc44b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3854,7 +3854,6 @@ dependencies = [ "parking_lot 0.12.3", "paste", "pin-project", - "rand", "reqwest 0.12.8", "scopeguard", "serde", diff --git a/volo-http/Cargo.toml b/volo-http/Cargo.toml index 857d0e8c..9ed50dfa 100644 --- a/volo-http/Cargo.toml +++ b/volo-http/Cargo.toml @@ -85,7 +85,6 @@ sonic-rs = { workspace = true, optional = true } async-stream.workspace = true libc.workspace = true serde = { workspace = true, features = ["derive"] } -rand.workspace = true reqwest = { workspace = true, features = ["multipart"] } tokio-test.workspace = true url.workspace = true diff --git a/volo-http/src/server/layer/body_limit.rs b/volo-http/src/server/layer/body_limit.rs index bc7c4b99..24c2778e 100644 --- a/volo-http/src/server/layer/body_limit.rs +++ b/volo-http/src/server/layer/body_limit.rs @@ -18,23 +18,6 @@ impl BodyLimitLayer { /// Create a new [`BodyLimitLayer`] with given `body_limit`. /// /// If the Body is larger than the `body_limit`, the request will be rejected. - /// - /// # Examples - /// - /// ``` - /// use http::StatusCode; - /// use volo_http::server::{ - /// layer::BodyLimitLayer, - /// route::{post, Router}, - /// }; - /// - /// async fn handler() -> &'static str { - /// "Hello, World" - /// } - /// - /// let router: Router = Router::new() - /// .route("/", post(handler)) - /// .layer(BodyLimitLayer::new(1024)); // limit body size to 1KB /// ``` pub fn new(body_limit: usize) -> Self { Self { limit: body_limit } @@ -64,7 +47,6 @@ impl Service> for BodyLimitService where S: Service> + Send + Sync + 'static, S::Response: IntoResponse, - S::Error: IntoResponse, B: Body + Send, { type Response = ServerResponse; @@ -101,7 +83,6 @@ where mod tests { use http::{Method, StatusCode}; use motore::{layer::Layer, Service}; - use rand::Rng; use crate::{ server::{ @@ -118,25 +99,19 @@ mod tests { "Hello, World" } - let body_limit_layer = BodyLimitLayer::new(1024); + let body_limit_layer = BodyLimitLayer::new(8); let route: Route<_> = Route::new(any(handler)); let service = body_limit_layer.layer(route); let mut cx = empty_cx(); // Test case 1: reject - let mut rng = rand::thread_rng(); - let min_part_size = 4096; - let mut body: Vec = vec![0; min_part_size]; - rng.fill(&mut body[..]); - let req = simple_req(Method::GET, "/", unsafe { - String::from_utf8_unchecked(body) - }); + let req = simple_req(Method::GET, "/", "111111111".to_string()); let res = service.call(&mut cx, req).await.unwrap(); assert_eq!(res.status(), StatusCode::PAYLOAD_TOO_LARGE); // Test case 2: not reject - let req = simple_req(Method::GET, "/", "Hello, World".to_string()); + let req = simple_req(Method::GET, "/", "1".to_string()); let res = service.call(&mut cx, req).await.unwrap(); assert_eq!(res.status(), StatusCode::OK); } diff --git a/volo-http/src/server/utils/multipart.rs b/volo-http/src/server/utils/multipart.rs index dd00a893..c7abee8d 100644 --- a/volo-http/src/server/utils/multipart.rs +++ b/volo-http/src/server/utils/multipart.rs @@ -237,10 +237,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)), @@ -324,7 +324,7 @@ mod multipart_tests { Ok(()) } - let form1 = Form::new().part( + let form = Form::new().part( FIELD_NAME1, reqwest::multipart::Part::bytes(BYTES) .file_name(FILE_NAME1) @@ -334,8 +334,7 @@ mod multipart_tests { reqwest::header::HeaderName::from_static("foo1"), reqwest::header::HeaderValue::from_static("bar1"), )])), - ); - let form2 = Form::new().part( + ).part( FIELD_NAME2, reqwest::multipart::Part::bytes(BYTES) .file_name(FILE_NAME2) @@ -352,13 +351,11 @@ mod multipart_tests { let url_str = format!("http://127.0.0.1:{}", 25242); let url = url::Url::parse(url_str.as_str()).unwrap(); - for form in vec![form1, form2] { - reqwest::Client::new() - .post(url.clone()) - .multipart(form) - .send() - .await - .unwrap(); - } + reqwest::Client::new() + .post(url.clone()) + .multipart(form) + .send() + .await + .unwrap(); } }