fix(headers): Remove raw part when getting mutable reference to typed header

If you get a mutable reference to a typed header it is possible to make
the two representations be out of sync. To avoid this, after parsing the
raw part it should be removed.

Fixes #821.
This commit is contained in:
Andre Silva
2016-06-17 01:51:34 +01:00
committed by Sean McArthur
parent 43ac0dd095
commit f38717e422
2 changed files with 4 additions and 0 deletions

View File

@@ -78,6 +78,9 @@ impl Item {
Err(_) => ()
}
}
if self.raw.is_some() && self.typed.get_mut(tid).is_some() {
self.raw = OptCell::new(None);
}
self.typed.get_mut(tid).map(|typed| unsafe { typed.downcast_mut_unchecked() })
}
}

View File

@@ -670,6 +670,7 @@ mod tests {
fn test_get_mutable() {
let mut headers = Headers::from_raw(&raw!(b"Content-Length: 10")).unwrap();
*headers.get_mut::<ContentLength>().unwrap() = ContentLength(20);
assert_eq!(headers.get_raw("content-length").unwrap(), &[b"20".to_vec()][..]);
assert_eq!(*headers.get::<ContentLength>().unwrap(), ContentLength(20));
}