libhomegear-base  0.7
Base library for Homegear and Homegear family modules.
BaseLib::HttpClient Class Reference

This class provides a basic HTTP client. More...

#include <HttpClient.h>

Public Member Functions

 HttpClient (BaseLib::SharedObjects *baseLib, std::string hostname, int32_t port=80, bool keepAlive=true, bool useSSL=false, std::string caFile="", bool verifyCertificate=true, std::string certPath="", std::string keyPath="")
 Constructor. More...
 
 HttpClient (BaseLib::SharedObjects *baseLib, std::string hostname, int32_t port, bool keepAlive, bool useSSL, bool verifyCertificate, std::string caFile, std::string caData, std::string certPath, std::string certData, std::string keyPath, const std::shared_ptr< Security::SecureVector< uint8_t >> &keyData)
 Constructor. More...
 
virtual ~HttpClient ()
 Destructor. More...
 
std::shared_ptr< TcpSocketgetSocket ()
 Returns the underlying TcpSocket. More...
 
void setTimeout (uint32_t value)
 Sets the socket timeout. More...
 
void setVerifyHostname (bool value)
 
void setUserAgent (const std::string &value)
 By default the header "User-Agent" is set to "Homegear". More...
 
bool connected ()
 Returns "true" if the socket is connected, otherwise "false". More...
 
void disconnect ()
 Closes the socket. More...
 
void enableRawContent (bool value)
 Enables storage of raw content. More...
 
std::vector< char > & getRawContent ()
 Returns the raw content. More...
 
std::string getIpAddress ()
 Returns the IP address of the HTTP server. More...
 
void sendRequest (const std::string &request, std::string &response, bool responseIsHeaderOnly=false)
 
void sendRequest (const std::string &request, Http &response, bool responseIsHeaderOnly=false)
 
void delete_ (const std::string &path, std::string &data, const std::string &additionalHeaders="")
 
void delete_ (const std::string &path, Http &data, const std::string &additionalHeaders="")
 
void get (const std::string &path, std::string &data, const std::string &additionalHeaders="")
 
void get (const std::string &path, Http &data, const std::string &additionalHeaders="")
 
void patch (const std::string &path, std::string &dataIn, std::string &dataOut, const std::string &additionalHeaders="")
 
void patch (const std::string &path, std::string &dataIn, Http &dataOut, const std::string &additionalHeaders="")
 
void post (const std::string &path, std::string &dataIn, std::string &dataOut, const std::string &additionalHeaders="")
 
void post (const std::string &path, std::string &dataIn, Http &dataOut, const std::string &additionalHeaders="")
 
void put (const std::string &path, std::string &dataIn, std::string &dataOut, const std::string &additionalHeaders="")
 
void put (const std::string &path, std::string &dataIn, Http &dataOut, const std::string &additionalHeaders="")
 

Protected Attributes

BaseLib::SharedObjects_bl = nullptr
 The common base library object. More...
 
std::mutex _socketMutex
 Protects _socket to only allow one operation at a time. More...
 
std::shared_ptr< TcpSocket_socket
 The socket object. More...
 
std::string _hostname = ""
 The hostname of the HTTP server. More...
 
int32_t _port = 80
 The port the HTTP server listens on. More...
 
bool _keepAlive = true
 Stores the information if the socket connection should be kept open after each request. More...
 
bool _keepRawContent = false
 When true, the raw content is stored. More...
 
std::string _userAgent = "Homegear"
 
std::vector< char > _rawContent
 Stores the raw response. More...
 

Detailed Description

This class provides a basic HTTP client.

The class is thread safe.

See also
HTTPClientException

Constructor & Destructor Documentation

§ HttpClient() [1/2]

BaseLib::HttpClient::HttpClient ( BaseLib::SharedObjects baseLib,
std::string  hostname,
int32_t  port = 80,
bool  keepAlive = true,
bool  useSSL = false,
std::string  caFile = "",
bool  verifyCertificate = true,
std::string  certPath = "",
std::string  keyPath = "" 
)

Constructor.

Parameters
baseLibThe common base library object.
hostnameThe hostname of the client to connect to without "http://".
port(default 80) The port to connect to.
keepAlive(default true) Keep the socket open after each request.
useSSL(default false) Set to "true" to use "https".
caFile(default "") Path to the certificate authority file of the certificate authority which signed the server certificate.
verifyCertificate(default true) Set to "true" to verify the server certificate (highly recommended).
certPath(default "") Path to the PEM encoded client certificate
keyPath(default "") Path to the PEM encoded client keyfile

§ HttpClient() [2/2]

BaseLib::HttpClient::HttpClient ( BaseLib::SharedObjects baseLib,
std::string  hostname,
int32_t  port,
bool  keepAlive,
bool  useSSL,
bool  verifyCertificate,
std::string  caFile,
std::string  caData,
std::string  certPath,
std::string  certData,
std::string  keyPath,
const std::shared_ptr< Security::SecureVector< uint8_t >> &  keyData 
)

Constructor.

...Data or ...File are chosen automatically. Just leave the one you don't want to use empty.

