Actix-web: multipart always consumes body

Created on 17 May 2020  路  2Comments  路  Source: actix/actix-web

Expected Behavior

Multipart only consumes payload in extractor after header checks.

Current Behavior

Payload is always consumed preventing optional extraction.

Possible Solution

Perform header checks in FromRequest to prevent always calling payload.take()

Context

Precedent set with web::Json extraction. It receives a &mut Payload and does not modify it until header content type checks passed.

C-bug P-multipart

All 2 comments

Hi @robjtede I took a stab at this.

I ended up with an implementation for FromRequest that looks like this:

impl FromRequest for Multipart {
    type Error = Error;
    type Future = Ready<Result<Multipart, Error>>;
    type Config = ();

    #[inline]
    fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
        ok(match Multipart::boundary(req.headers()) {
            Ok(boundary) => Multipart::from_boundary_stream(boundary, payload.take()),
            Err(err) => Multipart::from_error(err),
        })
    }
}

The idea is to add two new methods 馃槵

  • from_boundary_stream: build Multipart from boundary and stream
  • from_error: build Multipart for MultipartError

Also we make the boundary, from_boundary_stream, from_error methods public within the crate so that we can use them in the extractor.

_PS: from_boundary_stream name kinda falls short, open to better names_

_I would like feedback on this approach, if it makes sense I'll submit a fully fledged PR_

Maybe just from_boundary. Just my two cents.

Was this page helpful?
0 / 5 - 0 ratings