Skip to content

Commit

Permalink
feat(http): add multipart for server
Browse files Browse the repository at this point in the history
  • Loading branch information
StellarisW committed Oct 25, 2024
1 parent 94b27ef commit f0351e6
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 45 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion volo-http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 3 additions & 28 deletions volo-http/src/server/layer/body_limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -64,7 +47,6 @@ impl<S, B> Service<ServerContext, ServerRequest<B>> for BodyLimitService<S>
where
S: Service<ServerContext, ServerRequest<B>> + Send + Sync + 'static,
S::Response: IntoResponse,
S::Error: IntoResponse,
B: Body + Send,
{
type Response = ServerResponse;
Expand Down Expand Up @@ -101,7 +83,6 @@ where
mod tests {
use http::{Method, StatusCode};
use motore::{layer::Layer, Service};
use rand::Rng;

use crate::{
server::{
Expand All @@ -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<u8> = 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);
}
Expand Down
27 changes: 12 additions & 15 deletions volo-http/src/server/utils/multipart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,10 @@ mod multipart_tests {

async fn run_handler<S>(service: S, port: u16)
where
S: Service<ServerContext, ServerRequest, Response = ServerResponse, Error = Infallible>
+ Send
+ Sync
+ 'static,
S: Service<ServerContext, ServerRequest, Response=ServerResponse, Error=Infallible>
+ Send
+ Sync
+ 'static,
{
let addr = Address::Ip(SocketAddr::new(
IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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();
}
}

0 comments on commit f0351e6

Please sign in to comment.