libhomegear-base
0.7
Base library for Homegear and Homegear family modules.
|
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 |
This class provides a basic HTTP server.
The class is thread safe.
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(); }
BaseLib::HttpServer::HttpServer | ( | BaseLib::SharedObjects * | baseLib, |
HttpServerInfo & | serverInfo | ||
) |
|
virtual |
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.
address | The address to bind the server to (e. g. :: or 0.0.0.0 ). | |
port | The port number to bind the server to. | |
[out] | listenAddress | The IP address the server was bound to (e. g. 192.168.0.152 ). |
|
protected |
std::string BaseLib::HttpServer::getClientCertDn | ( | int32_t | clientId | ) |
int64_t BaseLib::HttpServer::getClientCertExpiration | ( | int32_t | clientId | ) |
std::string BaseLib::HttpServer::getClientCertSerial | ( | int32_t | clientId | ) |
double BaseLib::HttpServer::GetPacketsPerMinuteReceived | ( | ) |
double BaseLib::HttpServer::GetPacketsPerMinuteSent | ( | ) |
double BaseLib::HttpServer::GetProcessingThreadLoad | ( | ) |
double BaseLib::HttpServer::GetProcessingThreadLoadMax | ( | ) |
double BaseLib::HttpServer::GetServerThreadLoad | ( | ) |
|
protected |
|
protected |
void BaseLib::HttpServer::reload | ( | ) |
void BaseLib::HttpServer::send | ( | int32_t | clientId, |
const TcpSocket::TcpPacket & | packet, | ||
bool | closeConnection = true |
||
) |
void BaseLib::HttpServer::send | ( | int32_t | clientId, |
const std::vector< char > & | packet, | ||
bool | closeConnection = true |
||
) |
void BaseLib::HttpServer::start | ( | std::string | address, |
std::string | port, | ||
std::string & | listenAddress, | ||
size_t | processingThreads = 0 |
||
) |
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.
[out] | listenAddress | The IP address the server was bound to (e. g. 192.168.0.152 ). |
void BaseLib::HttpServer::stop | ( | ) |
void BaseLib::HttpServer::waitForStop | ( | ) |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |