1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2025-06-16 13:32:27 -04:00

HttpServer: use settings structures in place of QSettings (4)

This commit is contained in:
f4exb 2017-11-13 01:36:00 +01:00
parent ef2f591bcd
commit ab55400cc2
4 changed files with 70 additions and 9 deletions

View File

@ -5,15 +5,47 @@
#include "httpconnectionhandler.h" #include "httpconnectionhandler.h"
#include "httpresponse.h" #include "httpresponse.h"
#include "httplistenersettings.h"
using namespace qtwebapp; using namespace qtwebapp;
HttpConnectionHandler::HttpConnectionHandler(QSettings* settings, HttpRequestHandler* requestHandler, QSslConfiguration* sslConfiguration) HttpConnectionHandler::HttpConnectionHandler(QSettings* settings, HttpRequestHandler* requestHandler, QSslConfiguration* sslConfiguration)
: QThread() : QThread(), useQtSettings(true)
{ {
Q_ASSERT(settings!=0); Q_ASSERT(settings!=0);
Q_ASSERT(requestHandler!=0); Q_ASSERT(requestHandler!=0);
this->settings=settings; this->settings=settings;
this->listenerSettings=0;
this->requestHandler=requestHandler;
this->sslConfiguration=sslConfiguration;
currentRequest=0;
busy=false;
// Create TCP or SSL socket
createSocket();
// execute signals in my own thread
moveToThread(this);
socket->moveToThread(this);
readTimer.moveToThread(this);
// Connect signals
connect(socket, SIGNAL(readyRead()), SLOT(read()));
connect(socket, SIGNAL(disconnected()), SLOT(disconnected()));
connect(&readTimer, SIGNAL(timeout()), SLOT(readTimeout()));
readTimer.setSingleShot(true);
qDebug("HttpConnectionHandler (%p): constructed", this);
this->start();
}
HttpConnectionHandler::HttpConnectionHandler(HttpListenerSettings* settings, HttpRequestHandler* requestHandler, QSslConfiguration* sslConfiguration)
: QThread(), useQtSettings(false)
{
Q_ASSERT(settings!=0);
Q_ASSERT(requestHandler!=0);
this->settings=0;
this->listenerSettings=settings;
this->requestHandler=requestHandler; this->requestHandler=requestHandler;
this->sslConfiguration=sslConfiguration; this->sslConfiguration=sslConfiguration;
currentRequest=0; currentRequest=0;
@ -109,7 +141,7 @@ void HttpConnectionHandler::handleConnection(tSocketDescriptor socketDescriptor)
#endif #endif
// Start timer for read timeout // Start timer for read timeout
int readTimeout=settings->value("readTimeout",10000).toInt(); int readTimeout = useQtSettings ? settings->value("readTimeout",10000).toInt() : listenerSettings->readTimeout;
readTimer.start(readTimeout); readTimer.start(readTimeout);
// delete previous request // delete previous request
delete currentRequest; delete currentRequest;
@ -173,7 +205,7 @@ void HttpConnectionHandler::read()
{ {
// Restart timer for read timeout, otherwise it would // Restart timer for read timeout, otherwise it would
// expire during large file uploads. // expire during large file uploads.
int readTimeout=settings->value("readTimeout",10000).toInt(); int readTimeout = useQtSettings ? settings->value("readTimeout",10000).toInt() : listenerSettings->readTimeout;
readTimer.start(readTimeout); readTimer.start(readTimeout);
} }
} }
@ -267,7 +299,7 @@ void HttpConnectionHandler::read()
else else
{ {
// Start timer for next request // Start timer for next request
int readTimeout=settings->value("readTimeout",10000).toInt(); int readTimeout = useQtSettings ? settings->value("readTimeout",10000).toInt() : listenerSettings->readTimeout;
readTimer.start(readTimeout); readTimer.start(readTimeout);
} }
delete currentRequest; delete currentRequest;

View File

