Skip to main content

Response

Struct Response 

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

A runtime-neutral HTTP response.

Implementations§

Source§

impl Response

Source

pub fn from_bytes(status: u16, body: impl Into<Vec<u8>>) -> Self

Creates a buffered response with no headers.

Source

pub fn empty(status: u16) -> Self

Creates an empty buffered response.

Source

pub fn payload_too_large(max_body_bytes: usize) -> Self

Creates the canonical response used when an adapter rejects an oversized body before dispatch.

Source

pub const fn status(&self) -> u16

Source

pub const fn set_status(&mut self, status: u16)

Replaces the response status.

Source

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

Source

pub fn headers(&self) -> impl Iterator<Item = (&str, &str)>

Source

pub fn set_header(&mut self, name: impl AsRef<str>, value: impl Into<String>)

Inserts or replaces a response header. Set-Cookie is appended so independent cookie mutations are preserved.

Source

pub fn remove_header(&mut self, name: &str)

Removes every response header with the supplied name.

Source

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

Source

pub fn replace_body(&mut self, body: impl Into<Vec<u8>>) -> bool

Replaces a buffered response body. Streaming responses are left untouched and return false.

Source

pub const fn is_streaming(&self) -> bool

Returns whether this response owns a pull-based streaming body.

Source

pub fn take_body_stream(&mut self) -> Option<StreamingBody>

Takes the pull-based streaming body, leaving the response buffered.

A middleware layer that transforms a streamed body, such as a chunk-wise content encoder, takes the source stream here and installs its wrapper with Response::set_body_stream.

Source

pub fn set_body_stream(&mut self, stream: StreamingBody)

Installs a pull-based streaming body, discarding any buffered bytes.

The caller owns the framing headers: a streamed body has no known length, so content-length must be removed rather than left stale.

Source

pub const fn is_upgrade(&self) -> bool

Source

pub fn take_upgrade(&mut self) -> Option<HttpUpgrade>

Takes ownership of a validated one-shot protocol upgrade.

Source

pub fn take_background_tasks(&mut self) -> Vec<BackgroundTask>

Takes the after-response tasks so a network adapter can schedule them after the body has been written.

Source

pub async fn run_background(&mut self) -> Result<(), BackgroundTaskError>

Runs after-response tasks sequentially and gives every task a chance to complete.

§Errors

Returns the first task failure after all tasks have run.

Source

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

Returns the wire body length when it is known before streaming starts.

Source

pub async fn next_body_chunk( &mut self, ) -> Option<Result<Vec<u8>, BodyStreamError>>

Pulls one streaming body chunk.

Calling this method is the backpressure demand boundary. Buffered responses return None; use Self::body for their bytes.

Source

pub async fn collect_body( self, limit: usize, ) -> Result<Vec<u8>, CollectBodyError>

Collects buffered or streaming bytes with an explicit memory bound.

This is intended for tests and clients that deliberately want to turn a stream back into one allocation. Network adapters should pull and write chunks directly.

§Errors

Returns a producer error or CollectBodyError::LimitExceeded.

Source

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

Decodes the response body as UTF-8.

§Errors

Returns an error when the response body is not valid UTF-8.

Source

pub fn json<T: DeserializeOwned>(&self) -> Result<T, Error>

Deserializes the response body as JSON.

§Errors

Returns an error when the response is not valid JSON for T.

Source§

impl Response

Source

pub fn with_header( self, name: impl AsRef<str>, value: impl Into<String>, ) -> Self

Trait Implementations§

Source§

impl Debug for Response

Source§

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

Formats the value using the given formatter. Read more

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> BackgroundExt for T

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> 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> ResponseExt for T

Source§

fn header( self, name: impl Into<String>, value: impl Into<String>, ) -> WithHeaders<Self>

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.