pub struct HeaderValue { /* private fields */ }api only.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 usable 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§
Source§impl 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.
§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.
§Panics
In a debug build this will panic if src is not valid UTF-8.
§Safety
src must contain valid UTF-8. In a release build it is undefined
behaviour to call this with src that is not valid UTF-8.
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] ⓘ
pub fn as_bytes(&self) -> &[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§
Source§impl AsRef<[u8]> for HeaderValue
impl AsRef<[u8]> for HeaderValue
Source§impl Clone for HeaderValue
impl Clone for HeaderValue
Source§fn clone(&self) -> HeaderValue
fn clone(&self) -> HeaderValue
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for HeaderValue
impl Debug for HeaderValue
Source§impl<'a> From<&'a HeaderValue> for HeaderValue
impl<'a> From<&'a HeaderValue> for HeaderValue
Source§fn from(t: &'a HeaderValue) -> HeaderValue
fn from(t: &'a HeaderValue) -> HeaderValue
Source§impl From<Application> for HeaderValue
impl From<Application> for HeaderValue
Source§fn from(value: Application) -> Self
fn from(value: Application) -> Self
Source§impl From<Audio> for HeaderValue
impl From<Audio> for HeaderValue
Source§impl From<Chemical> for HeaderValue
impl From<Chemical> for HeaderValue
Source§impl From<Font> for HeaderValue
impl From<Font> for HeaderValue
Source§impl From<HeaderName> for HeaderValue
impl From<HeaderName> for HeaderValue
Source§fn from(h: HeaderName) -> HeaderValue
fn from(h: HeaderName) -> HeaderValue
Source§impl From<Image> for HeaderValue
impl From<Image> for HeaderValue
Source§impl From<Message> for HeaderValue
impl From<Message> for HeaderValue
Source§impl From<MimeType> for HeaderValue
impl From<MimeType> for HeaderValue
Source§impl From<Model> for HeaderValue
impl From<Model> for HeaderValue
Source§impl From<Multipart> for HeaderValue
impl From<Multipart> for HeaderValue
Source§impl From<Text> for HeaderValue
impl From<Text> for HeaderValue
Source§impl From<Video> for HeaderValue
impl From<Video> for HeaderValue
Source§impl From<i16> for HeaderValue
impl From<i16> for HeaderValue
Source§fn from(num: i16) -> HeaderValue
fn from(num: i16) -> HeaderValue
Source§impl From<i32> for HeaderValue
impl From<i32> for HeaderValue
Source§fn from(num: i32) -> HeaderValue
fn from(num: i32) -> HeaderValue
Source§impl From<i64> for HeaderValue
impl From<i64> for HeaderValue
Source§fn from(num: i64) -> HeaderValue
fn from(num: i64) -> HeaderValue
Source§impl From<isize> for HeaderValue
impl From<isize> for HeaderValue
Source§fn from(num: isize) -> HeaderValue
fn from(num: isize) -> HeaderValue
Source§impl From<u16> for HeaderValue
impl From<u16> for HeaderValue
Source§fn from(num: u16) -> HeaderValue
fn from(num: u16) -> HeaderValue
Source§impl From<u32> for HeaderValue
impl From<u32> for HeaderValue
Source§fn from(num: u32) -> HeaderValue
fn from(num: u32) -> HeaderValue
Source§impl From<u64> for HeaderValue
impl From<u64> for HeaderValue
Source§fn from(num: u64) -> HeaderValue
fn from(num: u64) -> HeaderValue
Source§impl From<usize> for HeaderValue
impl From<usize> for HeaderValue
Source§fn from(num: usize) -> HeaderValue
fn from(num: usize) -> HeaderValue
Source§impl FromStr for HeaderValue
impl FromStr for HeaderValue
Source§type Err = InvalidHeaderValue
type Err = InvalidHeaderValue
Source§fn from_str(s: &str) -> Result<HeaderValue, <HeaderValue as FromStr>::Err>
fn from_str(s: &str) -> Result<HeaderValue, <HeaderValue as FromStr>::Err>
s to return a value of this type. Read moreSource§impl Hash for HeaderValue
impl Hash for HeaderValue
Source§impl Ord for HeaderValue
impl Ord for HeaderValue
Source§fn cmp(&self, other: &HeaderValue) -> Ordering
fn cmp(&self, other: &HeaderValue) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<'a, T> PartialEq<&'a T> for HeaderValue
impl<'a, T> PartialEq<&'a T> for HeaderValue
Source§impl PartialEq<[u8]> for HeaderValue
impl PartialEq<[u8]> for HeaderValue
Source§impl<'a> PartialEq<HeaderValue> for &'a HeaderValue
impl<'a> PartialEq<HeaderValue> for &'a HeaderValue
Source§impl<'a> PartialEq<HeaderValue> for &'a str
impl<'a> PartialEq<HeaderValue> for &'a str
Source§impl PartialEq<HeaderValue> for [u8]
impl PartialEq<HeaderValue> for [u8]
Source§impl PartialEq<HeaderValue> for MimeType
impl PartialEq<HeaderValue> for MimeType
Source§impl PartialEq<HeaderValue> for str
impl PartialEq<HeaderValue> for str
Source§impl PartialEq<MimeType> for HeaderValue
impl PartialEq<MimeType> for HeaderValue
Source§impl PartialEq<String> for HeaderValue
impl PartialEq<String> for HeaderValue
Source§impl PartialEq<str> for HeaderValue
impl PartialEq<str> for HeaderValue
Source§impl PartialEq for HeaderValue
impl PartialEq for HeaderValue
Source§impl<'a, T> PartialOrd<&'a T> for HeaderValue
impl<'a, T> PartialOrd<&'a T> for HeaderValue
Source§impl PartialOrd<[u8]> for HeaderValue
impl PartialOrd<[u8]> for HeaderValue
Source§impl<'a> PartialOrd<HeaderValue> for &'a HeaderValue
impl<'a> PartialOrd<HeaderValue> for &'a HeaderValue
Source§impl<'a> PartialOrd<HeaderValue> for &'a str
impl<'a> PartialOrd<HeaderValue> for &'a str
Source§impl PartialOrd<HeaderValue> for [u8]
impl PartialOrd<HeaderValue> for [u8]
Source§impl PartialOrd<HeaderValue> for str
impl PartialOrd<HeaderValue> for str
Source§impl PartialOrd<String> for HeaderValue
impl PartialOrd<String> for HeaderValue
Source§impl PartialOrd<str> for HeaderValue
impl PartialOrd<str> for HeaderValue
Source§impl PartialOrd for HeaderValue
impl PartialOrd for HeaderValue
Source§impl<'a> TryFrom<&'a [u8]> for HeaderValue
impl<'a> TryFrom<&'a [u8]> for HeaderValue
Source§type Error = InvalidHeaderValue
type Error = InvalidHeaderValue
Source§fn try_from(
t: &'a [u8],
) -> Result<HeaderValue, <HeaderValue as TryFrom<&'a [u8]>>::Error>
fn try_from( t: &'a [u8], ) -> Result<HeaderValue, <HeaderValue as TryFrom<&'a [u8]>>::Error>
Source§impl TryFrom<&HeaderValue> for Application
impl TryFrom<&HeaderValue> for Application
Source§impl TryFrom<&HeaderValue> for Audio
impl TryFrom<&HeaderValue> for Audio
Source§impl TryFrom<&HeaderValue> for Chemical
impl TryFrom<&HeaderValue> for Chemical
Source§impl TryFrom<&HeaderValue> for Font
impl TryFrom<&HeaderValue> for Font
Source§impl TryFrom<&HeaderValue> for Image
impl TryFrom<&HeaderValue> for Image
Source§impl TryFrom<&HeaderValue> for Message
impl TryFrom<&HeaderValue> for Message
Source§impl TryFrom<&HeaderValue> for MimeType
impl TryFrom<&HeaderValue> for MimeType
Source§impl TryFrom<&HeaderValue> for Model
impl TryFrom<&HeaderValue> for Model
Source§impl TryFrom<&HeaderValue> for Multipart
impl TryFrom<&HeaderValue> for Multipart
Source§impl TryFrom<&HeaderValue> for Text
impl TryFrom<&HeaderValue> for Text
Source§impl TryFrom<&HeaderValue> for Video
impl TryFrom<&HeaderValue> for Video
Source§impl<'a> TryFrom<&'a String> for HeaderValue
impl<'a> TryFrom<&'a String> for HeaderValue
Source§type Error = InvalidHeaderValue
type Error = InvalidHeaderValue
Source§fn try_from(
s: &'a String,
) -> Result<HeaderValue, <HeaderValue as TryFrom<&'a String>>::Error>
fn try_from( s: &'a String, ) -> Result<HeaderValue, <HeaderValue as TryFrom<&'a String>>::Error>
Source§impl<'a> TryFrom<&'a str> for HeaderValue
impl<'a> TryFrom<&'a str> for HeaderValue
Source§type Error = InvalidHeaderValue
type Error = InvalidHeaderValue
Source§fn try_from(
t: &'a str,
) -> Result<HeaderValue, <HeaderValue as TryFrom<&'a str>>::Error>
fn try_from( t: &'a str, ) -> Result<HeaderValue, <HeaderValue as TryFrom<&'a str>>::Error>
Source§impl TryFrom<String> for HeaderValue
impl TryFrom<String> for HeaderValue
Source§type Error = InvalidHeaderValue
type Error = InvalidHeaderValue
Source§fn try_from(
t: String,
) -> Result<HeaderValue, <HeaderValue as TryFrom<String>>::Error>
fn try_from( t: String, ) -> Result<HeaderValue, <HeaderValue as TryFrom<String>>::Error>
impl Eq for HeaderValue
Auto Trait Implementations§
impl !Freeze for HeaderValue
impl RefUnwindSafe for HeaderValue
impl Send for HeaderValue
impl Sync for HeaderValue
impl Unpin for HeaderValue
impl UnwindSafe for HeaderValue
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> ToHex for T
impl<T> ToHex for T
Source§fn encode_hex<U>(&self) -> Uwhere
U: FromIterator<char>,
fn encode_hex<U>(&self) -> Uwhere
U: FromIterator<char>,
self into the result. Lower case
letters are used (e.g. f9b4ca)Source§fn encode_hex_upper<U>(&self) -> Uwhere
U: FromIterator<char>,
fn encode_hex_upper<U>(&self) -> Uwhere
U: FromIterator<char>,
self into the result. Upper case
letters are used (e.g. F9B4CA)