D++ (DPP)
C++ Discord API Bot Library
dpp::http_server_request Class Reference

#include <http_server_request.h>

+ Inheritance diagram for dpp::http_server_request:
+ Collaboration diagram for dpp::http_server_request:

Public Member Functions

http_state get_state () const
 Get request state. More...
 
std::string get_response_body () const
 Get current response body. More...
 
std::string get_request_body () const
 Get current request body. More...
 
uint16_t get_status () const
 Get current status code. More...
 
 http_server_request (cluster *creator, socket fd, uint16_t port, bool plaintext_downgrade, const std::string &private_key, const std::string &public_key, http_server_request_event handle_request)
 Construct a new server request object. Server request objects are instantiated for an incoming server connection, as such they already have a file descriptor. More...
 
virtual ~http_server_request () override
 Destroy the https client object. More...
 
virtual bool handle_buffer (std::string &buffer) override
 Processes incoming data from the SSL socket input buffer. More...
 
virtual void close () override
 Close HTTPS socket. More...
 
virtual void one_second_timer () override
 Fires every second from the underlying socket I/O loop, used for timeouts. More...
 
const std::string get_header (const std::string &header_name) const
 Get a HTTP request header. More...
 
size_t get_header_count (const std::string &header_name) const
 Get the number of headers with the same header name. More...
 
std::list< std::string > get_header_list (const std::string &header_name) const
 Get a set of HTTP request headers with a common name. More...
 
std::multimap< std::string, std::string > get_headers () const
 Get all HTTP request headers. More...
 
http_server_requestset_response_header (const std::string &header, const std::string &value)
 Set a response header. More...
 
http_server_requestset_response_body (const std::string &new_content)
 Set the response content. More...
 
http_server_requestset_status (uint16_t new_status)
 Set the response HTTP status, e.g. 200 for OK, 404 for not found, 429 for rate limited etc. More...
 
std::string get_response ()
 Get whole response as a string. More...
 
void enable_raw_tracing ()
 For low-level debugging, calling this function will enable low level I/O logging for this connection to the logger. This can be very loud, and output a lot of data, so only enable it selectively where you need it. More...
 
uint64_t get_bytes_out ()
 Get the bytes out objectGet total bytes sent. More...
 
uint64_t get_bytes_in ()
 Get total bytes received. More...
 
uint64_t get_unique_id () const
 Every request made has a unique ID. This increments for every request, starting at 1. You can use this for statistics, or to associate requests and replies in external event loops. More...
 
std::string get_cipher ()
 Get SSL cipher name. More...
 
void read_loop ()
 Set up non blocking I/O and configure on_read, on_write and on_error. More...
 
void socket_write (const std::string_view data)
 Write to the output buffer. More...
 
virtual void log (dpp::loglevel severity, const std::string &msg) const
 Log a message. More...
 
void complete_handshake (const struct socket_events *ev)
 Called while SSL handshake is in progress. If the handshake completes, the state of the socket is progressed to an established state. More...
 
void on_read (dpp::socket fd, const struct dpp::socket_events &ev)
 Called when the TCP socket has data to read. More...
 
void on_write (dpp::socket fd, const struct dpp::socket_events &e)
 Called when the TCP socket can be written to without blocking. More...
 
void on_error (dpp::socket fd, const struct dpp::socket_events &, int error_code)
 Called when there is an error on the TCP socket. More...
 

Public Attributes

bool timed_out
 If true the response timed out while waiting. More...
 
uint64_t content_length {ULLONG_MAX}
 Content length sent by client. More...
 
bool keepalive
 True if we are keeping the connection alive after it has finished. More...
 
class clusterowner
 Owning cluster. More...
 
std::string private_key_file
 Private key PEM file path for inbound SSL connections. More...
 
std::string public_key_file
 Public key PEM file path for inbound SSL connections. More...
 

Protected Member Functions

virtual void connect () override
 Start the connection. More...
 
void on_buffer_drained () override
 Called when the output buffer is drained to empty. More...
 
uint64_t get_max_post_size () const
 Maximum size of POST body. More...
 
uint64_t get_max_header_size () const
 Maximum size of headers. More...
 
void generate_error (uint16_t error_code, const std::string &message)
 Reply with an error message. More...
 
void do_raw_trace (const std::string &message) const
 If raw_trace is set to true, log a debug message for this connection. More...
 

Protected Attributes

http_state state
 Current connection state. More...
 
uint16_t status {0}
 HTTP status code for response. More...
 
std::string buffer
 Input buffer received from socket. More...
 
std::string obuffer
 Output buffer for sending to socket. More...
 
