Skip to content

Multipart constraints POC #1625

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions axum/src/extract/multipart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ use std::{
/// ```
#[cfg_attr(docsrs, doc(cfg(feature = "multipart")))]
#[derive(Debug)]
pub struct Multipart {
pub struct Multipart<const C: Constraints = { Constraints::DEFAULT }> {
inner: multer::Multipart<'static>,
}

#[async_trait]
impl<S, B> FromRequest<S, B> for Multipart
impl<S, B, const C: Constraints> FromRequest<S, B> for Multipart<C>
where
B: HttpBody + Send + 'static,
B::Data: Into<Bytes>,
Expand All @@ -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<const C: Constraints> Multipart<C> {
/// Yields the next [`Field`] if available.
pub async fn next_field(&mut self) -> Result<Option<Field<'_>>, MultipartError> {
let field = self
Expand All @@ -88,21 +89,43 @@ impl Multipart {
if let Some(field) = field {
Ok(Some(Field {
inner: field,
_multipart: self,
_multipart: &mut self.inner,
}))
} else {
Ok(None)
}
}
}

#[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> {
Expand Down
7 changes: 6 additions & 1 deletion axum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand Down