class request_handler - 5cript/attender GitHub Wiki

class request_handler

The request_handler is for anything request and reading related.

Summary

Members Descriptions
public explicit request_handler(http_connection_interface * connection) noexcept
public ~request_handler()
public request_header get_header() const Returns the request header.
public bool accept_and_continue(std::function< void(boost::system::error_code)> const & continuation) In case of Expect 100 continue headers, write out a continue message if you want to continue and receive the body.
public bool expects_continue() const Returns true if a "Expect: 100-continue" header entry is present.
public callback_wrapper & read_body(std::ostream & stream,size_type max) Reads tcp-stream contents to the provided sink (in this case an ostream).
public callback_wrapper & read_body(std::string & str,size_type max) Reads tcp-stream contents to the provided sink (in this case a string).
public callback_wrapper& read_body(std::shared_ptr<http_read_sink > sink,size_type max) Reads tcp-stream contents to the provided sink (in this case a customary sink).
public size_type get_read_amount() const Returns the amount of total bytes read in the last read call that was issued.
public std::string hostname() const Contains the hostname derived from the Host HTTP header.
public std::string ip() const Contains the remote IP address of the request.
public unsigned short port() const Contains the remote port address of the request.
public std::string method() const Returns a string corresponding to the HTTP method of the request: GET, POST, PUT, and so on.
public std::string url() const Returns the original request url.
public std::string ipv6Address() const Returns ip and port in ipv6 format with port.
public std::string param(std::string const & key) const Returns parsed path parameters by key.
public std::string path() const Contains the path part of the request URL.
public std::string protocol() const Returns the underlying protocol.
public boost::optional< std::string > query(std::string const & key) const Retrieves a query value for a given key.
public bool secure() const Will return whether this is an encrypted connection or not.
public boost::optional< std::string > get_header_field(std::string const & key) const Returns a header field from the request header.
public boost::optional< std::string > get_cookie_value(std::string const & name) const Returns a cookie field from the request header.

Members


public explicit request_handler(http_connection_interface * connection) noexcept


public ~request_handler()


public request_header get_header() const

Returns the request header.

Returns: A http request header.


public bool accept_and_continue(std::function< void(boost::system::error_code)> const & continuation)

In case of Expect 100 continue headers, write out a continue message if you want to continue and receive the body.

Parameters

  • the function called after the write completes. Dont directly continue or you cannot technically be sure that, the whole write completed before you continue using the connection.

Returns: returns false if no expect-continue header is present. CONTINUATION IS NOT CALLED.


public bool expects_continue() const

Returns true if a "Expect: 100-continue" header entry is present.

Also use this if YOU (as server) want a 100 expect routine. Body reading is at your disposal after all and you can chose to outright close and fail if the client bombards you.


public callback_wrapper & read_body(std::ostream & stream,size_type max)

Reads tcp-stream contents to the provided sink (in this case an ostream).

This stream must be kept alive until the read operation finishes and fullfill or except is called.

Do not start multiple read operations at the same time! This will crash you!

Parameters

  • stream A stream to write to. This stream must survive until fullfill or except.

  • max The maximum amount of bytes to read. If max = 0, there is no limit.


public callback_wrapper & read_body(std::string & str,size_type max)

Reads tcp-stream contents to the provided sink (in this case a string).

This stream must be kept alive until the read operation finishes and fullfill or except is called.

Do not start multiple read operations at the same time! This will crash you!

Parameters

  • str A string to write to. This stream must survive until fullfill or except.

  • max The maximum amount of bytes to read. If max = 0, there is no limit.


public callback_wrapper& read_body(std::shared_ptr<http_read_sink > sink,size_type max)

Reads tcp-stream contents to the provided sink (in this case a customary sink).

Do not start multiple read operations at the same time! This will crash you!

Parameters

  • sink A customary sink. The request will share the shared_ptr with you.

  • max The maximum amount of bytes to read. If max = 0, there is no limit.


public size_type get_read_amount() const

Returns the amount of total bytes read in the last read call that was issued.


public std::string hostname() const

Contains the hostname derived from the Host HTTP header.

When the trust proxy setting does not evaluate to false, this property will instead have the value of the X-Forwarded-Host header field. This header can be set by the client or by the proxy.

Returns: Returns host name from header.


public std::string ip() const

Contains the remote IP address of the request.

This ip is taken from the system.

Returns: remote ip.


public unsigned short port() const

Contains the remote port address of the request.

This port is taken from the system.

Returns: remote port.


public std::string method() const

Returns a string corresponding to the HTTP method of the request: GET, POST, PUT, and so on.

Returns: request method / verb.


public std::string url() const

Returns the original request url.

Returns: request url.


public std::string ipv6Address() const

Returns ip and port in ipv6 format with port.

Returns: An address in this format: [::1]:1234


public std::string param(std::string const & key) const

Returns parsed path parameters by key.

e.g.: /api/:param1/:param2, :param1 and :param2 are the parameters.

Parameters

  • key A path parameter key. Leading colon can be omitted.

Returns: Returns the path part that contains the key.


public std::string path() const

Contains the path part of the request URL.

Returns: Returns a path, such as /api/path


public std::string protocol() const

Returns the underlying protocol.

Will allways be 'http', unless TLS is used, in which case it will be 'https'


public boost::optional< std::string > query(std::string const & key) const

Retrieves a query value for a given key.


public bool secure() const

Will return whether this is an encrypted connection or not.


public boost::optional< std::string > get_header_field(std::string const & key) const

Returns a header field from the request header.

e.g.: get_header_field("Host") -> "localhost".

Parameters

  • key The header fields key.

Returns: The Header fields value or boost::none.


public boost::optional< std::string > get_cookie_value(std::string const & name) const

Returns a cookie field from the request header.

Parameters

  • name The cookie name.

Returns: The cookie value or boost::none.