Typed multipart form extractor

This commit is contained in:
Jacob Halsey 2022-09-23 19:15:26 +01:00
parent 73b94e902d
commit 67e14cce75
15 changed files with 1932 additions and 5 deletions

View File

@ -5,6 +5,7 @@ members = [
"actix-http-test",
"actix-http",
"actix-multipart",
"actix-multipart-derive",
"actix-router",
"actix-test",
"actix-web-actors",

View File

@ -0,0 +1,16 @@
[package]
name = "actix-multipart-derive"
version = "0.2.0"
authors = ["Jacob Halsey <jacob@jhalsey.com>"]
edition = "2021"
license = "MIT OR Apache-2.0"
[lib]
proc-macro = true
[dependencies]
darling = "0.14.1"
proc-macro2 = "1.0.37"
quote = "1.0.18"
syn = "1.0.92"
parse-size = "1.0.0"

View File

@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "{}"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright 2017-NOW Actix Team
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

View File

@ -0,0 +1,25 @@
Copyright (c) 2017-NOW Actix Team
Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the
Software without restriction, including without
limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice
shall be included in all copies or substantial portions
of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

View File

@ -0,0 +1,3 @@
# actix-multipart-derive
> The derive macro implementation for actix-multipart

View File

@ -0,0 +1,195 @@
extern crate proc_macro;
use darling::{FromDeriveInput, FromField, FromMeta};
use parse_size::parse_size;
use proc_macro2::{Ident, TokenStream};
use quote::quote;
use std::collections::HashSet;
use syn::{parse_macro_input, PathArguments, Type};
#[derive(FromDeriveInput, Default)]
#[darling(attributes(multipart), default)]
struct MultipartFormAttrs {
deny_unknown_fields: bool,
duplicate_action: DuplicateAction,
}
#[derive(FromMeta)]
enum DuplicateAction {
Ignore,
Deny,
Replace,
}
impl Default for DuplicateAction {
fn default() -> Self {
Self::Ignore
}
}
#[derive(FromField, Default)]
#[darling(attributes(multipart), default)]
struct FieldAttrs {
rename: Option<String>,
limit: Option<String>,
}
struct ParsedField<'t> {
serialization_name: String,
rust_name: &'t Ident,
limit: Option<usize>,
path: TokenStream,
}
#[proc_macro_derive(MultipartForm, attributes(multipart))]
pub fn impl_multipart_form(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input: syn::DeriveInput = parse_macro_input!(input);
let name = &input.ident;
let str = match &input.data {
syn::Data::Struct(s) => s,
_ => panic!("This trait can only be derived for a struct"),
};
let fields = match &str.fields {
syn::Fields::Named(n) => n,
_ => panic!("This trait can only be derived for a struct"),
};
let attrs: MultipartFormAttrs = match MultipartFormAttrs::from_derive_input(&input) {
Ok(attrs) => attrs,
Err(e) => return e.write_errors().into(),
};
// Parse the field attributes
let parsed = match fields
.named
.iter()
.map(|field| {
let rust_name = field.ident.as_ref().unwrap();
let attrs: FieldAttrs = FieldAttrs::from_field(field)?;
let serialization_name = attrs.rename.unwrap_or_else(|| rust_name.to_string());
let limit = attrs.limit.map(|l| {
parse_size(&l).unwrap_or_else(|_| panic!("Unable to parse limit `{l}`"))
as usize
});
// Converts `TextField<String>` into `TextField::<String>` where appropriate
let ty = &field.ty;
let mut tp = match ty {
Type::Path(ref p) => p,
_ => panic!("Field must be a TypePath"),
}
.clone();
let last = tp.path.segments.last_mut().unwrap();
let type_args = last.arguments.clone();
last.arguments = PathArguments::None;
let path = if matches!(type_args, PathArguments::None) {
quote!(#tp)
} else {
quote!(#tp::#type_args)
};
Ok(ParsedField {
serialization_name,
rust_name,
limit,
path,
})
})
.collect::<Result<Vec<_>, darling::Error>>()
{
Ok(attrs) => attrs,
Err(e) => return e.write_errors().into(),
};
// Check that field names are unique
let mut set = HashSet::new();
for f in &parsed {
if !set.insert(f.serialization_name.clone()) {
panic!("Multiple fields named: `{}`", f.serialization_name);
}
}
// Return value when a field name is not supported by the form
let unknown_field_result = if attrs.deny_unknown_fields {
quote!(::std::result::Result::Err(
::actix_multipart::MultipartError::UnsupportedField(field.name().to_string())
))
} else {
quote!(::std::result::Result::Ok(()))
};
// Value for duplicate action
let duplicate_action = match attrs.duplicate_action {
DuplicateAction::Ignore => quote!(::actix_multipart::form::DuplicateAction::Ignore),
DuplicateAction::Deny => quote!(::actix_multipart::form::DuplicateAction::Deny),
DuplicateAction::Replace => quote!(::actix_multipart::form::DuplicateAction::Replace),
};
// read_field() implementation
let mut read_field_impl = quote!();
for field in &parsed {
let name = &field.serialization_name;
let path = &field.path;
read_field_impl.extend(quote!(
#name => ::std::boxed::Box::pin(#path::handle_field(req, field, limits, state, #duplicate_action)),
));
}
// limit() implementation
let mut limit_impl = quote!();
for field in &parsed {
let name = &field.serialization_name;
if let Some(value) = field.limit {
limit_impl.extend(quote!(
#name => ::std::option::Option::Some(#value),
));
}
}
// from_state() implementation
let mut from_state_impl = quote!();
for field in &parsed {
let name = &field.serialization_name;
let rust_name = &field.rust_name;
let path = &field.path;
from_state_impl.extend(quote!(
#rust_name: #path::from_state(#name, &mut state)?,
));
}
let gen = quote! {
impl ::actix_multipart::form::MultipartFormTrait for #name {
fn limit(field_name: &str) -> ::std::option::Option<usize> {
match field_name {
#limit_impl
_ => None,
}
}
fn handle_field<'t>(
req: &'t ::actix_web::HttpRequest,
field: ::actix_multipart::Field,
limits: &'t mut ::actix_multipart::form::Limits,
state: &'t mut ::actix_multipart::form::State,
) -> ::std::pin::Pin<::std::boxed::Box<dyn ::std::future::Future<Output = ::std::result::Result<(), ::actix_multipart::MultipartError>> + 't>> {
use ::actix_multipart::form::FieldGroupReader;
match field.name() {
#read_field_impl
_ => return ::std::boxed::Box::pin(::std::future::ready(#unknown_field_result)),
}
}
fn from_state(mut state: ::actix_multipart::form::State) -> ::std::result::Result<Self, ::actix_multipart::MultipartError> {
use ::actix_multipart::form::FieldGroupReader;
Ok(Self {
#from_state_impl
})
}
}
};
gen.into()
}

View File

@ -2,10 +2,11 @@
## Unreleased - 2022-xx-xx
- Minimum supported Rust version (MSRV) is now 1.59 due to transitive `time` dependency.
- `Field::content_type()` now returns `Option<&mime::Mime>` [#2880]
- `Field::content_type()` now returns `Option<&mime::Mime>` [#2880].
- Added `MultipartForm` typed data extractor [#2883].
[#2880]: https://github.com/actix/actix-web/pull/2880
[#2883]: https://github.com/actix/actix-web/pull/2883
## 0.4.0 - 2022-02-25
- No significant changes since `0.4.0-beta.13`.

View File

@ -9,6 +9,10 @@ repository = "https://github.com/actix/actix-web.git"
license = "MIT OR Apache-2.0"
edition = "2018"
[features]
default = ["tempfile"]
tempfile = ["tempfile-dep", "tokio/fs"]
[lib]
name = "actix_multipart"
path = "src/lib.rs"
@ -16,19 +20,29 @@ path = "src/lib.rs"
[dependencies]
actix-utils = "3"
actix-web = { version = "4", default-features = false }
actix-http = "3"
actix-multipart-derive = { path = "../actix-multipart-derive" }
bytes = "1"
derive_more = "0.99.5"
futures-core = { version = "0.3.7", default-features = false, features = ["alloc"] }
futures-util = { version = "0.3.7", default-features = false }
httparse = "1.3"
local-waker = "0.1"
log = "0.4"
mime = "0.3"
twoway = "0.2"
serde = "1.0"
serde_plain = "1.0"
serde_json = "1.0"
# TODO: Replace with dep: prefix in newer versions of Cargo
tempfile-dep = { package = "tempfile", version = "3.3.0", optional = true }
tokio = { version = "1.8.4", features = ["sync"] }
[dev-dependencies]
actix-rt = "2.2"
actix-http = "3"
actix-test = "0.1.0"
awc = "3.0.1"
actix-multipart-rfc7578 = "0.10.0"
futures-util = { version = "0.3.7", default-features = false, features = ["alloc"] }
tokio = { version = "1.8.4", features = ["sync"] }
tokio-stream = "0.1"

View File

@ -46,12 +46,37 @@ pub enum MultipartError {
/// Not consumed
#[display(fmt = "Multipart stream is not consumed")]
NotConsumed,
/// An error from a field handler in a form
#[display(fmt = "An error occurred processing field `{field_name}`: {source}")]
Field {
field_name: String,
source: actix_web::Error,
},
/// Duplicate field
#[display(fmt = "Duplicate field found for: `{}`", _0)]
#[from(ignore)]
DuplicateField(#[error(not(source))] String),
/// Missing field
#[display(fmt = "Field with name `{}` is required", _0)]
#[from(ignore)]
MissingField(#[error(not(source))] String),
/// Unknown field
#[display(fmt = "Unsupported field `{}`", _0)]
#[from(ignore)]
UnsupportedField(#[error(not(source))] String),
}
/// Return `BadRequest` for `MultipartError`
impl ResponseError for MultipartError {
fn status_code(&self) -> StatusCode {
StatusCode::BAD_REQUEST
match &self {
MultipartError::Field { source, .. } => source.as_response_error().status_code(),
_ => StatusCode::BAD_REQUEST,
}
}
}

View File

@ -0,0 +1,46 @@
//! Reads a field into memory.
use crate::form::{FieldReader, Limits};
use crate::{Field, MultipartError};
use actix_web::HttpRequest;
use bytes::BytesMut;
use futures_core::future::LocalBoxFuture;
use futures_util::{FutureExt, TryStreamExt};
use mime::Mime;
/// Read the field into memory.
#[derive(Debug)]
pub struct Bytes {
/// The data.
pub data: bytes::Bytes,
/// The value of the `content-type` header.
pub content_type: Option<Mime>,
/// The `filename` value in the `content-disposition` header.
pub file_name: Option<String>,
}
impl<'t> FieldReader<'t> for Bytes {
type Future = LocalBoxFuture<'t, Result<Self, MultipartError>>;
fn read_field(
_: &'t HttpRequest,
mut field: Field,
limits: &'t mut Limits,
) -> Self::Future {
async move {
let mut data = BytesMut::new();
while let Some(chunk) = field.try_next().await? {
limits.try_consume_limits(chunk.len(), true)?;
data.extend(chunk);
}
Ok(Bytes {
data: data.freeze(),
content_type: field.content_type().map(ToOwned::to_owned),
file_name: field
.content_disposition()
.get_filename()
.map(str::to_owned),
})
}
.boxed_local()
}
}

View File

@ -0,0 +1,186 @@
//! Deserializes a field as JSON.
use crate::form::bytes::Bytes;
use crate::form::{FieldReader, Limits};
use crate::{Field, MultipartError};
use actix_web::http::StatusCode;
use actix_web::{web, Error, HttpRequest, ResponseError};
use derive_more::{Deref, DerefMut, Display, Error};
use futures_core::future::LocalBoxFuture;
use futures_util::FutureExt;
use serde::de::DeserializeOwned;
use std::sync::Arc;
/// Deserialize from JSON.
#[derive(Debug, Deref, DerefMut)]
pub struct Json<T: DeserializeOwned>(pub T);
impl<T: DeserializeOwned> Json<T> {
pub fn into_inner(self) -> T {
self.0
}
}
impl<'t, T: DeserializeOwned + 'static> FieldReader<'t> for Json<T> {
type Future = LocalBoxFuture<'t, Result<Self, MultipartError>>;
fn read_field(req: &'t HttpRequest, field: Field, limits: &'t mut Limits) -> Self::Future {
async move {
let config = JsonConfig::from_req(req);
let field_name = field.name().to_owned();
if config.validate_content_type {
let valid = if let Some(mime) = field.content_type() {
mime.subtype() == mime::JSON || mime.suffix() == Some(mime::JSON)
} else {
false
};
if !valid {
return Err(MultipartError::Field {
field_name,
source: config.map_error(req, JsonFieldError::ContentType),
});
}
}
let bytes = Bytes::read_field(req, field, limits).await?;
Ok(Json(serde_json::from_slice(bytes.data.as_ref()).map_err(
|e| MultipartError::Field {
field_name,
source: config.map_error(req, JsonFieldError::Deserialize(e)),
},
)?))
}
.boxed_local()
}
}
#[derive(Debug, Display, Error)]
#[non_exhaustive]
pub enum JsonFieldError {
/// Deserialize error
#[display(fmt = "Json deserialize error: {}", _0)]
Deserialize(serde_json::Error),
/// Content type error
#[display(fmt = "Content type error")]
ContentType,
}
impl ResponseError for JsonFieldError {
fn status_code(&self) -> StatusCode {
StatusCode::BAD_REQUEST
}
}
/// Configuration for the [`Json`] field reader.
#[derive(Clone)]
pub struct JsonConfig {
err_handler: Option<Arc<dyn Fn(JsonFieldError, &HttpRequest) -> Error + Send + Sync>>,
validate_content_type: bool,
}
const DEFAULT_CONFIG: JsonConfig = JsonConfig {
err_handler: None,
validate_content_type: true,
};
impl JsonConfig {
pub fn error_handler<F>(mut self, f: F) -> Self
where
F: Fn(JsonFieldError, &HttpRequest) -> Error + Send + Sync + 'static,
{
self.err_handler = Some(Arc::new(f));
self
}
/// Extract payload config from app data. Check both `T` and `Data<T>`, in that order, and fall
/// back to the default payload config.
fn from_req(req: &HttpRequest) -> &Self {
req.app_data::<Self>()
.or_else(|| req.app_data::<web::Data<Self>>().map(|d| d.as_ref()))
.unwrap_or(&DEFAULT_CONFIG)
}
fn map_error(&self, req: &HttpRequest, err: JsonFieldError) -> Error {
if let Some(err_handler) = self.err_handler.as_ref() {
(*err_handler)(err, req)
} else {
err.into()
}
}
/// Sets whether or not the field must have a valid `Content-Type` header to be parsed.
pub fn validate_content_type(mut self, validate_content_type: bool) -> Self {
self.validate_content_type = validate_content_type;
self
}
}
impl Default for JsonConfig {
fn default() -> Self {
DEFAULT_CONFIG
}
}
#[cfg(test)]
mod tests {
use crate::form::json::{Json, JsonConfig};
use crate::form::tests::send_form;
use crate::form::MultipartForm;
use actix_http::StatusCode;
use actix_multipart_rfc7578::client::multipart;
use actix_web::{web, App, HttpResponse, Responder};
use std::collections::HashMap;
use std::io::Cursor;
#[derive(MultipartForm)]
struct JsonForm {
json: Json<HashMap<String, String>>,
}
async fn test_json_route(form: MultipartForm<JsonForm>) -> impl Responder {
let mut expected = HashMap::new();
expected.insert("key1".to_owned(), "value1".to_owned());
expected.insert("key2".to_owned(), "value2".to_owned());
assert_eq!(&*form.json, &expected);
HttpResponse::Ok().finish()
}
#[actix_rt::test]
async fn test_json_without_content_type() {
let srv = actix_test::start(|| {
App::new()
.route("/", web::post().to(test_json_route))
.app_data(JsonConfig::default().validate_content_type(false))
});
let mut form = multipart::Form::default();
form.add_text("json", "{\"key1\": \"value1\", \"key2\": \"value2\"}");
let response = send_form(&srv, form, "/").await;
assert_eq!(response.status(), StatusCode::OK);
}
#[actix_rt::test]
async fn test_content_type_validation() {
let srv = actix_test::start(|| {
App::new()
.route("/", web::post().to(test_json_route))
.app_data(JsonConfig::default().validate_content_type(true))
});
// Deny because wrong content type
let bytes = Cursor::new("{\"key1\": \"value1\", \"key2\": \"value2\"}");
let mut form = multipart::Form::default();
form.add_reader_file_with_mime("json", bytes, "", mime::APPLICATION_OCTET_STREAM);
let response = send_form(&srv, form, "/").await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
// Allow because correct content type
let bytes = Cursor::new("{\"key1\": \"value1\", \"key2\": \"value2\"}");
let mut form = multipart::Form::default();
form.add_reader_file_with_mime("json", bytes, "", mime::APPLICATION_JSON);
let response = send_form(&srv, form, "/").await;
assert_eq!(response.status(), StatusCode::OK);
}
}

View File

@ -0,0 +1,832 @@
//! Process and extract typed data from a multipart stream.
pub mod bytes;
pub mod json;
#[cfg(feature = "tempfile")]
pub mod tempfile;
pub mod text;
use crate::{Field, Multipart, MultipartError};
use actix_http::error::PayloadError;
use actix_web::dev::Payload;
use actix_web::{web, Error, FromRequest, HttpRequest};
use derive_more::{Deref, DerefMut};
use futures_core::future::LocalBoxFuture;
use futures_util::TryFutureExt;
use futures_util::{FutureExt, TryStreamExt};
use std::any::Any;
use std::collections::HashMap;
use std::future::{ready, Future};
use std::sync::Arc;
/// Implements the [`MultipartFormTrait`] for a struct so that it can be used with the
/// [`struct@MultipartForm`] extractor.
///
/// ## Simple Example
///
/// Each field type should implement the [`FieldReader`] trait:
///
/// ```
/// # use actix_multipart::form::tempfile::Tempfile;
/// # use actix_multipart::form::text::Text;
/// # use actix_multipart::form::MultipartForm;
/// #[derive(MultipartForm)]
/// struct ImageUpload {
/// description: Text<String>,
/// timestamp: Text<i64>,
/// image: Tempfile,
/// }
/// ```
///
/// ## Optional and List Fields
///
/// You can also use `Vec<T>` and `Option<T>` provided that `T: FieldReader`.
///
/// A [`Vec`] field corresponds to an upload with multiple parts under the
/// [same field name](https://www.rfc-editor.org/rfc/rfc7578#section-4.3).
///
/// ```
/// # use actix_multipart::form::tempfile::Tempfile;
/// # use actix_multipart::form::text::Text;
/// # use actix_multipart::form::MultipartForm;
/// #[derive(MultipartForm)]
/// struct Form {
/// category: Option<Text<String>>,
/// files: Vec<Tempfile>,
/// }
/// ```
///
/// ## Field Renaming
///
/// You can use the `#[multipart(rename="")]` attribute to receive a field by a different name.
///
/// ```
/// # use actix_multipart::form::tempfile::Tempfile;
/// # use actix_multipart::form::MultipartForm;
/// #[derive(MultipartForm)]
/// struct Form {
/// #[multipart(rename="files[]")]
/// files: Vec<Tempfile>,
/// }
/// ```
///
/// ## Field Limits
///
/// You can use the `#[multipart(limit="")]` attribute to set field level limits. The limit
/// string is parsed using [parse_size](https://docs.rs/parse-size/1.0.0/parse_size/).
///
/// Note: the form is also subject to the global limits configured using the
/// [`MultipartFormConfig`].
///
/// ```
/// # use actix_multipart::form::tempfile::Tempfile;
/// # use actix_multipart::form::text::Text;
/// # use actix_multipart::form::MultipartForm;
/// #[derive(MultipartForm)]
/// struct Form {
/// #[multipart(limit="2KiB")]
/// description: Text<String>,
/// #[multipart(limit="512MiB")]
/// files: Vec<Tempfile>,
/// }
/// ```
///
/// ## Unknown Fields
///
/// By default fields with an unknown name are ignored. You can change this using the
/// `#[multipart(deny_unknown_fields)]` attribute:
///
/// ```
/// # use actix_multipart::form::MultipartForm;
/// #[derive(MultipartForm)]
/// #[multipart(deny_unknown_fields)]
/// struct Form { }
/// ```
///
/// ## Duplicate Fields
///
/// You can change the behaviour for when multiple fields are received with the same name using the
/// `#[multipart(duplicate_action = "")]` attribute:
///
/// - "ignore": Extra fields are ignored (default).
/// - "replace": Each field is processed, but only the last one is persisted.
/// - "deny": A [MultipartError::UnsupportedField] error is returned.
///
/// (Note this option does not apply to `Vec` fields)
///
/// ```
/// # use actix_multipart::form::MultipartForm;
/// #[derive(MultipartForm)]
/// #[multipart(duplicate_action = "deny")]
/// struct Form { }
/// ```
pub use actix_multipart_derive::MultipartForm;
/// Trait that data types to be used in a multipart form struct should implement.
///
/// It represents an asynchronous handler that processes a multipart field to produce `Self`.
pub trait FieldReader<'t>: Sized + Any {
/// Future that resolves to a `Self`.
type Future: Future<Output = Result<Self, MultipartError>>;
/// The form will call this function to handle the field.
fn read_field(req: &'t HttpRequest, field: Field, limits: &'t mut Limits) -> Self::Future;
}
/// Used to accumulate the state of the loaded fields.
#[doc(hidden)]
#[derive(Default, Deref, DerefMut)]
pub struct State(pub HashMap<String, Box<dyn Any>>);
// Trait that the field collection types implement, i.e. `Vec<T>`, `Option<T>`, or `T` itself.
#[doc(hidden)]
pub trait FieldGroupReader<'t>: Sized + Any {
type Future: Future<Output = Result<(), MultipartError>>;
/// The form will call this function for each matching field
fn handle_field(
req: &'t HttpRequest,
field: Field,
limits: &'t mut Limits,
state: &'t mut State,
duplicate_action: DuplicateAction,
) -> Self::Future;
/// Create `Self` from the group of processed fields
fn from_state(name: &str, state: &'t mut State) -> Result<Self, MultipartError>;
}
impl<'t, T> FieldGroupReader<'t> for Option<T>
where
T: FieldReader<'t>,
{
type Future = LocalBoxFuture<'t, Result<(), MultipartError>>;
fn handle_field(
req: &'t HttpRequest,
field: Field,
limits: &'t mut Limits,
state: &'t mut State,
duplicate_action: DuplicateAction,
) -> Self::Future {
if state.contains_key(field.name()) {
match duplicate_action {
DuplicateAction::Ignore => return ready(Ok(())).boxed_local(),
DuplicateAction::Deny => {
return ready(Err(MultipartError::DuplicateField(
field.name().to_string(),
)))
.boxed_local()
}
DuplicateAction::Replace => {}
}
}
async move {
let field_name = field.name().to_string();
let t = T::read_field(req, field, limits).await?;
state.insert(field_name, Box::new(t));
Ok(())
}
.boxed_local()
}
fn from_state(name: &str, state: &'t mut State) -> Result<Self, MultipartError> {
Ok(state.remove(name).map(|m| *m.downcast::<T>().unwrap()))
}
}
impl<'t, T> FieldGroupReader<'t> for Vec<T>
where
T: FieldReader<'t>,
{
type Future = LocalBoxFuture<'t, Result<(), MultipartError>>;
fn handle_field(
req: &'t HttpRequest,
field: Field,
limits: &'t mut Limits,
state: &'t mut State,
_duplicate_action: DuplicateAction,
) -> Self::Future {
// Vec GroupReader always allows duplicates!
async move {
let field_name = field.name().to_string();
let vec = state
.entry(field_name)
.or_insert_with(|| Box::new(Vec::<T>::new()))
.downcast_mut::<Vec<T>>()
.unwrap();
let item = T::read_field(req, field, limits).await?;
vec.push(item);
Ok(())
}
.boxed_local()
}
fn from_state(name: &str, state: &'t mut State) -> Result<Self, MultipartError> {
Ok(state
.remove(name)
.map(|m| *m.downcast::<Vec<T>>().unwrap())
.unwrap_or_default())
}
}
impl<'t, T> FieldGroupReader<'t> for T
where
T: FieldReader<'t>,
{
type Future = LocalBoxFuture<'t, Result<(), MultipartError>>;
fn handle_field(
req: &'t HttpRequest,
field: Field,
limits: &'t mut Limits,
state: &'t mut State,
duplicate_action: DuplicateAction,
) -> Self::Future {
if state.contains_key(field.name()) {
match duplicate_action {
DuplicateAction::Ignore => return ready(Ok(())).boxed_local(),
DuplicateAction::Deny => {
return ready(Err(MultipartError::DuplicateField(
field.name().to_string(),
)))
.boxed_local()
}
DuplicateAction::Replace => {}
}
}
async move {
let field_name = field.name().to_string();
let t = T::read_field(req, field, limits).await?;
state.insert(field_name, Box::new(t));
Ok(())
}
.boxed_local()
}
fn from_state(name: &str, state: &'t mut State) -> Result<Self, MultipartError> {
state
.remove(name)
.map(|m| *m.downcast::<T>().unwrap())
.ok_or_else(|| MultipartError::MissingField(name.to_owned()))
}
}
/// Trait that allows a type to be used in the [`struct@MultipartForm`] extractor. You should use
/// the [`macro@MultipartForm`] to implement this for your struct.
pub trait MultipartFormTrait: Sized {
/// An optional limit in bytes to be applied a given field name. Note this limit will be shared
/// across all fields sharing the same name.
fn limit(field_name: &str) -> Option<usize>;
/// The extractor will call this function for each incoming field, the state can be updated
/// with the processed field data.
fn handle_field<'t>(
req: &'t HttpRequest,
field: Field,
limits: &'t mut Limits,
state: &'t mut State,
) -> LocalBoxFuture<'t, Result<(), MultipartError>>;
/// Once all the fields have been processed and stored in the state, this is called
/// to convert into the struct representation.
fn from_state(state: State) -> Result<Self, MultipartError>;
}
#[doc(hidden)]
pub enum DuplicateAction {
/// Additional fields are not processed
Ignore,
/// An error will be raised
Deny,
/// All fields will be processed, the last one will replace all previous
Replace,
}
/// Used to keep track of the remaining limits for the form and current field.
pub struct Limits {
pub total_limit_remaining: usize,
pub memory_limit_remaining: usize,
pub field_limit_remaining: Option<usize>,
}
impl Limits {
pub fn new(total_limit: usize, memory_limit: usize) -> Self {
Self {
total_limit_remaining: total_limit,
memory_limit_remaining: memory_limit,
field_limit_remaining: None,
}
}
/// This function should be called within a [`FieldReader`] when reading each chunk of a field
/// to ensure that the form limits are not exceeded.
///
/// # Arguments
///
/// * `bytes` - The number of bytes being read from this chunk
/// * `in_memory` - Whether to consume from the memory limits
pub fn try_consume_limits(
&mut self,
bytes: usize,
in_memory: bool,
) -> Result<(), MultipartError> {
self.total_limit_remaining = self
.total_limit_remaining
.checked_sub(bytes)
.ok_or(MultipartError::Payload(PayloadError::Overflow))?;
if in_memory {
self.memory_limit_remaining = self
.memory_limit_remaining
.checked_sub(bytes)
.ok_or(MultipartError::Payload(PayloadError::Overflow))?;
}
if let Some(field_limit) = self.field_limit_remaining {
self.field_limit_remaining = Some(
field_limit
.checked_sub(bytes)
.ok_or(MultipartError::Payload(PayloadError::Overflow))?,
);
}
Ok(())
}
}
/// Typed `multipart/form-data` extractor.
///
/// To extract typed data from a multipart stream, the inner type `T` must implement the
/// [`MultipartFormTrait`] trait, you should use the [`macro@MultipartForm`] macro to derive this for
/// your struct.
///
/// Use [`MultipartFormConfig`] to configure extraction options.
#[derive(Deref, DerefMut)]
pub struct MultipartForm<T: MultipartFormTrait>(pub T);
impl<T: MultipartFormTrait> MultipartForm<T> {
/// Unwrap into inner `T` value.
pub fn into_inner(self) -> T {
self.0
}
}
impl<T> FromRequest for MultipartForm<T>
where
T: MultipartFormTrait,
{
type Error = Error;
type Future = LocalBoxFuture<'static, Result<Self, Self::Error>>;
#[inline]
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
let mut payload = Multipart::new(req.headers(), payload.take());
let config = MultipartFormConfig::from_req(req);
let mut limits = Limits::new(config.total_limit, config.memory_limit);
let req = req.clone();
let req2 = req.clone();
let err_handler = config.err_handler.clone();
async move {
let mut state = State::default();
// We need to ensure field limits are shared for all instances of this field name
let mut field_limits = HashMap::<String, Option<usize>>::new();
while let Some(field) = payload.try_next().await? {
// Retrieve the limit for this field
let entry = field_limits
.entry(field.name().to_owned())
.or_insert_with(|| T::limit(field.name()));
limits.field_limit_remaining = entry.to_owned();
T::handle_field(&req, field, &mut limits, &mut state).await?;
// Update the stored limit
*entry = limits.field_limit_remaining;
}
let inner = T::from_state(state)?;
Ok(MultipartForm(inner))
}
.map_err(move |e| {
if let Some(handler) = err_handler {
(*handler)(e, &req2)
} else {
e.into()
}
})
.boxed_local()
}
}
type MultipartFormErrorHandler =
Option<Arc<dyn Fn(MultipartError, &HttpRequest) -> Error + Send + Sync>>;
/// [`struct@MultipartForm`] extractor configuration.
#[derive(Clone)]
pub struct MultipartFormConfig {
total_limit: usize,
memory_limit: usize,
err_handler: MultipartFormErrorHandler,
}
impl MultipartFormConfig {
/// Set maximum accepted payload size for the entire form. By default this limit is 50MiB.
pub fn total_limit(mut self, total_limit: usize) -> Self {
self.total_limit = total_limit;
self
}
/// Set maximum accepted data that will be read into memory. By default this limit is 2MiB.
pub fn memory_limit(mut self, memory_limit: usize) -> Self {
self.memory_limit = memory_limit;
self
}
/// Set custom error handler.
pub fn error_handler<F>(mut self, f: F) -> Self
where
F: Fn(MultipartError, &HttpRequest) -> Error + Send + Sync + 'static,
{
self.err_handler = Some(Arc::new(f));
self
}
/// Extract payload config from app data. Check both `T` and `Data<T>`, in that order, and fall
/// back to the default payload config.
fn from_req(req: &HttpRequest) -> &Self {
req.app_data::<Self>()
.or_else(|| req.app_data::<web::Data<Self>>().map(|d| d.as_ref()))
.unwrap_or(&DEFAULT_CONFIG)
}
}
const DEFAULT_CONFIG: MultipartFormConfig = MultipartFormConfig {
total_limit: 52_428_800, // 50 MiB
memory_limit: 2_097_152, // 2 MiB
err_handler: None,
};
impl Default for MultipartFormConfig {
fn default() -> Self {
DEFAULT_CONFIG.clone()
}
}
#[cfg(test)]
mod tests {
use super::MultipartForm;
use crate::form::bytes::Bytes;
use crate::form::tempfile::Tempfile;
use crate::form::text::Text;
use crate::form::MultipartFormConfig;
use actix_http::encoding::Decoder;
use actix_http::Payload;
use actix_multipart_rfc7578::client::multipart;
use actix_test::TestServer;
use actix_web::http::StatusCode;
use actix_web::{web, App, HttpResponse, Responder};
use awc::{Client, ClientResponse};
pub async fn send_form(
srv: &TestServer,
form: multipart::Form<'static>,
uri: &'static str,
) -> ClientResponse<Decoder<Payload>> {
Client::default()
.post(srv.url(uri))
.content_type(form.content_type())
.send_body(multipart::Body::from(form))
.await
.unwrap()
}
/// Test `Option` fields
#[derive(MultipartForm)]
struct TestOptions {
field1: Option<Text<String>>,
field2: Option<Text<String>>,
}
async fn test_options_route(form: MultipartForm<TestOptions>) -> impl Responder {
assert!(form.field1.is_some());
assert!(form.field2.is_none());
HttpResponse::Ok().finish()
}
#[actix_rt::test]
async fn test_options() {
let srv =
actix_test::start(|| App::new().route("/", web::post().to(test_options_route)));
let mut form = multipart::Form::default();
form.add_text("field1", "value");
let response = send_form(&srv, form, "/").await;
assert_eq!(response.status(), StatusCode::OK);
}
/// Test `Vec` fields
#[derive(MultipartForm)]
struct TestVec {
list1: Vec<Text<String>>,
list2: Vec<Text<String>>,
}
async fn test_vec_route(form: MultipartForm<TestVec>) -> impl Responder {
let form = form.into_inner();
let strings = form
.list1
.into_iter()
.map(|s| s.into_inner())
.collect::<Vec<_>>();
assert_eq!(strings, vec!["value1", "value2", "value3"]);
assert_eq!(form.list2.len(), 0);
HttpResponse::Ok().finish()
}
#[actix_rt::test]
async fn test_vec() {
let srv = actix_test::start(|| App::new().route("/", web::post().to(test_vec_route)));
let mut form = multipart::Form::default();
form.add_text("list1", "value1");
form.add_text("list1", "value2");
form.add_text("list1", "value3");
let response = send_form(&srv, form, "/").await;
assert_eq!(response.status(), StatusCode::OK);
}
/// Test the `rename` field attribute
#[derive(MultipartForm)]
struct TestFieldRenaming {
#[multipart(rename = "renamed")]
field1: Text<String>,
#[multipart(rename = "field1")]
field2: Text<String>,
field3: Text<String>,
}
async fn test_field_renaming_route(
form: MultipartForm<TestFieldRenaming>,
) -> impl Responder {
assert_eq!(&*form.field1, "renamed");
assert_eq!(&*form.field2, "field1");
assert_eq!(&*form.field3, "field3");
HttpResponse::Ok().finish()
}
#[actix_rt::test]
async fn test_field_renaming() {
let srv = actix_test::start(|| {
App::new().route("/", web::post().to(test_field_renaming_route))
});
let mut form = multipart::Form::default();
form.add_text("renamed", "renamed");
form.add_text("field1", "field1");
form.add_text("field3", "field3");
let response = send_form(&srv, form, "/").await;
assert_eq!(response.status(), StatusCode::OK);
}
/// Test the `deny_unknown_fields` struct attribute
#[derive(MultipartForm)]
#[multipart(deny_unknown_fields)]
struct TestDenyUnknown {}
#[derive(MultipartForm)]
struct TestAllowUnknown {}
async fn test_deny_unknown_route(_: MultipartForm<TestDenyUnknown>) -> impl Responder {
HttpResponse::Ok().finish()
}
async fn test_allow_unknown_route(_: MultipartForm<TestAllowUnknown>) -> impl Responder {
HttpResponse::Ok().finish()
}
#[actix_rt::test]
async fn test_deny_unknown() {
let srv = actix_test::start(|| {
App::new()
.route("/deny", web::post().to(test_deny_unknown_route))
.route("/allow", web::post().to(test_allow_unknown_route))
});
let mut form = multipart::Form::default();
form.add_text("unknown", "value");
let response = send_form(&srv, form, "/deny").await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
let mut form = multipart::Form::default();
form.add_text("unknown", "value");
let response = send_form(&srv, form, "/allow").await;
assert_eq!(response.status(), StatusCode::OK);
}
/// Test the `duplicate_action` struct attribute
#[derive(MultipartForm)]
#[multipart(duplicate_action = "deny")]
struct TestDuplicateDeny {
_field: Text<String>,
}
#[derive(MultipartForm)]
#[multipart(duplicate_action = "replace")]
struct TestDuplicateReplace {
field: Text<String>,
}
#[derive(MultipartForm)]
#[multipart(duplicate_action = "ignore")]
struct TestDuplicateIgnore {
field: Text<String>,
}
async fn test_duplicate_deny_route(_: MultipartForm<TestDuplicateDeny>) -> impl Responder {
HttpResponse::Ok().finish()
}
async fn test_duplicate_replace_route(
form: MultipartForm<TestDuplicateReplace>,
) -> impl Responder {
assert_eq!(&*form.field, "second_value");
HttpResponse::Ok().finish()
}
async fn test_duplicate_ignore_route(
form: MultipartForm<TestDuplicateIgnore>,
) -> impl Responder {
assert_eq!(&*form.field, "first_value");
HttpResponse::Ok().finish()
}
#[actix_rt::test]
async fn test_duplicate_action() {
let srv = actix_test::start(|| {
App::new()
.route("/deny", web::post().to(test_duplicate_deny_route))
.route("/replace", web::post().to(test_duplicate_replace_route))
.route("/ignore", web::post().to(test_duplicate_ignore_route))
});
let mut form = multipart::Form::default();
form.add_text("_field", "first_value");
form.add_text("_field", "second_value");
let response = send_form(&srv, form, "/deny").await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
let mut form = multipart::Form::default();
form.add_text("field", "first_value");
form.add_text("field", "second_value");
let response = send_form(&srv, form, "/replace").await;
assert_eq!(response.status(), StatusCode::OK);
let mut form = multipart::Form::default();
form.add_text("field", "first_value");
form.add_text("field", "second_value");
let response = send_form(&srv, form, "/ignore").await;
assert_eq!(response.status(), StatusCode::OK);
}
/// Test the Limits
#[derive(MultipartForm)]
struct TestMemoryUploadLimits {
field: Bytes,
}
#[derive(MultipartForm)]
struct TestFileUploadLimits {
field: Tempfile,
}
async fn test_upload_limits_memory(
form: MultipartForm<TestMemoryUploadLimits>,
) -> impl Responder {
assert!(form.field.data.len() > 0);
HttpResponse::Ok().finish()
}
async fn test_upload_limits_file(
form: MultipartForm<TestFileUploadLimits>,
) -> impl Responder {
assert!(form.field.size > 0);
HttpResponse::Ok().finish()
}
#[actix_rt::test]
async fn test_memory_limits() {
let srv = actix_test::start(|| {
App::new()
.route("/text", web::post().to(test_upload_limits_memory))
.route("/file", web::post().to(test_upload_limits_file))
.app_data(
MultipartFormConfig::default()
.memory_limit(20)
.total_limit(usize::MAX),
)
});
// Exceeds the 20 byte memory limit
let mut form = multipart::Form::default();
form.add_text("field", "this string is 28 bytes long");
let response = send_form(&srv, form, "/text").await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
// Memory limit should not apply when the data is being streamed to disk
let mut form = multipart::Form::default();
form.add_text("field", "this string is 28 bytes long");
let response = send_form(&srv, form, "/file").await;
assert_eq!(response.status(), StatusCode::OK);
}
#[actix_rt::test]
async fn test_total_limit() {
let srv = actix_test::start(|| {
App::new()
.route("/text", web::post().to(test_upload_limits_memory))
.route("/file", web::post().to(test_upload_limits_file))
.app_data(
MultipartFormConfig::default()
.memory_limit(usize::MAX)
.total_limit(20),
)
});
// Within the 20 byte limit
let mut form = multipart::Form::default();
form.add_text("field", "7 bytes");
let response = send_form(&srv, form, "/text").await;
assert_eq!(response.status(), StatusCode::OK);
// Exceeds the 20 byte overall limit
let mut form = multipart::Form::default();
form.add_text("field", "this string is 28 bytes long");
let response = send_form(&srv, form, "/text").await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
// Exceeds the 20 byte overall limit
let mut form = multipart::Form::default();
form.add_text("field", "this string is 28 bytes long");
let response = send_form(&srv, form, "/file").await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
}
#[derive(MultipartForm)]
struct TestFieldLevelLimits {
#[multipart(limit = "30B")]
field: Vec<Bytes>,
}
async fn test_field_level_limits_route(
form: MultipartForm<TestFieldLevelLimits>,
) -> impl Responder {
assert!(form.field.len() > 0);
HttpResponse::Ok().finish()
}
#[actix_rt::test]
async fn test_field_level_limits() {
let srv = actix_test::start(|| {
App::new()
.route("/", web::post().to(test_field_level_limits_route))
.app_data(
MultipartFormConfig::default()
.memory_limit(usize::MAX)
.total_limit(usize::MAX),
)
});
// Within the 30 byte limit
let mut form = multipart::Form::default();
form.add_text("field", "this string is 28 bytes long");
let response = send_form(&srv, form, "/").await;
assert_eq!(response.status(), StatusCode::OK);
// Exceeds the the 30 byte limit
let mut form = multipart::Form::default();
form.add_text("field", "this string is more than 30 bytes long");
let response = send_form(&srv, form, "/").await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
// Total of values (14 bytes) is within 30 byte limit for "field"
let mut form = multipart::Form::default();
form.add_text("field", "7 bytes");
form.add_text("field", "7 bytes");
let response = send_form(&srv, form, "/").await;
assert_eq!(response.status(), StatusCode::OK);
// Total of values exceeds 30 byte limit for "field"
let mut form = multipart::Form::default();
form.add_text("field", "this string is 28 bytes long");
form.add_text("field", "this string is 28 bytes long");
let response = send_form(&srv, form, "/").await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
}
}

View File

@ -0,0 +1,189 @@
//! Writes a field to a temporary file on disk.
use crate::form::tempfile::TempfileError::FileIo;
use crate::form::{FieldReader, Limits};
use crate::{Field, MultipartError};
use actix_web::http::StatusCode;
use actix_web::{web, Error, HttpRequest, ResponseError};
use derive_more::{Display, Error};
use futures_core::future::LocalBoxFuture;
use futures_util::{FutureExt, TryStreamExt};
use mime::Mime;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use tempfile_dep::NamedTempFile;
use tokio::io::AsyncWriteExt;
/// Write the field to a temporary file on disk.
#[derive(Debug)]
pub struct Tempfile {
/// The temporary file on disk.
pub file: NamedTempFile,
/// The value of the `content-type` header.
pub content_type: Option<Mime>,
/// The `filename` value in the `content-disposition` header.
pub file_name: Option<String>,
/// The size in bytes of the file.
pub size: usize,
}
impl<'t> FieldReader<'t> for Tempfile {
type Future = LocalBoxFuture<'t, Result<Self, MultipartError>>;
fn read_field(
req: &'t HttpRequest,
mut field: Field,
limits: &'t mut Limits,
) -> Self::Future {
async move {
let config = TempfileConfig::from_req(req);
let field_name = field.name().to_owned();
let mut size = 0;
let file = if let Some(dir) = &config.directory {
NamedTempFile::new_in(dir)
} else {
NamedTempFile::new()
}
.map_err(|e| config.map_error(req, &field_name, FileIo(e)))?;
let mut file_async = tokio::fs::File::from_std(
file.reopen()
.map_err(|e| config.map_error(req, &field_name, FileIo(e)))?,
);
while let Some(chunk) = field.try_next().await? {
limits.try_consume_limits(chunk.len(), false)?;
size += chunk.len();
file_async
.write_all(chunk.as_ref())
.await
.map_err(|e| config.map_error(req, &field_name, FileIo(e)))?;
}
file_async
.flush()
.await
.map_err(|e| config.map_error(req, &field_name, FileIo(e)))?;
Ok(Tempfile {
file,
content_type: field.content_type().map(ToOwned::to_owned),
file_name: field
.content_disposition()
.get_filename()
.map(str::to_owned),
size,
})
}
.boxed_local()
}
}
#[derive(Debug, Display, Error)]
#[non_exhaustive]
pub enum TempfileError {
/// IO Error
#[display(fmt = "File I/O error: {}", _0)]
FileIo(std::io::Error),
}
impl ResponseError for TempfileError {
fn status_code(&self) -> StatusCode {
StatusCode::INTERNAL_SERVER_ERROR
}
}
/// Configuration for the [`Tempfile`] field reader.
#[derive(Clone)]
pub struct TempfileConfig {
err_handler: Option<Arc<dyn Fn(TempfileError, &HttpRequest) -> Error + Send + Sync>>,
directory: Option<PathBuf>,
}
const DEFAULT_CONFIG: TempfileConfig = TempfileConfig {
err_handler: None,
directory: None,
};
impl TempfileConfig {
pub fn error_handler<F>(mut self, f: F) -> Self
where
F: Fn(TempfileError, &HttpRequest) -> Error + Send + Sync + 'static,
{
self.err_handler = Some(Arc::new(f));
self
}
/// Extract payload config from app data. Check both `T` and `Data<T>`, in that order, and fall
/// back to the default payload config.
fn from_req(req: &HttpRequest) -> &Self {
req.app_data::<Self>()
.or_else(|| req.app_data::<web::Data<Self>>().map(|d| d.as_ref()))
.unwrap_or(&DEFAULT_CONFIG)
}
fn map_error(
&self,
req: &HttpRequest,
field_name: &str,
err: TempfileError,
) -> MultipartError {
let source = if let Some(err_handler) = self.err_handler.as_ref() {
(*err_handler)(err, req)
} else {
err.into()
};
MultipartError::Field {
field_name: field_name.to_owned(),
source,
}
}
/// Set the directory tempfiles will be created in.
pub fn directory<P: AsRef<Path>>(mut self, dir: P) -> Self {
self.directory = Some(dir.as_ref().to_owned());
self
}
}
impl Default for TempfileConfig {
fn default() -> Self {
DEFAULT_CONFIG
}
}
#[cfg(test)]
mod tests {
use crate::form::tempfile::Tempfile;
use crate::form::tests::send_form;
use crate::form::MultipartForm;
use actix_http::StatusCode;
use actix_multipart_rfc7578::client::multipart;
use actix_web::{web, App, HttpResponse, Responder};
use std::io::{Cursor, Read};
#[derive(MultipartForm)]
struct FileForm {
file: Tempfile,
}
async fn test_file_route(form: MultipartForm<FileForm>) -> impl Responder {
let mut form = form.into_inner();
let mut contents = String::new();
form.file.file.read_to_string(&mut contents).unwrap();
assert_eq!(contents, "Hello, world!");
assert_eq!(form.file.file_name.unwrap(), "testfile.txt");
assert_eq!(form.file.content_type.unwrap(), mime::TEXT_PLAIN);
HttpResponse::Ok().finish()
}
#[actix_rt::test]
async fn test_file_upload() {
let srv = actix_test::start(|| App::new().route("/", web::post().to(test_file_route)));
let mut form = multipart::Form::default();
let bytes = Cursor::new("Hello, world!");
form.add_reader_file_with_mime("file", bytes, "testfile.txt", mime::TEXT_PLAIN);
let response = send_form(&srv, form, "/").await;
assert_eq!(response.status(), StatusCode::OK);
}
}

View File

@ -0,0 +1,186 @@
//! Deserializes a field from plain text.
use crate::form::bytes::Bytes;
use crate::form::{FieldReader, Limits};
use crate::{Field, MultipartError};
use actix_web::http::StatusCode;
use actix_web::{web, Error, HttpRequest, ResponseError};
use derive_more::{Deref, DerefMut, Display, Error};
use futures_core::future::LocalBoxFuture;
use futures_util::FutureExt;
use serde::de::DeserializeOwned;
use std::sync::Arc;
/// Deserialize from plain text.
///
/// Internally this uses [`serde_plain`] for deserialization, which supports primitive types
/// including strings, numbers, and simple enums.
#[derive(Debug, Deref, DerefMut)]
pub struct Text<T: DeserializeOwned>(pub T);
impl<T: DeserializeOwned> Text<T> {
pub fn into_inner(self) -> T {
self.0
}
}
impl<'t, T: DeserializeOwned + 'static> FieldReader<'t> for Text<T> {
type Future = LocalBoxFuture<'t, Result<Self, MultipartError>>;
fn read_field(req: &'t HttpRequest, field: Field, limits: &'t mut Limits) -> Self::Future {
async move {
let config = TextConfig::from_req(req);
let field_name = field.name().to_owned();
if config.validate_content_type {
let valid = if let Some(mime) = field.content_type() {
mime.subtype() == mime::PLAIN || mime.suffix() == Some(mime::PLAIN)
} else {
// https://www.rfc-editor.org/rfc/rfc7578#section-4.4
// content type defaults to text/plain, so None should be considered valid
true
};
if !valid && config.validate_content_type {
return Err(MultipartError::Field {
field_name,
source: config.map_error(req, TextError::ContentType),
});
}
}
let bytes = Bytes::read_field(req, field, limits).await?;
let text = std::str::from_utf8(bytes.data.as_ref()).map_err(|e| {
MultipartError::Field {
field_name: field_name.clone(),
source: config.map_error(req, TextError::Utf8Error(e)),
}
})?;
Ok(Text(serde_plain::from_str(text).map_err(|e| {
MultipartError::Field {
field_name,
source: config.map_error(req, TextError::Deserialize(e)),
}
})?))
}
.boxed_local()
}
}
#[derive(Debug, Display, Error)]
#[non_exhaustive]
pub enum TextError {
/// Utf8 error
#[display(fmt = "Utf8 decoding error: {}", _0)]
Utf8Error(std::str::Utf8Error),
/// Deserialize error
#[display(fmt = "Plain text deserialize error: {}", _0)]
Deserialize(serde_plain::Error),
/// Content type error
#[display(fmt = "Content type error")]
ContentType,
}
impl ResponseError for TextError {
fn status_code(&self) -> StatusCode {
StatusCode::BAD_REQUEST
}
}
/// Configuration for the [`Text`] field reader.
#[derive(Clone)]
pub struct TextConfig {
err_handler: Option<Arc<dyn Fn(TextError, &HttpRequest) -> Error + Send + Sync>>,
validate_content_type: bool,
}
const DEFAULT_CONFIG: TextConfig = TextConfig {
err_handler: None,
validate_content_type: true,
};
impl TextConfig {
pub fn error_handler<F>(mut self, f: F) -> Self
where
F: Fn(TextError, &HttpRequest) -> Error + Send + Sync + 'static,
{
self.err_handler = Some(Arc::new(f));
self
}
/// Extract payload config from app data. Check both `T` and `Data<T>`, in that order, and fall
/// back to the default payload config.
fn from_req(req: &HttpRequest) -> &Self {
req.app_data::<Self>()
.or_else(|| req.app_data::<web::Data<Self>>().map(|d| d.as_ref()))
.unwrap_or(&DEFAULT_CONFIG)
}
fn map_error(&self, req: &HttpRequest, err: TextError) -> Error {
if let Some(err_handler) = self.err_handler.as_ref() {
(*err_handler)(err, req)
} else {
err.into()
}
}
/// Sets whether or not the field must have a valid `Content-Type` header to be parsed.
/// Note that an empty `Content-Type` is also accepted, as the multipart specification defines
/// `text/plain` as the default for text fields.
pub fn validate_content_type(mut self, validate_content_type: bool) -> Self {
self.validate_content_type = validate_content_type;
self
}
}
impl Default for TextConfig {
fn default() -> Self {
DEFAULT_CONFIG
}
}
#[cfg(test)]
mod tests {
use crate::form::tests::send_form;
use crate::form::text::{Text, TextConfig};
use crate::form::MultipartForm;
use actix_http::StatusCode;
use actix_multipart_rfc7578::client::multipart;
use actix_web::{web, App, HttpResponse, Responder};
use std::io::Cursor;
#[derive(MultipartForm)]
struct TextForm {
number: Text<i32>,
}
async fn test_text_route(form: MultipartForm<TextForm>) -> impl Responder {
assert_eq!(*form.number, 1025);
HttpResponse::Ok().finish()
}
#[actix_rt::test]
async fn test_content_type_validation() {
let srv = actix_test::start(|| {
App::new()
.route("/", web::post().to(test_text_route))
.app_data(TextConfig::default().validate_content_type(true))
});
// Deny because wrong content type
let bytes = Cursor::new("1025");
let mut form = multipart::Form::default();
form.add_reader_file_with_mime("number", bytes, "", mime::APPLICATION_OCTET_STREAM);
let response = send_form(&srv, form, "/").await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
// Allow because correct content type
let bytes = Cursor::new("1025");
let mut form = multipart::Form::default();
form.add_reader_file_with_mime("number", bytes, "", mime::TEXT_PLAIN);
let response = send_form(&srv, form, "/").await;
assert_eq!(response.status(), StatusCode::OK);
}
}

View File

@ -3,10 +3,17 @@
#![deny(rust_2018_idioms, nonstandard_style)]
#![warn(future_incompatible)]
#![allow(clippy::borrow_interior_mutable_const)]
#![cfg_attr(docsrs, feature(doc_cfg))]
// This allows us to use the actix_multipart_derive within this crate's tests
#[cfg(test)]
extern crate self as actix_multipart;
mod error;
mod extractor;
mod server;
pub mod form;
pub use self::error::MultipartError;
pub use self::server::{Field, Multipart};