1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-27 17:52:56 +01:00

defer parsing until user method call

This commit is contained in:
axon-q 2018-06-07 17:29:46 +00:00
parent 97b5410aad
commit 56e0dc06c1
2 changed files with 11 additions and 21 deletions

View File

@ -353,9 +353,6 @@ pub enum MultipartError {
/// Can not parse Content-Type header /// Can not parse Content-Type header
#[fail(display = "Can not parse Content-Type header")] #[fail(display = "Can not parse Content-Type header")]
ParseContentType, ParseContentType,
/// Can not parse Content-Disposition header
#[fail(display = "Can not parse Content-Disposition header")]
ParseContentDisposition,
/// Multipart boundary is not found /// Multipart boundary is not found
#[fail(display = "Multipart boundary is not found")] #[fail(display = "Multipart boundary is not found")]
Boundary, Boundary,

View File

@ -317,15 +317,6 @@ where
return Ok(Async::NotReady); return Ok(Async::NotReady);
}; };
// content disposition
// RFC 7578: 'Each part MUST contain a Content-Disposition header field
// where the disposition type is "form-data".'
let cd = match headers.get(::http::header::CONTENT_DISPOSITION) {
Some(content_disposition) => ContentDisposition::from_raw(content_disposition)
.map_err(|_| MultipartError::ParseContentDisposition)?,
None => return Err(MultipartError::ParseContentDisposition)
};
// content type // content type
let mut mt = mime::APPLICATION_OCTET_STREAM; let mut mt = mime::APPLICATION_OCTET_STREAM;
if let Some(content_type) = headers.get(header::CONTENT_TYPE) { if let Some(content_type) = headers.get(header::CONTENT_TYPE) {
@ -369,7 +360,6 @@ where
Ok(Async::Ready(Some(MultipartItem::Field(Field::new( Ok(Async::Ready(Some(MultipartItem::Field(Field::new(
safety.clone(), safety.clone(),
headers, headers,
cd,
mt, mt,
field, field,
))))) )))))
@ -387,7 +377,6 @@ impl<S> Drop for InnerMultipart<S> {
/// A single field in a multipart stream /// A single field in a multipart stream
pub struct Field<S> { pub struct Field<S> {
cd: ContentDisposition,
ct: mime::Mime, ct: mime::Mime,
headers: HeaderMap, headers: HeaderMap,
inner: Rc<RefCell<InnerField<S>>>, inner: Rc<RefCell<InnerField<S>>>,
@ -399,11 +388,10 @@ where
S: Stream<Item = Bytes, Error = PayloadError>, S: Stream<Item = Bytes, Error = PayloadError>,
{ {
fn new( fn new(
safety: Safety, headers: HeaderMap, cd: ContentDisposition, ct: mime::Mime, safety: Safety, headers: HeaderMap, ct: mime::Mime,
inner: Rc<RefCell<InnerField<S>>>, inner: Rc<RefCell<InnerField<S>>>,
) -> Self { ) -> Self {
Field { Field {
cd,
ct, ct,
headers, headers,
inner, inner,
@ -421,9 +409,15 @@ where
&self.ct &self.ct
} }
/// Get the content disposition of the field /// Get the content disposition of the field, if it exists
pub fn content_disposition(&self) -> &ContentDisposition { pub fn content_disposition(&self) -> Option<ContentDisposition> {
&self.cd // RFC 7578: 'Each part MUST contain a Content-Disposition header field
// where the disposition type is "form-data".'
if let Some(content_disposition) = self.headers.get(::http::header::CONTENT_DISPOSITION) {
ContentDisposition::from_raw(content_disposition).ok()
} else {
None
}
} }
} }
@ -744,7 +738,6 @@ mod tests {
Content-Type: text/plain; charset=utf-8\r\nContent-Length: 4\r\n\r\n\ Content-Type: text/plain; charset=utf-8\r\nContent-Length: 4\r\n\r\n\
test\r\n\ test\r\n\
--abbc761f78ff4d7cb7573b5a23f96ef0\r\n\ --abbc761f78ff4d7cb7573b5a23f96ef0\r\n\
Content-Disposition: form-data; name=\"file\"; filename=\"fn.txt\"\r\n\
Content-Type: text/plain; charset=utf-8\r\nContent-Length: 4\r\n\r\n\ Content-Type: text/plain; charset=utf-8\r\nContent-Length: 4\r\n\r\n\
data\r\n\ data\r\n\
--abbc761f78ff4d7cb7573b5a23f96ef0--\r\n"); --abbc761f78ff4d7cb7573b5a23f96ef0--\r\n");
@ -759,7 +752,7 @@ mod tests {
MultipartItem::Field(mut field) => { MultipartItem::Field(mut field) => {
{ {
use http::header::{DispositionType, DispositionParam}; use http::header::{DispositionType, DispositionParam};
let cd = field.content_disposition(); let cd = field.content_disposition().unwrap();
assert_eq!(cd.disposition, DispositionType::Ext("form-data".into())); assert_eq!(cd.disposition, DispositionType::Ext("form-data".into()));
assert_eq!(cd.parameters[0], DispositionParam::Ext("name".into(), "file".into())); assert_eq!(cd.parameters[0], DispositionParam::Ext("name".into(), "file".into()));
} }