1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-28 18:58:48 -05:00

Http server: fixed parameters passing

This commit is contained in:
f4exb 2017-11-18 10:15:29 +01:00
parent 7305f2fb00
commit 2b65fb2d62
8 changed files with 57 additions and 58 deletions

View File

@ -11,12 +11,13 @@ using namespace qtwebapp;
HttpConnectionHandler::HttpConnectionHandler(QSettings* settings, HttpRequestHandler* requestHandler, QSslConfiguration* sslConfiguration) HttpConnectionHandler::HttpConnectionHandler(QSettings* settings, HttpRequestHandler* requestHandler, QSslConfiguration* sslConfiguration)
: QThread(), useQtSettings(true) : 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->requestHandler=requestHandler; this->listenerSettings = 0;
this->sslConfiguration=sslConfiguration; this->requestHandler = requestHandler;
currentRequest=0; this->sslConfiguration = sslConfiguration;
currentRequest = 0;
busy=false; busy=false;
// Create TCP or SSL socket // Create TCP or SSL socket
@ -37,16 +38,17 @@ HttpConnectionHandler::HttpConnectionHandler(QSettings* settings, HttpRequestHan
this->start(); this->start();
} }
HttpConnectionHandler::HttpConnectionHandler(const HttpListenerSettings& settings, HttpRequestHandler* requestHandler, QSslConfiguration* sslConfiguration) HttpConnectionHandler::HttpConnectionHandler(const HttpListenerSettings* settings, HttpRequestHandler* requestHandler, QSslConfiguration* sslConfiguration)
: QThread(), useQtSettings(false) : QThread(), useQtSettings(false)
{ {
Q_ASSERT(requestHandler!=0); Q_ASSERT(settings != 0);
this->settings=0; Q_ASSERT(requestHandler != 0);
listenerSettings=settings; this->settings = 0;
this->requestHandler=requestHandler; this->listenerSettings = settings;
this->sslConfiguration=sslConfiguration; this->requestHandler = requestHandler;
currentRequest=0; this->sslConfiguration = sslConfiguration;
busy=false; currentRequest = 0;
busy = false;
// Create TCP or SSL socket // Create TCP or SSL socket
createSocket(); createSocket();
@ -138,7 +140,7 @@ void HttpConnectionHandler::handleConnection(tSocketDescriptor socketDescriptor)
#endif #endif
// Start timer for read timeout // Start timer for read timeout
int readTimeout = useQtSettings ? settings->value("readTimeout",10000).toInt() : listenerSettings.readTimeout; 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;
@ -191,7 +193,11 @@ void HttpConnectionHandler::read()
// Create new HttpRequest object if necessary // Create new HttpRequest object if necessary
if (!currentRequest) if (!currentRequest)
{ {
currentRequest=new HttpRequest(settings); if (useQtSettings) {
currentRequest = new HttpRequest(settings);
} else {
currentRequest = new HttpRequest(listenerSettings);
}
} }
// Collect data for the request object // Collect data for the request object
@ -202,7 +208,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 = useQtSettings ? settings->value("readTimeout",10000).toInt() : listenerSettings.readTimeout; int readTimeout = useQtSettings ? settings->value("readTimeout",10000).toInt() : listenerSettings->readTimeout;
readTimer.start(readTimeout); readTimer.start(readTimeout);
} }
} }
@ -296,7 +302,7 @@ void HttpConnectionHandler::read()
else else
{ {
// Start timer for next request // Start timer for next request
int readTimeout = useQtSettings ? settings->value("readTimeout",10000).toInt() : listenerSettings.readTimeout; int readTimeout = useQtSettings ? settings->value("readTimeout",10000).toInt() : listenerSettings->readTimeout;
readTimer.start(readTimeout); readTimer.start(readTimeout);
} }
delete currentRequest; delete currentRequest;

View File

