Skip to main content

Headers

Struct Headers 

Source
pub struct Headers(/* private fields */);
Expand description

Request or response headers.

A thin wrapper around http::HeaderMap that adds provider-oriented helpers: overriding merge, user-agent suffixing and masking of sensitive values in diagnostic output. Debug and Serialize never reveal authorization, API-key or cookie values.

Implementations§

Source§

impl Headers

Source

pub fn new() -> Self

Creates an empty header map.

Source

pub fn from_map(map: HeaderMap) -> Self

Wraps an existing HeaderMap.

Source

pub fn as_map(&self) -> &HeaderMap

Returns the wrapped HeaderMap.

Source

pub fn as_map_mut(&mut self) -> &mut HeaderMap

Returns the wrapped HeaderMap mutably.

Source

pub fn into_map(self) -> HeaderMap

Consumes the wrapper and returns the HeaderMap.

Source

pub fn is_empty(&self) -> bool

Returns true when no header is present.

Source

pub fn len(&self) -> usize

Returns the number of header entries (repeated names count once each).

Source

pub fn get_str(&self, name: &str) -> Option<&str>

Returns the first value of name as a string, if present and ASCII.

Source

pub fn contains(&self, name: &str) -> bool

Returns true when name is present.

Source

pub fn insert(&mut self, name: &str, value: &str) -> Result<(), InvalidHeader>

Inserts a header, replacing existing values with the same name.

Returns an error when the name or value is not a valid header.

§Errors

Returns InvalidHeader when name is not a valid header name or value contains characters not allowed in header values.

Source

pub fn with(self, name: &str, value: &str) -> Self

Builder-style Headers::insert; invalid pairs are skipped silently.

Intended for static configuration where the caller controls the values; use Headers::insert for untrusted input.

Source

pub fn remove(&mut self, name: &str)

Removes all values for name.

Source

pub fn merge(&mut self, other: &Headers)

Merges other into self; values in other override existing ones.

Repeated header names in other replace all existing values of that name, mirroring “later headers win” semantics for configuration layering (provider defaults, per-model settings, per-call headers).

Source

pub fn merged(self, other: &Headers) -> Self

Returns a copy with other merged on top; see Headers::merge.

Source

pub fn merge_pairs<'a, I>(&mut self, pairs: I)
where I: IntoIterator<Item = (&'a str, Option<&'a str>)>,

Merges optional string pairs; pairs whose value is None are skipped.

Invalid names or values are skipped as well; this mirrors the lenient behaviour needed when layering user-supplied header maps.

Source

pub fn with_user_agent_suffix<'a, I>(self, suffixes: I) -> Self
where I: IntoIterator<Item = &'a str>,

Appends suffixes to the user-agent header, creating it if absent.

Existing user-agent text is kept and each suffix is separated by a single space. Empty suffixes are ignored.

Source

pub fn iter_str(&self) -> impl Iterator<Item = (&str, String)> + '_

Iterates over (name, value) pairs with values rendered as strings.

Non-ASCII values are rendered lossily; sensitive values are not masked.

Source

pub fn masked(&self) -> Headers

Returns a copy with sensitive values replaced by ***.

Trait Implementations§

Source§

impl Clone for Headers

Source§

fn clone(&self) -> Headers

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Headers

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Headers

Source§

fn default() -> Headers

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Headers

Deserializes from an object of name -> value pairs; invalid entries are rejected with an error.

Source§

fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Eq for Headers

Source§

impl From<HeaderMap> for Headers

Source§

fn from(map: HeaderMap) -> Self

Converts to this type from the input type.
Source§

impl From<Headers> for HeaderMap

Source§

fn from(headers: Headers) -> Self

Converts to this type from the input type.
Source§

impl<'a> IntoIterator for &'a Headers

Source§

type Item = (&'a HeaderName, &'a HeaderValue)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, HeaderValue>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl PartialEq for Headers

Source§

fn eq(&self, other: &Headers) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for Headers

Serializes as an object of name -> value pairs with sensitive values masked. Repeated header names are joined with ", ".

Source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Headers

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.