Response
Include fastly/http/response.h to use this class.
An HTTP response, including body, headers, and status code.
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
Responses can be created programmatically:
Response::new()Response::from_body()Response::from_status()
Responses are also returned from backend requests:
Builder-style methods
Response can be used as a builder, allowing responses to be constructed and used through method chaining. Methods with the with_ name prefix, such as Response::with_header(), return std::move(*this) to allow chaining. The builder style is typically most useful when constructing and using a response in a single expression. For example:
Setter methods
Setter methods, such as Response::set_header(), are prefixed by set_, and can be used interchangeably with the builder-style methods, allowing you to mix and match styles based on what is most convenient for your program. Setter methods tend to work better than builder-style methods when constructing a value involves conditional branches or loops. For example:
Response() .with_header("my-header", "hello!") .with_header("my-other-header", "Здравствуйте!") .send_to_client();auto resp{Response().with_header("my-header", "hello!")};if (needs_translation) { resp.set_header("my-other-header", "Здравствуйте!");}resp.send_to_client();Constructor
fastly::http::Response::Response()Create a new Response.
The new response is created with status code 200 OK, no headers, and an empty body.
Methods
append_body
View sourceAppend another Body to the body of this response without reading or writing any body contents.
If this response does not have a body, the appended body is set as the response's body.
This method should be used when combining bodies that have not necessarily been read yet, such as a body returned from a backend response.
Examples
Parameters
- body: Body
Returns
auto resp{Response::from_body("hello! backend says: ")};auto backend_resp{ Request::get("https://example.com/").send("example_backend")};resp.append_body(backend_resp.into_body());resp.send_to_client();append_header
View sourcefastly::expected<void> fastly::http::Response::append_header(std::string_view name, std::string_view value)Add a request header with given value.
Unlike Response::set_header(), this does not discard existing values for the same header name.
Parameters
- name: std::string_view
- value: std::string_view
Returns
clone_with_body
View sourceResponse fastly::http::Response::clone_with_body()Clone this response by reading in its body, and then writing the same body to the original and the cloned response.
This method requires mutable access to this response because reading from and writing to the body can involve an HTTP connection.
Returns
clone_without_body
View sourceResponse fastly::http::Response::clone_without_body()Make a new response with the same headers, status, and version of this response, but no body.
If you also need to clone the response body, use Response::clone_with_body().
Returns
contains_header
View sourcefastly::expected<bool> fastly::http::Response::contains_header(std::string_view name)Returns whether the given header name is present in the response.
Parameters
- name: std::string_view
Returns
get_age
View sourcestd::optional<std::chrono::milliseconds> fastly::http::Response::get_age()The current age of the response, if it is cached.
Returns std::nullopt if the response is not cached.
Returns
get_backend
View sourceGet the backend this response came from, or std::nullopt if the response is synthetic.
Examples
From a backend response:
From a synthetic response:
Returns
auto backend_resp{ Request::get("https://example.com/").send("example_backend")};assert( backend_resp.get_backend() == std::optional(Backend::from_name("example_backend")));Response synthetic_resp;assert(synthetic_resp.get_backend() == std::nullopt);get_backend_addr
View sourcestd::optional<std::string> fastly::http::Response::get_backend_addr()Get the address of the backend this response came from, or std::nullopt when the response is synthetic or cached.
Examples
From a backend response:
From a synthetic response:
Returns
auto backend_resp{Request::get("https://example.com/") .with_pass(true) .send("example_backend")};assert( backend_resp.get_backend_addr() == std::optional("127.0.0.1:443"));Response synthetic_resp;assert(synthetic_resp.get_backend_addr() == std::nullopt);get_backend_name
View sourcestd::optional<std::string> fastly::http::Response::get_backend_name()Get the name of the Backend this response came from, or std::nullopt if the response is synthetic.
Examples
From a backend response:
From a synthetic response:
Returns
autobackend_resp{Request::get("https://example.com/").send("example_backend")};assert(backend_resp.get_backend_name(),std::optional("example_backend"));Response synthetic_resp;assert(synthetic_resp.get_backend_name() == std::nullopt);get_content_length
View sourcestd::optional<size_t> fastly::http::Response::get_content_length()Get the value of the response's Content-Length header, if it exists.
Returns
get_content_type
View sourcestd::optional<std::string> fastly::http::Response::get_content_type()Get the MIME type described by the response's Content-Type header, or std::nullopt if that header is absent or contains an invalid MIME type.
Returns
get_header
View sourcefastly::expected<std::optional<HeaderValue>> fastly::http::Response::get_header(std::string_view name)Get the value of a header, or std::nullopt if the header is not present.
If there are multiple values for the header, only one is returned, which may be any of the values. See Response::get_header_all() all of the values.
Parameters
- name: std::string_view
Returns
get_header_all
View sourceGet an iterator of all the values of a header.
Parameters
- name: std::string_view
Returns
get_stale_while_revalidate
View sourcestd::optional<std::chrono::milliseconds> fastly::http::Response::get_stale_while_revalidate()The time for which the response can safely be used despite being considered stale, if it is cached.
Returns std::nullopt if the response is not cached.
Returns
get_ttl
View sourcestd::optional<std::chrono::milliseconds> fastly::http::Response::get_ttl()Get the Time to Live (TTL) in the cache for this response, if it is cached.
The TTL provides the duration of "freshness" for the cached response after it is inserted into the cache. If the response is stale, the TTL is 0 (i.e. this returns std::optional(0).
Returns std::nullopt if the response is not cached.
Returns
get_version
View sourceVersion fastly::http::Response::get_version()Get the HTTP version of this response.
Returns
has_body
View sourcebool fastly::http::Response::has_body()Returns true if this response has a body.
Returns
into_body_bytes
View sourceConsume the response and return its body as a byte vector.
Returns
into_body_string
View sourcestd::string fastly::http::Response::into_body_string()Consume the response and return its body as a string.
Returns
is_from_backend
View sourcebool fastly::http::Response::is_from_backend()Return whether the response is from a backend request.
Returns
static Response fastly::http::Response::redirect(std::string_view destination)Create a 308 Permanent Redirect response with the given value as the Location header.
Examples
Parameters
- destination: std::string_view
Returns
auto resp{Response::redirect("https://www.fastly.com")};assert(resp.get_status() == StatusCode::PERMANENT_REDIRECT);assert(resp.get_header("Location").value(), "https://www.fastly.com");remove_header
View sourcefastly::expected<std::optional<std::string>> fastly::http::Response::remove_header(std::string_view name)Remove all request headers of the given name, and return one of the removed header values if any were present.
Parameters
- name: std::string_view
Returns
static Response fastly::http::Response::see_other(std::string_view destination)Create a 303 See Other response with the given value as the Location header.
Examples
Parameters
- destination: std::string_view
Returns
auto resp{Response::see_other("https://www.fastly.com")};assert(resp.get_status() == StatusCode::SEE_OTHER);assert(resp.get_header("Location").value(), "https://www.fastly.com");send_to_client
View sourcevoid fastly::http::Response::send_to_client()Begin sending the response to the client.
This method returns as soon as the response header begins sending to the client, and transmission of the response will continue in the background.
Once this method is called, nothing else may be added to the response body. To stream additional data to a response body after it begins to send, use Response::stream_to_client().
Panics
This method panics if another response has already been sent to the client by this method, by Response::stream_to_client().
Examples
Sending a backend response without modification:
Removing a header from a backend response before sending to the client:
Sending a synthetic response:
Returns
Request::get("https://example.com/").send("example_backend").send_to_client();autobackend_resp{Request::get("https://example.com/").send("example_backend")};backend_resp.remove_header("bad-header");backend_resp.send_to_client();Response::from_body("hello, world!").send_to_client();set_body_octet_stream
View sourceSet the given bytes as the response's body with content type application/octet-stream.
Parameters
- body: std::vector<uint8_t>
Returns
set_body_text_html
View sourcefastly::expected<void> fastly::http::Response::set_body_text_html(std::string_view body)Set the given string as the response's body with content type text/html; charset=UTF-8.
Parameters
- body: std::string_view
Returns
set_body_text_plain
View sourcefastly::expected<void> fastly::http::Response::set_body_text_plain(std::string_view body)Set the given string as the response's body with content type text/plain; charset=UTF-8.
Parameters
- body: std::string_view
Returns
set_content_type
View sourcevoid fastly::http::Response::set_content_type(std::string_view mime)Set the MIME type described by the response's Content-Type header.
Any existing Content-Type header values will be overwritten.
Parameters
- mime: std::string_view
Returns
set_header
View sourcefastly::expected<void> fastly::http::Response::set_header(std::string_view name, std::string_view value)Set a response header to the given value, discarding any previous values for the given header name.
Parameters
- name: std::string_view
- value: std::string_view
Returns
set_status
View sourceSet the HTTP status code of the response.
Examples
Using the constants from StatusCode:
Using a uint16_t:
Parameters
- status: StatusCode
Returns
auto resp{fastly::Response::from_body("not found!")};resp.set_status(fastly::http::StatusCode::NOT_FOUND);resp.send_to_client();auto resp{fastly::Response::from_body("not found!")};resp.set_status(404);resp.send_to_client();set_version
View sourcevoid fastly::http::Response::set_version(Version version)Set the HTTP version of this response.
Parameters
- version: Version
Returns
stream_to_client
View sourceBegin sending the response to the client, and return a StreamingBody that can accept further data to send.
The client connection must be closed when finished writing the response by calling StreamingBody::finish().
This method is most useful for programs that do some sort of processing or inspection of a potentially-large backend response body. Streaming allows the program to operate on small parts of the body rather than having to read it all into memory at once.
This method returns as soon as the response header begins sending to the client, and transmission of the response will continue in the background.
Panics
This method panics if another response has already been sent to the client by this method, by Response::send_to_client().
Examples
Count the number of lines in a UTF-8 backend response body while sending it to the client:
Returns
123456789101112131415161718192021222324auto backend_resp{ Request::get("https://example.com/").send("example_backend")};
// Take the body so we can iterate through its lines laterauto backend_resp_body{backend_resp.take_body()};
// Start sending the backend response to the client with a now-empty bodyauto client_body{backend_resp.stream_to_client()};
size_t num_lines{0};std::string line;while (getline(backend_resp_body, line)) { num_lines++; client_body << line;}// Finish the streaming body to close the client connection.client_body.finish();
std::cout << "backend response body contained " << num_lines << " lines" << std::endl;take_backend_request
View sourceTake and return the request this response came from, or std::nullopt if the response is synthetic.
Note that the returned request will only have the headers and metadata of the original request, as the body is consumed when sending the request.
Examples
From a backend response:
From a synthetic response:
Returns
auto backend_resp{Request::post("https://example.com/") .with_body("hello") .send("example_backend")};auto backend_req{backend_resp.take_backend_request().value()};assert(backend_req.get_url() == "https://example.com/");assert(!backend_req.has_body());backend_req.with_body("goodbye").send("example_backend");Response synthetic_resp;assert(synthetic_resp.take_backend_request() == std::nullopt);take_body
View sourceTake and return the body from this response.
After calling this method, this response will no longer have a body.
Returns
take_body_bytes
View sourceTake and return the body from this response as a vector of bytes.
After calling this method, this response will no longer have a body.
Returns
take_body_string
View sourcestd::string fastly::http::Response::take_body_string()Take and return the body from this response as a string.
After calling this method, this response will no longer have a body.
Returns
static Response fastly::http::Response::temporary_redirect(std::string_view destination)Create a 307 Temporary Redirect response with the given value as the Location header.
Examples
Parameters
- destination: std::string_view
Returns
auto resp{Response::temporary_redirect("https://www.fastly.com")};assert(resp.get_status() == StatusCode::TEMPORARY_REDIRECT);assert(resp.get_header("Location").value(), "https://www.fastly.com");with_body_octet_stream
View sourceBuilder-style equivalent of Response::set_body_octet_stream().
Parameters
- body: std::vector<uint8_t>
Returns
with_body_text_html
View sourcefastly::expected<Response> fastly::http::Response::with_body_text_html(std::string_view body) &&Builder-style equivalent of Response::set_body_text_html().
Parameters
- body: std::string_view
Returns
with_body_text_plain
View sourcefastly::expected<Response> fastly::http::Response::with_body_text_plain(std::string_view body) &&Builder-style equivalent of Response::set_body_text_plain().
Parameters
- body: std::string_view
Returns
with_content_type
View sourceResponse fastly::http::Response::with_content_type(std::string_view mime) &&Builder-style equivalent of Response::set_content_type().
Parameters
- mime: std::string_view
Returns
with_header
View sourcefastly::expected<Response> fastly::http::Response::with_header(std::string_view name, std::string_view value) &&Builder-style equivalent of Response::append_header().
Parameters
- name: std::string_view
- value: std::string_view
Returns
with_set_header
View sourcefastly::expected<Response> fastly::http::Response::with_set_header(std::string_view name, std::string_view value) &&Builder-style equivalent of Response::set_header().
Parameters
- name: std::string_view
- value: std::string_view
Returns
with_status
View sourceBuilder-style equivalent of Response::set_status().
Parameters
- status: StatusCode
Returns
with_version
View sourceResponse fastly::http::Response::with_version(Version version) &&Builder-style equivalent of Response::set_version().
Parameters
- version: Version
Returns