libcyberradio  22.01.24
HttpsSession Class Reference

Class that encapsulates an HTTPS session. More...

#include <HttpsSession.h>

Inheritance diagram for HttpsSession:
Debuggable

Public Member Functions

 HttpsSession (bool debug=false)
 Constructs an HttpsSession object. More...
 
virtual ~HttpsSession ()
 Destroys an HttpsSession object.
 
virtual bool get (const std::string &url, bool verify=true)
 Gets data from a given URL over HTTPS. More...
 
virtual bool post (const std::string &url, void *data, size_t length, const char *contentType="text/plain", bool verify=true)
 Posts data to a given URL over HTTPS. More...
 
virtual long getResponseCode () const
 Gets the HTTPS response code from the last request. More...
 
virtual std::string getResponseHeader () const
 Gets the HTTPS header from the last request. More...
 
virtual std::string getResponseBody () const
 Gets the HTTPS response body from the last request. More...
 
virtual std::string getLastRequestErrorInfo () const
 Gets the error information for the last request. More...
 
virtual size_t writeHeader (char *ptr, size_t size)
 Write data into the header buffer. More...
 
virtual size_t writeResponseData (char *ptr, size_t size)
 Write data into the response buffer. More...
 
virtual void setDebugName (const std::string &debug_name)
 Sets the debug name for this object. More...
 
virtual void setDebugFile (FILE *debug_fp)
 Sets the debug file pointer for this object. More...
 
virtual void setDebugTimeFormat (const std::string &debug_timefmt)
 Sets the debug time format for this object. More...
 
virtual int debug (const char *format,...)
 Outputs debug information. More...
 
virtual const char * debugBool (bool x)
 Gets a debug output string for a Boolean value. More...
 
virtual bool isDebug () const
 Gets whether this object produces debug output. More...
 
virtual std::string getDebugName () const
 Gets the debug name for this object. More...
 
virtual std::string rawString (const std::string &data)
 Gets a "raw" string representation of a given data string. More...
 

Protected Member Functions

virtual CURLcode initializeRequest ()
 Initializes the session object to handle a new request. More...
 
virtual const char * debugCurl (CURLcode x)
 Returns a string corresponding to a libcurl error code. More...
 

Static Protected Member Functions

static size_t headerCallback (char *buffer, size_t size, size_t nitems, void *userdata)
 Callback function that libcurl "writes" data to when retrieving an HTTPS response header. More...
 
static size_t writeDataCallback (char *ptr, size_t size, size_t nmemb, void *userdata)
 Callback function that libcurl "writes" data to when retrieving an HTTPS response body. More...
 

Detailed Description

Class that encapsulates an HTTPS session.

Definition at line 29 of file HttpsSession.h.

Constructor & Destructor Documentation

◆ HttpsSession()

HttpsSession ( bool  debug = false)

Constructs an HttpsSession object.

Parameters
debugWhether this object produces debug output.

Definition at line 47 of file HttpsSession.cpp.

Member Function Documentation

◆ debug()

int debug ( const char *  format,
  ... 
)
virtualinherited

Outputs debug information.

This method follows the same semantics as printf(). Output is preceded by a timestamp and the name of the object, if provided.

Parameters
formatThe printf()-style format string.
...Comma-separated list of arguments to print. Note that these need to be arguments that can be supported natively through printf().
Returns
The number of characters outputted.

Definition at line 95 of file Debuggable.cpp.

◆ debugBool()

const char * debugBool ( bool  x)
virtualinherited

Gets a debug output string for a Boolean value.

Parameters
xBoolean value
Returns
A constant string, either "true" or "false".

Definition at line 126 of file Debuggable.cpp.

◆ debugCurl()

const char * debugCurl ( CURLcode  x)
protectedvirtual

Returns a string corresponding to a libcurl error code.

Returns
A string containing an error message.

Definition at line 248 of file HttpsSession.cpp.

◆ get()

bool get ( const std::string &  url,
bool  verify = true 
)
virtual

Gets data from a given URL over HTTPS.

Parameters
urlURL to use for the request.
verifyWhether or not to use SSL verification.
Returns
Whether the request was successfully performed. If this returns false, use getLastRequestErrorInfo() to get the reason why the request failed.

Definition at line 70 of file HttpsSession.cpp.

◆ getDebugName()

std::string getDebugName ( ) const
virtualinherited

Gets the debug name for this object.

Returns
The debug name, as a string.

Definition at line 138 of file Debuggable.cpp.

◆ getLastRequestErrorInfo()

std::string getLastRequestErrorInfo ( ) const
virtual

Gets the error information for the last request.

Returns
A string containing the error message.

Definition at line 181 of file HttpsSession.cpp.

◆ getResponseBody()

