From c061578ba0883a57953d04e43e4a67e9b9345771 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Wed, 7 Dec 2022 15:25:22 +0100 Subject: [PATCH] POC --- axum/src/extract/multipart.rs | 35 +++++++++++++++++++++++++++++------ axum/src/lib.rs | 7 ++++++- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/axum/src/extract/multipart.rs b/axum/src/extract/multipart.rs index 76dd52dec9..4f2b90ec7c 100644 --- a/axum/src/extract/multipart.rs +++ b/axum/src/extract/multipart.rs @@ -50,12 +50,12 @@ use std::{ /// ``` #[cfg_attr(docsrs, doc(cfg(feature = "multipart")))] #[derive(Debug)] -pub struct Multipart { +pub struct Multipart { inner: multer::Multipart<'static>, } #[async_trait] -impl FromRequest for Multipart +impl FromRequest for Multipart where B: HttpBody + Send + 'static, B::Data: Into, @@ -71,12 +71,13 @@ where Err(unlimited) => BodyStream::from_request(unlimited, state).await, }; let stream = stream_result.unwrap_or_else(|err| match err {}); - let multipart = multer::Multipart::new(stream, boundary); + let multipart = + multer::Multipart::with_constraints(stream, boundary, C.to_multer_constraints()); Ok(Self { inner: multipart }) } } -impl Multipart { +impl Multipart { /// Yields the next [`Field`] if available. pub async fn next_field(&mut self) -> Result>, MultipartError> { let field = self @@ -88,7 +89,7 @@ impl Multipart { if let Some(field) = field { Ok(Some(Field { inner: field, - _multipart: self, + _multipart: &mut self.inner, })) } else { Ok(None) @@ -96,13 +97,35 @@ impl Multipart { } } +#[derive(PartialEq, Eq)] +pub struct Constraints { + //size_limit: SizeLimit. + allowed_fields: &'static [&'static str], +} + +impl Constraints { + pub const DEFAULT: Self = Self { + allowed_fields: &[], + }; + + pub const fn allowed_fields(self, value: &'static [&'static str]) -> Self { + Self { + allowed_fields: value, + } + } + + fn to_multer_constraints(&self) -> multer::Constraints { + multer::Constraints::new().allowed_fields(self.allowed_fields.iter().copied().collect()) + } +} + /// A single field in a multipart stream. #[derive(Debug)] pub struct Field<'a> { inner: multer::Field<'static>, // multer requires there to only be one live `multer::Field` at any point. This enforces that // statically, which multer does not do, it returns an error instead. - _multipart: &'a mut Multipart, + _multipart: &'a mut multer::Multipart<'static>, } impl<'a> Stream for Field<'a> { diff --git a/axum/src/lib.rs b/axum/src/lib.rs index 314f844ff0..1ab5a92e38 100644 --- a/axum/src/lib.rs +++ b/axum/src/lib.rs @@ -429,7 +429,12 @@ missing_docs )] #![deny(unreachable_pub, private_in_public)] -#![allow(elided_lifetimes_in_paths, clippy::type_complexity)] +#![allow( + elided_lifetimes_in_paths, + incomplete_features, + clippy::type_complexity +)] +#![feature(adt_const_params)] #![forbid(unsafe_code)] #![cfg_attr(docsrs, feature(doc_auto_cfg, doc_cfg))] #![cfg_attr(test, allow(clippy::float_cmp))]