1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-30 18:44:35 +01:00

remove Option from ContentDisposition::from_raw() argument

This commit is contained in:
axon-q 2018-06-07 12:55:35 +00:00
parent a6e07c06b6
commit 97b5410aad
2 changed files with 19 additions and 13 deletions

View File

@ -89,8 +89,8 @@ pub struct ContentDisposition {
}
impl ContentDisposition {
/// Parse a raw Content-Disposition header value
pub fn from_raw(hv: Option<&header::HeaderValue>) -> Result<Self, ::error::ParseError> {
header::from_one_raw_str(hv).and_then(|s: String| {
pub fn from_raw(hv: &header::HeaderValue) -> Result<Self, ::error::ParseError> {
header::from_one_raw_str(Some(hv)).and_then(|s: String| {
let mut sections = s.split(';');
let disposition = match sections.next() {
Some(s) => s.trim(),
@ -158,7 +158,11 @@ impl Header for ContentDisposition {
}
fn parse<T: ::HttpMessage>(msg: &T) -> Result<Self, ::error::ParseError> {
Self::from_raw(msg.headers().get(Self::name()))
if let Some(h) = msg.headers().get(Self::name()) {
Self::from_raw(&h)
} else {
Err(::error::ParseError::Header)
}
}
}
@ -209,10 +213,10 @@ mod tests {
use header::shared::Charset;
#[test]
fn test_from_raw() {
assert!(ContentDisposition::from_raw(Some(&HeaderValue::from_static(""))).is_err());
assert!(ContentDisposition::from_raw(&HeaderValue::from_static("")).is_err());
let a = HeaderValue::from_static("form-data; dummy=3; name=upload; filename=\"sample.png\"");
let a: ContentDisposition = ContentDisposition::from_raw(Some(&a)).unwrap();
let a: ContentDisposition = ContentDisposition::from_raw(&a).unwrap();
let b = ContentDisposition {
disposition: DispositionType::Ext("form-data".to_owned()),
parameters: vec![
@ -226,7 +230,7 @@ mod tests {
assert_eq!(a, b);
let a = HeaderValue::from_static("attachment; filename=\"image.jpg\"");
let a: ContentDisposition = ContentDisposition::from_raw(Some(&a)).unwrap();
let a: ContentDisposition = ContentDisposition::from_raw(&a).unwrap();
let b = ContentDisposition {
disposition: DispositionType::Attachment,
parameters: vec![
@ -238,7 +242,7 @@ mod tests {
assert_eq!(a, b);
let a = HeaderValue::from_static("attachment; filename*=UTF-8''%c2%a3%20and%20%e2%82%ac%20rates");
let a: ContentDisposition = ContentDisposition::from_raw(Some(&a)).unwrap();
let a: ContentDisposition = ContentDisposition::from_raw(&a).unwrap();
let b = ContentDisposition {
disposition: DispositionType::Attachment,
parameters: vec![
@ -255,17 +259,17 @@ mod tests {
fn test_display() {
let as_string = "attachment; filename*=UTF-8'en'%C2%A3%20and%20%E2%82%AC%20rates";
let a = HeaderValue::from_static(as_string);
let a: ContentDisposition = ContentDisposition::from_raw(Some(&a)).unwrap();
let a: ContentDisposition = ContentDisposition::from_raw(&a).unwrap();
let display_rendered = format!("{}",a);
assert_eq!(as_string, display_rendered);
let a = HeaderValue::from_static("attachment; filename*=UTF-8''black%20and%20white.csv");
let a: ContentDisposition = ContentDisposition::from_raw(Some(&a)).unwrap();
let a: ContentDisposition = ContentDisposition::from_raw(&a).unwrap();
let display_rendered = format!("{}",a);
assert_eq!("attachment; filename=\"black and white.csv\"".to_owned(), display_rendered);
let a = HeaderValue::from_static("attachment; filename=colourful.csv");
let a: ContentDisposition = ContentDisposition::from_raw(Some(&a)).unwrap();
let a: ContentDisposition = ContentDisposition::from_raw(&a).unwrap();
let display_rendered = format!("{}",a);
assert_eq!("attachment; filename=\"colourful.csv\"".to_owned(), display_rendered);
}

View File

@ -320,9 +320,11 @@ where
// content disposition
// RFC 7578: 'Each part MUST contain a Content-Disposition header field
// where the disposition type is "form-data".'
let cd = ContentDisposition::from_raw(
headers.get(::http::header::CONTENT_DISPOSITION)
).map_err(|_| MultipartError::ParseContentDisposition)?;
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
let mut mt = mime::APPLICATION_OCTET_STREAM;