@ -67,7 +67,7 @@ public:
@param requestHandler Handler that will process each incoming HTTP request @param requestHandler Handler that will process each incoming HTTP request
@param sslConfiguration SSL (HTTPS) will be used if not NULL @param sslConfiguration SSL (HTTPS) will be used if not NULL
*/ */
HttpConnectionHandler(const HttpListenerSettings& settings, HttpRequestHandler* requestHandler, QSslConfiguration* sslConfiguration=NULL); HttpConnectionHandler(const HttpListenerSettings* settings, HttpRequestHandler* requestHandler, QSslConfiguration* sslConfiguration=NULL);
/** Destructor */ /** Destructor */
virtual ~HttpConnectionHandler(); virtual ~HttpConnectionHandler();
@ -79,16 +79,10 @@ public:
void setBusy(); void setBusy();
/** /**
* Get a listener settings copy * Get a listener settings constant reference. Can be changed on the HttpListener only.
* @return The current listener settings * @return The current listener settings
*/ */
HttpListenerSettings getListenerSettings() const { return listenerSettings; } const HttpListenerSettings *getListenerSettings() const { return listenerSettings; }
/**
* Set new listener settings data
* @param Listener settings to replace current data
*/
void setListenerSettings(const HttpListenerSettings& settings) { listenerSettings = settings; }
private: private:
@ -97,7 +91,7 @@ private:
QSettings* settings; QSettings* settings;
/** Configuration settings */ /** Configuration settings */
HttpListenerSettings listenerSettings; const HttpListenerSettings* listenerSettings;
/** TCP socket of the current connection */ /** TCP socket of the current connection */
QTcpSocket* socket; QTcpSocket* socket;

View File

