Response

Classin httpcompute-sdk-cpp 0.7.0View source

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:

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 source
void fastly::http::Response::append_body(Body body)

Append 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

void
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 source
fastly::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

fastly::expected<void>

clone_with_body

View source
Response 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 source
Response 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 source
fastly::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

fastly::expected<bool>

from_body

staticView source
static Response fastly::http::Response::from_body(Body body)

Create a new Response with the given value as the body.

Parameters

body: Body

Returns

from_status

staticView source
static Response fastly::http::Response::from_status(StatusCode status)

Create a new response with the given status code.

Parameters

status: StatusCode

Returns

std::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

std::optional<std::chrono::milliseconds>

get_backend

View source
std::optional<fastly::backend::Backend> fastly::http::Response::get_backend()

Get the backend this response came from, or std::nullopt if the response is synthetic.

Examples

From a backend response:

From a synthetic response:

Returns

std::optional<fastly::backend::Backend>
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 source
std::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

std::optional<std::string>
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 source
std::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

std::optional<std::string>
auto
backend_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 source
std::optional<size_t> fastly::http::Response::get_content_length()

Get the value of the response's Content-Length header, if it exists.

Returns

std::optional<size_t>

get_content_type

View source
std::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

std::optional<std::string>

get_header

View source
fastly::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

fastly::expected<std::optional<HeaderValue>>

get_header_all

View source
fastly::expected<HeaderValuesRange> fastly::http::Response::get_header_all(std::string_view name)

Get an iterator of all the values of a header.

Parameters

name: std::string_view

Returns

fastly::expected<HeaderValuesRange>

get_header_names

View source
fastly::expected<HeaderNamesRange> fastly::http::Response::get_header_names()

Returns

fastly::expected<HeaderNamesRange>

get_headers

View source
fastly::expected<HeadersRange> fastly::http::Response::get_headers()

Returns

fastly::expected<HeadersRange>

get_stale_while_revalidate

View source
std::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

std::optional<std::chrono::milliseconds>
std::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

std::optional<std::chrono::milliseconds>

get_version

View source
Version fastly::http::Response::get_version()

Get the HTTP version of this response.

Returns

Version

has_body

View source
bool fastly::http::Response::has_body()

Returns true if this response has a body.

Returns

bool

into_body

View source
Body fastly::http::Response::into_body()

Consume the response and return its body.

Returns

into_body_bytes

View source
std::vector<uint8_t> fastly::http::Response::into_body_bytes()

Consume the response and return its body as a byte vector.

Returns

std::vector<uint8_t>

into_body_string

View source
std::string fastly::http::Response::into_body_string()

Consume the response and return its body as a string.

Returns

std::string

is_from_backend

View source
bool fastly::http::Response::is_from_backend()

Return whether the response is from a backend request.

Returns

bool

redirect

staticView source
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 source
fastly::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

fastly::expected<std::optional<std::string>>

see_other

staticView source
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 source
void 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

void
Request::get("https://example.com/").send("example_backend").send_to_client();
auto
backend_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

View source
void fastly::http::Response::set_body(Body body)

Set the given value as the response's body.

Parameters

body: Body

Returns

void

set_body_octet_stream

View source
void fastly::http::Response::set_body_octet_stream(std::vector<uint8_t> body)

Set the given bytes as the response's body with content type application/octet-stream.

Parameters

body: std::vector<uint8_t>

Returns

void

set_body_text_html

View source
fastly::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

fastly::expected<void>

set_body_text_plain

View source
fastly::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

fastly::expected<void>

set_content_type

View source
void 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

void

set_header

View source
fastly::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

fastly::expected<void>

set_status

View source
void fastly::http::Response::set_status(StatusCode status)

Set the HTTP status code of the response.

Examples

Using the constants from StatusCode:

Using a uint16_t:

Parameters

status: StatusCode

Returns

void
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 source
void fastly::http::Response::set_version(Version version)

Set the HTTP version of this response.

Parameters

version: Version

Returns

void

stream_to_client

View source
StreamingBody fastly::http::Response::stream_to_client()

Begin 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
auto backend_resp{
Request::get("https://example.com/").send("example_backend")
};
// Take the body so we can iterate through its lines later
auto backend_resp_body{backend_resp.take_body()};
// Start sending the backend response to the client with a now-empty body
auto 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 source
std::optional<Request> fastly::http::Response::take_backend_request()

Take 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

std::optional<Request>
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 source
Body fastly::http::Response::take_body()

Take and return the body from this response.

After calling this method, this response will no longer have a body.

Returns

take_body_bytes

View source
std::vector<uint8_t> fastly::http::Response::take_body_bytes()

Take 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

std::vector<uint8_t>

take_body_string

View source
std::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

std::string

temporary_redirect

staticView source
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

View source
Response fastly::http::Response::with_body(Body body) &&

Builder-style equivalent of Response::set_body().

Parameters

body: Body

Returns

with_body_octet_stream

View source
Response fastly::http::Response::with_body_octet_stream(std::vector<uint8_t> body) &&

Builder-style equivalent of Response::set_body_octet_stream().

Parameters

body: std::vector<uint8_t>

Returns

with_body_text_html

View source
fastly::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

fastly::expected<Response>

with_body_text_plain

View source
fastly::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

fastly::expected<Response>

with_content_type

View source
Response 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 source
fastly::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

fastly::expected<Response>

with_set_header

View source
fastly::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

fastly::expected<Response>

with_status

View source
Response fastly::http::Response::with_status(StatusCode status) &&

Builder-style equivalent of Response::set_status().

Parameters

status: StatusCode

Returns

with_version

View source
Response fastly::http::Response::with_version(Version version) &&

Builder-style equivalent of Response::set_version().

Parameters

version: Version

Returns