mirror of
https://github.com/fafhrd91/actix-web
synced 2024-12-18 01:43:58 +01:00
37c76a39ab
* Fix Multipart consuming payload before header checks What -- Split up logic in the constructor into two functions: - **from_boundary:** build Multipart from boundary and stream - **from_error:** build Multipart for MultipartError Also we make the `boundary`, `from_boundary`, `from_error` methods public within the crate so that we can use them in the extractor. The extractor is then able to perform header checks and only consume the payload if the checks pass. * Add tests * Add payload consumption test Co-authored-by: Rob Ede <robjtede@icloud.com>
45 lines
1.3 KiB
Rust
45 lines
1.3 KiB
Rust
//! Multipart payload support
|
|
use actix_web::{dev::Payload, Error, FromRequest, HttpRequest};
|
|
use futures_util::future::{ok, Ready};
|
|
|
|
use crate::server::Multipart;
|
|
|
|
/// Get request's payload as multipart stream
|
|
///
|
|
/// Content-type: multipart/form-data;
|
|
///
|
|
/// ## Server example
|
|
///
|
|
/// ```rust
|
|
/// use futures_util::stream::{Stream, StreamExt};
|
|
/// use actix_web::{web, HttpResponse, Error};
|
|
/// use actix_multipart as mp;
|
|
///
|
|
/// async fn index(mut payload: mp::Multipart) -> Result<HttpResponse, Error> {
|
|
/// // iterate over multipart stream
|
|
/// while let Some(item) = payload.next().await {
|
|
/// let mut field = item?;
|
|
///
|
|
/// // Field in turn is stream of *Bytes* object
|
|
/// while let Some(chunk) = field.next().await {
|
|
/// println!("-- CHUNK: \n{:?}", std::str::from_utf8(&chunk?));
|
|
/// }
|
|
/// }
|
|
/// Ok(HttpResponse::Ok().into())
|
|
/// }
|
|
/// # fn main() {}
|
|
/// ```
|
|
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(boundary, payload.take()),
|
|
Err(err) => Multipart::from_error(err),
|
|
})
|
|
}
|
|
}
|