class response_handler - 5cript/attender GitHub Wiki

class response_handler

The response_handler is for everything writing and sending related.

Summary

Members Descriptions
public explicit response_handler(http_connection_interface * connection) noexcept
public ~response_handler()
public std::shared_ptr< conclusion_observer > observe_conclusion() Creates an object that can be used to observe the lifetime of the object.
public response_handler & append(std::string const & field,std::string const & value) Appends the specified value to the HTTP response header field.
public response_handler & set(std::string const & field,std::string const & value) Sets the response�s HTTP header field to value.
public response_handler & status(int code) Sets the response code.
public response_handler & type(std::string const & mime,bool no_except) Performs a mime type lookup and sets the Content-Type to the appropriate value.
public void send(std::string const & body) Sends the HTTP response.
public void send(std::vector< char > const & body) Sends the HTTP response.
public void send(std::istream & body,std::function< void()> const & on_finish) Sends the HTTP response.
public void send_chunked(producer & prod,std::function< void(boost::system::error_code)> const & on_finish) Sends the HTTP response.
public bool send_file(std::string const & fileName) Sends the HTTP response.
public void send_status(int code) This function will set the status and send the status message a string in the body.
public response_handler & location(std::string const & where) Sets the location http header value to the specified path value.
public response_handler & links(std::map< std::string, std::string > const & links) Sets the HTTP Link response header entry.
public response_handler & redirect(std::string const & where,int code) Performs a redirect to a different url or path.
public void end() Ends the response process.
public http_connection_interface * get_connection() Do NOT use this function! Returns a handle to the underlying connection.
public void send_header(write_callback continuation) Do NOT use this function! Will send the header and set the "headerSent_" flag.
public void set_cookie(cookie ck) Will set a cookie.
public bool has_concluded() const Will return true if a header has already been sent.

Members


public explicit response_handler(http_connection_interface * connection) noexcept


public ~response_handler()


public std::shared_ptr< conclusion_observer > observe_conclusion()

Creates an object that can be used to observe the lifetime of the object.


public response_handler & append(std::string const & field,std::string const & value)

Appends the specified value to the HTTP response header field.

If the header is not already set, it creates the header with the specified value.

Parameters

  • field A header field identifier, such as "Warning" or "Link"

  • value A value that is to be appended.

Returns: *this for chaining.


public response_handler & set(std::string const & field,std::string const & value)

Sets the response�s HTTP header field to value.

Parameters

  • field A header field identifier, such as "Warning" or "Link"

  • value A value that is to be appended.

Returns: *this for chaining.


public response_handler & status(int code)

Sets the response code.

Parameters

  • code A response code 1xx, 2xx, 3xx, 4xx or 5xx

Returns: *this for chaining.


public response_handler & type(std::string const & mime,bool no_except)

Performs a mime type lookup and sets the Content-Type to the appropriate value.

If the string contains a slash, it will just take the parameter "mime" as the type. If the string starts with a dot, it will try to interpret it as a file extension. Otherwise, it will try to find it in a list. This function will throw if no mime type could be matched. Please do note, that the convenience for writing "png" instead of "image/png" results in a much higher computational cost.

Parameters

  • mime A type or a file extension. Examples: .html -> text/html html -> text/html png -> image/png application/json -> application/json json -> application/json

  • no_except Does not throw on failure, but instead fails silently by doing nothing.

Returns: *this for chaining.


public void send(std::string const & body)

Sends the HTTP response.

After a call to send, the status and header fields can no longer be changed as they will be sent with this function. As this function completes the response, chaining will no longer be possible.

Content-Length will automatically be set, if not previously defined. Content-Type will be set to "text/plain" for this overload.

Parameters

  • body A body to send.

public void send(std::vector< char > const & body)

Sends the HTTP response.

After a call to send, the status and header fields can no longer be changed as they will be sent with this function. As this function completes the response, chaining will no longer be possible.

Content-Length will automatically be set, if not previously defined. Content-Type will be set to "application/octet-stream" for this overload.

Parameters

  • body A body to send.

public void send(std::istream & body,std::function< void()> const & on_finish)

Sends the HTTP response.

After a call to send, the status and header fields can no longer be changed as they will be sent with this function. As this function completes the response, chaining will no longer be possible. THE STREAM MUST BE SEEKABLE (some boost iostreams do not). THE STREAM MUST SURVIVE FOR THE ENTIRE CONNECTION.

Content-Length will automatically be set, if not previously defined.

Parameters

  • body A body to send.

  • on_finish A callback function, that is called after the send operation finished.


public void send_chunked(producer & prod,std::function< void(boost::system::error_code)> const & on_finish)

Sends the HTTP response.

After a call to send, the status and header fiels can no longer be changed as they will be sent with this function. As this function completes the response, chaining will no longer be possible.

Content-Length will automatically be set, if not previously defined. Will force set Transfer-Encoding to chunked.

Parameters

  • body A body to send.

  • on_finish A callback function that is called after the send operation finished. This function CANNOT determine the prod.complete(), because its only called when the production already completed.


public bool send_file(std::string const & fileName)

Sends the HTTP response.

After a call to send, the status and header fields can no longer be changed as they will be sent with this function. As this function completes the response, chaining will no longer be possible. The stream must provide seek and tell (some boost iostreams do not).

Content-Length will automatically be set, if not previously defined. Content-Type will be deduced from the filename if possible, "application/octet-stream" otherwise.

Parameters

  • fileName A file to open in binary read mode and send.

Returns: Returns false if the file could not be opened. The connection will not be closed and nothing will be sent.


public void send_status(int code)

This function will set the status and send the status message a string in the body.

The code must be supported / known to attender.

Content-Length will automatically be set, if not previously defined. Content-Type will be set to "text" for this overload.

Parameters

  • code A response code 1xx, 2xx, 3xx, 4xx or 5xx

public response_handler & location(std::string const & where)

Sets the location http header value to the specified path value.

Parameters

  • where The location to be set in the location header.

public response_handler & links(std::map< std::string, std::string > const & links)

Sets the HTTP Link response header entry.

Parameters

  • links a map that maps rel (see documentation of Link header entry) to an url.

public response_handler & redirect(std::string const & where,int code)

Performs a redirect to a different url or path.

This only sets the code and "Location" header field. This still has to be completed with an end statement (send, end)

Parameters

  • where Where to redirect to.

  • code A response code to send. Defaults to 302.


public void end()

Ends the response process.

Use to quickly end the response without any data. If you need to respond with data, instead use methods such as send and json. As this function completes the response, chaining will no longer be possible.

Do not call this function or any for that matter, after an exception was thrown or the request got finalized by a previous end call. The call to end invalidates all functions on request_handler, response_handler and the http_connection itself.


public http_connection_interface * get_connection()

Do NOT use this function! Returns a handle to the underlying connection.


public void send_header(write_callback continuation)

Do NOT use this function! Will send the header and set the "headerSent_" flag.


public void set_cookie(cookie ck)

Will set a cookie.


public bool has_concluded() const

Will return true if a header has already been sent.

⚠️ **GitHub.com Fallback** ⚠️