cutelyst 4.0.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 "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
17using namespace Cutelyst;
18
19Q_LOGGING_CATEGORY(C_STATICSIMPLE, "cutelyst.plugin.staticsimple", QtWarningMsg)
20
22 : Plugin(parent)
23 , d_ptr(new StaticSimplePrivate)
24{
25 Q_D(StaticSimple);
26 d->includePaths.append(parent->config(QLatin1String("root")).toString());
27}
28
29StaticSimple::~StaticSimple()
30{
31 delete d_ptr;
32}
33
34void 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
43void StaticSimple::setDirs(const QStringList &dirs)
44{
45 Q_D(StaticSimple);
46 d->dirs = dirs;
47}
48
50{
51 connect(app, &Application::beforePrepareAction, this, &StaticSimple::beforePrepareAction);
52 return true;
53}
54
55void StaticSimple::beforePrepareAction(Context *c, bool *skipMethod)
56{
57 Q_D(StaticSimple);
58
59 if (*skipMethod) {
60 return;
61 }
62
63 // TODO mid(1) quick fix for path now having leading slash
64 const QString path = c->req()->path().mid(1);
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("text/html"_qba);
73 res->setBody("File not found: " + path.toUtf8());
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
87bool 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().toLatin1());
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("Cache-Control"_qba, "public"_qba);
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:38
Response * res() const noexcept
Definition: context.cpp:102
Response * response() const noexcept
Definition: context.cpp:96
QByteArray ifModifiedSince() const noexcept
Definition: headers.cpp:205
void setContentLength(qint64 value)
Definition: headers.cpp:172
void setLastModified(const QByteArray &value)
Definition: headers.cpp:271
void setContentType(const QByteArray &contentType)
Definition: headers.cpp:76
void setHeader(const QByteArray &key, const QByteArray &value)
Definition: headers.cpp:436
Headers headers() const noexcept
Definition: request.cpp:307
void setContentType(const QByteArray &type)
Definition: response.h:203
void setStatus(quint16 status) noexcept
Definition: response.cpp:72
void setBody(QIODevice *body)
Definition: response.cpp:102
Headers & headers() noexcept
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