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 (cluster *creator, 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", https_client_completion_event done={}) | |
Connect to a specific HTTP(S) server and complete a request. More... | |
virtual | ~https_client () 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 (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... | |
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... | |
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... | |
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 | |
bool | timed_out |
If true the response timed out while waiting. More... | |
https_client_completion_event | completed |
Function to call when HTTP request is completed. More... | |
http_state | state |
Current connection state. More... | |
bool | keepalive |
True if we are keeping the connection alive after it has finished. More... | |
class cluster * | owner |
Owning cluster. More... | |
Protected Member Functions | |
virtual void | connect () override |
Start the connection. More... | |
http_state | get_state () |
Get request state. 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 | |
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... | |
Implements a HTTPS socket client based on the SSL client.
dpp::https_client::https_client | ( | cluster * | creator, |
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" , |
||
https_client_completion_event | done = {} |
||
) |
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 |
protocol | Request HTTP protocol (default: 1.1) |
done | Function to call when https_client request is completed |
|
overridevirtual |
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 |
mimetypes | MIME types of each of the files to send |
|
overridevirtual |
Close HTTPS socket.
Reimplemented from dpp::ssl_client.
|
inherited |
Called while SSL handshake is in progress. If the handshake completes, the state of the socket is progressed to an established state.
ev | Socket events for the socket |
|
overrideprotectedvirtual |
Start the connection.
Reimplemented from dpp::ssl_client.
|
protectedinherited |
If raw_trace is set to true, log a debug message for this connection.
message | debug message |
|
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.
|
inherited |
Get total bytes received.
|
inherited |
Get the bytes out objectGet 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 |
size_t dpp::https_client::get_header_count | ( | std::string | header_name | ) | const |
Get the number of headers with the same header name.
header_name |
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.
header_name |
const std::multimap< 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.
|
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.
|
overridevirtual |
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.
|
inherited |
Called when there is an error on the TCP socket.
fd | File descriptor |
error_code | Error code |
|
inherited |
Called when the TCP socket has data to read.
fd | File descriptor |
ev | Socket events |
|
inherited |
Called when the TCP socket can be written to without blocking.
fd | File descriptor |
e | Socket events |
|
overridevirtual |
Fires every second from the underlying socket I/O loop, used for timeouts.
Reimplemented from dpp::ssl_client.
|
inherited |
Set up non blocking I/O and configure on_read, on_write and on_error.
std::exception | Any std::exception (or derivative) thrown from read_loop() indicates setup failed |
|
inherited |
Write to the output buffer.
data | Data to be written to the buffer. |
|
protectedinherited |
Input buffer received from socket.
|
protectedinherited |
Bytes in.
|
protectedinherited |
Bytes out.
|
protectedinherited |
SSL cipher in use.
https_client_completion_event dpp::https_client::completed |
Function to call when HTTP request is completed.
|
protectedinherited |
How many times we retried connect()
|
protectedinherited |
True if connection is completed.
|
protectedinherited |
Hostname connected to.
|
inherited |
True if we are keeping the connection alive after it has finished.
|
protectedinherited |
For timers.
|
protectedinherited |
Output buffer for sending to socket.
|
inherited |
Owning cluster.
|
protectedinherited |
True for a plain text connection.
|
protectedinherited |
Port connected to.
|
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.
|
protectedinherited |
Raw file descriptor of connection.
|
protectedinherited |
Openssl opaque contexts.
|
protectedinherited |
Start time of connection.
http_state dpp::https_client::state |
Current connection state.
|
protectedinherited |
True if tcp connect() succeeded.
bool dpp::https_client::timed_out |
If true the response timed out while waiting.
|
protectedinherited |
Timer handle for one second timer.
|
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.