1
1
//! Multipart body filters
2
2
//!
3
- //! Filters that extract a multipart body for a route.
3
+ //! [`Filter`](crate::Filter)s that extract a multipart body for a route.
4
4
5
5
use std:: error:: Error as StdError ;
6
6
use std:: fmt:: { Display , Formatter } ;
@@ -22,12 +22,12 @@ use crate::reject::{self, Rejection};
22
22
// If not otherwise configured, default to 2MB.
23
23
const DEFAULT_FORM_DATA_MAX_LENGTH : u64 = 1024 * 1024 * 2 ;
24
24
25
- /// A `Filter` to extract a `multipart/form-data` body from a request.
25
+ /// A [ `Filter`](crate::Filter) to extract a `multipart/form-data` body from a request.
26
26
///
27
27
/// Create with the `warp::multipart::form()` function.
28
28
#[ derive( Debug , Clone ) ]
29
29
pub struct FormOptions {
30
- max_length : u64 ,
30
+ max_length : Option < u64 > ,
31
31
}
32
32
33
33
/// A `Stream` of multipart/form-data `Part`s.
@@ -44,13 +44,13 @@ pub struct Part {
44
44
part : PartInner < ' static > ,
45
45
}
46
46
47
- /// Create a `Filter` to extract a `multipart/form-data` body from a request.
47
+ /// Create a [ `Filter`](crate::Filter) to extract a `multipart/form-data` body from a request.
48
48
///
49
49
/// The extracted `FormData` type is a `Stream` of `Part`s, and each `Part`
50
50
/// in turn is a `Stream` of bytes.
51
51
pub fn form ( ) -> FormOptions {
52
52
FormOptions {
53
- max_length : DEFAULT_FORM_DATA_MAX_LENGTH ,
53
+ max_length : Some ( DEFAULT_FORM_DATA_MAX_LENGTH ) ,
54
54
}
55
55
}
56
56
@@ -59,9 +59,10 @@ pub fn form() -> FormOptions {
59
59
impl FormOptions {
60
60
/// Set the maximum byte length allowed for this body.
61
61
///
62
+ /// `max_length(None)` means that maximum byte length is not checked.
62
63
/// Defaults to 2MB.
63
- pub fn max_length ( mut self , max : u64 ) -> Self {
64
- self . max_length = max;
64
+ pub fn max_length ( mut self , max : impl Into < Option < u64 > > ) -> Self {
65
+ self . max_length = max. into ( ) ;
65
66
self
66
67
}
67
68
}
@@ -83,8 +84,7 @@ impl FilterBase for FormOptions {
83
84
future:: ready ( mime)
84
85
} ) ;
85
86
86
- let filt = super :: body:: content_length_limit ( self . max_length )
87
- . and ( boundary)
87
+ let filt = boundary
88
88
. and ( super :: body:: body ( ) )
89
89
. map ( |boundary : String , body| {
90
90
let body = BodyIoError ( body) ;
@@ -93,9 +93,15 @@ impl FilterBase for FormOptions {
93
93
}
94
94
} ) ;
95
95
96
- let fut = filt. filter ( Internal ) ;
97
-
98
- Box :: pin ( fut)
96
+ if let Some ( max_length) = self . max_length {
97
+ Box :: pin (
98
+ super :: body:: content_length_limit ( max_length)
99
+ . and ( filt)
100
+ . filter ( Internal ) ,
101
+ )
102
+ } else {
103
+ Box :: pin ( filt. filter ( Internal ) )
104
+ }
99
105
}
100
106
}
101
107
@@ -142,7 +148,7 @@ impl Part {
142
148
/// Get the content-type of this part, if present.
143
149
pub fn content_type ( & self ) -> Option < & str > {
144
150
let content_type = self . part . content_type ( ) ;
145
- content_type. map ( |t| t. type_ ( ) . as_str ( ) )
151
+ content_type. map ( |t| t. as_ref ( ) )
146
152
}
147
153
148
154
/// Asynchronously get some of the data for this `Part`.
0 commit comments