Struct http::header::HeaderValue
source · [−]pub struct HeaderValue { /* private fields */ }
Expand description
Represents an HTTP header field value.
In practice, HTTP header field values are usually valid ASCII. However, the HTTP spec allows for a header value to contain opaque bytes as well. In this case, the header field value is not able to be represented as a string.
To handle this, the HeaderValue
is useable as a type and can be compared
with strings and implements Debug
. A to_str
fn is provided that returns
an Err
if the header value contains non visible ascii characters.
Implementations
sourceimpl HeaderValue
impl HeaderValue
sourcepub const fn from_static(src: &'static str) -> HeaderValue
pub const fn from_static(src: &'static str) -> HeaderValue
Convert a static string to a HeaderValue
.
This function will not perform any copying, however the string is checked to ensure that no invalid characters are present. Only visible ASCII characters (32-127) are permitted.
Panics
This function panics if the argument contains invalid header value characters.
Until Allow panicking in constants makes its way into stable, the panic message at compile-time is going to look cryptic, but should at least point at your header value:
error: any use of this value will cause an error
--> http/src/header/value.rs:67:17
|
67 | ([] as [u8; 0])[0]; // Invalid header value
| ^^^^^^^^^^^^^^^^^^
| |
| index out of bounds: the length is 0 but the index is 0
| inside `HeaderValue::from_static` at http/src/header/value.rs:67:17
| inside `INVALID_HEADER` at src/main.rs:73:33
|
::: src/main.rs:73:1
|
73 | const INVALID_HEADER: HeaderValue = HeaderValue::from_static("жsome value");
| ----------------------------------------------------------------------------
Examples
let val = HeaderValue::from_static("hello");
assert_eq!(val, "hello");
sourcepub fn from_str(src: &str) -> Result<HeaderValue, InvalidHeaderValue>
pub fn from_str(src: &str) -> Result<HeaderValue, InvalidHeaderValue>
Attempt to convert a string to a HeaderValue
.
If the argument contains invalid header value characters, an error is
returned. Only visible ASCII characters (32-127) are permitted. Use
from_bytes
to create a HeaderValue
that includes opaque octets
(128-255).
This function is intended to be replaced in the future by a TryFrom
implementation once the trait is stabilized in std.
Examples
let val = HeaderValue::from_str("hello").unwrap();
assert_eq!(val, "hello");
An invalid value
let val = HeaderValue::from_str("\n");
assert!(val.is_err());
sourcepub fn from_name(name: HeaderName) -> HeaderValue
pub fn from_name(name: HeaderName) -> HeaderValue
Converts a HeaderName into a HeaderValue
Since every valid HeaderName is a valid HeaderValue this is done infallibly.
Examples
let val = HeaderValue::from_name(ACCEPT);
assert_eq!(val, HeaderValue::from_bytes(b"accept").unwrap());
sourcepub fn from_bytes(src: &[u8]) -> Result<HeaderValue, InvalidHeaderValue>
pub fn from_bytes(src: &[u8]) -> Result<HeaderValue, InvalidHeaderValue>
Attempt to convert a byte slice to a HeaderValue
.
If the argument contains invalid header value bytes, an error is returned. Only byte values between 32 and 255 (inclusive) are permitted, excluding byte 127 (DEL).
This function is intended to be replaced in the future by a TryFrom
implementation once the trait is stabilized in std.
Examples
let val = HeaderValue::from_bytes(b"hello\xfa").unwrap();
assert_eq!(val, &b"hello\xfa"[..]);
An invalid value
let val = HeaderValue::from_bytes(b"\n");
assert!(val.is_err());
Attempt to convert a Bytes
buffer to a HeaderValue
.
This will try to prevent a copy if the type passed is the type used internally, and will copy the data if it is not.
Convert a Bytes
directly into a HeaderValue
without validating.
This function does NOT validate that illegal bytes are not contained within the buffer.
sourcepub fn to_str(&self) -> Result<&str, ToStrError>
pub fn to_str(&self) -> Result<&str, ToStrError>
Yields a &str
slice if the HeaderValue
only contains visible ASCII
chars.
This function will perform a scan of the header value, checking all the characters.
Examples
let val = HeaderValue::from_static("hello");
assert_eq!(val.to_str().unwrap(), "hello");
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the length of self
.
This length is in bytes.
Examples
let val = HeaderValue::from_static("hello");
assert_eq!(val.len(), 5);
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the HeaderValue
has a length of zero bytes.
Examples
let val = HeaderValue::from_static("");
assert!(val.is_empty());
let val = HeaderValue::from_static("hello");
assert!(!val.is_empty());
sourcepub fn as_bytes(&self) -> &[u8]ⓘNotable traits for &mut [u8]impl Write for &mut [u8]impl Read for &[u8]
pub fn as_bytes(&self) -> &[u8]ⓘNotable traits for &mut [u8]impl Write for &mut [u8]impl Read for &[u8]
Converts a HeaderValue
to a byte slice.
Examples
let val = HeaderValue::from_static("hello");
assert_eq!(val.as_bytes(), b"hello");
sourcepub fn set_sensitive(&mut self, val: bool)
pub fn set_sensitive(&mut self, val: bool)
Mark that the header value represents sensitive information.
Examples
let mut val = HeaderValue::from_static("my secret");
val.set_sensitive(true);
assert!(val.is_sensitive());
val.set_sensitive(false);
assert!(!val.is_sensitive());
sourcepub fn is_sensitive(&self) -> bool
pub fn is_sensitive(&self) -> bool
Returns true
if the value represents sensitive data.
Sensitive data could represent passwords or other data that should not be stored on disk or in memory. By marking header values as sensitive, components using this crate can be instructed to treat them with special care for security reasons. For example, caches can avoid storing sensitive values, and HPACK encoders used by HTTP/2.0 implementations can choose not to compress them.
Additionally, sensitive values will be masked by the Debug
implementation of HeaderValue
.
Note that sensitivity is not factored into equality or ordering.
Examples
let mut val = HeaderValue::from_static("my secret");
val.set_sensitive(true);
assert!(val.is_sensitive());
val.set_sensitive(false);
assert!(!val.is_sensitive());
Trait Implementations
sourceimpl AsRef<[u8]> for HeaderValue
impl AsRef<[u8]> for HeaderValue
sourceimpl Clone for HeaderValue
impl Clone for HeaderValue
sourcefn clone(&self) -> HeaderValue
fn clone(&self) -> HeaderValue
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresourceimpl Debug for HeaderValue
impl Debug for HeaderValue
sourceimpl<'a> From<&'a HeaderValue> for HeaderValue
impl<'a> From<&'a HeaderValue> for HeaderValue
sourcefn from(t: &'a HeaderValue) -> Self
fn from(t: &'a HeaderValue) -> Self
sourceimpl From<HeaderName> for HeaderValue
impl From<HeaderName> for HeaderValue
sourcefn from(h: HeaderName) -> HeaderValue
fn from(h: HeaderName) -> HeaderValue
sourceimpl From<i16> for HeaderValue
impl From<i16> for HeaderValue
sourcefn from(num: i16) -> HeaderValue
fn from(num: i16) -> HeaderValue
sourceimpl From<i32> for HeaderValue
impl From<i32> for HeaderValue
sourcefn from(num: i32) -> HeaderValue
fn from(num: i32) -> HeaderValue
sourceimpl From<i64> for HeaderValue
impl From<i64> for HeaderValue
sourcefn from(num: i64) -> HeaderValue
fn from(num: i64) -> HeaderValue
sourceimpl From<isize> for HeaderValue
impl From<isize> for HeaderValue
sourcefn from(num: isize) -> HeaderValue
fn from(num: isize) -> HeaderValue
sourceimpl From<u16> for HeaderValue
impl From<u16> for HeaderValue
sourcefn from(num: u16) -> HeaderValue
fn from(num: u16) -> HeaderValue
sourceimpl From<u32> for HeaderValue
impl From<u32> for HeaderValue
sourcefn from(num: u32) -> HeaderValue
fn from(num: u32) -> HeaderValue
sourceimpl From<u64> for HeaderValue
impl From<u64> for HeaderValue
sourcefn from(num: u64) -> HeaderValue
fn from(num: u64) -> HeaderValue
sourceimpl From<usize> for HeaderValue
impl From<usize> for HeaderValue
sourcefn from(num: usize) -> HeaderValue
fn from(num: usize) -> HeaderValue
sourceimpl FromStr for HeaderValue
impl FromStr for HeaderValue
type Err = InvalidHeaderValue
type Err = InvalidHeaderValue
sourceimpl Hash for HeaderValue
impl Hash for HeaderValue
sourceimpl Ord for HeaderValue
impl Ord for HeaderValue
1.21.0 · sourcefn max(self, other: Self) -> Self
fn max(self, other: Self) -> Self
1.21.0 · sourcefn min(self, other: Self) -> Self
fn min(self, other: Self) -> Self
1.50.0 · sourcefn clamp(self, min: Self, max: Self) -> Selfwhere
Self: PartialOrd<Self>,
fn clamp(self, min: Self, max: Self) -> Selfwhere
Self: PartialOrd<Self>,
sourceimpl<'a, T: ?Sized> PartialEq<&'a T> for HeaderValuewhere
HeaderValue: PartialEq<T>,
impl<'a, T: ?Sized> PartialEq<&'a T> for HeaderValuewhere
HeaderValue: PartialEq<T>,
sourceimpl<'a> PartialEq<HeaderValue> for &'a HeaderValue
impl<'a> PartialEq<HeaderValue> for &'a HeaderValue
sourcefn eq(&self, other: &HeaderValue) -> bool
fn eq(&self, other: &HeaderValue) -> bool
sourceimpl<'a> PartialEq<HeaderValue> for &'a str
impl<'a> PartialEq<HeaderValue> for &'a str
sourcefn eq(&self, other: &HeaderValue) -> bool
fn eq(&self, other: &HeaderValue) -> bool
sourceimpl PartialEq<HeaderValue> for [u8]
impl PartialEq<HeaderValue> for [u8]
sourcefn eq(&self, other: &HeaderValue) -> bool
fn eq(&self, other: &HeaderValue) -> bool
sourceimpl PartialEq<HeaderValue> for HeaderValue
impl PartialEq<HeaderValue> for HeaderValue
sourcefn eq(&self, other: &HeaderValue) -> bool
fn eq(&self, other: &HeaderValue) -> bool
sourceimpl PartialEq<HeaderValue> for String
impl PartialEq<HeaderValue> for String
sourcefn eq(&self, other: &HeaderValue) -> bool
fn eq(&self, other: &HeaderValue) -> bool
sourceimpl PartialEq<HeaderValue> for str
impl PartialEq<HeaderValue> for str
sourcefn eq(&self, other: &HeaderValue) -> bool
fn eq(&self, other: &HeaderValue) -> bool
sourceimpl<'a, T: ?Sized> PartialOrd<&'a T> for HeaderValuewhere
HeaderValue: PartialOrd<T>,
impl<'a, T: ?Sized> PartialOrd<&'a T> for HeaderValuewhere
HeaderValue: PartialOrd<T>,
sourcefn partial_cmp(&self, other: &&'a T) -> Option<Ordering>
fn partial_cmp(&self, other: &&'a T) -> Option<Ordering>
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresourceimpl PartialOrd<[u8]> for HeaderValue
impl PartialOrd<[u8]> for HeaderValue
sourcefn partial_cmp(&self, other: &[u8]) -> Option<Ordering>
fn partial_cmp(&self, other: &[u8]) -> Option<Ordering>
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresourceimpl<'a> PartialOrd<HeaderValue> for &'a HeaderValue
impl<'a> PartialOrd<HeaderValue> for &'a HeaderValue
sourcefn partial_cmp(&self, other: &HeaderValue) -> Option<Ordering>
fn partial_cmp(&self, other: &HeaderValue) -> Option<Ordering>
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresourceimpl<'a> PartialOrd<HeaderValue> for &'a str
impl<'a> PartialOrd<HeaderValue> for &'a str
sourcefn partial_cmp(&self, other: &HeaderValue) -> Option<Ordering>
fn partial_cmp(&self, other: &HeaderValue) -> Option<Ordering>
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresourceimpl PartialOrd<HeaderValue> for [u8]
impl PartialOrd<HeaderValue> for [u8]
sourcefn partial_cmp(&self, other: &HeaderValue) -> Option<Ordering>
fn partial_cmp(&self, other: &HeaderValue) -> Option<Ordering>
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresourceimpl PartialOrd<HeaderValue> for HeaderValue
impl PartialOrd<HeaderValue> for HeaderValue
sourcefn partial_cmp(&self, other: &HeaderValue) -> Option<Ordering>
fn partial_cmp(&self, other: &HeaderValue) -> Option<Ordering>
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresourceimpl PartialOrd<HeaderValue> for String
impl PartialOrd<HeaderValue> for String
sourcefn partial_cmp(&self, other: &HeaderValue) -> Option<Ordering>
fn partial_cmp(&self, other: &HeaderValue) -> Option<Ordering>
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresourceimpl PartialOrd<HeaderValue> for str
impl PartialOrd<HeaderValue> for str
sourcefn partial_cmp(&self, other: &HeaderValue) -> Option<Ordering>
fn partial_cmp(&self, other: &HeaderValue) -> Option<Ordering>
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresourceimpl PartialOrd<String> for HeaderValue
impl PartialOrd<String> for HeaderValue
sourcefn partial_cmp(&self, other: &String) -> Option<Ordering>
fn partial_cmp(&self, other: &String) -> Option<Ordering>
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresourceimpl PartialOrd<str> for HeaderValue
impl PartialOrd<str> for HeaderValue
sourcefn partial_cmp(&self, other: &str) -> Option<Ordering>
fn partial_cmp(&self, other: &str) -> Option<Ordering>
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more