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

Implements a HTTPS socket client based on the SSL client. More...

#include "httpsclient.h"

Inheritance diagram for dpp::https_client:
Collaboration diagram for dpp::https_client:

Public Member Functions

 https_client (const std::string &hostname, uint16_t port=443, const std::string &urlpath="/", const std::string &verb="GET", const std::string &req_body="", const http_headers &extra_headers={}, bool plaintext_connection=false, uint16_t request_timeout=5, const std::string &protocol="1.1")
 Connect to a specific HTTP(S) server and complete a request. More...
 
virtual ~https_client ()=default
 Destroy the https client object. More...
 
virtual void close ()
 Close HTTPS socket. More...
 
uint64_t get_bytes_in ()
 Get total bytes received. More...
 
uint64_t get_bytes_out ()
 Get the bytes out objectGet total bytes sent. More...
 
std::string get_cipher ()
 Get SSL cipher name. More...
 
const std::string get_content () const
 Get the response content. More...
 
const std::string get_header (std::string header_name) const
 Get a HTTP response header. More...
 
size_t get_header_count (std::string header_name) const
 Get the number of headers with the same header name. More...
 
const std::list< std::string > get_header_list (std::string header_name) const
 Get a set of HTTP response headers with a common name. More...
 
const std::multimap< std::string, std::string > get_headers () const
 Get all HTTP response headers. More...
 
uint16_t get_status () const
 Get the response HTTP status, e.g. More...
 
virtual bool handle_buffer (std::string &buffer)
 Processes incoming data from the SSL socket input buffer. More...
 
virtual void log (dpp::loglevel severity, const std::string &msg) const
 Log a message. More...
 
virtual void one_second_timer ()
 Fires every second from the underlying socket I/O loop, used for timeouts. More...
 
void read_loop ()
 Nonblocking I/O loop. More...
 
virtual void write (const std::string &data)
 Write to the output buffer. More...
 

Static Public Member Functions

static multipart_content build_multipart (const std::string &json, const std::vector< std::string > &filenames={}, const std::vector< std::string > &contents={}, const std::vector< std::string > &mimetypes={})
 Build a multipart content from a set of files and some json. More...
 
static http_connect_info get_host_info (std::string url)
 Break down a scheme, hostname and port into a http_connect_info. More...
 

Public Attributes

socket_callback_t custom_readable_fd
 Attaching an additional file descriptor to this function will send notifications when there is data to read. More...
 
socket_notification_t custom_readable_ready
 This event will be called when you can read from the custom fd. More...
 
socket_callback_t custom_writeable_fd
 Attaching an additional file descriptor to this function will send notifications when you are able to write to the socket. More...
 
socket_notification_t custom_writeable_ready
 This event will be called when you can write to a custom fd. More...
 
bool keepalive
 True if we are keeping the connection alive after it has finished. More...
 

Protected Member Functions

virtual void connect ()
 Start the connection. More...
 
http_state get_state ()
 Get request state. More...
 

Protected Attributes

std::string buffer
 Input buffer received from socket. More...
 
uint64_t bytes_in
 Bytes in. More...
 
uint64_t bytes_out
 Bytes out. More...
 
std::string cipher
 SSL cipher in use. More...
 
std::string hostname
 Hostname connected to. More...
 
time_t last_tick
 For timers. More...
 
bool make_new
 True if we are establishing a new connection, false if otherwise. More...
 
bool nonblocking
 True if in nonblocking mode. More...
 
std::string obuffer
 Output buffer for sending to socket. More...
 
bool plaintext
 True for a plain text connection. More...
 
std::string port
 Port connected to. More...
 
dpp::socket sfd
 Raw file descriptor of connection. More...
 
openssl_connectionssl
 Openssl opaque contexts. More...
 

Detailed Description

Implements a HTTPS socket client based on the SSL client.

Note
plaintext HTTP without SSL is also supported via a "downgrade" setting

Constructor & Destructor Documentation

◆ https_client()

dpp::https_client::https_client ( const std::string &  hostname,
uint16_t  port = 443,
const std::string &  urlpath = "/",
const std::string &  verb = "GET",
const std::string &  req_body = "",
const http_headers extra_headers = {},
bool  plaintext_connection = false,
uint16_t  request_timeout = 5,
const std::string &  protocol = "1.1" 
)

