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

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

#include <HttpServer.h>

Classes

struct  HttpClientInfo
 
struct  HttpServerInfo
 

Public Member Functions

 HttpServer (BaseLib::SharedObjects *baseLib, HttpServerInfo &serverInfo)
 
virtual ~HttpServer ()
 
void bind (std::string address, std::string port, std::string &listenAddress)
 Binds a socket to an IP address and a port. More...
 
void startPrebound (std::string &listenAddress, size_t processingThreads=0)
 Starts listening on the already bound socket (created with bind()). More...
 
void start (std::string address, std::string port, std::string &listenAddress, size_t processingThreads=0)
 
void stop ()
 
void waitForStop ()
 
void reload ()
 
double GetPacketsPerMinuteReceived ()
 
double GetPacketsPerMinuteSent ()
 
double GetServerThreadLoad ()
 
double GetProcessingThreadLoad ()
 
double GetProcessingThreadLoadMax ()
 
void send (int32_t clientId, const TcpSocket::TcpPacket &packet, bool closeConnection=true)
 
void send (int32_t clientId, const std::vector< char > &packet, bool closeConnection=true)
 
std::string getClientCertDn (int32_t clientId)
 
std::string getClientCertSerial (int32_t clientId)
 
int64_t getClientCertExpiration (int32_t clientId)
 

Protected Member Functions

void newConnection (int32_t clientId, std::string address, uint16_t port)
 
void connectionClosed (int32_t clientId)
 
void packetReceived (int32_t clientId, TcpSocket::TcpPacket &packet)
 

Protected Attributes

BaseLib::SharedObjects_bl = nullptr
 
std::shared_ptr< TcpSocket_socket
 
std::mutex _httpClientInfoMutex
 
std::unordered_map< int32_t, HttpClientInfo_httpClientInfo
 
std::function< void(int32_t clientId, std::string address, uint16_t port)> _newConnectionCallback
 
std::function< void(int32_t clientId)> _connectionClosedCallback
 
std::function< void(int32_t clientId, Http &http)> _packetReceivedCallback
 

Detailed Description

This class provides a basic HTTP server.

The class is thread safe.

HTTP Server Example Code

Save the example below in main.cpp and compile with:

g++ -o main -std=c++11 main.cpp -lhomegear-base -lgcrypt -lgnutls

Start and connect using a browser:

./main

Example code:

#include <homegear-base/BaseLib.h>

std::shared_ptr<BaseLib::SharedObjects> _bl;
std::shared_ptr<BaseLib::HttpServer> _httpServer;

void newConnection(int32_t clientId, std::string address, uint16_t port)
{
    std::cout << "New connection from " << address << " on port " << port << std::endl;
}

void packetReceived(int32_t clientId, BaseLib::Http http)
{
    if(http.getHeader().method != "GET") return;

    std::cout << "Client requested " << http.getHeader().path << std::endl;

    std::string json = "{\"result\":\"It worked!\",\"time\":" + std::to_string(BaseLib::HelperFunctions::getTime()) + ",\"path\":\"" + http.getHeader().path + "\"}";

    std::string header;
    header.append("HTTP/1.1 200 OK\r\n");
    header.append("Connection: close\r\n");
    header.append("Content-Type: application/json\r\n");
    header.append("Content-Length: ").append(std::to_string(json.size())).append("\r\n\r\n");

    BaseLib::TcpSocket::TcpPacket response;
    response.insert(response.end(), header.begin(), header.end());
    response.insert(response.end(), json.begin(), json.end());

    _httpServer->send(clientId, response);
}

int main()
{
    _bl.reset(new BaseLib::SharedObjects(false));

    BaseLib::HttpServer::HttpServerInfo serverInfo;
    serverInfo.packetReceivedCallback = std::bind(&packetReceived, std::placeholders::_1, std::placeholders::_2);

    _httpServer = std::make_shared<BaseLib::HttpServer>(_bl.get(), serverInfo);

    std::string listenAddress;
    _httpServer->start("::", "8082", listenAddress);
    std::cout << "Started listening on " + listenAddress << std::endl;

    for(int32_t i = 0; i < 300; i++) //Run for 300 seconds
    {
        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    }

    _httpServer->stop();
    _httpServer->waitForStop();
}

