tokio-rs / axum

Ergonomic and modular web framework built with Tokio, Tower, and Hyper
18.79k stars 1.05k forks source link

Allow setting multer::Multipart constraints #1623

Open martijnarts opened 1 year ago

martijnarts commented 1 year ago

Feature Request

Motivation

I'm running into a length limit exceeded error from multer when trying to read a file coming in from a Multipart form. Multer supports Constraints to avoid this, but axum's multipart extractor does not allow forwarding these.

Proposal

I'm not super sure how this could be implemented, since the constraints can't be passed through the parameter typing. Either there might need to be some documented recommended way of parsing the request manually inside the handler, or maybe the multipart extractor needs to be a bit fancier to allow passing constraints.

Alternatives

For now, I'll "vendor" the Multipart extractor, since it seems simple enough, to add that functionality, but it seems like something axum's native extractor should support.

martijnarts commented 1 year ago

I am willing to work on implementing this, btw!

davidpdrsn commented 1 year ago

The limit you're running into is most likely https://docs.rs/axum/latest/axum/extract/struct.DefaultBodyLimit.html. It has a method to disable the default limit.

martijnarts commented 1 year ago

You're right, after implement the vendored Multipart extractor I've realized Constraints are an opt-in thing. Thanks for linking that, I'll look into it.

Either way, it's probably not a bad idea to support these more specific constraints that multer exposes.

davidpdrsn commented 1 year ago

Yeah I'm open to that.

However its a bit tricky since extractors can't really be configured currently, as they're types in function arguments. So if you have async fn handler(form: Multipart) how would you configure the Multipart? actix-web uses configs in their version of extensions but I'm not a fan of that approach since its very loosely typed. There is some more discussion about that here https://github.com/tokio-rs/axum/issues/1116

If one really needs the constraints today then I'd probably just recommend writing their own extractor that uses multer.

jplatte commented 1 year ago

I've created a POC for how we could support this eventually, once https://github.com/rust-lang/rust/issues/95174 is stable.