Connect to a specific HTTP(S) server and complete a request.

The constructor will attempt the connection, and return the content. By the time the constructor completes, the HTTP request will be stored in the object.

Note
This is a blocking call. It starts a loop which runs non-blocking functions within it, but does not return until the request completes. See queues.cpp for how to make this asynchronous.
Parameters
hostnameHostname to connect to
portPort number to connect to, usually 443 for SSL and 80 for plaintext
urlpathpath part of URL, e.g. "/api"
verbRequest verb, e.g. GET or POST
req_bodyRequest body, use dpp::https_client::build_multipart() to build a multipart MIME body (e.g. for multiple file upload)
extra_headersAdditional request headers, e.g. user-agent, authorization, etc
plaintext_connectionSet to true to make the connection plaintext (turns off SSL)
request_timeoutHow many seconds before the connection is considered failed if not finished
protocolRequest HTTP protocol (default: 1.1)

References dpp::unicode_emoji::v.

◆ ~https_client()

virtual dpp::https_client::~https_client ( )
virtualdefault

Destroy the https client object.

Member Function Documentation

◆ build_multipart()

multipart_content dpp::https_client::build_multipart ( const std::string &  json,
const std::vector< std::string > &  filenames = {},
const std::vector< std::string > &  contents = {},
const std::vector< std::string > &  mimetypes = {} 
)
static

Build a multipart content from a set of files and some json.

Parameters
jsonThe json content
filenamesFile names of files to send
contentsContents of each of the files to send
mimetypesMIME types of each of the files to send
Returns
multipart mime content and headers

Referenced by main().

Here is the caller graph for this function:

◆ close()

void dpp::https_client::close ( )
virtual

Close HTTPS socket.

Reimplemented from dpp::ssl_client.

◆ connect()

void dpp::https_client::connect ( )
protectedvirtual

Start the connection.

Reimplemented from dpp::ssl_client.

◆ get_bytes_in()

uint64_t dpp::ssl_client::get_bytes_in ( )
inherited

Get total bytes received.

Returns
uint64_t bytes received

References dpp::ssl_client::bytes_in.

◆ get_bytes_out()

uint64_t dpp::ssl_client::get_bytes_out ( )
inherited

Get the bytes out objectGet total bytes sent.

Returns
uint64_t bytes sent

References dpp::ssl_client::bytes_out.

◆ get_cipher()

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

Get SSL cipher name.

Returns
std::string ssl cipher name

References dpp::ssl_client::cipher.

◆ get_content()

const std::string dpp::https_client::get_content ( ) const

Get the response content.

Returns
response content

Referenced by dpp::http_request::http_request().

Here is the caller graph for this function:

◆ get_header()

const std::string dpp::https_client::get_header ( std::string  header_name) const

Get a HTTP response 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

Referenced by dpp::http_request::complete(), and main().

Here is the caller graph for this function:

◆ get_header_count()

