diff --git a/src/header/common/content_length.rs b/src/header/common/content_length.rs index 68ba8104..45fd6572 100644 --- a/src/header/common/content_length.rs +++ b/src/header/common/content_length.rs @@ -66,7 +66,7 @@ impl Header for ContentLength { #[inline] fn fmt_header(&self, f: &mut ::header::Formatter) -> fmt::Result { - f.fmt_line(self) + f.danger_fmt_line_without_newline_replacer(self) } } diff --git a/src/header/common/content_type.rs b/src/header/common/content_type.rs index 0124a79e..b77e475e 100644 --- a/src/header/common/content_type.rs +++ b/src/header/common/content_type.rs @@ -45,7 +45,7 @@ header! { /// ContentType(mime::TEXT_HTML) /// ); /// ``` - (ContentType, "Content-Type") => [Mime] + (ContentType, "Content-Type") => danger [Mime] test_content_type { test_header!( diff --git a/src/header/common/mod.rs b/src/header/common/mod.rs index 6b823589..d1b8f358 100644 --- a/src/header/common/mod.rs +++ b/src/header/common/mod.rs @@ -275,6 +275,34 @@ macro_rules! header { } } }; + // Single value header (internal) + ($(#[$a:meta])*($id:ident, $n:expr) => danger [$value:ty]) => { + $(#[$a])* + #[derive(Clone, Debug, PartialEq)] + pub struct $id(pub $value); + __hyper__deref!($id => $value); + impl $crate::header::Header for $id { + #[inline] + fn header_name() -> &'static str { + static NAME: &'static str = $n; + NAME + } + #[inline] + fn parse_header(raw: &$crate::header::Raw) -> $crate::Result { + $crate::header::parsing::from_one_raw_str(raw).map($id) + } + #[inline] + fn fmt_header(&self, f: &mut $crate::header::Formatter) -> ::std::fmt::Result { + f.danger_fmt_line_without_newline_replacer(self) + } + } + impl ::std::fmt::Display for $id { + #[inline] + fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { + ::std::fmt::Display::fmt(&self.0, f) + } + } + }; // Single value cow header ($(#[$a:meta])*($id:ident, $n:expr) => Cow[$value:ty]) => { $(#[$a])* @@ -383,6 +411,14 @@ macro_rules! header { __hyper__tm! { $id, $tm { $($tf)* }} }; + ($(#[$a:meta])*($id:ident, $n:expr) => danger [$item:ty] $tm:ident{$($tf:item)*}) => { + header! { + $(#[$a])* + ($id, $n) => danger [$item] + } + + __hyper__tm! { $id, $tm { $($tf)* }} + }; ($(#[$a:meta])*($id:ident, $n:expr) => Cow[$item:ty] $tm:ident{$($tf:item)*}) => { header! { $(#[$a])* diff --git a/src/header/mod.rs b/src/header/mod.rs index 60b7ee64..432d7ab6 100644 --- a/src/header/mod.rs +++ b/src/header/mod.rs @@ -224,6 +224,32 @@ impl<'a, 'b> Formatter<'a, 'b> { } } } + + fn danger_fmt_line_without_newline_replacer(&mut self, line: &T) -> fmt::Result { + use std::fmt::Write; + match self.0 { + Multi::Line(name, ref mut f) => { + try!(f.write_str(name)); + try!(f.write_str(": ")); + try!(fmt::Display::fmt(line, f)); + f.write_str("\r\n") + }, + Multi::Join(ref mut first, ref mut f) => { + if !*first { + try!(f.write_str(", ")); + } else { + *first = false; + } + fmt::Display::fmt(line, f) + } + Multi::Raw(ref mut raw) => { + let mut s = String::new(); + try!(write!(s, "{}", line)); + raw.push(s); + Ok(()) + } + } + } } struct ValueString<'a>(&'a Item);