SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
Public Member Functions | Private Slots | Private Member Functions | Private Attributes | List of all members
qtwebapp::HttpConnectionHandlerPool Class Reference

#include <httpconnectionhandlerpool.h>

Inherits QObject.

+ Collaboration diagram for qtwebapp::HttpConnectionHandlerPool:

Public Member Functions

 HttpConnectionHandlerPool (QSettings *settings, HttpRequestHandler *requestHandler)
 
 HttpConnectionHandlerPool (const HttpListenerSettings *settings, HttpRequestHandler *requestHandler)
 
virtual ~HttpConnectionHandlerPool ()
 
HttpConnectionHandlergetConnectionHandler ()
 
const HttpListenerSettingsgetListenerSettings () const
 

Private Slots

void cleanup ()
 

Private Member Functions

void loadSslConfig ()
 

Private Attributes

QSettings * settings
 
const HttpListenerSettingslistenerSettings
 
HttpRequestHandlerrequestHandler
 
QList< HttpConnectionHandler * > pool
 
QTimer cleanupTimer
 
QMutex mutex
 
QSslConfiguration * sslConfiguration
 
bool useQtSettings
 

Detailed Description

Pool of http connection handlers. The size of the pool grows and shrinks on demand.

Example for the required configuration settings:

minThreads=4
maxThreads=100
cleanupInterval=60000
readTimeout=60000
;sslKeyFile=ssl/my.key
;sslCertFile=ssl/my.cert
maxRequestSize=16000
maxMultiPartSize=1000000

After server start, the size of the thread pool is always 0. Threads are started on demand when requests come in. The cleanup timer reduces the number of idle threads slowly by closing one thread in each interval. But the configured minimum number of threads are kept running.

For SSL support, you need an OpenSSL certificate file and a key file. Both can be created with the command

    openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout my.key -out my.cert

Visit http://slproweb.com/products/Win32OpenSSL.html to download the Light version of OpenSSL for Windows.

Please note that a listener with SSL settings can only handle HTTPS protocol. To support both HTTP and HTTPS simultaneously, you need to start two listeners on different ports - one with SLL and one without SSL.

See also
HttpConnectionHandler for description of the readTimeout
HttpRequest for description of config settings maxRequestSize and maxMultiPartSize

Definition at line 51 of file httpconnectionhandlerpool.h.

Constructor & Destructor Documentation

◆ HttpConnectionHandlerPool() [1/2]

HttpConnectionHandlerPool::HttpConnectionHandlerPool ( QSettings *  settings,
HttpRequestHandler requestHandler 
)

Constructor.

Parameters
settingsConfiguration settings for the HTTP server. Must not be 0.
requestHandlerThe handler that will process each received HTTP request.
Warning
The requestMapper gets deleted by the destructor of this pool

Definition at line 12 of file httpconnectionhandlerpool.cpp.

References cleanup(), cleanupTimer, listenerSettings, loadSslConfig(), requestHandler, settings, and sslConfiguration.

13  : QObject(), useQtSettings(true)
14 {
15  Q_ASSERT(settings != 0);
16  this->settings = settings;
17  this->listenerSettings = 0;
18  this->requestHandler = requestHandler;
19  this->sslConfiguration = 0;
20  loadSslConfig();
21  cleanupTimer.start(settings->value("cleanupInterval",1000).toInt());
22  connect(&cleanupTimer, SIGNAL(timeout()), SLOT(cleanup()));
23 }
const HttpListenerSettings * listenerSettings
+ Here is the call graph for this function:

◆ HttpConnectionHandlerPool() [2/2]

HttpConnectionHandlerPool::HttpConnectionHandlerPool ( const HttpListenerSettings settings,
HttpRequestHandler requestHandler 
)

Constructor.

Parameters
settingsConfiguration settings for the HTTP server as structure
requestHandlerThe handler that will process each received HTTP request.
Warning
The requestMapper gets deleted by the destructor of this pool

Definition at line 25 of file httpconnectionhandlerpool.cpp.

References cleanup(), qtwebapp::HttpListenerSettings::cleanupInterval, cleanupTimer, listenerSettings, loadSslConfig(), requestHandler, settings, and sslConfiguration.

26  : QObject(), useQtSettings(false)
27 {
28  Q_ASSERT(settings != 0);
29  this->settings = 0;
30  this->listenerSettings = settings;
31  this->requestHandler = requestHandler;
32  this->sslConfiguration = 0;
33  loadSslConfig();
34  cleanupTimer.start(settings->cleanupInterval);
35  connect(&cleanupTimer, SIGNAL(timeout()), SLOT(cleanup()));
36 }
const HttpListenerSettings * listenerSettings
+ Here is the call graph for this function:

◆ ~HttpConnectionHandlerPool()

