diff --git a/httpserver/httpconnectionhandler.cpp b/httpserver/httpconnectionhandler.cpp index d9d8af5b3..a83b37d70 100644 --- a/httpserver/httpconnectionhandler.cpp +++ b/httpserver/httpconnectionhandler.cpp @@ -5,15 +5,47 @@ #include "httpconnectionhandler.h" #include "httpresponse.h" +#include "httplistenersettings.h" using namespace qtwebapp; HttpConnectionHandler::HttpConnectionHandler(QSettings* settings, HttpRequestHandler* requestHandler, QSslConfiguration* sslConfiguration) - : QThread() + : QThread(), useQtSettings(true) { Q_ASSERT(settings!=0); Q_ASSERT(requestHandler!=0); 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->sslConfiguration=sslConfiguration; currentRequest=0; @@ -109,7 +141,7 @@ void HttpConnectionHandler::handleConnection(tSocketDescriptor socketDescriptor) #endif // 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); // delete previous request delete currentRequest; @@ -173,7 +205,7 @@ void HttpConnectionHandler::read() { // Restart timer for read timeout, otherwise it would // 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); } } @@ -267,7 +299,7 @@ void HttpConnectionHandler::read() else { // 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); } delete currentRequest; diff --git a/httpserver/httpconnectionhandler.h b/httpserver/httpconnectionhandler.h index ab0ea7c81..52c6f9f1c 100644 --- a/httpserver/httpconnectionhandler.h +++ b/httpserver/httpconnectionhandler.h @@ -19,6 +19,8 @@ namespace qtwebapp { +class HttpListenerSettings; + /** Alias type definition, for compatibility to different Qt versions */ #if QT_VERSION >= 0x050000 typedef qintptr tSocketDescriptor; @@ -59,6 +61,7 @@ public: @param sslConfiguration SSL (HTTPS) will be used if not NULL */ HttpConnectionHandler(QSettings* settings, HttpRequestHandler* requestHandler, QSslConfiguration* sslConfiguration=NULL); + HttpConnectionHandler(HttpListenerSettings* settings, HttpRequestHandler* requestHandler, QSslConfiguration* sslConfiguration=NULL); /** Destructor */ virtual ~HttpConnectionHandler(); @@ -73,6 +76,7 @@ private: /** Configuration settings */ QSettings* settings; + HttpListenerSettings* listenerSettings; /** TCP socket of the current connection */ QTcpSocket* socket; @@ -98,6 +102,9 @@ private: /** Create SSL or TCP socket */ void createSocket(); + /** Settings flag */ + bool useQtSettings; + public slots: /** diff --git a/httpserver/httpconnectionhandlerpool.cpp b/httpserver/httpconnectionhandlerpool.cpp index c199fc330..5e10ec902 100644 --- a/httpserver/httpconnectionhandlerpool.cpp +++ b/httpserver/httpconnectionhandlerpool.cpp @@ -6,14 +6,16 @@ #endif #include #include "httpconnectionhandlerpool.h" +#include "httplistenersettings.h" using namespace qtwebapp; HttpConnectionHandlerPool::HttpConnectionHandlerPool(QSettings* settings, HttpRequestHandler* requestHandler) - : QObject() + : QObject(), useQtSettings(true) { Q_ASSERT(settings!=0); this->settings=settings; + this->listenerSettings=0; this->requestHandler=requestHandler; this->sslConfiguration=NULL; loadSslConfig(); @@ -21,6 +23,19 @@ HttpConnectionHandlerPool::HttpConnectionHandlerPool(QSettings* settings, HttpRe 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() { @@ -51,7 +66,7 @@ HttpConnectionHandler* HttpConnectionHandlerPool::getConnectionHandler() // create a new handler, if necessary if (!freeHandler) { - int maxConnectionHandlers=settings->value("maxThreads",100).toInt(); + int maxConnectionHandlers = useQtSettings ? settings->value("maxThreads",100).toInt() : listenerSettings->maxThreads; if (pool.count()value("minThreads",1).toInt(); + int maxIdleHandlers = useQtSettings ? settings->value("minThreads",1).toInt() : listenerSettings->minThreads; int idleCounter=0; mutex.lock(); foreach(HttpConnectionHandler* handler, pool) @@ -89,8 +104,8 @@ void HttpConnectionHandlerPool::cleanup() void HttpConnectionHandlerPool::loadSslConfig() { // If certificate and key files are configured, then load them - QString sslKeyFileName=settings->value("sslKeyFile","").toString(); - QString sslCertFileName=settings->value("sslCertFile","").toString(); + QString sslKeyFileName = useQtSettings ? settings->value("sslKeyFile","").toString() : listenerSettings->sslKeyFile; + QString sslCertFileName = useQtSettings ? settings->value("sslCertFile","").toString() : listenerSettings->sslCertFile; if (!sslKeyFileName.isEmpty() && !sslCertFileName.isEmpty()) { #ifdef QT_NO_OPENSSL diff --git a/httpserver/httpconnectionhandlerpool.h b/httpserver/httpconnectionhandlerpool.h index 320ab97fd..6c695a924 100644 --- a/httpserver/httpconnectionhandlerpool.h +++ b/httpserver/httpconnectionhandlerpool.h @@ -45,6 +45,8 @@ namespace qtwebapp { @see HttpRequest for description of config settings maxRequestSize and maxMultiPartSize */ +class HttpListenerSettings; + class DECLSPEC HttpConnectionHandlerPool : public QObject { Q_OBJECT Q_DISABLE_COPY(HttpConnectionHandlerPool) @@ -57,6 +59,7 @@ public: @warning The requestMapper gets deleted by the destructor of this pool */ HttpConnectionHandlerPool(QSettings* settings, HttpRequestHandler* requestHandler); + HttpConnectionHandlerPool(HttpListenerSettings* settings, HttpRequestHandler* requestHandler); /** Destructor */ virtual ~HttpConnectionHandlerPool(); @@ -68,6 +71,7 @@ private: /** Settings for this pool */ QSettings* settings; + HttpListenerSettings* listenerSettings; /** Will be assigned to each Connectionhandler during their creation */ HttpRequestHandler* requestHandler; @@ -87,6 +91,9 @@ private: /** Load SSL configuration */ void loadSslConfig(); + /** Settings flag */ + bool useQtSettings; + private slots: /** Received from the clean-up timer. */