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
impl RequestHeaders
Sourcepub fn new() -> Self
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());Sourcepub fn insert<K, V>(&mut self, key: K, value: V)
pub fn insert<K, V>(&mut self, key: K, value: V)
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"));Sourcepub fn append<K, V>(&mut self, key: K, value: V)
pub fn append<K, V>(&mut self, key: K, value: V)
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);Sourcepub fn get<K>(&self, key: K) -> Option<&str>
pub fn get<K>(&self, key: K) -> Option<&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"));Sourcepub fn get_all<K>(&self, key: K) -> Vec<&str>
pub fn get_all<K>(&self, key: K) -> Vec<&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);Sourcepub fn contains_key<K>(&self, key: K) -> bool
pub fn contains_key<K>(&self, key: K) -> bool
Checks whether a header exists.
Sourcepub fn remove<K>(&mut self, key: K) -> Option<String>
pub fn remove<K>(&mut self, key: K) -> Option<String>
Removes a header entirely, returning its first value if present.
Sourcepub fn content_type(&self) -> Option<&str>
pub fn content_type(&self) -> Option<&str>
Returns the value of the Content-Type header, if present.
Returns the value of the Authorization header, if present.
Sourcepub fn user_agent(&self) -> Option<&str>
pub fn user_agent(&self) -> Option<&str>
Returns the value of the User-Agent header, if present.
Sourcepub fn x_forwarded_for(&self) -> Option<&str>
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.
Sourcepub fn accepts_json(&self) -> bool
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 */*.
Sourcepub fn accepts_html(&self) -> bool
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 */*.
Sourcepub fn keys(&self) -> impl Iterator<Item = &HeaderName>
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);
}Sourcepub fn iter(&self) -> impl Iterator<Item = (&HeaderName, &HeaderValue)>
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.
Sourcepub fn iter_all(&self) -> impl Iterator<Item = (&HeaderName, &str)>
pub fn iter_all(&self) -> impl Iterator<Item = (&HeaderName, &str)>
Iterates over all headers as (name, value) pairs, including duplicates.
Sourcepub fn as_header_map(&self) -> &HeaderMap
pub fn as_header_map(&self) -> &HeaderMap
Returns a reference to the inner HeaderMap for advanced usage.
Sourcepub fn into_header_map(self) -> HeaderMap
pub fn into_header_map(self) -> HeaderMap
Consumes self and returns the inner HeaderMap.
Trait Implementations§
Source§impl Clone for RequestHeaders
impl Clone for RequestHeaders
Source§fn clone(&self) -> RequestHeaders
fn clone(&self) -> RequestHeaders
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for RequestHeaders
impl Debug for RequestHeaders
Source§impl Default for RequestHeaders
impl Default for RequestHeaders
Source§impl Display for RequestHeaders
impl Display for RequestHeaders
Source§impl From<HeaderMap> for RequestHeaders
impl From<HeaderMap> for RequestHeaders
Source§impl From<RequestHeaders> for HeaderMap
impl From<RequestHeaders> for HeaderMap
Source§fn from(headers: RequestHeaders) -> Self
fn from(headers: RequestHeaders) -> Self
Source§impl Index<&str> for RequestHeaders
impl Index<&str> for RequestHeaders
Source§fn index(&self, key: &str) -> &Self::Output
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.