fix(http): avoid infinite recursion when Body::from is called with Cow::Owned. (#1343)

When cow is a Cow::Owned, cow.to_owned() returns a Cow::Owned, which leads to infinite recursion.
Extract the owned or borrowed values from the cow to ensure progress is made in either case.
This commit is contained in:
Aaron Riekenberg
2017-10-01 11:49:36 -05:00
committed by Sean McArthur
parent 9c80fdbb9e
commit e8d6173734

View File

@@ -98,10 +98,9 @@ impl From<&'static [u8]> for Body {
impl From<Cow<'static, [u8]>> for Body { impl From<Cow<'static, [u8]>> for Body {
#[inline] #[inline]
fn from (cow: Cow<'static, [u8]>) -> Body { fn from (cow: Cow<'static, [u8]>) -> Body {
if let Cow::Borrowed(value) = cow { match cow {
Body::from(value) Cow::Borrowed(b) => Body::from(b),
} else { Cow::Owned(o) => Body::from(o)
Body::from(cow.to_owned())
} }
} }
} }
@@ -123,10 +122,9 @@ impl From<&'static str> for Body {
impl From<Cow<'static, str>> for Body { impl From<Cow<'static, str>> for Body {
#[inline] #[inline]
fn from(cow: Cow<'static, str>) -> Body { fn from(cow: Cow<'static, str>) -> Body {
if let Cow::Borrowed(value) = cow { match cow {
Body::from(value) Cow::Borrowed(b) => Body::from(b),
} else { Cow::Owned(o) => Body::from(o)
Body::from(cow.to_owned())
} }
} }
} }