pub struct Headers { /* private fields */ }Expand description
An immutable view of headers
Header values are stored exactly as received and are not required to be valid UTF-8: an HTTP
header value may contain any octet in 0x80..=0xFF (obs-text, RFC 7230), and an arbitrary
sequence of those is not necessarily valid UTF-8. The string-typed accessors (get, get_all,
iter, remove) therefore yield only values that are
valid UTF-8, and skip those that are not. Use the corresponding byte accessors
(get_bytes, get_all_bytes,
iter_bytes) to observe every value.
Consequently len and contains_key count and
report values the string accessors skip.
Implementations§
Source§impl Headers
impl Headers
Sourcepub fn get(&self, key: impl AsRef<str>) -> Option<&str>
pub fn get(&self, key: impl AsRef<str>) -> Option<&str>
Returns the value for a given key
Returns None if the header is absent, or if its value is not valid UTF-8; use
get_bytes to read a value of any encoding.
If multiple values are associated, the first value is returned See HeaderMap::get
Sourcepub fn try_get(&self, key: impl AsRef<str>) -> Option<Result<&str, &[u8]>>
pub fn try_get(&self, key: impl AsRef<str>) -> Option<Result<&str, &[u8]>>
Returns the value for a given key, distinguishing an unreadable value from an absent header
Some(Ok(_)) is a value that is valid UTF-8, Some(Err(_)) is the raw octets of one that is
not, and None means the header is absent. get collapses the first two into
None, so use this where the difference matters.
If multiple values are associated, the first value is returned.
Sourcepub fn get_all(&self, key: impl AsRef<str>) -> impl Iterator<Item = &str>
pub fn get_all(&self, key: impl AsRef<str>) -> impl Iterator<Item = &str>
Returns all values for a given key
Values that are not valid UTF-8 are skipped; use
get_all_bytes to read values of any encoding.
Sourcepub fn get_bytes(&self, key: impl AsRef<str>) -> Option<&[u8]>
pub fn get_bytes(&self, key: impl AsRef<str>) -> Option<&[u8]>
Returns the value for a given key as raw bytes
Unlike get, the returned bytes are not required to be valid UTF-8.
If multiple values are associated, the first value is returned.
Sourcepub fn get_all_bytes(&self, key: impl AsRef<str>) -> impl Iterator<Item = &[u8]>
pub fn get_all_bytes(&self, key: impl AsRef<str>) -> impl Iterator<Item = &[u8]>
Returns all values for a given key as raw bytes
Unlike get_all, the returned bytes are not required to be valid UTF-8.
Sourcepub fn iter(&self) -> HeadersIter<'_> ⓘ
pub fn iter(&self) -> HeadersIter<'_> ⓘ
Returns an iterator over the headers
Sourcepub fn iter_bytes(&self) -> impl Iterator<Item = (&str, &[u8])>
pub fn iter_bytes(&self) -> impl Iterator<Item = (&str, &[u8])>
Returns an iterator over the headers, pairing each name with its raw value bytes
Unlike iter, the returned bytes are not required to be valid UTF-8.
Sourcepub fn contains_key(&self, key: impl AsRef<str>) -> bool
pub fn contains_key(&self, key: impl AsRef<str>) -> bool
Returns true if this header is present
Sourcepub fn insert(
&mut self,
key: impl AsHeaderComponent,
value: impl AsHeaderComponent,
) -> Option<String>
pub fn insert( &mut self, key: impl AsHeaderComponent, value: impl AsHeaderComponent, ) -> Option<String>
Insert a value into the headers structure.
This will replace any existing value for this key. Returns the previous associated value if any.
§Panics
If the key is not valid ASCII, or if the value is not valid UTF-8, this function will panic.
Sourcepub fn try_insert(
&mut self,
key: impl AsHeaderComponent,
value: impl AsHeaderComponent,
) -> Result<Option<String>, HttpError>
pub fn try_insert( &mut self, key: impl AsHeaderComponent, value: impl AsHeaderComponent, ) -> Result<Option<String>, HttpError>
Insert a value into the headers structure.
This will replace any existing value for this key. Returns the previous associated value if any.
If the key is not valid ASCII, or if the value is not valid UTF-8, this function will return an error.
Sourcepub fn append(
&mut self,
key: impl AsHeaderComponent,
value: impl AsHeaderComponent,
) -> bool
pub fn append( &mut self, key: impl AsHeaderComponent, value: impl AsHeaderComponent, ) -> bool
Appends a value to a given key
§Panics
If the key is not valid ASCII, or if the value is not valid UTF-8, this function will panic.
Sourcepub fn try_append(
&mut self,
key: impl AsHeaderComponent,
value: impl AsHeaderComponent,
) -> Result<bool, HttpError>
pub fn try_append( &mut self, key: impl AsHeaderComponent, value: impl AsHeaderComponent, ) -> Result<bool, HttpError>
Appends a value to a given key
If the key is not valid ASCII, or if the value is not valid UTF-8, this function will return an error.
Trait Implementations§
Source§impl<'a> IntoIterator for &'a Headers
impl<'a> IntoIterator for &'a Headers
Auto Trait Implementations§
impl Freeze for Headers
impl RefUnwindSafe for Headers
impl Send for Headers
impl Sync for Headers
impl Unpin for Headers
impl UnsafeUnpin for Headers
impl UnwindSafe for Headers
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more