cutelyst  3.7.0
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 "staticsimple_p.h"
6 
7 #include "application.h"
8 #include "request.h"
9 #include "response.h"
10 #include "context.h"
11 
12 #include <QMimeDatabase>
13 #include <QFile>
14 #include <QDir>
15 #include <QDateTime>
16 #include <QLoggingCategory>
17 
18 using namespace Cutelyst;
19 
20 Q_LOGGING_CATEGORY(C_STATICSIMPLE, "cutelyst.plugin.staticsimple", QtWarningMsg)
21 
23  , d_ptr(new StaticSimplePrivate)
24 {
25  Q_D(StaticSimple);
26  d->includePaths.append(parent->config(QLatin1String("root")).toString());
27 }
28 
29 StaticSimple::~StaticSimple()
30 {
31  delete d_ptr;
32 }
33 
34 void StaticSimple::setIncludePaths(const QStringList &paths)
35 {
36  Q_D(StaticSimple);
37  d->includePaths.clear();
38  for (const QString &path : paths) {
39  d->includePaths.append(QDir(path));
40  }
41 }
42 
43 void StaticSimple::setDirs(const QStringList &dirs)
44 {
45  Q_D(StaticSimple);
46  d->dirs = dirs;
47 }
48 
50 {
52  this, &StaticSimple::beforePrepareAction);
53  return true;
54 }
55 
56 void StaticSimple::beforePrepareAction(Context *c, bool *skipMethod)
57 {
58  Q_D(StaticSimple);
59 
60  if (*skipMethod) {
61  return;
62  }
63 
64  const QString path = c->req()->path();
65  const QRegularExpression re = d->re; // Thread-safe
66 
67  for (const QString &dir : d->dirs) {
68  if (path.startsWith(dir)) {
69  if (!locateStaticFile(c, path)) {
70  Response *res = c->response();
71  res->setStatus(Response::NotFound);
72  res->setContentType(QStringLiteral("text/html"));
73  res->setBody(QStringLiteral("File not found: ") + path);
74  }
75 
76  *skipMethod = true;
77  return;
78  }
79  }
80 
81  QRegularExpressionMatch match = re.match(path);
82  if (match.hasMatch() && locateStaticFile(c, path)) {
83  *skipMethod = true;
84  }
85 }
86 
87 bool StaticSimple::locateStaticFile(Context *c, const QString &relPath)
88 {
89  Q_D(const StaticSimple);
90 
91  for (const QDir &includePath : d->includePaths) {
92  QString path = includePath.absoluteFilePath(relPath);
93  QFileInfo fileInfo(path);
94  if (fileInfo.exists()) {
95  Response *res = c->res();
96  const QDateTime currentDateTime = fileInfo.lastModified();
97  if (!c->req()->headers().ifModifiedSince(currentDateTime)) {
98  res->setStatus(Response::NotModified);
99  return true;
100  }
101 
102  QFile *file = new QFile(path);
103  if (file->open(QFile::ReadOnly)) {
104  qCDebug(C_STATICSIMPLE) << "Serving" << path;
105  Headers &headers = res->headers();
106 
107  // set our open file
108  res->setBody(file);
109 
110  static QMimeDatabase db;
111  // use the extension to match to be faster
112  QMimeType mimeType = db.mimeTypeForFile(path, QMimeDatabase::MatchExtension);
113  if (mimeType.isValid()) {
114  headers.setContentType(mimeType.name());
115  }
116  headers.setContentLength(file->size());
117 
118  headers.setLastModified(currentDateTime);
119  // Tell Firefox & friends its OK to cache, even over SSL
120  headers.setHeader(QStringLiteral("CACHE_CONTROL"), QStringLiteral("public"));
121 
122  return true;
123  }
124 
125  qCWarning(C_STATICSIMPLE) << "Could not serve" << path << file->errorString();
126  return false;
127  }
128  }
129 
130  qCWarning(C_STATICSIMPLE) << "File not found" << relPath;
131  return false;
132 }
133 
134 #include "moc_staticsimple.cpp"
The Cutelyst Application.
Definition: application.h:43
void beforePrepareAction(Cutelyst::Context *c, bool *skipMethod)
The Cutelyst Context.
Definition: context.h:39
Response * res() const noexcept
Definition: context.cpp:103
Response * response() const noexcept
Definition: context.cpp:97
void setLastModified(const QString &value)
Definition: headers.cpp:270
void setContentLength(qint64 value)
Definition: headers.cpp:168
void setContentType(const QString &contentType)
Definition: headers.cpp:68
QString ifModifiedSince() const
Definition: headers.cpp:203
void setHeader(const QString &field, const QString &value)
Definition: headers.cpp:400
Headers headers() const noexcept
Definition: request.cpp:308
void setStatus(quint16 status) noexcept
Definition: response.cpp:72
Headers & headers() noexcept
void setBody(QIODevice *body)
Definition: response.cpp:101
void setContentType(const QString &type)
Definition: response.h:217
void setDirs(const QStringList &dirs)
virtual bool setup(Application *app) override
void setIncludePaths(const QStringList &paths)
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:8