Skip to main content

Header

Struct Header 

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

Handle for accessing and mutating HTTP headers.

A Header is scoped to either the request or response, depending on how it is constructed.

Implementations§

Source§

impl Header

Source

pub fn names_iter(&self) -> impl Iterator<Item = Bytes> + use<'_>

Returns an iterator over all header names as raw bytes without allocating into a vector.

Header names are returned in the order provided by the host runtime. This method is zero-allocation and returns an iterator that yields each header name as Bytes. For heap-allocated results, use names.

Source

pub fn names(&self) -> Vec<Bytes>

Returns all header names as raw bytes, allocating into a vector.

Header names are returned in the order provided by the host runtime. This method collects results into a Vec, which allocates heap memory. Use names_iter for zero-allocation access.

Source

pub fn values_iter(&self, name: &[u8]) -> impl Iterator<Item = Bytes> + use<'_>

Returns an iterator over all values for the given header name without allocating into a vector.

The name is matched by the host according to its header normalization rules (often case-insensitive). This method is zero-allocation and returns an iterator that yields each header value as Bytes. For heap-allocated results, use values.

Source

pub fn get(&self, name: &[u8]) -> Option<Bytes>

Return the first value for the given header name, if present.

Source

pub fn values(&self, name: &[u8]) -> Vec<Bytes>

Returns all values for the given header name, allocating into a vector.

The name is matched by the host according to its header normalization rules (often case-insensitive). This method collects results into a Vec, which allocates heap memory. Use values_iter for zero-allocation access.

Source

pub fn set(&self, name: &[u8], value: &[u8])

Set a header value, replacing any existing values.

Source

pub fn add(&self, name: &[u8], value: &[u8])

Add an additional value for a header name.

Examples found in repository?
examples/header.rs (line 28)
27    fn handle_request(&self, request: &Request, _response: &Response) -> (bool, i32) {
28        request.header.add(b"X-Custom-Header", b"FooBar");
29        (true, 0)
30    }
Source

pub fn remove(&self, name: &[u8])

Remove a header and all of its values.

Source

pub fn entries_iter(&self) -> impl Iterator<Item = (Bytes, Vec<Bytes>)> + '_

Return all headers as an iterator of names to value lists.

This returns an iterator over all header entries. Each entry contains the header name paired with a vector containing its associated values. For zero-allocation access, use names_iter and values_iter.

Examples found in repository?
examples/info.rs (line 19)
17    fn handle_request(&self, request: &Request, _response: &Response) -> (bool, i32) {
18        info!("Request: {} {} {} {}", request.method(), request.version(), request.uri(), request.source_addr());
19        for (name, values) in request.header.entries_iter() {
20            let values = values.iter().map(|v| format!("{v}")).collect::<Vec<_>>().join(", ");
21            info!("Header: {} [{}]", name, values);
22        }
23        info!("Body: {}", request.body.read());
24        (true, 0)
25    }
Source

pub fn entries(&self) -> HashMap<Bytes, Vec<Bytes>>

Return all headers as a map of names to value lists.

This collects all names and then queries each set of values, allocating into a HashMap and multiple Vecs for the values. Each header name is paired with a vector containing its associated values. Use entries_iter for zero-allocation access.

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.