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 22, 2024
1 parent e57c052 commit 427fd20
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
15 changes: 9 additions & 6 deletions volo-http/src/server/layer/body_limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -67,8 +67,7 @@ impl BodyLimitLayer {
}
}

impl<S> Layer<S> for BodyLimitLayer
{
impl<S> Layer<S> for BodyLimitLayer {
type Service = BodyLimitService<S>;

fn layer(self, inner: S) -> Self::Service {
Expand Down Expand Up @@ -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::<usize>().ok())) {
if let Some(size) = parts
.headers
.get(http::header::CONTENT_LENGTH)
.and_then(|v| v.to_str().ok().and_then(|s| s.parse::<usize>().ok()))
{
if size > limit {
return Ok(StatusCode::PAYLOAD_TOO_LARGE.into_response());
}
Expand Down
8 changes: 4 additions & 4 deletions volo-http/src/server/utils/multipart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,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

0 comments on commit 427fd20

Please sign in to comment.