pub struct ResponseHandle { /* private fields */ }
Expand description

A low-level interface to HTTP responses.

For most applications, you should use Response instead of this interface. See the top-level handle documentation for more details.

Sending to the client

Each execution of a Compute program may send a single response back to the client:

If no response is explicitly sent by the program, a default 200 OK response is sent.

Creation and conversion

Response handles can be created programmatically using ResponseHandle::new()

Response handles are also returned from backend requests:

Implementations§

source§

impl ResponseHandle

source

pub const INVALID: Self = _

An invalid handle.

This is primarily useful to represent uninitialized values when using the interfaces in fastly_sys.

source

pub const fn is_valid(&self) -> bool

Returns true if the response handle is valid.

source

pub const fn is_invalid(&self) -> bool

Returns true if the response handle is invalid.

source

pub fn as_u32_mut(&mut self) -> &mut u32

Get a mutable reference to the underlying u32 representation of the handle.

This should only be used when calling the raw ABI directly, and care should be taken not to reuse or alias handle values.

source

pub fn into_u32(self) -> u32

Turn a handle into its representation without closing the underlying resource.

This should only be used when calling the raw ABI directly, and care should be taken not to reuse or alias handle values.

source

pub fn new() -> Self

Acquire a new response handle.

By default, the response will have a status code of 200 OK and empty headers.

source

pub fn get_header_names<'a>( &'a self, buf_size: usize ) -> impl Iterator<Item = Result<HeaderName, BufferSizeError>> + 'a

Read the response’s header names via a buffer of the provided size.

If there is a header name that is longer than buf_size, this will return a BufferSizeError; you can retry with a larger buffer size if necessary.

source

pub fn get_header_values<'a>( &'a self, name: &'a HeaderName, max_len: usize ) -> impl Iterator<Item = Result<HeaderValue, BufferSizeError>> + 'a

Get a response’s header values given a header name, via a buffer of the provided size.

If there is a header value that is longer than the buffer, this will return a BufferSizeError; you can retry with a larger buffer size if necessary.

Examples

Collect all the header values into a Vec:

let name = HeaderName::from_static("My-App-Header");
let buf_size = 128;
let header_values: Vec<HeaderValue> = response
    .get_header_values(&name, buf_size)
    .collect::<Result<Vec<HeaderValue>, _>>()?;

To try again with a larger buffer if the first call fails, you can use unwrap_or_else():

let name = HeaderName::from_static("My-App-Header");
let buf_size = 128;

// Collect header values into a `Vec<HeaderValue>`, with a buffer size of `128`.
// If the first call fails, print our error and then try to collect header values
// again. The second call will use a larger buffer size of `1024`.
let header_values: Vec<HeaderValue> = response
    .get_header_values(&name, buf_size)
    .collect::<Result<_, _>>()
    .unwrap_or_else(|err: BufferSizeError| {
        eprintln!("buffer size error: {}", err);
        let larger_buf_size = 1024;
        response
            .get_header_values(&name, larger_buf_size)
            .collect::<Result<_, _>>()
            .unwrap()
    });
source

pub fn set_header_values<'a, I>(&mut self, name: &HeaderName, values: I)
where I: IntoIterator<Item = &'a HeaderValue>,

Set the values for the given header name, replacing any headers that previously existed for that name.

source

pub fn get_header_value( &self, name: &HeaderName, max_len: usize ) -> Result<Option<HeaderValue>, BufferSizeError>

Get the value of a header, or None if the header is not present.

If there are multiple values for the header, only one is returned. See get_header_values() if you need to get all of the values.

If the value is longer than max_len, this will return a BufferSizeError; you can retry with a larger buffer size if necessary.

source

pub fn insert_header(&mut self, name: &HeaderName, value: &HeaderValue)

Set a response header to the given value, discarding any previous values for the given header name.

source

pub fn append_header(&mut self, name: &HeaderName, value: &HeaderValue)

Add a response header with given value.

Unlike insert_header(), this does not discard existing values for the same header name.

source

pub fn remove_header(&mut self, name: &HeaderName) -> bool

Remove all response headers of the given name, and return whether any headers were removed.

source

pub fn set_status(&mut self, status: StatusCode)

Set the HTTP status code of this response.

source

pub fn get_status(&self) -> StatusCode

Get the HTTP status code of this response.

source

pub fn get_version(&self) -> Version

Get the HTTP version of this response.

source

pub fn set_version(&mut self, v: Version)

Set the HTTP version of this response.

source

pub fn send_to_client(self, body: BodyHandle)

Immediately begin sending this response downstream to the client with the given body.

source

pub fn stream_to_client(self, body: BodyHandle) -> StreamingBodyHandle

Immediately begin sending this response downstream to the client, and return a StreamingBodyHandle that can accept further data to send.

source

pub fn set_framing_headers_mode(&mut self, mode: FramingHeadersMode)

Sets the way that framing headers are determined for this response.

source

pub fn close(self) -> Result<(), HandleError>

Close the ResponseHandle by removing it from the host Session. If the handle has already been closed an error will be returned. A ResponseHandle is only consumed when you send a response to a client or stream one to a client. You should call close only if you don’t intend to use that Response anymore.

Examples
let response = ResponseHandle::new();
// The handle is not being used so we can close it out without any
// trouble
response.close()?;

Trait Implementations§

source§

impl Debug for ResponseHandle

source§

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

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

impl Drop for ResponseHandle

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl Hash for ResponseHandle

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 ResponseHandle

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for ResponseHandle

source§

impl StructuralEq for ResponseHandle

source§

impl StructuralPartialEq for ResponseHandle

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

§

type Output = T

Should always be Self
source§

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

§

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>,

§

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.