HttpConnectionHandlerPool::~HttpConnectionHandlerPool ( )
virtual

Destructor

Definition at line 38 of file httpconnectionhandlerpool.cpp.

References handler(), pool, and sslConfiguration.

39 {
40  // delete all connection handlers and wait until their threads are closed
42  {
43  delete handler;
44  }
45  delete sslConfiguration;
46  qDebug("HttpConnectionHandlerPool (%p): destroyed", this);
47 }
QList< HttpConnectionHandler * > pool
void handler(int sig)
Definition: main.cpp:32
+ Here is the call graph for this function:

Member Function Documentation

◆ cleanup

void HttpConnectionHandlerPool::cleanup ( )
privateslot

Received from the clean-up timer.

Definition at line 85 of file httpconnectionhandlerpool.cpp.

References handler(), qtwebapp::HttpConnectionHandler::isBusy(), listenerSettings, qtwebapp::HttpListenerSettings::minThreads, mutex, pool, settings, and useQtSettings.

Referenced by HttpConnectionHandlerPool().

86 {
87  int maxIdleHandlers = useQtSettings ? settings->value("minThreads",1).toInt() : listenerSettings->minThreads;
88  int idleCounter=0;
89  mutex.lock();
91  {
92  if (!handler->isBusy())
93  {
94  if (++idleCounter > maxIdleHandlers)
95  {
96  pool.removeOne(handler);
97  qDebug("HttpConnectionHandlerPool: Removed connection handler (%p), pool size is now %i",handler,pool.size());
98  delete handler;
99  break; // remove only one handler in each interval
100  }
101  }
102  }
103  mutex.unlock();
104 }
QList< HttpConnectionHandler * > pool
const HttpListenerSettings * listenerSettings
void handler(int sig)
Definition: main.cpp:32
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getConnectionHandler()

HttpConnectionHandler * HttpConnectionHandlerPool::getConnectionHandler ( )

Get a free connection handler, or 0 if not available.

Definition at line 50 of file httpconnectionhandlerpool.cpp.

References handler(), qtwebapp::HttpConnectionHandler::isBusy(), listenerSettings, qtwebapp::HttpListenerSettings::maxThreads, mutex, pool, requestHandler, qtwebapp::HttpConnectionHandler::setBusy(), settings, sslConfiguration, and useQtSettings.

Referenced by qtwebapp::HttpListener::incomingConnection().

51 {
52  HttpConnectionHandler* freeHandler=0;
53  mutex.lock();
54  // find a free handler in pool
56  {
57  if (!handler->isBusy())
58  {
59  freeHandler=handler;
60  freeHandler->setBusy();
61  break;
62  }
63  }
64  // create a new handler, if necessary
65  if (!freeHandler)
66  {
67  int maxConnectionHandlers = useQtSettings ? settings->value("maxThreads",100).toInt() : listenerSettings->maxThreads;
68  if (pool.count()<maxConnectionHandlers)
69  {
70  if (useQtSettings) {
72  } else {
74  }
75 
76  freeHandler->setBusy();
77  pool.append(freeHandler);
78  }
79  }
80  mutex.unlock();
81  return freeHandler;
82 }
QList< HttpConnectionHandler * > pool
const HttpListenerSettings * listenerSettings
void handler(int sig)
Definition: main.cpp:32
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getListenerSettings()

const HttpListenerSettings* qtwebapp::HttpConnectionHandlerPool::getListenerSettings ( ) const
inline

Get a listener settings const reference. Can be changed on the HttpListener only.

Returns
The current listener settings

Definition at line 82 of file httpconnectionhandlerpool.h.

82 { return listenerSettings; }
const HttpListenerSettings * listenerSettings

◆ loadSslConfig()

void HttpConnectionHandlerPool::loadSslConfig ( )
private

Load SSL configuration

Definition at line 107 of file httpconnectionhandlerpool.cpp.

References listenerSettings, settings, qtwebapp::HttpListenerSettings::sslCertFile, sslConfiguration, qtwebapp::HttpListenerSettings::sslKeyFile, and useQtSettings.

Referenced by HttpConnectionHandlerPool().

