5#include "application.h"
10#include "staticsimple_p.h"
15#include <QLoggingCategory>
16#include <QMimeDatabase>
20Q_LOGGING_CATEGORY(C_STATICSIMPLE,
"cutelyst.plugin.staticsimple", QtWarningMsg)
24 , d_ptr(new StaticSimplePrivate)
30StaticSimple::~StaticSimple()
38 d->includePaths.clear();
39 for (
const QString &path : paths) {
40 d->includePaths.append(
QDir(path));
54 const QVariantMap config = app->
engine()->
config(QStringLiteral(
"Cutelyst_StaticSimple_Plugin"));
56 d->logFailedIp = config.value(QStringLiteral(
"log_failed_ip"),
false).toBool();
62void StaticSimple::beforePrepareAction(
Context *c,
bool *skipMethod)
70 const QString path = c->req()->path();
73 for (
const QString &dir : d->dirs) {
75 if (!locateStaticFile(c, path)) {
79 res->
setBody(QStringLiteral(
"File not found: ") + path);
87 QRegularExpressionMatch match = re.
match(path);
88 if (match.
hasMatch() && locateStaticFile(c, path)) {
93bool StaticSimple::locateStaticFile(
Context *c,
const QString &relPath)
97 for (
const QDir &includePath : d->includePaths) {
99 QFileInfo fileInfo(path);
100 if (fileInfo.exists()) {
101 Response *res = c->
res();
102 const QDateTime currentDateTime = fileInfo.lastModified();
108 QFile *file =
new QFile(path);
110 qCDebug(C_STATICSIMPLE) <<
"Serving" << path;
111 Headers &headers = res->
headers();
116 static QMimeDatabase db;
126 headers.
setHeader(QStringLiteral(
"CACHE_CONTROL"), QStringLiteral(
"public"));
131 qCWarning(C_STATICSIMPLE) <<
"Could not serve" << path << file->
errorString();
136 if (C_STATICSIMPLE().isWarningEnabled()) {
137 if (d->logFailedIp) {
138 qCWarning(C_STATICSIMPLE).nospace().noquote()
139 <<
"File not found: \"" << relPath <<
'"' <<
" [client "
142 qCWarning(C_STATICSIMPLE).nospace().noquote() <<
"File not found: \"" << relPath <<
'"';
149#include "moc_staticsimple.cpp"
The Cutelyst Application.
Engine * engine() const noexcept
void beforePrepareAction(Cutelyst::Context *c, bool *skipMethod)
Response * res() const noexcept
Response * response() const noexcept
QVariantMap config(const QString &entity) const
user configuration for the application
Plugin(Application *parent)
QString addressString() const
Headers headers() const noexcept
Headers & headers() noexcept
void setStatus(quint16 status) noexcept
void setBody(QIODevice *body)
void setContentType(const QString &type)
void setDirs(const QStringList &dirs)
virtual bool setup(Application *app) override
StaticSimple(Application *parent)
void setIncludePaths(const QStringList &paths)
The Cutelyst namespace holds all public Cutelyst API.
QString absoluteFilePath(const QString &fileName) const const
bool open(FILE *fh, QIODeviceBase::OpenMode mode, QFileDevice::FileHandleFlags handleFlags)
virtual qint64 size() const const override
QString errorString() const const
QMimeType mimeTypeForFile(const QFileInfo &fileInfo, QMimeDatabase::MatchMode mode) const const
bool isValid() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
QRegularExpressionMatch match(QStringView subjectView, qsizetype offset, QRegularExpression::MatchType matchType, QRegularExpression::MatchOptions matchOptions) const const
bool hasMatch() const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const