Skip to main content

ResponseHead

Struct ResponseHead 

Source
pub struct ResponseHead<'a> { /* private fields */ }
Expand description

A parsed response head, borrowed from the client connection’s buffer.

§Examples

use http_net::{ClientConn, ClientEvent, Request};

let mut conn = ClientConn::new();
let mut out = Vec::new();
conn.send_request(&Request::get("/").header("host", "a"), &mut out).unwrap();
let _ = conn.feed(b"HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n");
let ClientEvent::Response(resp) = conn.poll().unwrap() else { panic!() };
assert_eq!(resp.status(), 404);
assert_eq!(resp.reason(), "Not Found");

Implementations§

Source§

impl<'a> ResponseHead<'a>

Source

pub fn status(&self) -> u16

The status code (100..=599).

§Examples
assert_eq!(resp.status(), 200);
Source

pub fn reason(&self) -> &'a str

The reason phrase, if it is UTF-8 (it carries no meaning; RFC 9112 tells clients to ignore it). Empty for a status line without one, including HTTP/1.1 200 with no space after the code.

§Examples
assert_eq!(resp.reason(), "");
Source

pub fn version(&self) -> Version

The protocol version.

§Examples
assert_eq!(resp.version(), Version::Http10);
Source

pub fn headers(&self) -> Headers<'a> ⓘ

All fields in arrival order.

§Examples
assert_eq!(resp.headers().count(), 2);
Source

pub fn header(&self, name: &str) -> Option<&'a [u8]>

The value of the first field named name (ASCII case-insensitive).

§Examples
assert_eq!(resp.header("content-type"), Some(&b"text/plain"[..]));
Source

pub fn header_str(&self, name: &str) -> Option<&'a str>

Like header, but only when the value is UTF-8.

§Examples
assert_eq!(resp.header_str("server"), Some("x"));
Source

pub fn header_all<'n>(&self, name: &'n str) -> HeaderValues<'a, 'n> ⓘ

The values of every field named name, in arrival order. Each Set-Cookie is its own field (they are never comma-joined), so this is how a client reads all of them.

§Examples
let cookies: Vec<&[u8]> = resp.header_all("set-cookie").collect();
assert_eq!(cookies, [&b"a=1"[..], &b"b=2; Path=/"[..]]);
Source

pub fn content_length(&self) -> Option<u64>

The declared Content-Length, if the response has one, whatever the framing rules made of it (a HEAD response’s length is reported here while framing is None).

§Examples
assert_eq!(resp.framing(), BodyFraming::None);
assert_eq!(resp.content_length(), Some(1234));
Source

pub fn framing(&self) -> BodyFraming

How the response body is delimited (accounts for HEAD requests and bodiless status codes).

§Examples
assert_eq!(resp.framing(), BodyFraming::UntilClose);
Source

pub fn keep_alive(&self) -> bool

Whether the connection can carry another request after this response.

§Examples
assert!(resp.keep_alive());

Trait Implementations§

Source§

impl<'a> Clone for ResponseHead<'a>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<'a> Copy for ResponseHead<'a>

Source§

impl Debug for ResponseHead<'_>

Shows status, reason, version, fields, and framing; the values of credential fields (set-cookie above all) are replaced by <redacted N bytes>.

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for ResponseHead<'a>

§

impl<'a> RefUnwindSafe for ResponseHead<'a>

§

impl<'a> Send for ResponseHead<'a>

§

impl<'a> Sync for ResponseHead<'a>

§

impl<'a> Unpin for ResponseHead<'a>

§

impl<'a> UnsafeUnpin for ResponseHead<'a>

§

impl<'a> UnwindSafe for ResponseHead<'a>

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, 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.