libhomegear-base  0.7
Base library for Homegear and Homegear family modules.
HttpServer.h
Go to the documentation of this file.
1 /* Copyright 2013-2019 Homegear GmbH
2  *
3  * Homegear is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU Lesser General Public License as
5  * published by the Free Software Foundation, either version 3 of the
6  * License, or (at your option) any later version.
7  *
8  * Homegear is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with Homegear. If not, see
15  * <http://www.gnu.org/licenses/>.
16  *
17  * In addition, as a special exception, the copyright holders give
18  * permission to link the code of portions of this program with the
19  * OpenSSL library under certain conditions as described in each
20  * individual source file, and distribute linked combinations
21  * including the two.
22  * You must obey the GNU Lesser General Public License in all respects
23  * for all of the code used other than OpenSSL. If you modify
24  * file(s) with this exception, you may extend this exception to your
25  * version of the file(s), but you are not obligated to do so. If you
26  * do not wish to do so, delete this exception statement from your
27  * version. If you delete this exception statement from all source
28  * files in the program, then also delete it here.
29 */
30 
31 #ifndef HTTPSERVER_H_
32 #define HTTPSERVER_H_
33 
34 #include "../Exception.h"
35 #include "../Managers/FileDescriptorManager.h"
36 #include "../Encoding/Http.h"
37 #include "TcpSocket.h"
38 
39 #include <atomic>
40 
41 namespace BaseLib {
42 
49  public:
50  HttpServerException(std::string message) : Exception(message) {}
51 };
52 
123 class HttpServer {
124  public:
125  struct HttpServerInfo {
126  bool useSsl = false;
127  uint32_t connectionBacklogSize = 100;
128  uint32_t maxConnections = 10;
129  uint32_t serverThreads = 1;
130  std::unordered_map<std::string, TcpSocket::PCertificateInfo> certificates;
131  std::string dhParamFile;
132  std::string dhParamData;
133  bool requireClientCert = false;
134 
135  std::function<void(int32_t clientId, std::string address, uint16_t port)> newConnectionCallback;
136  std::function<void(int32_t clientId)> connectionClosedCallback;
137  std::function<void(int32_t clientId, Http &http)> packetReceivedCallback;
138  };
139 
140  HttpServer(BaseLib::SharedObjects *baseLib, HttpServerInfo &serverInfo);
141  virtual ~HttpServer();
142 
152  void bind(std::string address, std::string port, std::string &listenAddress);
153 
163  void startPrebound(std::string &listenAddress, size_t processingThreads = 0);
164 
165  void start(std::string address, std::string port, std::string &listenAddress, size_t processingThreads = 0);
166  void stop();
167  void waitForStop();
168  void reload();
169 
170  double GetPacketsPerMinuteReceived();
171  double GetPacketsPerMinuteSent();
172  double GetServerThreadLoad();
173  double GetProcessingThreadLoad();
174  double GetProcessingThreadLoadMax();
175 
176  void send(int32_t clientId, const TcpSocket::TcpPacket &packet, bool closeConnection = true);
177  void send(int32_t clientId, const std::vector<char> &packet, bool closeConnection = true);
178 
179  std::string getClientCertDn(int32_t clientId);
180  std::string getClientCertSerial(int32_t clientId);
181  int64_t getClientCertExpiration(int32_t clientId);
182  protected:
183  struct HttpClientInfo {
184  std::shared_ptr<Http> http;
185  };
186 
187  BaseLib::SharedObjects *_bl = nullptr;
188  std::shared_ptr<TcpSocket> _socket;
189 
191  std::unordered_map<int32_t, HttpClientInfo> _httpClientInfo;
192 
193  std::function<void(int32_t clientId, std::string address, uint16_t port)> _newConnectionCallback;
194  std::function<void(int32_t clientId)> _connectionClosedCallback;
195  std::function<void(int32_t clientId, Http &http)> _packetReceivedCallback;
196 
197  void newConnection(int32_t clientId, std::string address, uint16_t port);
198  void connectionClosed(int32_t clientId);
199  void packetReceived(int32_t clientId, TcpSocket::TcpPacket &packet);
200 };
201 
202 }
203 #endif
This class provides a basic HTTP server.
Definition: HttpServer.h:123
This is the base library main class.
Definition: BaseLib.h:95
Definition: HttpServer.h:183
std::string dhParamData
Definition: HttpServer.h:132
std::mutex _httpClientInfoMutex
Definition: HttpServer.h:190
Definition: BaseLib.cpp:34
std::function< void(int32_t clientId)> _connectionClosedCallback
Definition: HttpServer.h:194
std::unordered_map< std::string, TcpSocket::PCertificateInfo > certificates
Definition: HttpServer.h:130
Exception class for the HTTP server.
Definition: HttpServer.h:48
std::function< void(int32_t clientId, std::string address, uint16_t port)> _newConnectionCallback
Definition: HttpServer.h:193
std::vector< uint8_t > TcpPacket
Definition: TcpSocket.h:172
std::shared_ptr< Http > http
Definition: HttpServer.h:184
std::function< void(int32_t clientId, std::string address, uint16_t port)> newConnectionCallback
Definition: HttpServer.h:135
std::string dhParamFile
Definition: HttpServer.h:131
std::shared_ptr< TcpSocket > _socket
Definition: HttpServer.h:188
std::function< void(int32_t clientId)> connectionClosedCallback
Definition: HttpServer.h:136
Definition: HttpServer.h:125
std::function< void(int32_t clientId, Http &http)> packetReceivedCallback
Definition: HttpServer.h:137
std::unordered_map< int32_t, HttpClientInfo > _httpClientInfo
Definition: HttpServer.h:191
Base class for all exceptions defined in Homegear.
Definition: Exception.h:41
std::function< void(int32_t clientId, Http &http)> _packetReceivedCallback
Definition: HttpServer.h:195
HttpServerException(std::string message)
Definition: HttpServer.h:50