From 9a77311b2a481214c8bb76ae02a991abd6796efb Mon Sep 17 00:00:00 2001 From: f4exb Date: Mon, 13 Nov 2017 01:01:15 +0100 Subject: [PATCH] HttpServer: use settings structures in place of QSettings --- httpserver/CMakeLists.txt | 3 ++ httpserver/httpdocrootsettings.h | 39 ++++++++++++++++++++++++ httpserver/httplistenersettings.h | 47 +++++++++++++++++++++++++++++ httpserver/httpserver.pro | 6 +++- httpserver/httpsessionssettings.h | 40 ++++++++++++++++++++++++ httpserver/staticfilecontroller.cpp | 24 ++++++++++++++- httpserver/staticfilecontroller.h | 8 +++++ 7 files changed, 165 insertions(+), 2 deletions(-) create mode 100644 httpserver/httpdocrootsettings.h create mode 100644 httpserver/httplistenersettings.h create mode 100644 httpserver/httpsessionssettings.h diff --git a/httpserver/CMakeLists.txt b/httpserver/CMakeLists.txt index 739565246..31d74f277 100644 --- a/httpserver/CMakeLists.txt +++ b/httpserver/CMakeLists.txt @@ -26,6 +26,9 @@ set(httpserver_HEADERS httpsession.h httpsessionstore.h staticfilecontroller.h + httplistenersettings.h + httpdocrootsettings.h + httpsessionssettings.h ) include_directories( diff --git a/httpserver/httpdocrootsettings.h b/httpserver/httpdocrootsettings.h new file mode 100644 index 000000000..3946f6b19 --- /dev/null +++ b/httpserver/httpdocrootsettings.h @@ -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_ */ diff --git a/httpserver/httplistenersettings.h b/httpserver/httplistenersettings.h new file mode 100644 index 000000000..378da1c66 --- /dev/null +++ b/httpserver/httplistenersettings.h @@ -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_ */ diff --git a/httpserver/httpserver.pro b/httpserver/httpserver.pro index b244633a0..33e3b89a4 100644 --- a/httpserver/httpserver.pro +++ b/httpserver/httpserver.pro @@ -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 \ diff --git a/httpserver/httpsessionssettings.h b/httpserver/httpsessionssettings.h new file mode 100644 index 000000000..3240bc5b4 --- /dev/null +++ b/httpserver/httpsessionssettings.h @@ -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_ */ diff --git a/httpserver/staticfilecontroller.cpp b/httpserver/staticfilecontroller.cpp index 3bbdefc47..1f80aa06f 100644 --- a/httpserver/staticfilecontroller.cpp +++ b/httpserver/staticfilecontroller.cpp @@ -4,6 +4,8 @@ */ #include "staticfilecontroller.h" +#include "httpdocrootsettings.h" + #include #include #include @@ -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) { diff --git a/httpserver/staticfilecontroller.h b/httpserver/staticfilecontroller.h index 27a07e909..950ae3ac0 100644 --- a/httpserver/staticfilecontroller.h +++ b/httpserver/staticfilecontroller.h @@ -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; };