5 #include "staticsimple_p.h"
7 #include "application.h"
12 #include <QMimeDatabase>
16 #include <QLoggingCategory>
20 Q_LOGGING_CATEGORY(C_STATICSIMPLE,
"cutelyst.plugin.staticsimple", QtWarningMsg)
23 , d_ptr(new StaticSimplePrivate)
26 d->includePaths.append(parent->config(QLatin1String(
"root")).toString());
29 StaticSimple::~StaticSimple()
37 d->includePaths.clear();
38 for (
const QString &path : paths) {
39 d->includePaths.append(QDir(path));
52 this, &StaticSimple::beforePrepareAction);
56 void StaticSimple::beforePrepareAction(
Context *c,
bool *skipMethod)
64 const QString path = c->req()->path();
65 const QRegularExpression re = d->re;
67 for (
const QString &dir : d->dirs) {
68 if (path.startsWith(dir)) {
69 if (!locateStaticFile(c, path)) {
73 res->
setBody(QStringLiteral(
"File not found: ") + path);
81 QRegularExpressionMatch match = re.match(path);
82 if (match.hasMatch() && locateStaticFile(c, path)) {
87 bool StaticSimple::locateStaticFile(
Context *c,
const QString &relPath)
91 for (
const QDir &includePath : d->includePaths) {
92 QString path = includePath.absoluteFilePath(relPath);
93 QFileInfo fileInfo(path);
94 if (fileInfo.exists()) {
96 const QDateTime currentDateTime = fileInfo.lastModified();
102 QFile *file =
new QFile(path);
103 if (file->open(QFile::ReadOnly)) {
104 qCDebug(C_STATICSIMPLE) <<
"Serving" << path;
110 static QMimeDatabase db;
112 QMimeType mimeType = db.mimeTypeForFile(path, QMimeDatabase::MatchExtension);
113 if (mimeType.isValid()) {
120 headers.
setHeader(QStringLiteral(
"CACHE_CONTROL"), QStringLiteral(
"public"));
125 qCWarning(C_STATICSIMPLE) <<
"Could not serve" << path << file->errorString();
130 qCWarning(C_STATICSIMPLE) <<
"File not found" << relPath;
134 #include "moc_staticsimple.cpp"
The Cutelyst Application.
void beforePrepareAction(Cutelyst::Context *c, bool *skipMethod)
Response * res() const noexcept
Response * response() const noexcept
Headers headers() const noexcept
void setStatus(quint16 status) noexcept
Headers & headers() noexcept
void setBody(QIODevice *body)
void setContentType(const QString &type)
void setDirs(const QStringList &dirs)
virtual bool setup(Application *app) override
void setIncludePaths(const QStringList &paths)
The Cutelyst namespace holds all public Cutelyst API.