pub struct Headers { /* private fields */ }
Expand description
A map of header fields on requests and responses.
Implementations
sourceimpl Headers
impl Headers
sourcepub fn with_capacity(len: usize) -> Headers
pub fn with_capacity(len: usize) -> Headers
Creates a new Headers
struct with space reserved for len
headers.
sourcepub fn set<H>(&mut self, value: H)where
H: Header,
pub fn set<H>(&mut self, value: H)where
H: Header,
Set a header field to the corresponding value.
The field is determined by the type of the value being set.
sourcepub fn get<H>(&self) -> Option<&H>where
H: Header,
pub fn get<H>(&self) -> Option<&H>where
H: Header,
Get a reference to the header field’s value, if it exists.
sourcepub fn get_mut<H>(&mut self) -> Option<&mut H>where
H: Header,
pub fn get_mut<H>(&mut self) -> Option<&mut H>where
H: Header,
Get a mutable reference to the header field’s value, if it exists.
sourcepub fn has<H>(&self) -> boolwhere
H: Header,
pub fn has<H>(&self) -> boolwhere
H: Header,
Returns a boolean of whether a certain header is in the map.
Example:
headers.set(ContentType::json());
assert!(headers.has::<ContentType>());
sourcepub fn remove<H>(&mut self) -> Option<H>where
H: Header,
pub fn remove<H>(&mut self) -> Option<H>where
H: Header,
Removes a header from the map, if one existed. Returns the header, if one has been removed and could be parsed.
Note that this function may return None
even though a header was removed. If you want to
know whether a header exists, rather rely on has
.
sourcepub fn iter(&self) -> HeadersItems<'_> ⓘ
pub fn iter(&self) -> HeadersItems<'_> ⓘ
Returns an iterator over the header fields.
sourcepub fn get_raw(&self, name: &str) -> Option<&Raw>
pub fn get_raw(&self, name: &str) -> Option<&Raw>
Access the raw value of a header.
Prefer to use the typed getters instead.
Example:
let raw = headers.get_raw("content-type").unwrap();
assert_eq!(raw, "text/plain");
sourcepub fn set_raw<K, V>(&mut self, name: K, value: V)where
K: Into<Cow<'static, str>>,
V: Into<Raw>,
pub fn set_raw<K, V>(&mut self, name: K, value: V)where
K: Into<Cow<'static, str>>,
V: Into<Raw>,
Set the raw value of a header, bypassing any typed headers.
Example:
headers.set_raw("content-length", b"1".as_ref());
headers.set_raw("content-length", "2");
headers.set_raw("content-length", "3".to_string());
headers.set_raw("content-length", vec![vec![b'4']]);
sourcepub fn append_raw<K, V>(&mut self, name: K, value: V)where
K: Into<Cow<'static, str>>,
V: Into<Raw>,
pub fn append_raw<K, V>(&mut self, name: K, value: V)where
K: Into<Cow<'static, str>>,
V: Into<Raw>,
Append a value to raw value of this header.
If a header already contains a value, this will add another line to it.
If a header does not exist for this name, a new one will be created with the value.
Example:
headers.append_raw("x-foo", b"bar".to_vec());
headers.append_raw("x-foo", b"quux".to_vec());
sourcepub fn append_raw_str<V>(&mut self, name: &str, value: V)where
V: Into<Raw>,
pub fn append_raw_str<V>(&mut self, name: &str, value: V)where
V: Into<Raw>,
Alternative to append_raw that avoids an allocation if name already exists as a key.
sourcepub fn remove_raw(&mut self, name: &str)
pub fn remove_raw(&mut self, name: &str)
Remove a header by name.
Trait Implementations
sourceimpl<'a> Extend<(&'a str, Bytes)> for Headers
impl<'a> Extend<(&'a str, Bytes)> for Headers
sourcefn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = (&'a str, Bytes)>,
fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = (&'a str, Bytes)>,
sourcefn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)sourcefn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)sourceimpl<'a> Extend<HeaderView<'a>> for Headers
impl<'a> Extend<HeaderView<'a>> for Headers
sourcefn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = HeaderView<'a>>,
fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = HeaderView<'a>>,
sourcefn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)sourcefn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)