Skip to main content

RequestHeaders

Struct RequestHeaders 

Source
pub struct RequestHeaders { /* private fields */ }
Expand description

A case-insensitive collection of HTTP request headers.

RequestHeaders wraps Hyper’s HeaderMap to provide a convenient API for working with HTTP headers without unnecessary allocations.

§Example

use ripress::req::request_headers::RequestHeaders;

let mut headers = RequestHeaders::new();
headers.insert("Content-Type", "application/json");
headers.append("Set-Cookie", "id=123");
headers.append("Set-Cookie", "theme=dark");

assert_eq!(headers.content_type(), Some("application/json"));
assert_eq!(headers.get_all("set-cookie").len(), 2);

Implementations§

Source§

impl RequestHeaders

Source

pub fn new() -> Self

Creates a new, empty RequestHeaders collection.

§Example
use ripress::req::request_headers::RequestHeaders;

let headers = RequestHeaders::new();
assert!(headers.is_empty());
Source

pub fn insert<K, V>(&mut self, key: K, value: V)
where K: AsRef<str>, V: AsRef<str>,

Inserts a header value, replacing any existing values for the header name.

Header names are case-insensitive.

§Example
use ripress::req::request_headers::RequestHeaders;

let mut headers = RequestHeaders::new();
headers.insert("Content-Type", "application/json");
assert_eq!(headers.content_type(), Some("application/json"));
Source

pub fn append<K, V>(&mut self, key: K, value: V)
where K: AsRef<str>, V: AsRef<str>,

Appends a value to an existing header or creates it if not present.

Useful for headers that allow multiple values, such as Set-Cookie or Accept.

§Example
use ripress::req::request_headers::RequestHeaders;

let mut headers = RequestHeaders::new();
headers.append("Set-Cookie", "id=1");
headers.append("Set-Cookie", "theme=dark");
assert_eq!(headers.get_all("Set-Cookie").len(), 2);
Source

pub fn get<K>(&self, key: K) -> Option<&str>
where K: AsRef<str>,

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

§Example
use ripress::req::request_headers::RequestHeaders;

let mut headers = RequestHeaders::new();
headers.append("Accept", "application/json");
headers.append("Accept", "text/html");
assert_eq!(headers.get("Accept"), Some("application/json"));
Source

pub fn get_all<K>(&self, key: K) -> Vec<&str>
where K: AsRef<str>,

Returns all values for the given header name.

§Example
use ripress::req::request_headers::RequestHeaders;

let mut headers = RequestHeaders::new();
headers.append("Accept", "application/json");
headers.append("Accept", "text/html");
assert_eq!(headers.get_all("Accept").len(), 2);
Source

pub fn contains_key<K>(&self, key: K) -> bool
where K: AsRef<str>,

Checks whether a header exists.

Source

pub fn remove<K>(&mut self, key: K) -> Option<String>
where K: AsRef<str>,

Removes a header entirely, returning its first value if present.

Source

pub fn content_type(&self) -> Option<&str>

Returns the value of the Content-Type header, if present.

Source

pub fn authorization(&self) -> Option<&str>

Returns the value of the Authorization header, if present.

Source

pub fn user_agent(&self) -> Option<&str>

Returns the value of the User-Agent header, if present.

Source

pub fn accept(&self) -> Option<&str>

Returns the value of the Accept header, if present.

Source

pub fn host(&self) -> Option<&str>

Returns the value of the Host header, if present.

Source

pub fn x_forwarded_for(&self) -> Option<&str>

Returns the value of the X-Forwarded-For header, if present.

This can be useful for retrieving the real IP address of a client behind proxies.

Source

pub fn accepts_json(&self) -> bool

Returns true if the Accept header indicates the client accepts JSON.

Matches if the Accept header contains application/json or */*.

Source

pub fn accepts_html(&self) -> bool

Returns true if the Accept header indicates the client accepts HTML.

Matches if the Accept header contains text/html or */*.

Source

pub fn keys(&self) -> impl Iterator<Item = &HeaderName>

Returns an iterator over all header names.

§Example
use ripress::req::request_headers::RequestHeaders;

let mut headers = RequestHeaders::new();
headers.insert("Content-Type", "application/json");
for key in headers.keys() {
    println!("{}", key);
}
Source

pub fn len(&self) -> usize

Returns the number of unique header names.

Source

pub fn is_empty(&self) -> bool

Returns true if there are no headers.

Source

pub fn iter(&self) -> impl Iterator<Item = (&HeaderName, &HeaderValue)>

Iterates over all headers as (name, first_value) pairs.

Useful when you only need the first value for each header.

Source

pub fn iter_all(&self) -> impl Iterator<Item = (&HeaderName, &str)>

Iterates over all headers as (name, value) pairs, including duplicates.

Source

pub fn as_header_map(&self) -> &HeaderMap

Returns a reference to the inner HeaderMap for advanced usage.

Source

pub fn into_header_map(self) -> HeaderMap

Consumes self and returns the inner HeaderMap.

Trait Implementations§

Source§

impl Clone for RequestHeaders

Source§

fn clone(&self) -> RequestHeaders

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for RequestHeaders

Source§

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

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

impl Default for RequestHeaders

Source§

fn default() -> Self

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

impl Display for RequestHeaders

Source§

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

Formats the headers as key: value lines.

§Example
use ripress::req::request_headers::RequestHeaders;

let mut headers = RequestHeaders::new();
headers.insert("Content-Type", "application/json");
println!("{}", headers);
Source§

impl From<HeaderMap> for RequestHeaders

Source§

fn from(map: HeaderMap) -> Self

Converts to this type from the input type.
Source§

impl From<RequestHeaders> for HeaderMap

Source§

fn from(headers: RequestHeaders) -> Self

Converts to this type from the input type.
Source§

impl Index<&str> for RequestHeaders

Source§

fn index(&self, key: &str) -> &Self::Output

Provides convenient indexing syntax:

use ripress::req::request_headers::RequestHeaders;

let mut headers = RequestHeaders::new();
headers.insert("Content-Type", "application/json");
assert_eq!(&headers["content-type"], "application/json");
§Panics

Panics if the header does not exist.

Source§

type Output = str

The returned type after indexing.

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more