5#include "application.h"
9#include "staticsimple_p.h"
14#include <QLoggingCategory>
15#include <QMimeDatabase>
20Q_LOGGING_CATEGORY(C_STATICSIMPLE,
"cutelyst.plugin.staticsimple", QtWarningMsg)
24 , d_ptr(new StaticSimplePrivate)
27 d->includePaths.append(
parent->config(u
"root"_s).toString());
38 d->includePaths.clear();
39 for (
const QString &path : paths) {
40 d->includePaths.append(
QDir(path));
53 d->serveDirsOnly = dirsOnly;
62void StaticSimple::beforePrepareAction(
Context *c,
bool *skipMethod)
const
73 bool found = std::ranges::any_of(d->dirs, [&](
const QString &dir) {
74 if (path.startsWith(dir)) {
75 if (!locateStaticFile(c, path)) {
76 Response *res = c->response();
77 res->setStatus(Response::NotFound);
78 res->setContentType(
"text/html"_ba);
79 res->setBody(
"File not found: " + path.toUtf8());
91 if (d->serveDirsOnly) {
95 QRegularExpressionMatch match = re.
match(path);
96 if (match.
hasMatch() && locateStaticFile(c, path)) {
101bool StaticSimple::locateStaticFile(
Context *c,
const QString &relPath)
const
105 for (
const QDir &includePath : d->includePaths) {
107 QFileInfo fileInfo(path);
108 if (fileInfo.exists()) {
109 Response *res = c->
res();
110 const QDateTime currentDateTime = fileInfo.lastModified();
118 auto file =
new QFile(path);
120 qCDebug(C_STATICSIMPLE) <<
"Serving" << path;
121 Headers &headers = res->
headers();
126 static QMimeDatabase db;
135 headers.
setHeader(
"Cache-Control"_ba,
"public"_ba);
140 qCWarning(C_STATICSIMPLE) <<
"Could not serve" << path << file->errorString();
145 qCWarning(C_STATICSIMPLE) <<
"File not found" << relPath;
149#include "moc_staticsimple.cpp"
The Cutelyst application.
void beforePrepareAction(Cutelyst::Context *c, bool *skipMethod)
Response * res() const noexcept
Plugin(Application *parent)
Headers headers() const noexcept
Headers & headers() noexcept
void setStatus(quint16 status) noexcept
void setBody(QIODevice *body)
Serve static files directly from your application.
void setServeDirsOnly(bool dirsOnly)
void setDirs(const QStringList &dirs)
virtual bool setup(Application *app) override
StaticSimple(Application *parent)
void setIncludePaths(const QStringList &paths)
virtual ~StaticSimple() override
The Cutelyst namespace holds all public Cutelyst API.
QString absoluteFilePath(const QString &fileName) 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
QString mid(qsizetype position, qsizetype n) &&