@ -12,28 +12,29 @@ using namespace qtwebapp;
HttpConnectionHandlerPool::HttpConnectionHandlerPool(QSettings* settings, HttpRequestHandler* requestHandler) HttpConnectionHandlerPool::HttpConnectionHandlerPool(QSettings* settings, HttpRequestHandler* requestHandler)
: QObject(), useQtSettings(true) : QObject(), useQtSettings(true)
{ {
Q_ASSERT(settings!=0); Q_ASSERT(settings != 0);
this->settings=settings; this->settings = settings;
this->requestHandler=requestHandler; this->listenerSettings = 0;
this->sslConfiguration=NULL; this->requestHandler = requestHandler;
this->sslConfiguration = 0;
loadSslConfig(); loadSslConfig();
cleanupTimer.start(settings->value("cleanupInterval",1000).toInt()); cleanupTimer.start(settings->value("cleanupInterval",1000).toInt());
connect(&cleanupTimer, SIGNAL(timeout()), SLOT(cleanup())); connect(&cleanupTimer, SIGNAL(timeout()), SLOT(cleanup()));
} }
HttpConnectionHandlerPool::HttpConnectionHandlerPool(const HttpListenerSettings& settings, HttpRequestHandler* requestHandler) HttpConnectionHandlerPool::HttpConnectionHandlerPool(const HttpListenerSettings* settings, HttpRequestHandler* requestHandler)
: QObject(), useQtSettings(false) : QObject(), useQtSettings(false)
{ {
this->settings=0; Q_ASSERT(listenerSettings != 0);
listenerSettings=settings; this->settings = 0;
this->requestHandler=requestHandler; this->listenerSettings = settings;
this->sslConfiguration=NULL; this->requestHandler = requestHandler;
this->sslConfiguration = 0;
loadSslConfig(); loadSslConfig();
cleanupTimer.start(settings.cleanupInterval); cleanupTimer.start(settings->cleanupInterval);
connect(&cleanupTimer, SIGNAL(timeout()), SLOT(cleanup())); connect(&cleanupTimer, SIGNAL(timeout()), SLOT(cleanup()));
} }
HttpConnectionHandlerPool::~HttpConnectionHandlerPool() HttpConnectionHandlerPool::~HttpConnectionHandlerPool()
{ {
// delete all connection handlers and wait until their threads are closed // delete all connection handlers and wait until their threads are closed
@ -63,7 +64,7 @@ HttpConnectionHandler* HttpConnectionHandlerPool::getConnectionHandler()
// create a new handler, if necessary // create a new handler, if necessary
if (!freeHandler) if (!freeHandler)
{ {
int maxConnectionHandlers = useQtSettings ? settings->value("maxThreads",100).toInt() : listenerSettings.maxThreads; int maxConnectionHandlers = useQtSettings ? settings->value("maxThreads",100).toInt() : listenerSettings->maxThreads;
if (pool.count()<maxConnectionHandlers) if (pool.count()<maxConnectionHandlers)
{ {
if (useQtSettings) { if (useQtSettings) {
@ -83,7 +84,7 @@ HttpConnectionHandler* HttpConnectionHandlerPool::getConnectionHandler()
void HttpConnectionHandlerPool::cleanup() void HttpConnectionHandlerPool::cleanup()
{ {
int maxIdleHandlers = useQtSettings ? settings->value("minThreads",1).toInt() : listenerSettings.minThreads; 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)
@ -106,8 +107,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 = useQtSettings ? settings->value("sslKeyFile","").toString() : listenerSettings.sslKeyFile; QString sslKeyFileName = useQtSettings ? settings->value("sslKeyFile","").toString() : listenerSettings->sslKeyFile;
QString sslCertFileName = useQtSettings ? settings->value("sslCertFile","").toString() : listenerSettings.sslCertFile; 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

@ -65,7 +65,7 @@ public:
@param requestHandler The handler that will process each received HTTP request. @param requestHandler The handler that will process each received HTTP request.
@warning The requestMapper gets deleted by the destructor of this pool @warning The requestMapper gets deleted by the destructor of this pool
*/ */
HttpConnectionHandlerPool(const HttpListenerSettings& settings, HttpRequestHandler* requestHandler); HttpConnectionHandlerPool(const HttpListenerSettings* settings, HttpRequestHandler* requestHandler);
/** Destructor */ /** Destructor */
virtual ~HttpConnectionHandlerPool(); virtual ~HttpConnectionHandlerPool();
@ -74,16 +74,10 @@ public:
HttpConnectionHandler* getConnectionHandler(); HttpConnectionHandler* getConnectionHandler();
/** /**
* Get a listener settings copy * Get a listener settings const reference. Can be changed on the HttpListener only.
* @return The current listener settings * @return The current listener settings
*/ */
HttpListenerSettings getListenerSettings() const { return listenerSettings; } const HttpListenerSettings* getListenerSettings() const { return listenerSettings; }
/**
* Set new listener settings data
* @param Listener settings to replace current data
*/
void setListenerSettings(const HttpListenerSettings& settings) { listenerSettings = settings; }
private: private:
@ -91,7 +85,7 @@ private:
QSettings* settings; QSettings* settings;
/** Settings for this pool as structure*/ /** Settings for this pool as structure*/
HttpListenerSettings listenerSettings; const HttpListenerSettings *listenerSettings;
/** Will be assigned to each Connectionhandler during their creation */ /** Will be assigned to each Connectionhandler during their creation */
HttpRequestHandler* requestHandler; HttpRequestHandler* requestHandler;

View File

@ -54,7 +54,7 @@ void HttpListener::listen()
if (useQtSettings) { if (useQtSettings) {
pool = new HttpConnectionHandlerPool(settings, requestHandler); pool = new HttpConnectionHandlerPool(settings, requestHandler);
} else { } else {
pool = new HttpConnectionHandlerPool(listenerSettings, requestHandler); pool = new HttpConnectionHandlerPool(&listenerSettings, requestHandler);
} }
} }
QString host = useQtSettings ? settings->value("host").toString() : listenerSettings.host; QString host = useQtSettings ? settings->value("host").toString() : listenerSettings.host;

View File

@ -15,6 +15,7 @@ using namespace qtwebapp;
HttpRequest::HttpRequest(QSettings* settings) : HttpRequest::HttpRequest(QSettings* settings) :
useQtSettings(true) useQtSettings(true)
{ {
Q_ASSERT(settings != 0);
status=waitForRequest; status=waitForRequest;
currentSize=0; currentSize=0;
expectedBodySize=0; expectedBodySize=0;
@ -23,14 +24,15 @@ HttpRequest::HttpRequest(QSettings* settings) :
tempFile=0; tempFile=0;
} }
HttpRequest::HttpRequest(const HttpListenerSettings& settings) : HttpRequest::HttpRequest(const HttpListenerSettings* settings) :
useQtSettings(false) useQtSettings(false)
{ {
Q_ASSERT(settings != 0);
status=waitForRequest; status=waitForRequest;
currentSize=0; currentSize=0;
expectedBodySize=0; expectedBodySize=0;
maxSize=settings.maxRequestSize; maxSize=settings->maxRequestSize;
maxMultiPartSize=settings.maxMultiPartSize; maxMultiPartSize=settings->maxMultiPartSize;
tempFile=0; tempFile=0;
} }

View File

@ -55,7 +55,7 @@ public:
Constructor. Constructor.
@param settings Configuration settings as a structure @param settings Configuration settings as a structure
*/ */
HttpRequest(const HttpListenerSettings& settings); HttpRequest(const HttpListenerSettings* settings);
/** /**
Destructor. Destructor.

View File

@ -37,6 +37,8 @@ public:
void stop(); void stop();
void setHostAndPort(const QString& host, uint16_t port); void setHostAndPort(const QString& host, uint16_t port);
const QString& getHost() const { return m_settings.host; }
int getPort() const { return m_settings.port; }
private: private:
WebAPIRequestMapper *m_requestMapper; WebAPIRequestMapper *m_requestMapper;