pub struct Response { /* private fields */ }Expand description
A runtime-neutral HTTP response.
Implementations§
Source§impl Response
impl Response
Sourcepub fn from_bytes(status: u16, body: impl Into<Vec<u8>>) -> Self
pub fn from_bytes(status: u16, body: impl Into<Vec<u8>>) -> Self
Creates a buffered response with no headers.
Sourcepub fn payload_too_large(max_body_bytes: usize) -> Self
pub fn payload_too_large(max_body_bytes: usize) -> Self
Creates the canonical response used when an adapter rejects an oversized body before dispatch.
pub const fn status(&self) -> u16
Sourcepub const fn set_status(&mut self, status: u16)
pub const fn set_status(&mut self, status: u16)
Replaces the response status.
pub fn get_header(&self, name: &str) -> Option<&str>
pub fn headers(&self) -> impl Iterator<Item = (&str, &str)>
Sourcepub fn set_header(&mut self, name: impl AsRef<str>, value: impl Into<String>)
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.
Sourcepub fn remove_header(&mut self, name: &str)
pub fn remove_header(&mut self, name: &str)
Removes every response header with the supplied name.
pub fn body(&self) -> &[u8] ⓘ
Sourcepub fn replace_body(&mut self, body: impl Into<Vec<u8>>) -> bool
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.
Sourcepub const fn is_streaming(&self) -> bool
pub const fn is_streaming(&self) -> bool
Returns whether this response owns a pull-based streaming body.
Sourcepub fn take_body_stream(&mut self) -> Option<StreamingBody>
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.
Sourcepub fn set_body_stream(&mut self, stream: StreamingBody)
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.
pub const fn is_upgrade(&self) -> bool
Sourcepub fn take_upgrade(&mut self) -> Option<HttpUpgrade>
pub fn take_upgrade(&mut self) -> Option<HttpUpgrade>
Takes ownership of a validated one-shot protocol upgrade.
Sourcepub fn take_background_tasks(&mut self) -> Vec<BackgroundTask>
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.
Sourcepub async fn run_background(&mut self) -> Result<(), BackgroundTaskError>
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.
Sourcepub fn exact_body_length(&self) -> Option<u64>
pub fn exact_body_length(&self) -> Option<u64>
Returns the wire body length when it is known before streaming starts.
Sourcepub async fn next_body_chunk(
&mut self,
) -> Option<Result<Vec<u8>, BodyStreamError>>
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.
Sourcepub async fn collect_body(
self,
limit: usize,
) -> Result<Vec<u8>, CollectBodyError>
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.