dpp::socket sfd
 Raw file descriptor of connection. More...
 
openssl_connection * ssl
 Openssl opaque contexts. More...
 
std::string cipher
 SSL cipher in use. More...
 
time_t last_tick
 For timers. More...
 
time_t start
 Start time of connection. More...
 
uint8_t connect_retries {0}
 How many times we retried connect() More...
 
std::string hostname
 Hostname connected to. More...
 
std::string port
 Port connected to. More...
 
uint64_t bytes_out
 Bytes out. More...
 
uint64_t bytes_in
 Bytes in. More...
 
bool plaintext
 True for a plain text connection. More...
 
bool connected {false}
 True if connection is completed. More...
 
bool tcp_connect_done {false}
 True if tcp connect() succeeded. More...
 
timer timer_handle
 Timer handle for one second timer. More...
 
uint64_t unique_id
 Unique ID of socket used as a nonce You can use this to identify requests vs reply if you want. D++ itself only sets this, and does not use it in any logic. It starts at 1 and increments for each request made. More...
 
bool raw_trace {false}
 Set this to true to log all IO to debug for this connection. This is an internal developer facility. Do not enable it unless you need to, as it will be very noisy. More...
 

Constructor & Destructor Documentation

◆ http_server_request()

dpp::http_server_request::http_server_request ( cluster creator,
socket  fd,
uint16_t  port,
bool  plaintext_downgrade,
const std::string &  private_key,
const std::string &  public_key,
http_server_request_event  handle_request 
)

Construct a new server request object. Server request objects are instantiated for an incoming server connection, as such they already have a file descriptor.

Parameters
creatorcreating owner
fdfile descriptor
portPort the connection came in on
plaintext_downgradetrue if plaintext, false if SSL
private_keyif SSL, the path to the private key PEM
public_keyif SSL, the path to the public key PEM
handle_requestrequest handler callback

◆ ~http_server_request()

virtual dpp::http_server_request::~http_server_request ( )
overridevirtual

Destroy the https client object.

Member Function Documentation

◆ close()

virtual void dpp::http_server_request::close ( )
overridevirtual

Close HTTPS socket.

Reimplemented from dpp::ssl_connection.

◆ complete_handshake()

void dpp::ssl_connection::complete_handshake ( const struct socket_events ev)
inherited

Called while SSL handshake is in progress. If the handshake completes, the state of the socket is progressed to an established state.

Parameters
evSocket events for the socket

◆ connect()

virtual void dpp::http_server_request::connect ( )
overrideprotectedvirtual

Start the connection.

Reimplemented from dpp::ssl_connection.

◆ do_raw_trace()

void dpp::ssl_connection::do_raw_trace ( const std::string &  message) const
protectedinherited

If raw_trace is set to true, log a debug message for this connection.

Parameters
messagedebug message

◆ enable_raw_tracing()

void dpp::ssl_connection::enable_raw_tracing ( )
inherited

For low-level debugging, calling this function will enable low level I/O logging for this connection to the logger. This can be very loud, and output a lot of data, so only enable it selectively where you need it.

Generally, you won't need this, it is a library development utility.

◆ generate_error()

void dpp::http_server_request::generate_error ( uint16_t  error_code,
const std::string &  message 
)
protected

Reply with an error message.

Parameters
error_codeerror code
messagemessage

◆ get_bytes_in()

uint64_t dpp::ssl_connection::get_bytes_in ( )
inherited

Get total bytes received.

Returns
uint64_t bytes received

◆ get_bytes_out()

uint64_t dpp::ssl_connection::get_bytes_out ( )
inherited

Get the bytes out objectGet total bytes sent.

Returns
uint64_t bytes sent

◆ get_cipher()

std::string dpp::ssl_connection::get_cipher ( )
inherited

Get SSL cipher name.

Returns
std::string ssl cipher name

◆ get_header()

const std::string dpp::http_server_request::get_header ( const std::string &  header_name) const

Get a HTTP request header.

Parameters
header_nameHeader name to find, case insensitive
Returns
Header content or empty string if not found. If multiple values have the same header_name, this will return one of them.
See also
get_header_count to determine if multiple are present
get_header_list to retrieve all entries of the same header_name

◆ get_header_count()

size_t dpp::http_server_request::get_header_count ( const std::string &  header_name) const

Get the number of headers with the same header name.

Parameters
header_name
Returns
the number of headers with this count

◆ get_header_list()

std::list< std::string > dpp::http_server_request::get_header_list ( const std::string &  header_name) const

Get a set of HTTP request headers with a common name.

