From e3f317eea7f315d960dfd98e2ffa1462d5fd2cc0 Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Wed, 25 Jan 2017 22:38:48 -0800 Subject: [PATCH] fix(header): enable SetCookie.fmt_header when only 1 cookie --- src/header/common/set_cookie.rs | 8 ++++++-- src/header/mod.rs | 14 +++++++++----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/header/common/set_cookie.rs b/src/header/common/set_cookie.rs index 03b9f406a2..f2fd9efcb6 100644 --- a/src/header/common/set_cookie.rs +++ b/src/header/common/set_cookie.rs @@ -107,8 +107,12 @@ impl Header for SetCookie { } impl HeaderFormat for SetCookie { - fn fmt_header(&self, _f: &mut fmt::Formatter) -> fmt::Result { - panic!("SetCookie cannot be used with fmt_header, must use fmt_multi_header"); + fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result { + if self.0.len() == 1 { + write!(f, "{}", &self.0[0]) + } else { + panic!("SetCookie with multiple cookies cannot be used with fmt_header, must use fmt_multi_header"); + } } fn fmt_multi_header(&self, f: &mut ::header::MultilineFormatter) -> fmt::Result { diff --git a/src/header/mod.rs b/src/header/mod.rs index aa6520c016..0c3a24374f 100644 --- a/src/header/mod.rs +++ b/src/header/mod.rs @@ -539,28 +539,32 @@ impl<'a> FromIterator> for Headers { impl<'a> fmt::Display for &'a (HeaderFormat + Send + Sync) { #[inline] fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - (**self).fmt_header(f) + let mut multi = MultilineFormatter(Multi::Join(true, f)); + self.fmt_multi_header(&mut multi) } } /// A wrapper around any Header with a Display impl that calls fmt_header. /// /// This can be used like so: `format!("{}", HeaderFormatter(&header))` to -/// get the representation of a Header which will be written to an -/// outgoing `TcpStream`. +/// get the 'value string' representation of this Header. +/// +/// Note: This may not necessarily be the value written to stream, such +/// as with the SetCookie header. pub struct HeaderFormatter<'a, H: HeaderFormat>(pub &'a H); impl<'a, H: HeaderFormat> fmt::Display for HeaderFormatter<'a, H> { #[inline] fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - self.0.fmt_header(f) + let mut multi = MultilineFormatter(Multi::Join(true, f)); + self.0.fmt_multi_header(&mut multi) } } impl<'a, H: HeaderFormat> fmt::Debug for HeaderFormatter<'a, H> { #[inline] fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - self.0.fmt_header(f) + fmt::Display::fmt(self, f) } }