108 {
109  // If certificate and key files are configured, then load them
110  QString sslKeyFileName = useQtSettings ? settings->value("sslKeyFile","").toString() : listenerSettings->sslKeyFile;
111  QString sslCertFileName = useQtSettings ? settings->value("sslCertFile","").toString() : listenerSettings->sslCertFile;
112  if (!sslKeyFileName.isEmpty() && !sslCertFileName.isEmpty())
113  {
114  #ifdef QT_NO_OPENSSL
115  qWarning("HttpConnectionHandlerPool::loadSslConfig: SSL is not supported");
116  #else
117  // Convert relative fileNames to absolute, based on the directory of the config file.
118  QFileInfo configFile(settings->fileName());
119  #ifdef Q_OS_WIN32
120  if (QDir::isRelativePath(sslKeyFileName) && settings->format()!=QSettings::NativeFormat)
121  #else
122  if (QDir::isRelativePath(sslKeyFileName))
123  #endif
124  {
125  sslKeyFileName=QFileInfo(configFile.absolutePath(),sslKeyFileName).absoluteFilePath();
126  }
127  #ifdef Q_OS_WIN32
128  if (QDir::isRelativePath(sslCertFileName) && settings->format()!=QSettings::NativeFormat)
129  #else
130  if (QDir::isRelativePath(sslCertFileName))
131  #endif
132  {
133  sslCertFileName=QFileInfo(configFile.absolutePath(),sslCertFileName).absoluteFilePath();
134  }
135 
136  // Load the SSL certificate
137  QFile certFile(sslCertFileName);
138  if (!certFile.open(QIODevice::ReadOnly))
139  {
140  qCritical("HttpConnectionHandlerPool: cannot open sslCertFile %s", qPrintable(sslCertFileName));
141  return;
142  }
143  QSslCertificate certificate(&certFile, QSsl::Pem);
144  certFile.close();
145 
146  // Load the key file
147  QFile keyFile(sslKeyFileName);
148  if (!keyFile.open(QIODevice::ReadOnly))
149  {
150  qCritical("HttpConnectionHandlerPool: cannot open sslKeyFile %s", qPrintable(sslKeyFileName));
151  return;
152  }
153  QSslKey sslKey(&keyFile, QSsl::Rsa, QSsl::Pem);
154  keyFile.close();
155 
156  // Create the SSL configuration
157  sslConfiguration=new QSslConfiguration();
158  sslConfiguration->setLocalCertificate(certificate);
159  sslConfiguration->setPrivateKey(sslKey);
160  sslConfiguration->setPeerVerifyMode(QSslSocket::VerifyNone);
161  sslConfiguration->setProtocol(QSsl::TlsV1SslV3);
162 
163  qDebug("HttpConnectionHandlerPool: SSL settings loaded");
164  #endif
165  }
166 }
const HttpListenerSettings * listenerSettings
+ Here is the caller graph for this function:

Member Data Documentation

◆ cleanupTimer

QTimer qtwebapp::HttpConnectionHandlerPool::cleanupTimer
private

Timer to clean-up unused connection handler

Definition at line 99 of file httpconnectionhandlerpool.h.

Referenced by HttpConnectionHandlerPool().

◆ listenerSettings

const HttpListenerSettings* qtwebapp::HttpConnectionHandlerPool::listenerSettings
private

Settings for this pool as structure

Definition at line 90 of file httpconnectionhandlerpool.h.

Referenced by cleanup(), getConnectionHandler(), HttpConnectionHandlerPool(), and loadSslConfig().

◆ mutex

QMutex qtwebapp::HttpConnectionHandlerPool::mutex
private

Used to synchronize threads

Definition at line 102 of file httpconnectionhandlerpool.h.

Referenced by cleanup(), and getConnectionHandler().

◆ pool

QList<HttpConnectionHandler*> qtwebapp::HttpConnectionHandlerPool::pool
private

Pool of connection handlers

Definition at line 96 of file httpconnectionhandlerpool.h.

Referenced by cleanup(), getConnectionHandler(), and ~HttpConnectionHandlerPool().

◆ requestHandler

HttpRequestHandler* qtwebapp::HttpConnectionHandlerPool::requestHandler
private

Will be assigned to each Connectionhandler during their creation

Definition at line 93 of file httpconnectionhandlerpool.h.

Referenced by getConnectionHandler(), and HttpConnectionHandlerPool().

◆ settings

QSettings* qtwebapp::HttpConnectionHandlerPool::settings
private

Settings for this pool as Qt settings

Definition at line 87 of file httpconnectionhandlerpool.h.

Referenced by cleanup(), getConnectionHandler(), HttpConnectionHandlerPool(), and loadSslConfig().

◆ sslConfiguration

QSslConfiguration* qtwebapp::HttpConnectionHandlerPool::sslConfiguration
private

The SSL configuration (certificate, key and other settings)

Definition at line 105 of file httpconnectionhandlerpool.h.

Referenced by getConnectionHandler(), HttpConnectionHandlerPool(), loadSslConfig(), and ~HttpConnectionHandlerPool().

◆ useQtSettings

bool qtwebapp::HttpConnectionHandlerPool::useQtSettings
private

Settings flag

Definition at line 111 of file httpconnectionhandlerpool.h.

Referenced by cleanup(), getConnectionHandler(), and loadSslConfig().


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