Skip to content

Commit 7611c30

Browse files
committed
fix(header): enable SetCookie.fmt_header when only 1 cookie
1 parent 60b74eb commit 7611c30

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

src/header/common/set_cookie.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,12 @@ impl Header for SetCookie {
9393
}
9494

9595
impl HeaderFormat for SetCookie {
96-
fn fmt_header(&self, _f: &mut fmt::Formatter) -> fmt::Result {
97-
panic!("SetCookie cannot be used with fmt_header, must use fmt_multi_header");
96+
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
97+
if self.0.len() == 1 {
98+
write!(f, "{}", &self.0[0])
99+
} else {
100+
panic!("SetCookie with multiple cookies cannot be used with fmt_header, must use fmt_multi_header");
101+
}
98102
}
99103

100104
fn fmt_multi_header(&self, f: &mut ::header::MultilineFormatter) -> fmt::Result {

src/header/mod.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -498,28 +498,32 @@ impl<'a> FromIterator<HeaderView<'a>> for Headers {
498498
impl<'a> fmt::Display for &'a (HeaderFormat + Send + Sync) {
499499
#[inline]
500500
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
501-
(**self).fmt_header(f)
501+
let mut multi = MultilineFormatter(Multi::Join(true, f));
502+
self.fmt_multi_header(&mut multi)
502503
}
503504
}
504505

505506
/// A wrapper around any Header with a Display impl that calls fmt_header.
506507
///
507508
/// This can be used like so: `format!("{}", HeaderFormatter(&header))` to
508-
/// get the representation of a Header which will be written to an
509-
/// outgoing `TcpStream`.
509+
/// get the 'value string' representation of this Header.
510+
///
511+
/// Note: This may not necessarily be the value written to stream, such
512+
/// as with the SetCookie header.
510513
pub struct HeaderFormatter<'a, H: HeaderFormat>(pub &'a H);
511514

512515
impl<'a, H: HeaderFormat> fmt::Display for HeaderFormatter<'a, H> {
513516
#[inline]
514517
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
515-
self.0.fmt_header(f)
518+
let mut multi = MultilineFormatter(Multi::Join(true, f));
519+
self.0.fmt_multi_header(&mut multi)
516520
}
517521
}
518522

519523
impl<'a, H: HeaderFormat> fmt::Debug for HeaderFormatter<'a, H> {
520524
#[inline]
521525
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
522-
self.0.fmt_header(f)
526+
fmt::Display::fmt(self, f)
523527
}
524528
}
525529

0 commit comments

Comments
 (0)