cutelyst  5.0.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
staticsimple.cpp
1 /*
2  * SPDX-FileCopyrightText: (C) 2014-2022 Daniel Nicoletti <dantti12@gmail.com>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 #include "application.h"
6 #include "context.h"
7 #include "request.h"
8 #include "response.h"
9 #include "staticsimple_p.h"
10 
11 #include <QDateTime>
12 #include <QDir>
13 #include <QFile>
14 #include <QLoggingCategory>
15 #include <QMimeDatabase>
16 
17 using namespace Cutelyst;
18 using namespace Qt::Literals::StringLiterals;
19 
20 Q_LOGGING_CATEGORY(C_STATICSIMPLE, "cutelyst.plugin.staticsimple", QtWarningMsg)
21 
23  : Plugin(parent)
24  , d_ptr(new StaticSimplePrivate)
25 {
26  Q_D(StaticSimple);
27  d->includePaths.append(parent->config(u"root"_s).toString());
28 }
29 
31 {
32  delete d_ptr;
33 }
34 
36 {
37  Q_D(StaticSimple);
38  d->includePaths.clear();
39  for (const QString &path : paths) {
40  d->includePaths.append(QDir(path));
41  }
42 }
43 
45 {
46  Q_D(StaticSimple);
47  d->dirs = dirs;
48 }
49 
50 void StaticSimple::setServeDirsOnly(bool dirsOnly)
51 {
52  Q_D(StaticSimple);
53  d->serveDirsOnly = dirsOnly;
54 }
55 
57 {
58  connect(app, &Application::beforePrepareAction, this, &StaticSimple::beforePrepareAction);
59  return true;
60 }
61 
62 void StaticSimple::beforePrepareAction(Context *c, bool *skipMethod) const
63 {
64  Q_D(const StaticSimple);
65 
66  if (*skipMethod) {
67  return;
68  }
69 
70  const QString path = c->req()->path().mid(1);
71  const QRegularExpression re = d->re; // Thread-safe
72 
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());
80  }
81  return true;
82  }
83  return false;
84  });
85 
86  if (found) {
87  *skipMethod = true;
88  return;
89  }
90 
91  if (d->serveDirsOnly) {
92  return;
93  }
94 
95  QRegularExpressionMatch match = re.match(path);
96  if (match.hasMatch() && locateStaticFile(c, path)) {
97  *skipMethod = true;
98  }
99 }
100 
101 bool StaticSimple::locateStaticFile(Context *c, const QString &relPath) const
102 {
103  Q_D(const StaticSimple);
104 
105  for (const QDir &includePath : d->includePaths) {
106  QString path = includePath.absoluteFilePath(relPath);
107  QFileInfo fileInfo(path);
108  if (fileInfo.exists()) {
109  Response *res = c->res();
110  const QDateTime currentDateTime = fileInfo.lastModified();
111  if (!c->req()->headers().ifModifiedSince(currentDateTime)) {
112  res->setStatus(Response::NotModified);
113  return true;
114  }
115 
116  // Response::setBody() will take the ownership
117  // NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
118  auto file = new QFile(path);
119  if (file->open(QFile::ReadOnly)) {
120  qCDebug(C_STATICSIMPLE) << "Serving" << path;
121  Headers &headers = res->headers();
122 
123  // set our open file
124  res->setBody(file);
125 
126  static QMimeDatabase db;
127  // use the extension to match to be faster
129  if (mimeType.isValid()) {
130  headers.setContentType(mimeType.name().toLatin1());
131  }
132 
133  headers.setLastModified(currentDateTime);
134  // Tell Firefox & friends its OK to cache, even over SSL
135  headers.setHeader("Cache-Control"_ba, "public"_ba);
136 
137  return true;
138  }
139 
140  qCWarning(C_STATICSIMPLE) << "Could not serve" << path << file->errorString();
141  return false;
142  }
143  }
144 
145  qCWarning(C_STATICSIMPLE) << "File not found" << relPath;
146  return false;
147 }
148 
149 #include "moc_staticsimple.cpp"
void setDirs(const QStringList &dirs)
Headers & headers() noexcept
Response * res() const noexcept
Definition: context.cpp:104
Container for HTTP headers.
Definition: headers.h:23
void setLastModified(const QByteArray &value)
Definition: headers.cpp:298
Request req
Definition: context.h:67
virtual ~StaticSimple() override
A Cutelyst response.
Definition: response.h:28
The Cutelyst Context.
Definition: context.h:42
void setContentType(const QByteArray &contentType)
Definition: headers.cpp:103
Headers headers() const noexcept
Definition: request.cpp:312
void beforePrepareAction(Cutelyst::Context *c, bool *skipMethod)
Serve static files directly from your application.
Definition: staticsimple.h:61
virtual bool setup(Application *app) override
bool hasMatch() const const
The Cutelyst namespace holds all public Cutelyst API.
QMimeType mimeTypeForFile(const QFileInfo &fileInfo, MatchMode mode) const const
bool isValid() const const
QByteArray ifModifiedSince() const noexcept
Definition: headers.cpp:232
void setIncludePaths(const QStringList &paths)
QString mid(qsizetype position, qsizetype n) const const
QRegularExpressionMatch match(QStringView subjectView, qsizetype offset, MatchType matchType, MatchOptions matchOptions) const const
QString absoluteFilePath(const QString &fileName) const const
void setServeDirsOnly(bool dirsOnly)
Base class for Cutelyst Plugins.
Definition: plugin.h:24
The Cutelyst application.
Definition: application.h:72
void setBody(QIODevice *body)
Definition: response.cpp:105
void setStatus(quint16 status) noexcept
Definition: response.cpp:74
void setHeader(const QByteArray &key, const QByteArray &value)
Definition: headers.cpp:466