D++ (DPP)
C++ Discord API Bot Library
|
Implements a HTTPS socket client based on the SSL client. More...
#include <httpsclient.h>
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) | |
Connect to a specific HTTP(S) server and complete a request. More... | |
virtual | ~https_client () |
Destroy the https client object. More... | |
virtual bool | handle_buffer (std::string &buffer) |
Processes incoming data from the SSL socket input buffer. More... | |
virtual void | close () |
Close HTTPS socket. More... | |
virtual void | one_second_timer () |
const std::string | get_header (std::string header_name) const |
Get a HTTP response header. More... | |
const std::map< std::string, std::string > | get_headers () const |
Get all HTTP response headers. More... | |
const std::string | get_content () const |
Get the response content. More... | |
uint16_t | 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. More... | |
uint64_t | get_bytes_out () |
uint64_t | get_bytes_in () |
std::string | get_cipher () |
void | read_loop () |
Nonblocking I/O loop. More... | |
virtual void | write (const std::string &data) |
Write to the output buffer. More... | |
virtual void | log (dpp::loglevel severity, const std::string &msg) const |
Log a message. 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={}) |
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_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_readable_ready |
This event will be called when you can read from the custom fd. 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 |
std::string | obuffer |
bool | nonblocking |
dpp::socket | sfd |
opensslcontext * | ssl |
std::string | cipher |
time_t | last_tick |
std::string | hostname |
std::string | port |
uint64_t | bytes_out |
uint64_t | bytes_in |
bool | plaintext |
bool | make_new |
True if we are establishing a new connection, false if otherwise. More... | |
Implements a HTTPS socket client based on the SSL 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 |
||
) |
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.
hostname | Hostname to connect to |
port | Port number to connect to, usually 443 for SSL and 80 for plaintext |
urlpath | path part of URL, e.g. "/api" |
verb | Request verb, e.g. GET or POST |
req_body | Request body, use dpp::https_client::build_multipart() to build a multipart MIME body (e.g. for multiple file upload) |
extra_headers | Additional request headers, e.g. user-agent, authorization, etc |
plaintext_connection | Set to true to make the connection plaintext (turns off SSL) |
request_timeout | How many seconds before the connection is considered failed if not finished |
|
virtual |
Destroy the https client object.
|
static |
Build a multipart content from a set of files and some json.
json | The json content |
filenames | File names of files to send |
contents | Contents of each of the files to send |
|
virtual |
Close HTTPS socket.
Reimplemented from dpp::ssl_client.
|
protectedvirtual |
Start the connection.
Reimplemented from dpp::ssl_client.
|
inherited |
Get total bytes received
|
inherited |
Get total bytes sent
|
inherited |
Get SSL cipher name
const std::string dpp::https_client::get_content | ( | ) | const |
Get the response content.
const std::string dpp::https_client::get_header | ( | std::string | header_name | ) | const |
Get a HTTP response header.
header_name | Header name to find, case insensitive |
const std::map< std::string, std::string > dpp::https_client::get_headers | ( | ) | const |
Get all HTTP response headers.
|
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.
url | URL to break down |
|
protected |
Get request state.
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.
|
virtual |
Processes incoming data from the SSL socket input buffer.
buffer | The buffer contents. Can modify this value removing the head elements when processed. |
Reimplemented from dpp::ssl_client.
|
virtualinherited |
Log a message.
severity | severity of log message |
msg | Log message to send |
Reimplemented in dpp::discord_client, and dpp::discord_voice_client.
|
virtual |
Fires every second from the underlying socket I/O loop, used for sending websocket pings
Reimplemented from dpp::ssl_client.
|
inherited |
Nonblocking I/O loop.
std::exception | Any std::exception (or derivative) thrown from read_loop() causes reconnection of the shard |
|
virtualinherited |
Write to the output buffer.
data | Data to be written to the buffer |
Reimplemented in dpp::websocket_client.
|
protectedinherited |
Input buffer received from openssl
|
protectedinherited |
Bytes in
|
protectedinherited |
Bytes out
|
protectedinherited |
SSL cipher in use
|
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.
|
inherited |
This event will be called when you can read from the custom 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).
|
inherited |
This event will be called when you can write to a custom fd.
|
protectedinherited |
Hostname connected to
|
inherited |
True if we are keeping the connection alive after it has finished.
|
protectedinherited |
For timers
|
protectedinherited |
True if we are establishing a new connection, false if otherwise.
|
protectedinherited |
True if in nonblocking mode. The socket switches to nonblocking mode once ReadLoop is called.
|
protectedinherited |
Output buffer for sending to openssl
|
protectedinherited |
True for a plain text connection
|
protectedinherited |
Port connected to
|
protectedinherited |
Raw file descriptor of connection
|
protectedinherited |
Openssl opaque contexts