Struct HeaderValue

pub struct HeaderValue { /* private fields */ }

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

impl HeaderValue

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

# use http::header::HeaderValue;
let val = HeaderValue::from_static("hello");
assert_eq!(val, "hello");
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

# use http::header::HeaderValue;
let val = HeaderValue::from_str("hello").unwrap();
assert_eq!(val, "hello");

An invalid value

# use http::header::HeaderValue;
let val = HeaderValue::from_str("\n");
assert!(val.is_err());
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

# use http::header::{HeaderValue, HeaderName};
# use http::header::ACCEPT;
let val = HeaderValue::from_name(ACCEPT);
assert_eq!(val, HeaderValue::from_bytes(b"accept").unwrap());
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

# use http::header::HeaderValue;
let val = HeaderValue::from_bytes(b"hello\xfa").unwrap();
assert_eq!(val, &b"hello\xfa"[..]);

An invalid value

# use http::header::HeaderValue;
let val = HeaderValue::from_bytes(b"\n");
assert!(val.is_err());
fn from_maybe_shared<T>(src: T) -> Result<HeaderValue, InvalidHeaderValue>
where
    T: AsRef<[u8]> + 'static,

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.

unsafe fn from_maybe_shared_unchecked<T>(src: T) -> HeaderValue
where
    T: AsRef<[u8]> + 'static,

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.

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

# use http::header::HeaderValue;
let val = HeaderValue::from_static("hello");
assert_eq!(val.to_str().unwrap(), "hello");
fn len(&self) -> usize

Returns the length of self.

This length is in bytes.

Examples

# use http::header::HeaderValue;
let val = HeaderValue::from_static("hello");
assert_eq!(val.len(), 5);
fn is_empty(&self) -> bool

Returns true if the HeaderValue has a length of zero bytes.

Examples

# use http::header::HeaderValue;
let val = HeaderValue::from_static("");
assert!(val.is_empty());

let val = HeaderValue::from_static("hello");
assert!(!val.is_empty());
fn as_bytes(&self) -> &[u8]

Converts a HeaderValue to a byte slice.

Examples

# use http::header::HeaderValue;
let val = HeaderValue::from_static("hello");
assert_eq!(val.as_bytes(), b"hello");
fn set_sensitive(&mut self, val: bool)

Mark that the header value represents sensitive information.

Examples

# use http::header::HeaderValue;
let mut val = HeaderValue::from_static("my secret");

val.set_sensitive(true);
assert!(val.is_sensitive());

val.set_sensitive(false);
assert!(!val.is_sensitive());
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

# use http::header::HeaderValue;
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

impl AsRef<[u8]> for HeaderValue

fn as_ref(&self) -> &[u8]

impl Clone for HeaderValue

fn clone(&self) -> HeaderValue

impl Debug for HeaderValue

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

impl Eq for HeaderValue

impl From<&HeaderValue> for HeaderValue

fn from(t: &HeaderValue) -> Self

impl From<HeaderName> for HeaderValue

fn from(h: HeaderName) -> HeaderValue

impl From<i16> for HeaderValue

fn from(num: i16) -> HeaderValue

impl From<i32> for HeaderValue

fn from(num: i32) -> HeaderValue

impl From<i64> for HeaderValue

fn from(num: i64) -> HeaderValue

impl From<isize> for HeaderValue

fn from(num: isize) -> HeaderValue

impl From<u16> for HeaderValue

fn from(num: u16) -> HeaderValue

impl From<u32> for HeaderValue

fn from(num: u32) -> HeaderValue

impl From<u64> for HeaderValue

fn from(num: u64) -> HeaderValue

impl From<usize> for HeaderValue

fn from(num: usize) -> HeaderValue

impl FromStr for HeaderValue

type Err = InvalidHeaderValue;
fn from_str(s: &str) -> Result<HeaderValue, Self::Err>

impl Hash for HeaderValue

fn hash<H: Hasher>(&self, state: &mut H)

impl Ord for HeaderValue

fn cmp(&self, other: &Self) -> Ordering

impl PartialEq for HeaderValue

fn eq(&self, other: &HeaderValue) -> bool

impl PartialEq<String> for HeaderValue

fn eq(&self, other: &String) -> bool

impl PartialEq<[u8]> for HeaderValue

fn eq(&self, other: &[u8]) -> bool

impl PartialEq<str> for HeaderValue

fn eq(&self, other: &str) -> bool

impl PartialOrd for HeaderValue

fn partial_cmp(&self, other: &HeaderValue) -> Option<Ordering>

impl PartialOrd<String> for HeaderValue

fn partial_cmp(&self, other: &String) -> Option<Ordering>

impl PartialOrd<[u8]> for HeaderValue

fn partial_cmp(&self, other: &[u8]) -> Option<Ordering>

impl PartialOrd<str> for HeaderValue

fn partial_cmp(&self, other: &str) -> Option<Ordering>

impl TryFrom<&String> for HeaderValue

type Error = InvalidHeaderValue;
fn try_from(s: &String) -> Result<Self, Self::Error>

impl TryFrom<&[u8]> for HeaderValue

type Error = InvalidHeaderValue;
fn try_from(t: &[u8]) -> Result<Self, Self::Error>

impl TryFrom<&str> for HeaderValue

type Error = InvalidHeaderValue;
fn try_from(t: &str) -> Result<Self, Self::Error>

impl TryFrom<String> for HeaderValue

type Error = InvalidHeaderValue;
fn try_from(t: String) -> Result<Self, Self::Error>

impl TryFrom<Vec<u8>> for HeaderValue

type Error = InvalidHeaderValue;
fn try_from(vec: Vec<u8>) -> Result<Self, Self::Error>

impl<T: ?Sized> PartialEq<&T> for HeaderValue where HeaderValue: PartialEq<T>,

fn eq(&self, other: &&T) -> bool

impl<T: ?Sized> PartialOrd<&T> for HeaderValue where HeaderValue: PartialOrd<T>,

fn partial_cmp(&self, other: &&T) -> Option<Ordering>

Auto Trait Implementations

impl !Freeze for HeaderValue

impl RefUnwindSafe for HeaderValue

impl Send for HeaderValue

impl Sync for HeaderValue

impl Unpin for HeaderValue

impl UnsafeUnpin for HeaderValue

impl UnwindSafe for HeaderValue

Blanket Implementations

impl<T> Any for HeaderValue where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for HeaderValue where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for HeaderValue where T: ?Sized,

fn borrow_mut(&mut self) -> &mut T

impl<T> CloneToUninit for HeaderValue where T: Clone,

unsafe fn clone_to_uninit(&self, dest: *mut u8)

impl<T> From<T> for HeaderValue

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for HeaderValue where T: Clone,

type Owned = T;
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)

impl<T, U> Into<U> for HeaderValue where U: From<T>,

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom<U> for HeaderValue where U: Into<T>,

type Error = never;
fn try_from(value: U) -> Result<T, never>

impl<T, U> TryInto<U> for HeaderValue where U: TryFrom<T>,

type Error = <U as TryFrom<T>>::Error;
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>