5#include "application.h"
10#include "staticsimple_p.h"
15#include <QLoggingCategory>
16#include <QMimeDatabase>
21Q_LOGGING_CATEGORY(C_STATICSIMPLE,
"cutelyst.plugin.staticsimple", QtWarningMsg)
25 , d_ptr(new StaticSimplePrivate)
28 d->includePaths.append(
parent->config(u
"root"_s).toString());
33 , d_ptr(new StaticSimplePrivate)
36 d->includePaths.append(
parent->config(u
"root"_s).toString());
37 d->defaultConfig = defaultConfig;
48 d->includePaths.clear();
49 for (
const QString &path : paths) {
50 d->includePaths.append(
QDir(path));
63 d->serveDirsOnly = dirsOnly;
70 const QVariantMap config = app->
engine()->
config(u
"Cutelyst_StaticSimple_Plugin"_s);
73 config.value(u
"log_failed_ip"_s, d->defaultConfig.value(u
"log_failed_ip"_s,
false))
80void StaticSimple::beforePrepareAction(
Context *c,
bool *skipMethod)
const
91 bool found = std::ranges::any_of(d->dirs, [&](
const QString &dir) {
92 if (path.startsWith(dir)) {
93 if (!locateStaticFile(c, path)) {
94 Response *res = c->response();
95 res->setStatus(Response::NotFound);
96 res->setContentType(
"text/html"_ba);
97 res->setBody(
"File not found: " + path.toUtf8());
109 if (d->serveDirsOnly) {
113 QRegularExpressionMatch match = re.
match(path);
114 if (match.
hasMatch() && locateStaticFile(c, path)) {
119bool StaticSimple::locateStaticFile(
Context *c,
const QString &relPath)
const
123 for (
const QDir &includePath : d->includePaths) {
125 QFileInfo fileInfo(path);
126 if (fileInfo.exists()) {
127 Response *res = c->
res();
128 const QDateTime currentDateTime = fileInfo.lastModified();
136 auto file =
new QFile(path);
138 qCDebug(C_STATICSIMPLE) <<
"Serving" << path;
139 Headers &headers = res->
headers();
144 static QMimeDatabase db;
153 headers.
setHeader(
"Cache-Control"_ba,
"public"_ba);
158 qCWarning(C_STATICSIMPLE) <<
"Could not serve" << path << file->errorString();
163 if (C_STATICSIMPLE().isWarningEnabled()) {
164 if (d->logFailedIp) {
165 qCWarning(C_STATICSIMPLE).nospace().noquote()
166 <<
"File not found: \"" << relPath <<
'"' <<
" [client "
169 qCWarning(C_STATICSIMPLE).nospace().noquote() <<
"File not found: \"" << relPath <<
'"';
176#include "moc_staticsimple.cpp"
The Cutelyst application.
Engine * engine() const noexcept
void beforePrepareAction(Cutelyst::Context *c, bool *skipMethod)
Response * res() const noexcept
QVariantMap config(const QString &entity) const
Plugin(Application *parent)
QString addressString() const
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) &&