Constructor & Destructor Documentation

§ HttpServer()

BaseLib::HttpServer::HttpServer ( BaseLib::SharedObjects baseLib,
HttpServerInfo serverInfo 
)

§ ~HttpServer()

BaseLib::HttpServer::~HttpServer ( )
virtual

Member Function Documentation

§ bind()

void BaseLib::HttpServer::bind ( std::string  address,
std::string  port,
std::string &  listenAddress 
)

Binds a socket to an IP address and a port.

This splits up the start process to be able to listen on ports lower than 1024 and do a privilege drop. Call startPrebound() to start listening. Don't call start() when using pre-binding as this recreates the socket.

Parameters
addressThe address to bind the server to (e. g. :: or 0.0.0.0).
portThe port number to bind the server to.
[out]listenAddressThe IP address the server was bound to (e. g. 192.168.0.152).

§ connectionClosed()

void BaseLib::HttpServer::connectionClosed ( int32_t  clientId)
protected

§ getClientCertDn()

std::string BaseLib::HttpServer::getClientCertDn ( int32_t  clientId)

§ getClientCertExpiration()

int64_t BaseLib::HttpServer::getClientCertExpiration ( int32_t  clientId)

§ getClientCertSerial()

std::string BaseLib::HttpServer::getClientCertSerial ( int32_t  clientId)

§ GetPacketsPerMinuteReceived()

double BaseLib::HttpServer::GetPacketsPerMinuteReceived ( )

§ GetPacketsPerMinuteSent()

double BaseLib::HttpServer::GetPacketsPerMinuteSent ( )

§ GetProcessingThreadLoad()

double BaseLib::HttpServer::GetProcessingThreadLoad ( )

§ GetProcessingThreadLoadMax()

double BaseLib::HttpServer::GetProcessingThreadLoadMax ( )

§ GetServerThreadLoad()

double BaseLib::HttpServer::GetServerThreadLoad ( )

§ newConnection()

void BaseLib::HttpServer::newConnection ( int32_t  clientId,
std::string  address,
uint16_t  port 
)
protected

§ packetReceived()

void BaseLib::HttpServer::packetReceived ( int32_t  clientId,
TcpSocket::TcpPacket packet 
)
protected

§ reload()

void BaseLib::HttpServer::reload ( )

§ send() [1/2]

void BaseLib::HttpServer::send ( int32_t  clientId,
const TcpSocket::TcpPacket packet,
bool  closeConnection = true 
)

§ send() [2/2]

void BaseLib::HttpServer::send ( int32_t  clientId,
const std::vector< char > &  packet,
bool  closeConnection = true 
)

§ start()

void BaseLib::HttpServer::start ( std::string  address,
std::string  port,
std::string &  listenAddress,
size_t  processingThreads = 0 
)

§ startPrebound()

void BaseLib::HttpServer::startPrebound ( std::string &  listenAddress,
size_t  processingThreads = 0 
)

Starts listening on the already bound socket (created with bind()).

This splits up the start process to be able to listen on ports lower than 1024 and do a privilege drop. Don't call startServer() when using pre-binding as this recreates the socket.

See also
bind
Parameters
[out]listenAddressThe IP address the server was bound to (e. g. 192.168.0.152).

§ stop()

void BaseLib::HttpServer::stop ( )

§ waitForStop()

void BaseLib::HttpServer::waitForStop ( )

Member Data Documentation

§ _bl

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

§ _connectionClosedCallback

std::function<void(int32_t clientId)> BaseLib::HttpServer::_connectionClosedCallback
protected

§ _httpClientInfo

std::unordered_map<int32_t, HttpClientInfo> BaseLib::HttpServer::_httpClientInfo
protected

§ _httpClientInfoMutex

std::mutex BaseLib::HttpServer::_httpClientInfoMutex
protected

§ _newConnectionCallback

std::function<void(int32_t clientId, std::string address, uint16_t port)> BaseLib::HttpServer::_newConnectionCallback
protected

§ _packetReceivedCallback

std::function<void(int32_t clientId, Http &http)> BaseLib::HttpServer::_packetReceivedCallback
protected

§ _socket

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

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