ResponseHandle
pub struct ResponseHandle { /* private fields */ }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:
Constants
An invalid handle.
This is primarily useful to represent uninitialized values when using the interfaces in
fastly_sys.
Methods
append_header
View sourceAdd a response header with given value.
Unlike insert_header(), this does not discard existing values
for the same header name.
Parameters
- self: &mut Self
- name: &HeaderName
- value: &HeaderValue
as_u32
View sourcefn as_u32(&self) -> u32Get the underlying 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.
Parameters
- self: &Self
Returns
as_u32_mut
View sourcefn as_u32_mut(&mut self) -> &mut u32Get 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.
Parameters
- self: &mut Self
Returns
close
View sourceClose 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// troubleresponse.close()?;Parameters
- self: Self
Returns
get_header_names
View sourcefn get_header_names(&self, buf_size: usize) -> impl Iterator<Item = Result<HeaderName, BufferSizeError>> + '_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.
Parameters
- self: &Self
- buf_size: usize
Returns
get_header_value
View sourcefn 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.
Parameters
- self: &Self
- name: &HeaderName
- max_len: usize
Returns
get_header_values
View sourcefn get_header_values<'a>(&'a self, name: &'a HeaderName, max_len: usize) -> impl Iterator<Item = Result<HeaderValue, BufferSizeError>> + 'aGet 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():
1234567891011121314151617let 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() });Parameters
- self: &Self
- name: &HeaderName
- max_len: usize
Returns
get_status
View sourcefn get_status(&self) -> StatusCodeGet the HTTP status code of this response.
Parameters
- self: &Self
Returns
get_version
View sourcefn get_version(&self) -> VersionGet the HTTP version of this response.
Parameters
- self: &Self
Returns
insert_header
View sourceSet a response header to the given value, discarding any previous values for the given header name.
Parameters
- self: &mut Self
- name: &HeaderName
- value: &HeaderValue
into_u32
View sourcefn into_u32(self) -> u32Turn 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.
Parameters
- self: Self
Returns
is_invalid
View sourceconst fn is_invalid(&self) -> boolReturns true if the response handle is invalid.
Parameters
- self: &Self
Returns
is_valid
View sourceconst fn is_valid(&self) -> boolReturns true if the response handle is valid.
Parameters
- self: &Self
Returns
new
View sourcefn new() -> SelfAcquire a new response handle.
By default, the response will have a status code of 200 OK and empty headers.
Returns
remote_addr
View sourceGet the address of the server that sent the response.
Parameters
- self: &Self
Returns
remove_header
View sourceRemove all response headers of the given name, and return whether any headers were removed.
Parameters
- self: &mut Self
- name: &HeaderName
Returns
send_to_client
View sourceImmediately begin sending this response downstream to the client with the given body.
Parameters
- self: Self
- body: BodyHandle
set_framing_headers_mode
View sourceSets the way that framing headers are determined for this response.
Parameters
- self: &mut Self
- mode: FramingHeadersMode
set_header_values
View sourcewhere I: IntoIterator<Item = &'a HeaderValue>,Set the values for the given header name, replacing any headers that previously existed for that name.
Parameters
- self: &mut Self
- name: &HeaderName
- values: I
set_status
View sourceSet the HTTP status code of this response.
Parameters
- self: &mut Self
- status: StatusCode
stream_to_client
View sourceImmediately begin sending this response downstream to the client, and return a
StreamingBodyHandle that can accept further data to send.
Parameters
- self: Self
- body: BodyHandle
Returns