std::string getResponseBody ( ) const
virtual

Gets the HTTPS response body from the last request.

Returns
A string containing the response body.

Definition at line 176 of file HttpsSession.cpp.

◆ getResponseCode()

long getResponseCode ( ) const
virtual

Gets the HTTPS response code from the last request.

Returns
The response code.

Definition at line 166 of file HttpsSession.cpp.

◆ getResponseHeader()

std::string getResponseHeader ( ) const
virtual

Gets the HTTPS header from the last request.

Returns
A string containing the header.

Definition at line 171 of file HttpsSession.cpp.

◆ headerCallback()

size_t headerCallback ( char *  buffer,
size_t  size,
size_t  nitems,
void *  userdata 
)
staticprotected

Callback function that libcurl "writes" data to when retrieving an HTTPS response header.

Parameters
bufferBuffer containing the data retrieved via HTTPS
sizeSize (in bytes) of each data element
nitemsNumber of data elements retrieved
userdataA pointer containing data that the user specified using CURLOPT_HEADERDATA. For the purposes of this class, this will be the pointer to an HttpsSession object.
Returns
The number of bytes actually handled – ideally, size * nmemb.

Definition at line 255 of file HttpsSession.cpp.

◆ initializeRequest()

CURLcode initializeRequest ( )
protectedvirtual

Initializes the session object to handle a new request.

Returns
A libcurl error code.

Definition at line 208 of file HttpsSession.cpp.

◆ isDebug()

bool isDebug ( ) const
virtualinherited

Gets whether this object produces debug output.

Returns
True if producing debug, false otherwise.

Definition at line 133 of file Debuggable.cpp.

◆ post()

bool post ( const std::string &  url,
void *  data,
size_t  length,
const char *  contentType = "text/plain",
bool  verify = true 
)
virtual

Posts data to a given URL over HTTPS.

Parameters
urlURL to use for the request.
dataData to send in the body of the request.
lengthLength of the data to send (in bytes).
contentTypeContent type of the data.
verifyWhether or not to use SSL verification.
Returns
Whether the request was successfully performed. If this returns false, use getLastRequestErrorInfo() to get the reason why the request failed.

Definition at line 111 of file HttpsSession.cpp.

◆ rawString()

std::string rawString ( const std::string &  data)
virtualinherited

Gets a "raw" string representation of a given data string.

"Raw" string representations mimic Python string representations. Whitespace characters are denoted by backslash representations ("\\r", "\\n", "\\t", "\\v", "\\f"), while other non-printable characters are represented with hex representation ("\\x00", etc.)

Parameters
dataData string
Returns
The data's "raw" representation.

Definition at line 143 of file Debuggable.cpp.

◆ setDebugFile()

void setDebugFile ( FILE *  debug_fp)
virtualinherited

Sets the debug file pointer for this object.

Parameters
debug_fpFile to send debug output to.

Definition at line 81 of file Debuggable.cpp.

◆ setDebugName()

void setDebugName ( const std::string &  debug_name)
virtualinherited

Sets the debug name for this object.

Use this method to set unique debug names for objects of the same class for easy differentiation.

Parameters
debug_nameName for identifying this object in debug output.

Definition at line 74 of file Debuggable.cpp.

◆ setDebugTimeFormat()

void setDebugTimeFormat ( const std::string &  debug_timefmt)
virtualinherited

Sets the debug time format for this object.

Parameters
debug_timefmtFormat string for displaying timestamp, as compatible with strftime(). If this is an empty string, don't display a timestamp.

Definition at line 88 of file Debuggable.cpp.

◆ writeDataCallback()

size_t writeDataCallback ( char *  ptr,
size_t  size,
size_t  nmemb,
void *  userdata 
)
staticprotected

Callback function that libcurl "writes" data to when retrieving an HTTPS response body.

Parameters
ptrBuffer containing the data retrieved via HTTPS
sizeSize (in bytes) of each data element
nmembNumber of data elements retrieved
userdataA pointer containing data that the user specified using CURLOPT_WRITEDATA. For the purposes of this class, this will be the pointer to an HttpsSession object.
Returns
The number of bytes actually handled – ideally, size * nmemb.

Definition at line 268 of file HttpsSession.cpp.

◆ writeHeader()

size_t writeHeader ( char *  ptr,
size_t  size 
)
virtual

Write data into the header buffer.

Note
Not intended to be called by user code.
Returns
The number of bytes processed.

Definition at line 186 of file HttpsSession.cpp.

◆ writeResponseData()

size_t writeResponseData ( char *  ptr,
size_t  size 
)
virtual

Write data into the response buffer.

Note
Not intended to be called by user code.
Returns
The number of bytes processed.

Definition at line 197 of file HttpsSession.cpp.


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