Parameters
header_name
Returns
A list of headers with the same name, or an empty list if not found

◆ get_headers()

std::multimap< std::string, std::string > dpp::http_server_request::get_headers ( ) const

Get all HTTP request headers.

Returns
headers as a map

◆ get_max_header_size()

uint64_t dpp::http_server_request::get_max_header_size ( ) const
protected

Maximum size of headers.

◆ get_max_post_size()

uint64_t dpp::http_server_request::get_max_post_size ( ) const
protected

Maximum size of POST body.

◆ get_request_body()

std::string dpp::http_server_request::get_request_body ( ) const

Get current request body.

Returns
request body

◆ get_response()

std::string dpp::http_server_request::get_response ( )

Get whole response as a string.

◆ get_response_body()

std::string dpp::http_server_request::get_response_body ( ) const

Get current response body.

Returns
response body

◆ get_state()

http_state dpp::http_server_request::get_state ( ) const

Get request state.

Returns
request state

◆ get_status()

uint16_t dpp::http_server_request::get_status ( ) const

Get current status code.

Returns
status code

◆ get_unique_id()

uint64_t dpp::ssl_connection::get_unique_id ( ) const
inherited

Every request made has a unique ID. This increments for every request, starting at 1. You can use this for statistics, or to associate requests and replies in external event loops.

Returns
Unique ID

◆ handle_buffer()

virtual bool dpp::http_server_request::handle_buffer ( std::string &  buffer)
overridevirtual

Processes incoming data from the SSL socket input buffer.

Parameters
bufferThe buffer contents. Can modify this value removing the head elements when processed.

Reimplemented from dpp::ssl_connection.

◆ log()

virtual void dpp::ssl_connection::log ( dpp::loglevel  severity,
const std::string &  msg 
) const
virtualinherited

Log a message.

Parameters
severityseverity of log message
msgLog message to send

Reimplemented in dpp::discord_client, and dpp::discord_voice_client.

◆ on_buffer_drained()

void dpp::http_server_request::on_buffer_drained ( )
overrideprotectedvirtual

Called when the output buffer is drained to empty.

Reimplemented from dpp::ssl_connection.

◆ on_error()

void dpp::ssl_connection::on_error ( dpp::socket  fd,
const struct dpp::socket_events ,
int  error_code 
)
inherited

Called when there is an error on the TCP socket.

Parameters
fdFile descriptor
error_codeError code

◆ on_read()

void dpp::ssl_connection::on_read ( dpp::socket  fd,
const struct dpp::socket_events ev 
)
inherited

Called when the TCP socket has data to read.

Parameters
fdFile descriptor
evSocket events

◆ on_write()

void dpp::ssl_connection::on_write ( dpp::socket  fd,
const struct dpp::socket_events e 
)
inherited

Called when the TCP socket can be written to without blocking.

Parameters
fdFile descriptor
eSocket events

◆ one_second_timer()

virtual void dpp::http_server_request::one_second_timer ( )
overridevirtual

Fires every second from the underlying socket I/O loop, used for timeouts.

Reimplemented from dpp::ssl_connection.

◆ read_loop()

void dpp::ssl_connection::read_loop ( )
inherited

Set up non blocking I/O and configure on_read, on_write and on_error.

Exceptions
std::exceptionAny std::exception (or derivative) thrown from read_loop() indicates setup failed

◆ set_response_body()

http_server_request & dpp::http_server_request::set_response_body ( const std::string &  new_content)

Set the response content.

Parameters
new_contentresponse content

◆ set_response_header()

http_server_request & dpp::http_server_request::set_response_header ( const std::string &  header,
const std::string &  value 
)

Set a response header.

Parameters
headerheader name
valueheader value
Returns
ref to self

◆ set_status()

http_server_request & dpp::http_server_request::set_status ( uint16_t  new_status)

Set the response HTTP status, e.g. 200 for OK, 404 for not found, 429 for rate limited etc.

Parameters
new_statusHTTP status

◆ socket_write()

void dpp::ssl_connection::socket_write ( const std::string_view  data)
inherited

Write to the output buffer.

Parameters
dataData to be written to the buffer.
Note
The data may not be written immediately and may be written at a later time to the socket.

Member Data Documentation

◆ buffer

std::string dpp::ssl_connection::buffer
protectedinherited

Input buffer received from socket.

◆ bytes_in

uint64_t dpp::ssl_connection::bytes_in
protectedinherited

Bytes in.

◆ bytes_out

uint64_t dpp::ssl_connection::bytes_out
protectedinherited

Bytes out.

◆ cipher

std::string dpp::ssl_connection::cipher
protectedinherited

