Cutelyst  2.3.0
staticsimple.cpp
1 /*
2  * Copyright (C) 2014-2017 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")
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  QString path = c->req()->path();
78  QRegularExpression re = d->re; // Thread-safe
79  QRegularExpressionMatch match = re.match(path);
80  if (match.hasMatch() && locateStaticFile(c, path)) {
81  *skipMethod = true;
82  }
83 }
84 
85 bool StaticSimple::locateStaticFile(Context *c, const QString &relPath)
86 {
87  Q_D(const StaticSimple);
88 
89  for (const QDir &includePath : d->includePaths) {
90  QString path = includePath.absoluteFilePath(relPath);
91  QFileInfo fileInfo(path);
92  if (fileInfo.exists()) {
93  Response *res = c->res();
94  const QDateTime currentDateTime = fileInfo.lastModified();
95  if (currentDateTime == c->req()->headers().ifModifiedSinceDateTime()) {
96  res->setStatus(Response::NotModified);
97  return true;
98  }
99 
100  QFile *file = new QFile(path);
101  if (file->open(QFile::ReadOnly)) {
102  qCDebug(C_STATICSIMPLE) << "Serving" << path;
103  Headers &headers = res->headers();
104 
105  // set our open file
106  res->setBody(file);
107 
108  static QMimeDatabase db;
109  // use the extension to match to be faster
110  QMimeType mimeType = db.mimeTypeForFile(path, QMimeDatabase::MatchExtension);
111  if (mimeType.isValid()) {
112  headers.setContentType(mimeType.name());
113  }
114  headers.setContentLength(file->size());
115 
116  headers.setLastModified(currentDateTime);
117  // Tell Firefox & friends its OK to cache, even over SSL
118  headers.setHeader(QStringLiteral("CACHE_CONTROL"), QStringLiteral("public"));
119 
120  return true;
121  }
122 
123  qCWarning(C_STATICSIMPLE) << "Could not serve" << path << file->errorString();
124  return false;
125  }
126  }
127 
128  qCWarning(C_STATICSIMPLE) << "File not found" << relPath;
129  return false;
130 }
131 
132 #include "moc_staticsimple.cpp"
void setDirs(const QStringList &dirs)
Response * res() const
Definition: context.cpp:116
void setStatus(quint16 status)
Definition: response.cpp:85
The Cutelyst Context.
Definition: context.h:50
Headers & headers()
Definition: response.cpp:290
virtual bool setup(Application *app)
void setHeader(const QString &field, const QString &value)
Definition: headers.cpp:353
void setLastModified(const QString &value)
Definition: headers.cpp:233
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
void beforePrepareAction(Context *c, bool *skipMethod)
void setContentType(const QString &contentType)
Definition: headers.cpp:77
void setIncludePaths(const QStringList &paths)
The Cutelyst Application.
Definition: application.h:55
void setBody(QIODevice *body)
Definition: response.cpp:114
void setContentLength(qint64 value)
Definition: headers.cpp:168