Cutelyst  2.13.0
staticsimple.cpp
1 /*
2  * Copyright (C) 2014-2018 Daniel Nicoletti <dantti12@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 #include "staticsimple_p.h"
19 
20 #include "application.h"
21 #include "request.h"
22 #include "response.h"
23 #include "context.h"
24 
25 #include <QMimeDatabase>
26 #include <QFile>
27 #include <QDir>
28 #include <QDateTime>
29 #include <QLoggingCategory>
30 
31 using namespace Cutelyst;
32 
33 Q_LOGGING_CATEGORY(C_STATICSIMPLE, "cutelyst.plugin.staticsimple", QtWarningMsg)
34 
36  , d_ptr(new StaticSimplePrivate)
37 {
38  Q_D(StaticSimple);
39  d->includePaths.append(parent->config(QLatin1String("root")).toString());
40 }
41 
42 StaticSimple::~StaticSimple()
43 {
44  delete d_ptr;
45 }
46 
47 void StaticSimple::setIncludePaths(const QStringList &paths)
48 {
49  Q_D(StaticSimple);
50  d->includePaths.clear();
51  for (const QString &path : paths) {
52  d->includePaths.append(QDir(path));
53  }
54 }
55 
56 void StaticSimple::setDirs(const QStringList &dirs)
57 {
58  Q_D(StaticSimple);
59  d->dirs = dirs;
60 }
61 
63 {
65  this, &StaticSimple::beforePrepareAction);
66  return true;
67 }
68 
69 void StaticSimple::beforePrepareAction(Context *c, bool *skipMethod)
70 {
71  Q_D(StaticSimple);
72 
73  if (*skipMethod) {
74  return;
75  }
76 
77  const QString path = c->req()->path();
78  const QRegularExpression re = d->re; // Thread-safe
79 
80  for (const QString &dir : d->dirs) {
81  if (path.startsWith(dir)) {
82  if (!locateStaticFile(c, path)) {
83  Response *res = c->response();
84  res->setStatus(Response::NotFound);
85  res->setContentType(QStringLiteral("text/html"));
86  res->setBody(QStringLiteral("File not found: ") + path);
87  }
88 
89  *skipMethod = true;
90  return;
91  }
92  }
93 
94  QRegularExpressionMatch match = re.match(path);
95  if (match.hasMatch() && locateStaticFile(c, path)) {
96  *skipMethod = true;
97  }
98 }
99 
100 bool StaticSimple::locateStaticFile(Context *c, const QString &relPath)
101 {
102  Q_D(const StaticSimple);
103 
104  for (const QDir &includePath : d->includePaths) {
105  QString path = includePath.absoluteFilePath(relPath);
106  QFileInfo fileInfo(path);
107  if (fileInfo.exists()) {
108  Response *res = c->res();
109  const QDateTime currentDateTime = fileInfo.lastModified();
110  if (!c->req()->headers().ifModifiedSince(currentDateTime)) {
111  res->setStatus(Response::NotModified);
112  return true;
113  }
114 
115  QFile *file = new QFile(path);
116  if (file->open(QFile::ReadOnly)) {
117  qCDebug(C_STATICSIMPLE) << "Serving" << path;
118  Headers &headers = res->headers();
119 
120  // set our open file
121  res->setBody(file);
122 
123  static QMimeDatabase db;
124  // use the extension to match to be faster
125  QMimeType mimeType = db.mimeTypeForFile(path, QMimeDatabase::MatchExtension);
126  if (mimeType.isValid()) {
127  headers.setContentType(mimeType.name());
128  }
129  headers.setContentLength(file->size());
130 
131  headers.setLastModified(currentDateTime);
132  // Tell Firefox & friends its OK to cache, even over SSL
133  headers.setHeader(QStringLiteral("CACHE_CONTROL"), QStringLiteral("public"));
134 
135  return true;
136  }
137 
138  qCWarning(C_STATICSIMPLE) << "Could not serve" << path << file->errorString();
139  return false;
140  }
141  }
142 
143  qCWarning(C_STATICSIMPLE) << "File not found" << relPath;
144  return false;
145 }
146 
147 #include "moc_staticsimple.cpp"
Cutelyst::Plugin
Definition: plugin.h:30
Cutelyst::StaticSimple::setDirs
void setDirs(const QStringList &dirs)
Definition: staticsimple.cpp:56
Cutelyst::Headers::setLastModified
void setLastModified(const QString &value)
Definition: headers.cpp:283
Cutelyst::Response::setStatus
void setStatus(quint16 status)
Definition: response.cpp:85
Cutelyst::Application
The Cutelyst Application.
Definition: application.h:55
Cutelyst::Headers::setContentLength
void setContentLength(qint64 value)
Definition: headers.cpp:181
Cutelyst::StaticSimple::setIncludePaths
void setIncludePaths(const QStringList &paths)
Definition: staticsimple.cpp:47
Cutelyst::StaticSimple
Definition: staticsimple.h:28
Cutelyst::StaticSimple::setup
virtual bool setup(Application *app) override
Definition: staticsimple.cpp:62
Cutelyst::Context
The Cutelyst Context.
Definition: context.h:50
Cutelyst::Context::response
Response * response() const
Definition: context.cpp:110
Cutelyst::Response::setContentType
void setContentType(const QString &type)
Definition: response.h:218
Cutelyst::Application::beforePrepareAction
void beforePrepareAction(Cutelyst::Context *c, bool *skipMethod)
Cutelyst::Headers::setContentType
void setContentType(const QString &contentType)
Definition: headers.cpp:81
Cutelyst::Response::setBody
void setBody(QIODevice *body)
Definition: response.cpp:114
Cutelyst::Headers
Definition: headers.h:29
Cutelyst
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
Cutelyst::Context::res
Response * res() const
Definition: context.cpp:116
Cutelyst::Headers::setHeader
void setHeader(const QString &field, const QString &value)
Definition: headers.cpp:403
Cutelyst::Response::headers
Headers & headers()
Definition: response.cpp:316
Cutelyst::Response
Definition: response.h:34