@ -19,6 +19,8 @@
namespace qtwebapp { namespace qtwebapp {
class HttpListenerSettings;
/** Alias type definition, for compatibility to different Qt versions */ /** Alias type definition, for compatibility to different Qt versions */
#if QT_VERSION >= 0x050000 #if QT_VERSION >= 0x050000
typedef qintptr tSocketDescriptor; typedef qintptr tSocketDescriptor;
@ -59,6 +61,7 @@ public:
@param sslConfiguration SSL (HTTPS) will be used if not NULL @param sslConfiguration SSL (HTTPS) will be used if not NULL
*/ */
HttpConnectionHandler(QSettings* settings, HttpRequestHandler* requestHandler, QSslConfiguration* sslConfiguration=NULL); HttpConnectionHandler(QSettings* settings, HttpRequestHandler* requestHandler, QSslConfiguration* sslConfiguration=NULL);
HttpConnectionHandler(HttpListenerSettings* settings, HttpRequestHandler* requestHandler, QSslConfiguration* sslConfiguration=NULL);
/** Destructor */ /** Destructor */
virtual ~HttpConnectionHandler(); virtual ~HttpConnectionHandler();
@ -73,6 +76,7 @@ private:
/** Configuration settings */ /** Configuration settings */
QSettings* settings; QSettings* settings;
HttpListenerSettings* listenerSettings;
/** TCP socket of the current connection */ /** TCP socket of the current connection */
QTcpSocket* socket; QTcpSocket* socket;
@ -98,6 +102,9 @@ private:
/** Create SSL or TCP socket */ /** Create SSL or TCP socket */
void createSocket(); void createSocket();
/** Settings flag */
bool useQtSettings;
public slots: public slots:
/** /**

View File

@ -6,14 +6,16 @@
#endif #endif
#include <QDir> #include <QDir>
#include "httpconnectionhandlerpool.h" #include "httpconnectionhandlerpool.h"
#include "httplistenersettings.h"
using namespace qtwebapp; using namespace qtwebapp;
HttpConnectionHandlerPool::HttpConnectionHandlerPool(QSettings* settings, HttpRequestHandler* requestHandler) HttpConnectionHandlerPool::HttpConnectionHandlerPool(QSettings* settings, HttpRequestHandler* requestHandler)
: QObject() : QObject(), useQtSettings(true)
{ {
Q_ASSERT(settings!=0); Q_ASSERT(settings!=0);
this->settings=settings; this->settings=settings;
this->listenerSettings=0;
this->requestHandler=requestHandler; this->requestHandler=requestHandler;
this->sslConfiguration=NULL; this->sslConfiguration=NULL;
loadSslConfig(); loadSslConfig();
@ -21,6 +23,19 @@ HttpConnectionHandlerPool::HttpConnectionHandlerPool(QSettings* settings, HttpRe
connect(&cleanupTimer, SIGNAL(timeout()), SLOT(cleanup())); connect(&cleanupTimer, SIGNAL(timeout()), SLOT(cleanup()));
} }
HttpConnectionHandlerPool::HttpConnectionHandlerPool(HttpListenerSettings* settings, HttpRequestHandler* requestHandler)
: QObject()
{
Q_ASSERT(settings!=0);
this->settings=0;
this->listenerSettings=settings;
this->requestHandler=requestHandler;
this->sslConfiguration=NULL;
loadSslConfig();
cleanupTimer.start(settings->cleanupInterval);
connect(&cleanupTimer, SIGNAL(timeout()), SLOT(cleanup()));
}
HttpConnectionHandlerPool::~HttpConnectionHandlerPool() HttpConnectionHandlerPool::~HttpConnectionHandlerPool()
{ {
@ -51,7 +66,7 @@ HttpConnectionHandler* HttpConnectionHandlerPool::getConnectionHandler()
// create a new handler, if necessary // create a new handler, if necessary
if (!freeHandler) if (!freeHandler)
{ {
int maxConnectionHandlers=settings->value("maxThreads",100).toInt(); int maxConnectionHandlers = useQtSettings ? settings->value("maxThreads",100).toInt() : listenerSettings->maxThreads;
if (pool.count()<maxConnectionHandlers) if (pool.count()<maxConnectionHandlers)
{ {
freeHandler=new HttpConnectionHandler(settings,requestHandler,sslConfiguration); freeHandler=new HttpConnectionHandler(settings,requestHandler,sslConfiguration);
@ -66,7 +81,7 @@ HttpConnectionHandler* HttpConnectionHandlerPool::getConnectionHandler()
void HttpConnectionHandlerPool::cleanup() void HttpConnectionHandlerPool::cleanup()
{ {
int maxIdleHandlers=settings->value("minThreads",1).toInt(); int maxIdleHandlers = useQtSettings ? settings->value("minThreads",1).toInt() : listenerSettings->minThreads;
int idleCounter=0; int idleCounter=0;
mutex.lock(); mutex.lock();
foreach(HttpConnectionHandler* handler, pool) foreach(HttpConnectionHandler* handler, pool)
@ -89,8 +104,8 @@ void HttpConnectionHandlerPool::cleanup()
void HttpConnectionHandlerPool::loadSslConfig() void HttpConnectionHandlerPool::loadSslConfig()
{ {
// If certificate and key files are configured, then load them // If certificate and key files are configured, then load them
QString sslKeyFileName=settings->value("sslKeyFile","").toString(); QString sslKeyFileName = useQtSettings ? settings->value("sslKeyFile","").toString() : listenerSettings->sslKeyFile;
QString sslCertFileName=settings->value("sslCertFile","").toString(); QString sslCertFileName = useQtSettings ? settings->value("sslCertFile","").toString() : listenerSettings->sslCertFile;
if (!sslKeyFileName.isEmpty() && !sslCertFileName.isEmpty()) if (!sslKeyFileName.isEmpty() && !sslCertFileName.isEmpty())
{ {
#ifdef QT_NO_OPENSSL #ifdef QT_NO_OPENSSL

View File

@ -45,6 +45,8 @@ namespace qtwebapp {
@see HttpRequest for description of config settings maxRequestSize and maxMultiPartSize @see HttpRequest for description of config settings maxRequestSize and maxMultiPartSize
*/ */
class HttpListenerSettings;
class DECLSPEC HttpConnectionHandlerPool : public QObject { class DECLSPEC HttpConnectionHandlerPool : public QObject {
Q_OBJECT Q_OBJECT
Q_DISABLE_COPY(HttpConnectionHandlerPool) Q_DISABLE_COPY(HttpConnectionHandlerPool)
@ -57,6 +59,7 @@ public:
@warning The requestMapper gets deleted by the destructor of this pool @warning The requestMapper gets deleted by the destructor of this pool
*/ */
HttpConnectionHandlerPool(QSettings* settings, HttpRequestHandler* requestHandler); HttpConnectionHandlerPool(QSettings* settings, HttpRequestHandler* requestHandler);
HttpConnectionHandlerPool(HttpListenerSettings* settings, HttpRequestHandler* requestHandler);
/** Destructor */ /** Destructor */
virtual ~HttpConnectionHandlerPool(); virtual ~HttpConnectionHandlerPool();
@ -68,6 +71,7 @@ private:
/** Settings for this pool */ /** Settings for this pool */
QSettings* settings; QSettings* settings;
HttpListenerSettings* listenerSettings;
/** Will be assigned to each Connectionhandler during their creation */ /** Will be assigned to each Connectionhandler during their creation */
HttpRequestHandler* requestHandler; HttpRequestHandler* requestHandler;
@ -87,6 +91,9 @@ private:
/** Load SSL configuration */ /** Load SSL configuration */
void loadSslConfig(); void loadSslConfig();
/** Settings flag */
bool useQtSettings;
private slots: private slots:
/** Received from the clean-up timer. */ /** Received from the clean-up timer. */