$darkmode
cloe::Response Class Reference

#include <handler.hpp>

Public Member Functions

 Response ()
 
const std::map< std::string, std::string > & headers () const
 
std::map< std::string, std::string > & headers ()
 
bool has_header (const std::string &key)
 
const std::string & header (const std::string &key)
 
void set_header (const std::string &key, const std::string &value)
 
StatusCode status () const
 
void set_status (StatusCode code)
 
ContentType type () const
 
void set_type (ContentType type)
 
const std::string & body () const
 
void set_body (const std::string &s, ContentType type)
 
void set_body (const fable::Json &js)
 
void write (const fable::Json &js)
 
void bad_request (const fable::Json &js)
 
void not_found (const fable::Json &js)
 
void not_allowed (const RequestMethod &allow, const fable::Json &js)
 
void not_implemented (const fable::Json &js)
 
void server_error (const fable::Json &js)
 
void error (StatusCode code, const fable::Json &js)
 

Detailed Description

A Response is passed to a Handler, which can set its fields.

  • If the status code is not set, then 200 or 204 is set depending on whether any content was written or not.
  • In contrast to a Request, a Response is not inheritable.

Constructor & Destructor Documentation

◆ Response()

cloe::Response::Response ( )
inline

The default response if nothing else is done is the 204 No Content response.

Member Function Documentation

◆ bad_request()

void cloe::Response::bad_request ( const fable::Json js)
inline

Use bad_request when the method is correct, but the body content is not correct.

◆ has_header()

bool cloe::Response::has_header ( const std::string &  key)
inline

Return whether the header map has the given header set.

Here is the call graph for this function:

◆ header()

const std::string& cloe::Response::header ( const std::string &  key)
inline

Return the value of the specified header or throw an exception.

  • Use HasHeader or the Headers map directly if you do not want to handle an exception.
Here is the call graph for this function:

◆ headers()

const std::map<std::string, std::string>& cloe::Response::headers ( ) const
inline

Returns a map of headers set by the server.

Conformity

This is theoretically conform to the HTTP specification. According to HTTP 1.1 Section 4.2:

Multiple message-header fields with the same field-name MAY be present in a message if and only if the entire field-value for that header field is defined as a comma-separated list [i.e., #(values)]. It MUST be possible to combine the multiple header fields into one "field-name: field-value" pair, without changing the semantics of the message, by appending each subsequent field-value to the first, each separated by a comma. The order in which header fields with the same field-name are received is therefore significant to the interpretation of the combined field value, and thus a proxy MUST NOT change the order of these field values when a message is forwarded.

However, for the Set-Cookie header, there are several non-conforming syntaxes, which make use of the comma. Since our server currently does not set cookies with multiple expiration times, this is a non-issue for us.

Here is the caller graph for this function:

◆ not_allowed()

void cloe::Response::not_allowed ( const RequestMethod allow,
const fable::Json js 
)
inline

Use not_allowed when the method (GET, POST, PUT, DELETE) is not allowed.

Specify in allow which method is allowed:

r.not_allowed(RequestMethod::POST, fable::Json{{"error", "try something else"}});
nlohmann::json Json
Definition: fable_fwd.hpp:35

◆ not_found()

void cloe::Response::not_found ( const fable::Json js)
inline

Use not_found when the resource in question is not available.

◆ not_implemented()

void cloe::Response::not_implemented ( const fable::Json js)
inline

Use not_implemented when the functionality represented by the endpoint is not implemented yet.

◆ server_error()

void cloe::Response::server_error ( const fable::Json js)
inline

Use server_error when an internal error occurred, such as a panic.

◆ set_body() [1/2]

void cloe::Response::set_body ( const fable::Json js)
inline

Set the body to the JSON and set the content type.

  • When NDEBUG is set, then the serialization of JSON to string does not pretty print.
Here is the call graph for this function:

◆ set_body() [2/2]

void cloe::Response::set_body ( const std::string &  s,
ContentType  type 
)
inline

Sets the body of the response.

Here is the caller graph for this function:

◆ set_header()

void cloe::Response::set_header ( const std::string &  key,
const std::string &  value 
)
inline

This will set the header with the key to value, overwriting any previous value.

Here is the call graph for this function:

◆ write()

void cloe::Response::write ( const fable::Json js)
inline

Write is an alias for set_body(Json), for historical reasons.

Here is the call graph for this function:

The documentation for this class was generated from the following file: