Skip to content

Commit

Permalink
fix(header): fix panic from headers.remove when typed doesn't match
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmonstar committed Jul 24, 2017
1 parent 6f1a870 commit 4bd9746
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
32 changes: 13 additions & 19 deletions src/header/internals/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,20 @@ impl Item {
match self.typed.get(tid) {
Some(val) => Some(val),
None => {
match parse::<H>(self.raw.as_ref().expect("item.raw must exist")) {
Ok(typed) => {
unsafe { self.typed.insert(tid, typed); }
self.typed.get(tid)
},
Err(_) => None
}
parse::<H>(self.raw.as_ref().expect("item.raw must exist")).and_then(|typed| {
unsafe { self.typed.insert(tid, typed); }
self.typed.get(tid)
})
}
}.map(|typed| unsafe { typed.downcast_ref_unchecked() })
}

pub fn typed_mut<H: Header>(&mut self) -> Option<&mut H> {
let tid = TypeId::of::<H>();
if self.typed.get_mut(tid).is_none() {
match parse::<H>(self.raw.as_ref().expect("item.raw must exist")) {
Ok(typed) => {
unsafe { self.typed.insert(tid, typed); }
},
Err(_) => ()
}
parse::<H>(self.raw.as_ref().expect("item.raw must exist")).map(|typed| {
unsafe { self.typed.insert(tid, typed); }
});
}
if self.raw.is_some() && self.typed.get_mut(tid).is_some() {
self.raw = OptCell::new(None);
Expand All @@ -86,10 +80,10 @@ impl Item {

pub fn into_typed<H: Header>(self) -> Option<H> {
let tid = TypeId::of::<H>();
match self.typed.into_value(tid) {
Some(val) => Some(val),
None => parse::<H>(self.raw.as_ref().expect("item.raw must exist")).ok()
}.map(|typed| unsafe { typed.downcast_unchecked() })
let Item { typed, raw } = self;
typed.into_value(tid)
.or_else(|| raw.as_ref().and_then(parse::<H>))
.map(|typed| unsafe { typed.downcast_unchecked() })
}

pub fn write_h1(&self, f: &mut Formatter) -> fmt::Result {
Expand Down Expand Up @@ -117,9 +111,9 @@ impl Item {
}

#[inline]
fn parse<H: Header>(raw: &Raw) -> ::Result<Box<Header + Send + Sync>> {
fn parse<H: Header>(raw: &Raw) -> Option<Box<Header + Send + Sync>> {
H::parse_header(raw).map(|h| {
let h: Box<Header + Send + Sync> = Box::new(h);
h
})
}).ok()
}
6 changes: 6 additions & 0 deletions src/header/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,12 @@ mod tests {
let mut headers = Headers::new();
headers.set(ContentLength(10));
assert_eq!(headers.remove(), Some(ContentLength(10)));
assert_eq!(headers.len(), 0);

headers.set(ContentLength(9));
assert_eq!(headers.len(), 1);
assert!(headers.remove::<CrazyLength>().is_none());
assert_eq!(headers.len(), 0);
}

#[test]
Expand Down

0 comments on commit 4bd9746

Please sign in to comment.