Skip to main content

Response

Struct Response 

Source
pub struct Response { /* private fields */ }
Expand description

Parsed HTTP response.

§Examples

use barehttp::Response;

let raw = b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nok";
let response = Response::parse(raw).map_err(barehttp::Error::from)?;
assert_eq!(response.status_code(), 200);
assert_eq!(response.header("content-length"), Some("2"));
assert_eq!(response.to_text()?, "ok");

Implementations§

Source§

impl Response

Source

pub const fn status_code(&self) -> u16

Status code (e.g. 200).

Self::status is a deprecated alias.

Source

pub fn reason(&self) -> &str

Reason phrase from the status line.

Source

pub const fn headers(&self) -> &Headers

Response headers.

Source

pub fn body(&self) -> &[u8]

Response body bytes.

Self::as_bytes is a deprecated alias.

Source

pub const fn trailers(&self) -> &Headers

Trailer fields from chunked responses (RFC 9112 §7.1.2).

Same type as Self::headers: case-insensitive lookup, ordered iteration.

Source

pub fn parse(input: &[u8]) -> Result<Self, ParseError>

Parse a complete buffered HTTP/1.1 response.

One-pass header materialize + slice body decode (Callgrind-sensitive). The live receive path keeps owned-buffer adoption via parse_body_from_owned / wire spans on the connection buffer.

§Errors

ParseError when the status line, headers, framing, or body are illegal.

Source

pub fn header(&self, name: &str) -> Option<&str>

Look up a header by name (case-insensitive).

Source

pub const fn status(&self) -> u16

👎Deprecated:

use status_code

Deprecated alias of Self::status_code.

Source

pub const fn is_success(&self) -> bool

Status is 2xx.

Source

pub const fn is_redirect(&self) -> bool

Status is 3xx.

Source

pub const fn is_client_error(&self) -> bool

Status is 4xx.

Source

pub const fn is_server_error(&self) -> bool

Status is 5xx.

Source

pub fn as_bytes(&self) -> &[u8]

👎Deprecated:

use body

Deprecated alias of Self::body.

Source

pub fn to_text(&self) -> Result<&str, Utf8Error>

Body as UTF-8 text (borrowed).

Leaves self intact on failure, so status and headers stay available. Error type is core::str::Utf8Error; it converts via From / ? into crate::Error::Utf8Error.

§Errors

core::str::Utf8Error if the body is not valid UTF-8.

Source

pub fn into_string(self) -> Result<String, IntoStringError>

Consume the response; return the body as a UTF-8 String.

Non-UTF-8 body: IntoStringError holds the original Response (status, headers, body). Converting that error to crate::Error via From drops the response; recover with IntoStringError::into_response or IntoStringError::response first.

Unique body buffer: success builds the String with no copy.

§Errors

IntoStringError if the body is not valid UTF-8.

§Examples
use barehttp::Response;

let bad = Response::parse(b"HTTP/1.1 200 OK\r\nContent-Length: 1\r\n\r\n\xff")
  .map_err(barehttp::Error::from)?;
if let Err(err) = bad.into_string() {
  assert_eq!(err.response().status_code(), 200);
  assert_eq!(err.into_response().body(), &[0xff]);
}
Source

pub fn into_bytes(self) -> Vec<u8>

Consume the response; return body bytes as a Vec<u8>.

For a borrowed view, use Self::body. Reclaims the allocation without a copy when the internal buffer is uniquely owned.

Trait Implementations§

Source§

impl AsRef<[u8]> for Response

Source§

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

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for Response

Source§

fn clone(&self) -> Response

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 Debug for Response

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Eq for Response

Source§

impl Hash for Response

Source§

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

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Response

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Response

Auto Trait Implementations§

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.