Parameters
baseLibThe common base library object.
hostnameThe hostname of the client to connect to without "http://".
port(default 80) The port to connect to.
keepAlive(default true) Keep the socket open after each request.
useSSL(default false) Set to "true" to use "https".
verifyCertificate(default true) Set to "true" to verify the server certificate (highly recommended).
caFile(default "") Path to the certificate authority file of the certificate authority which signed the server certificate.
caDataThe PEM-encoded CA certificate (not the path) used to sign the server certificate.
certPath(default "") Path to the PEM encoded client certificate
clientCertDataThe PEM-encoded client certificate (not the path).
keyPath(default "") Path to the PEM encoded client keyfile
keyDataThe PEM-encoded client key (not the path).

§ ~HttpClient()

BaseLib::HttpClient::~HttpClient ( )
virtual

Destructor.

Member Function Documentation

§ connected()

bool BaseLib::HttpClient::connected ( )
inline

Returns "true" if the socket is connected, otherwise "false".

Returns
"true" if the socket is connected, otherwise "false".

§ delete_() [1/2]

void BaseLib::HttpClient::delete_ ( const std::string &  path,
std::string &  data,
const std::string &  additionalHeaders = "" 
)

§ delete_() [2/2]

void BaseLib::HttpClient::delete_ ( const std::string &  path,
Http data,
const std::string &  additionalHeaders = "" 
)

§ disconnect()

void BaseLib::HttpClient::disconnect ( )
inline

Closes the socket.

§ enableRawContent()

void BaseLib::HttpClient::enableRawContent ( bool  value)
inline

Enables storage of raw content.

The raw content can be retrieved with getRawContent().

Parameters
valueSet to true to enable.

§ get() [1/2]

void BaseLib::HttpClient::get ( const std::string &  path,
std::string &  data,
const std::string &  additionalHeaders = "" 
)

§ get() [2/2]

void BaseLib::HttpClient::get ( const std::string &  path,
Http data,
const std::string &  additionalHeaders = "" 
)

§ getIpAddress()

std::string BaseLib::HttpClient::getIpAddress ( )
inline

Returns the IP address of the HTTP server.

Returns
The IP address.

§ getRawContent()

std::vector<char>& BaseLib::HttpClient::getRawContent ( )
inline

Returns the raw content.

Only available when enableRawContent() was set to true.

Returns
The raw respones as received from the HTTP server.

§ getSocket()

std::shared_ptr<TcpSocket> BaseLib::HttpClient::getSocket ( )
inline

Returns the underlying TcpSocket.

Returns
Returns the underlying TcpSocket.

§ patch() [1/2]

void BaseLib::HttpClient::patch ( const std::string &  path,
std::string &  dataIn,
std::string &  dataOut,
const std::string &  additionalHeaders = "" 
)

§ patch() [2/2]

void BaseLib::HttpClient::patch ( const std::string &  path,
std::string &  dataIn,
Http dataOut,
const std::string &  additionalHeaders = "" 
)

§ post() [1/2]

void BaseLib::HttpClient::post ( const std::string &  path,
std::string &  dataIn,
std::string &  dataOut,
const std::string &  additionalHeaders = "" 
)

§ post() [2/2]

void BaseLib::HttpClient::post ( const std::string &  path,
std::string &  dataIn,
Http dataOut,
const std::string &  additionalHeaders = "" 
)

§ put() [1/2]

void BaseLib::HttpClient::put ( const std::string &  path,
std::string &  dataIn,
std::string &  dataOut,
const std::string &  additionalHeaders = "" 
)

§ put() [2/2]

void BaseLib::HttpClient::put ( const std::string &  path,
std::string &  dataIn,
Http dataOut,
const std::string &  additionalHeaders = "" 
)

§ sendRequest() [1/2]

void BaseLib::HttpClient::sendRequest ( const std::string &  request,
std::string &  response,
bool  responseIsHeaderOnly = false 
)

§ sendRequest() [2/2]

void BaseLib::HttpClient::sendRequest ( const std::string &  request,
Http response,
bool  responseIsHeaderOnly = false 
)

§ setTimeout()

void BaseLib::HttpClient::setTimeout ( uint32_t  value)

Sets the socket timeout.

Parameters
valueThe timeout in milliseconds.

§ setUserAgent()

void BaseLib::HttpClient::setUserAgent ( const std::string &  value)

By default the header "User-Agent" is set to "Homegear".

To change the user agent string, call this method.

Parameters
valueThe new user agent.

§ setVerifyHostname()

void BaseLib::HttpClient::setVerifyHostname ( bool  value)

Member Data Documentation

§ _bl

BaseLib::SharedObjects* BaseLib::HttpClient::_bl = nullptr
protected

The common base library object.

§ _hostname

std::string BaseLib::HttpClient::_hostname = ""
protected

The hostname of the HTTP server.

§ _keepAlive

bool BaseLib::HttpClient::_keepAlive = true
protected

Stores the information if the socket connection should be kept open after each request.

§ _keepRawContent

bool BaseLib::HttpClient::_keepRawContent = false
protected

When true, the raw content is stored.

§ _port

int32_t BaseLib::HttpClient::_port = 80
protected

The port the HTTP server listens on.

§ _rawContent

std::vector<char> BaseLib::HttpClient::_rawContent
protected

Stores the raw response.

§ _socket

std::shared_ptr<TcpSocket> BaseLib::HttpClient::_socket
protected

The socket object.

§ _socketMutex

std::mutex BaseLib::HttpClient::_socketMutex
protected

Protects _socket to only allow one operation at a time.

See also
_socket

§ _userAgent

std::string BaseLib::HttpClient::_userAgent = "Homegear"
protected

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