SSL cipher in use.

◆ connect_retries

uint8_t dpp::ssl_connection::connect_retries {0}
protectedinherited

How many times we retried connect()

◆ connected

bool dpp::ssl_connection::connected {false}
protectedinherited

True if connection is completed.

◆ content_length

uint64_t dpp::http_server_request::content_length {ULLONG_MAX}

Content length sent by client.

◆ hostname

std::string dpp::ssl_connection::hostname
protectedinherited

Hostname connected to.

◆ keepalive

bool dpp::ssl_connection::keepalive
inherited

True if we are keeping the connection alive after it has finished.

◆ last_tick

time_t dpp::ssl_connection::last_tick
protectedinherited

For timers.

◆ obuffer

std::string dpp::ssl_connection::obuffer
protectedinherited

Output buffer for sending to socket.

◆ owner

class cluster* dpp::ssl_connection::owner
inherited

Owning cluster.

◆ plaintext

bool dpp::ssl_connection::plaintext
protectedinherited

True for a plain text connection.

◆ port

std::string dpp::ssl_connection::port
protectedinherited

Port connected to.

◆ private_key_file

std::string dpp::ssl_connection::private_key_file
inherited

Private key PEM file path for inbound SSL connections.

◆ public_key_file

std::string dpp::ssl_connection::public_key_file
inherited

Public key PEM file path for inbound SSL connections.

◆ raw_trace

bool dpp::ssl_connection::raw_trace {false}
protectedinherited

Set this to true to log all IO to debug for this connection. This is an internal developer facility. Do not enable it unless you need to, as it will be very noisy.

◆ sfd

dpp::socket dpp::ssl_connection::sfd
protectedinherited

Raw file descriptor of connection.

◆ ssl

openssl_connection* dpp::ssl_connection::ssl
protectedinherited

Openssl opaque contexts.

◆ start

time_t dpp::ssl_connection::start
protectedinherited

Start time of connection.

◆ state

http_state dpp::http_server_request::state
protected

Current connection state.

◆ status

uint16_t dpp::http_server_request::status {0}
protected

HTTP status code for response.

◆ tcp_connect_done

bool dpp::ssl_connection::tcp_connect_done {false}
protectedinherited

True if tcp connect() succeeded.

◆ timed_out

bool dpp::http_server_request::timed_out

If true the response timed out while waiting.

◆ timer_handle

timer dpp::ssl_connection::timer_handle
protectedinherited

Timer handle for one second timer.

◆ unique_id

uint64_t dpp::ssl_connection::unique_id
protectedinherited

Unique ID of socket used as a nonce You can use this to identify requests vs reply if you want. D++ itself only sets this, and does not use it in any logic. It starts at 1 and increments for each request made.

D++ Library version 10.1.1D++ Library version 10.1.0D++ Library version 10.0.35D++ Library version 10.0.34D++ Library version 10.0.33D++ Library version 10.0.32D++ Library version 10.0.31D++ Library version 10.0.30D++ Library version 10.0.29D++ Library version 10.0.28D++ Library version 10.0.27D++ Library version 10.0.26D++ Library version 10.0.25D++ Library version 10.0.24D++ Library version 10.0.23D++ Library version 10.0.22D++ Library version 10.0.21D++ Library version 10.0.20D++ Library version 10.0.19D++ Library version 10.0.18D++ Library version 10.0.17D++ Library version 10.0.16D++ Library version 10.0.15D++ Library version 10.0.14D++ Library version 10.0.13D++ Library version 10.0.12D++ Library version 10.0.11D++ Library version 10.0.10D++ Library version 10.0.9D++ Library version 10.0.8D++ Library version 10.0.7D++ Library version 10.0.6D++ Library version 10.0.5D++ Library version 10.0.4D++ Library version 10.0.3D++ Library version 10.0.2D++ Library version 10.0.1D++ Library version 10.0.0D++ Library version 9.0.19D++ Library version 9.0.18D++ Library version 9.0.17D++ Library version 9.0.16D++ Library version 9.0.15D++ Library version 9.0.14D++ Library version 9.0.13D++ Library version 9.0.12D++ Library version 9.0.11D++ Library version 9.0.10D++ Library version 9.0.9D++ Library version 9.0.8D++ Library version 9.0.7D++ Library version 9.0.6D++ Library version 9.0.5D++ Library version 9.0.4D++ Library version 9.0.3D++ Library version 9.0.2D++ Library version 9.0.1D++ Library version 9.0.0D++ Library version 1.0.2D++ Library version 1.0.1D++ Library version 1.0.0