Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 95 additions & 11 deletions src/header/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1658,6 +1658,99 @@ impl<T> HeaderMap<T> {
}
}

/// Retains only the values specified by the predicate.
///
/// The predicate is called exactly once for each value, so a header name
/// with several values is visited once per value.
/// A header name is removed from the map once all of its values are
/// removed, and the remaining values of a header name keep their relative
/// order.
///
/// The values are visited in an unspecified order.
///
/// # Examples
///
/// ```
/// # use http::HeaderMap;
/// # use http::header::{CONNECTION, CONTENT_LENGTH, SET_COOKIE};
/// let mut map = HeaderMap::new();
/// map.insert(CONNECTION, "close".parse().unwrap());
/// map.insert(CONTENT_LENGTH, "123".parse().unwrap());
/// map.append(SET_COOKIE, "a=1".parse().unwrap());
/// map.append(SET_COOKIE, "b=2".parse().unwrap());
///
/// map.retain(|name, value| *name != CONNECTION && value != "a=1");
///
/// assert!(!map.contains_key(CONNECTION));
/// assert_eq!(map[CONTENT_LENGTH], "123");
///
/// let mut cookies = map.get_all(SET_COOKIE).iter();
/// assert_eq!(cookies.next().unwrap(), "b=2");
/// assert!(cookies.next().is_none());
/// ```
pub fn retain<F>(&mut self, mut f: F)
where
F: FnMut(&HeaderName, &mut T) -> bool,
{
let mut index = 0;

while index < self.entries.len() {
let keep_head = {
let entry = &mut self.entries[index];
f(&entry.key, &mut entry.value)
};

let mut next = self.entries[index].links.map(|links| links.next);
while let Some(extra_index) = next {
let key = &self.entries[index].key;
let extra = &mut self
.cold
.as_mut()
.expect("an entry with links has extra values")
.extra_values[extra_index];

let link = if f(key, &mut extra.value) {
extra.next
} else {
self.remove_extra_value(extra_index).next
};

next = match link {
Link::Extra(idx) => Some(idx),
Link::Entry(_) => None,
};
}

if keep_head {
index += 1;
} else if let Some(links) = self.entries[index].links {
let extra = self.remove_extra_value(links.next);
self.entries[index].value = extra.value;
index += 1;
} else {
let probe = self.find_probe(self.entries[index].hash, index);
self.remove_found(probe, index);
}
}
}

/// Returns the position in `indices` that points to entry `index`, whose
/// stored hash is `hash`.
///
/// Empty slots do not end the scan, so it also finds an entry past the
/// hole `remove_found` leaves behind.
fn find_probe(&self, hash: HashValue, index: usize) -> usize {
let mut probe = desired_pos(self.mask(), hash);

probe_loop!(probe < self.indices.len(), {
if let Some((i, _)) = self.indices[probe].resolve() {
if i == index {
return probe;
}
}
});
}

/// Remove an entry from the map.
///
/// Warning: To avoid inconsistent state, extra values _must_ be removed
Expand All @@ -1676,17 +1769,8 @@ impl<T> HeaderMap<T> {
if let Some(entry) = self.entries.get(found) {
// was not last element
// examine new element in `found` and find it in indices
let mut probe = desired_pos(mask, entry.hash);

probe_loop!(probe < self.indices.len(), {
if let Some((i, _)) = self.indices[probe].resolve() {
if i >= self.entries.len() {
// found it
self.indices[probe] = Pos::new(found, entry.hash);
break;
}
}
});
let moved = self.find_probe(entry.hash, self.entries.len());
self.indices[moved] = Pos::new(found, entry.hash);

// Update links
if let Some(links) = entry.links {
Expand Down
Loading
Loading