HttpServer: use settings structures in place of QSettings

This commit is contained in:
f4exb 2017-11-13 01:01:15 +01:00
parent bc793b4095
commit 9a77311b2a
7 changed files with 165 additions and 2 deletions

View File

@ -26,6 +26,9 @@ set(httpserver_HEADERS
httpsession.h
httpsessionstore.h
staticfilecontroller.h
httplistenersettings.h
httpdocrootsettings.h
httpsessionssettings.h
)
include_directories(

View File

@ -0,0 +1,39 @@
/*
* httpdocrootsettings.h
*
* Created on: Nov 13, 2017
* Author: f4exb
*/
#ifndef HTTPSERVER_HTTPDOCROOTSETTINGS_H_
#define HTTPSERVER_HTTPDOCROOTSETTINGS_H_
namespace qtwebapp {
struct HttpDocrootSettings
{
QString path;
QString encoding;
int maxAge;
int cacheTime;
int cacheSize;
int maxCachedFileSize;
HttpDocrootSettings() {
resetToDefaults();
}
void resetToDefaults()
{
path = ".";
encoding = "UTF-8";
maxAge = 60000;
cacheTime = 60000;
cacheSize = 1000000;
maxCachedFileSize = 65536;
}
};
} // end of namespace
#endif /* HTTPSERVER_HTTPDOCROOTSETTINGS_H_ */

View File

@ -0,0 +1,47 @@
/*
* httplistenersettings.h
*
* Created on: Nov 13, 2017
* Author: f4exb
*/
#ifndef HTTPSERVER_HTTPLISTENERSETTINGS_H_
#define HTTPSERVER_HTTPLISTENERSETTINGS_H_
namespace qtwebapp {
struct HttpListenerSettings
{
QString host;
int port;
int minThreads;
int maxThreads;
int cleanupInterval;
int readTimeout;
QString sslKeyFile;
QString sslCertFile;
int maxRequestSize;
int maxMultiPartSize;
HttpListenerSettings() {
resetToDefaults();
}
void resetToDefaults()
{
host = "192.168.0.100";
port = 8080;
minThreads = 1;
maxThreads = 100;
cleanupInterval = 1000;
readTimeout = 10000;
sslKeyFile = "";
sslCertFile = "";
maxRequestSize = 16000;
maxMultiPartSize = 1000000;
}
};
} // end of namespace
#endif /* HTTPSERVER_HTTPLISTENERSETTINGS_H_ */

View File

@ -30,7 +30,11 @@ HEADERS += $$PWD/httpglobal.h \
$$PWD/httprequesthandler.h \
$$PWD/httpsession.h \
$$PWD/httpsessionstore.h \
$$PWD/staticfilecontroller.h
$$PWD/staticfilecontroller.h \
$$PWD/httplistenersettings.h \
$$PWD/httpdocrootsettings.h \
$$PWD/httpsessionssettings.h
SOURCES += $$PWD/httpglobal.cpp \
$$PWD/httplistener.cpp \

View File

@ -0,0 +1,40 @@
/*
* httpsessionssettings.h
*
* Created on: Nov 13, 2017
* Author: f4exb
*/
#ifndef HTTPSERVER_HTTPSESSIONSSETTINGS_H_
#define HTTPSERVER_HTTPSESSIONSSETTINGS_H_
namespace qtwebapp {
struct HttpSessionsSettings
{
int expirationTime;
QString cookieName;
QString cookiePath;
QString cookieComment;
QString cookieDomain;
HttpSessionsSettings() {
resetToDefaults();
}
void resetToDefaults()
{
expirationTime = 3600000;
cookieName = "sessionid";
cookiePath = "";
cookieComment = "";
cookieDomain = "";
}
};
} // end of namespace
#endif /* HTTPSERVER_HTTPSESSIONSSETTINGS_H_ */

View File

@ -4,6 +4,8 @@
*/
#include "staticfilecontroller.h"
#include "httpdocrootsettings.h"
#include <QFileInfo>
#include <QDir>
#include <QDateTime>
@ -11,7 +13,7 @@
using namespace qtwebapp;
StaticFileController::StaticFileController(QSettings* settings, QObject* parent)
:HttpRequestHandler(parent)
:HttpRequestHandler(parent), useQtSettings(true)
{
maxAge=settings->value("maxAge","60000").toInt();
encoding=settings->value("encoding","UTF-8").toString();
@ -36,6 +38,26 @@ StaticFileController::StaticFileController(QSettings* settings, QObject* parent)
qDebug("StaticFileController: cache timeout=%i, size=%i",cacheTimeout,cache.maxCost());
}
StaticFileController::StaticFileController(HttpDocrootSettings* settings, QObject* parent)
:HttpRequestHandler(parent), useQtSettings(false)
{
maxAge=settings->maxAge;
encoding=settings->encoding;
docroot=settings->path;
if(!(docroot.startsWith(":/") || docroot.startsWith("qrc://")))
{
// Convert relative path to absolute, based on the directory of the config file.
if (QDir::isRelativePath(docroot))
{
docroot = QFileInfo(QDir::currentPath(), docroot).absoluteFilePath();
}
}
qDebug("StaticFileController: docroot=%s, encoding=%s, maxAge=%i",qPrintable(docroot),qPrintable(encoding),maxAge);
maxCachedFileSize=settings->maxCachedFileSize;
cache.setMaxCost(settings->cacheSize);
cacheTimeout=settings->cacheTime;
qDebug("StaticFileController: cache timeout=%i, size=%i",cacheTimeout,cache.maxCost());
}
void StaticFileController::service(HttpRequest& request, HttpResponse& response)
{

View File

@ -42,6 +42,8 @@ namespace qtwebapp {
received a related HTTP request.
*/
class HttpDocrootSettings;
class DECLSPEC StaticFileController : public HttpRequestHandler {
Q_OBJECT
Q_DISABLE_COPY(StaticFileController)
@ -50,6 +52,9 @@ public:
/** Constructor */
StaticFileController(QSettings* settings, QObject* parent = NULL);
/** Constructor */
StaticFileController(HttpDocrootSettings* settings, QObject* parent = NULL);
/** Generates the response */
void service(HttpRequest& request, HttpResponse& response);
@ -82,6 +87,9 @@ private:
/** Used to synchronize cache access for threads */
QMutex mutex;
/** Settings flag */
bool useQtSettings;
/** Set a content-type header in the response depending on the ending of the filename */
void setContentType(QString file, HttpResponse& response) const;
};