size_t dpp::https_client::get_header_count ( 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()

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

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

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

References dpp::HTTPS_HEADERS.

◆ get_headers()

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

Get all HTTP response headers.

Returns
headers as a map

Referenced by dpp::http_request::http_request().

Here is the caller graph for this function:

◆ get_host_info()

http_connect_info dpp::https_client::get_host_info ( std::string  url)
static

Break down a scheme, hostname and port into a http_connect_info.

All but the hostname portion are optional. The path component should not be passed to this function.

Parameters
urlURL to break down
Returns
Split URL

Referenced by main().

Here is the caller graph for this function:

◆ get_state()

http_state dpp::https_client::get_state ( )
protected

Get request state.

Returns
request state

References dpp::http_connect_info::is_ssl, dpp::http_connect_info::port, and dpp::http_connect_info::scheme.

◆ get_status()

uint16_t dpp::https_client::get_status ( ) const

Get the response HTTP status, e.g.

200 for OK, 404 for not found, 429 for rate limited. A value of 0 indicates the request was not completed.

Returns
uint16_t HTTP status

References dpp::ssl_client::close(), and dpp::HTTPS_DONE.

Referenced by dpp::http_request::http_request().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ handle_buffer()

bool dpp::https_client::handle_buffer ( std::string &  buffer)
virtual

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_client.

References dpp::HTTPS_CHUNK_LEN, dpp::HTTPS_CONTENT, dpp::unicode_emoji::key, and dpp::utility::tokenize().

Here is the call graph for this function:

◆ log()

void dpp::ssl_client::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_voice_client, and dpp::discord_client.

Referenced by dpp::ssl_client::read_loop().

Here is the caller graph for this function:

◆ one_second_timer()

void dpp::https_client::one_second_timer ( )
virtual

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

Reimplemented from dpp::ssl_client.

◆ read_loop()

◆ write()

void dpp::ssl_client::write ( const std::string &  data)
virtualinherited

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.

Reimplemented in dpp::websocket_client.

References dpp::err_ssl_write, dpp::err_write, INVALID_SOCKET, dpp::ssl_client::nonblocking, dpp::ssl_client::obuffer, dpp::ssl_client::plaintext, dpp::ssl_client::sfd, dpp::openssl_connection::ssl, and dpp::ssl_client::ssl.

Member Data Documentation

◆ buffer

std::string dpp::ssl_client::buffer
protectedinherited

Input buffer received from socket.

Referenced by dpp::ssl_client::close(), dpp::discord_client::handle_frame(), and dpp::ssl_client::read_loop().

◆ bytes_in

uint64_t dpp::ssl_client::bytes_in
protectedinherited

◆ bytes_out

uint64_t dpp::ssl_client::bytes_out
protectedinherited

◆ cipher

std::string dpp::ssl_client::cipher
protectedinherited

SSL cipher in use.

Referenced by dpp::ssl_client::connect(), and dpp::ssl_client::get_cipher().

◆ custom_readable_fd

socket_callback_t dpp::ssl_client::custom_readable_fd
inherited

Attaching an additional file descriptor to this function will send notifications when there is data to read.

NOTE: Only hook this if you NEED it as it can increase CPU usage of the thread! Returning -1 means that you don't want to be notified.

Referenced by dpp::discord_voice_client::handle_frame(), and dpp::ssl_client::read_loop().

◆ custom_readable_ready

socket_notification_t dpp::ssl_client::custom_readable_ready
inherited

This event will be called when you can read from the custom fd.

Referenced by dpp::discord_voice_client::handle_frame(), and dpp::ssl_client::read_loop().

◆ custom_writeable_fd

socket_callback_t dpp::ssl_client::custom_writeable_fd
inherited

Attaching an additional file descriptor to this function will send notifications when you are able to write to the socket.

NOTE: Only hook this if you NEED it as it can increase CPU usage of the thread! You should toggle this to -1 when you do not have anything to write otherwise it'll keep triggering repeatedly (it is level triggered).

Referenced by dpp::discord_voice_client::handle_frame(), and dpp::ssl_client::read_loop().

◆ custom_writeable_ready

socket_notification_t dpp::ssl_client::custom_writeable_ready
inherited

This event will be called when you can write to a custom fd.

Referenced by dpp::discord_voice_client::handle_frame(), and dpp::ssl_client::read_loop().

◆ hostname

std::string dpp::ssl_client::hostname
protectedinherited

◆ keepalive

bool dpp::ssl_client::keepalive
inherited

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

Referenced by dpp::ssl_client::close(), and dpp::ssl_client::ssl_client().

◆ last_tick

time_t dpp::ssl_client::last_tick
protectedinherited

For timers.

Referenced by dpp::ssl_client::read_loop().

◆ make_new

bool dpp::ssl_client::make_new
protectedinherited

True if we are establishing a new connection, false if otherwise.

Referenced by dpp::ssl_client::connect(), and dpp::ssl_client::ssl_client().

◆ nonblocking

bool dpp::ssl_client::nonblocking
protectedinherited

True if in nonblocking mode.

The socket switches to nonblocking mode once ReadLoop is called.

Referenced by dpp::ssl_client::connect(), dpp::ssl_client::read_loop(), and dpp::ssl_client::write().

◆ obuffer

std::string dpp::ssl_client::obuffer
protectedinherited

Output buffer for sending to socket.

Referenced by dpp::ssl_client::close(), dpp::ssl_client::read_loop(), and dpp::ssl_client::write().

◆ plaintext

bool dpp::ssl_client::plaintext
protectedinherited

◆ port

std::string dpp::ssl_client::port
protectedinherited

◆ sfd

◆ ssl


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