2019-04-03 12:28:58 -07:00
|
|
|
//! Multipart payload support
|
2021-04-01 15:26:13 +01:00
|
|
|
|
|
|
|
use actix_utils::future::{ready, Ready};
|
2019-04-13 14:50:54 -07:00
|
|
|
use actix_web::{dev::Payload, Error, FromRequest, HttpRequest};
|
2019-04-03 12:28:58 -07:00
|
|
|
|
|
|
|
use crate::server::Multipart;
|
|
|
|
|
2021-04-01 15:26:13 +01:00
|
|
|
/// Get request's payload as multipart stream.
|
2019-04-03 12:28:58 -07:00
|
|
|
///
|
|
|
|
/// Content-type: multipart/form-data;
|
|
|
|
///
|
|
|
|
/// ## Server example
|
|
|
|
///
|
2021-03-25 08:45:52 +00:00
|
|
|
/// ```
|
2019-04-03 12:28:58 -07:00
|
|
|
/// use actix_web::{web, HttpResponse, Error};
|
2021-04-01 15:26:13 +01:00
|
|
|
/// use actix_multipart::Multipart;
|
|
|
|
/// use futures_util::stream::StreamExt as _;
|
2019-04-03 12:28:58 -07:00
|
|
|
///
|
2021-04-01 15:26:13 +01:00
|
|
|
/// async fn index(mut payload: Multipart) -> Result<HttpResponse, Error> {
|
2019-11-21 14:25:50 +06:00
|
|
|
/// // iterate over multipart stream
|
|
|
|
/// while let Some(item) = payload.next().await {
|
|
|
|
/// let mut field = item?;
|
|
|
|
///
|
2019-04-13 10:11:07 -07:00
|
|
|
/// // Field in turn is stream of *Bytes* object
|
2019-11-21 14:25:50 +06:00
|
|
|
/// while let Some(chunk) = field.next().await {
|
|
|
|
/// println!("-- CHUNK: \n{:?}", std::str::from_utf8(&chunk?));
|
|
|
|
/// }
|
|
|
|
/// }
|
2021-04-01 15:26:13 +01:00
|
|
|
///
|
2019-11-21 14:25:50 +06:00
|
|
|
/// Ok(HttpResponse::Ok().into())
|
2019-04-03 12:28:58 -07:00
|
|
|
/// }
|
|
|
|
/// ```
|
2019-04-13 14:50:54 -07:00
|
|
|
impl FromRequest for Multipart {
|
2019-04-03 12:28:58 -07:00
|
|
|
type Error = Error;
|
2019-11-21 14:25:50 +06:00
|
|
|
type Future = Ready<Result<Multipart, Error>>;
|
2019-04-03 12:28:58 -07:00
|
|
|
|
|
|
|
#[inline]
|
2019-04-13 14:50:54 -07:00
|
|
|
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
|
2021-04-01 15:26:13 +01:00
|
|
|
ready(Ok(match Multipart::boundary(req.headers()) {
|
2020-09-25 15:50:37 +02:00
|
|
|
Ok(boundary) => Multipart::from_boundary(boundary, payload.take()),
|
|
|
|
Err(err) => Multipart::from_error(err),
|
2021-04-01 15:26:13 +01:00
|
|
|
}))
|
2019-04-03 12:28:58 -07:00
|
|
|
}
|
|
|
|
}
|