Skip to content

Commit a2dc290

Browse files
authored
Merge branch 'master' into fix/python-refcount-effectengine
2 parents 1f757c9 + bb08c7f commit a2dc290

2 files changed

Lines changed: 40 additions & 16 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2424
- EffectEngine - Follow-up Python C-API null-pointer and stability fixes (#2011)
2525
- EffectFileHandler - Path traversal vulnerability when saving user-defined effects (#2011)
2626
- Effect scripts: Minor stability and style fixes in `pacman.py`, `traces.py`, `trails.py`(#2011)
27+
- WebUI - Return a valid Content-Type for static assets to prevent module loading failures
28+
2729
---
2830
### Technical
2931

libsrc/webserver/StaticFileServing.cpp

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,37 @@
1414

1515
#include <exception>
1616

17+
namespace {
18+
19+
QByteArray resolveMimeType(const QString &fileName, const QMimeDatabase *mimeDb)
20+
{
21+
const QMimeType mime = mimeDb->mimeTypeForFile(fileName);
22+
23+
#if ((QT_VERSION >= QT_VERSION_CHECK(5, 15, 0) && QT_VERSION < QT_VERSION_CHECK(5, 15, 11)) || \
24+
(QT_VERSION >= QT_VERSION_CHECK(6, 2, 0) && QT_VERSION < QT_VERSION_CHECK(6, 5, 0)))
25+
// Workaround https://bugreports.qt.io/browse/QTBUG-97392 for affected Qt versions
26+
if (mime.name() == QStringLiteral("application/x-extension-html"))
27+
{
28+
return QByteArrayLiteral("text/html");
29+
}
30+
#endif
31+
32+
const QByteArray mimeName = mime.name().toLocal8Bit();
33+
if (mime.isValid() && !mime.isDefault() && !mimeName.isEmpty())
34+
{
35+
return mimeName;
36+
}
37+
38+
const QString suffix = QFileInfo(fileName).suffix().toLower();
39+
if (suffix == QStringLiteral("js") || suffix == QStringLiteral("mjs") || suffix == QStringLiteral("cjs"))
40+
{
41+
return QByteArrayLiteral("text/javascript");
42+
}
43+
44+
return QByteArrayLiteral("application/octet-stream");
45+
}
46+
}
47+
1748
StaticFileServing::StaticFileServing (QObject * parent)
1849
: QObject (parent)
1950
, _baseUrl ()
@@ -133,25 +164,16 @@ void StaticFileServing::onRequestNeedsReply (QtHttpRequest * request, QtHttpRepl
133164
QFile file(_baseUrl % "/" % path);
134165
if (file.exists())
135166
{
136-
QMimeType mime = _mimeDb->mimeTypeForFile (file.fileName ());
137-
if (file.open (QFile::ReadOnly)) {
138-
QByteArray data = file.readAll ();
139-
140-
// Workaround https://bugreports.qt.io/browse/QTBUG-97392
141-
if (mime.name() == QStringLiteral("application/x-extension-html"))
142-
{
143-
reply->addHeader ("Content-Type", "text/html");
144-
}
145-
else
146-
{
147-
reply->addHeader ("Content-Type", mime.name().toLocal8Bit());
148-
}
149-
reply->appendRawData (data);
150-
file.close ();
167+
if (file.open(QFile::ReadOnly))
168+
{
169+
QByteArray data = file.readAll();
170+
reply->addHeader("Content-Type", resolveMimeType(file.fileName(), _mimeDb));
171+
reply->appendRawData(data);
172+
file.close();
151173
}
152174
else
153175
{
154-
printErrorToReply (reply, QtHttpReply::Forbidden ,"Requested file: " % path);
176+
printErrorToReply(reply, QtHttpReply::Forbidden, "Requested file: " % path);
155177
}
156178
}
157179
else

0 commit comments

Comments
 (0)