Browse Source

Merge pull request #9027 from Chocobo1/cache

Send Cache-Control header in WebUI responses
adaptive-webui-19844
Mike Tzou 7 years ago committed by GitHub
parent
commit
40330c4606
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      src/base/http/types.h
  2. 30
      src/webui/webapplication.cpp
  3. 2
      src/webui/webui.qrc
  4. 0
      src/webui/www/public/css/login.css
  5. 2
      src/webui/www/public/login.html

4
src/base/http/types.h

@ -63,10 +63,12 @@ namespace Http
const char HEADER_REQUEST_METHOD_POST[] = "POST"; const char HEADER_REQUEST_METHOD_POST[] = "POST";
const char CONTENT_TYPE_HTML[] = "text/html"; const char CONTENT_TYPE_HTML[] = "text/html";
const char CONTENT_TYPE_CSS[] = "text/css";
const char CONTENT_TYPE_TXT[] = "text/plain";
const char CONTENT_TYPE_JS[] = "application/javascript";
const char CONTENT_TYPE_JSON[] = "application/json"; const char CONTENT_TYPE_JSON[] = "application/json";
const char CONTENT_TYPE_GIF[] = "image/gif"; const char CONTENT_TYPE_GIF[] = "image/gif";
const char CONTENT_TYPE_PNG[] = "image/png"; const char CONTENT_TYPE_PNG[] = "image/png";
const char CONTENT_TYPE_TXT[] = "text/plain";
const char CONTENT_TYPE_FORM_ENCODED[] = "application/x-www-form-urlencoded"; const char CONTENT_TYPE_FORM_ENCODED[] = "application/x-www-form-urlencoded";
const char CONTENT_TYPE_FORM_DATA[] = "multipart/form-data"; const char CONTENT_TYPE_FORM_DATA[] = "multipart/form-data";

30
src/webui/webapplication.cpp

@ -73,7 +73,6 @@ const QString PATH_PREFIX_THEME {QStringLiteral("/theme/")};
const QString WWW_FOLDER {QStringLiteral(":/www")}; const QString WWW_FOLDER {QStringLiteral(":/www")};
const QString PUBLIC_FOLDER {QStringLiteral("/public")}; const QString PUBLIC_FOLDER {QStringLiteral("/public")};
const QString PRIVATE_FOLDER {QStringLiteral("/private")}; const QString PRIVATE_FOLDER {QStringLiteral("/private")};
const QString MAX_AGE_MONTH {QStringLiteral("public, max-age=2592000")};
namespace namespace
{ {
@ -141,6 +140,22 @@ namespace
return QUrl(QLatin1String("http://") + hostHeader); return QUrl(QLatin1String("http://") + hostHeader);
return hostHeader; return hostHeader;
} }
QString getCachingInterval(QString contentType)
{
contentType = contentType.toLower();
if (contentType.startsWith(QLatin1String("image/")))
return QLatin1String("private, max-age=604800"); // 1 week
if ((contentType == Http::CONTENT_TYPE_CSS)
|| (contentType == Http::CONTENT_TYPE_JS)) {
// short interval in case of program update
return QLatin1String("private, max-age=43200"); // 12 hrs
}
return QLatin1String("no-store");
}
} }
WebApplication::WebApplication(QObject *parent) WebApplication::WebApplication(QObject *parent)
@ -176,14 +191,12 @@ void WebApplication::sendWebUIFile()
if (request().path.startsWith(PATH_PREFIX_IMAGES)) { if (request().path.startsWith(PATH_PREFIX_IMAGES)) {
const QString imageFilename {request().path.mid(PATH_PREFIX_IMAGES.size())}; const QString imageFilename {request().path.mid(PATH_PREFIX_IMAGES.size())};
sendFile(QLatin1String(":/icons/") + imageFilename); sendFile(QLatin1String(":/icons/") + imageFilename);
header(Http::HEADER_CACHE_CONTROL, MAX_AGE_MONTH);
return; return;
} }
if (request().path.startsWith(PATH_PREFIX_THEME)) { if (request().path.startsWith(PATH_PREFIX_THEME)) {
const QString iconId {request().path.mid(PATH_PREFIX_THEME.size())}; const QString iconId {request().path.mid(PATH_PREFIX_THEME.size())};
sendFile(IconProvider::instance()->getIconPath(iconId)); sendFile(IconProvider::instance()->getIconPath(iconId));
header(Http::HEADER_CACHE_CONTROL, MAX_AGE_MONTH);
return; return;
} }
} }
@ -455,7 +468,9 @@ void WebApplication::sendFile(const QString &path)
// find translated file in cache // find translated file in cache
auto it = m_translatedFiles.constFind(path); auto it = m_translatedFiles.constFind(path);
if ((it != m_translatedFiles.constEnd()) && (lastModified <= (*it).lastModified)) { if ((it != m_translatedFiles.constEnd()) && (lastModified <= (*it).lastModified)) {
print((*it).data, QMimeDatabase().mimeTypeForFileNameAndData(path, (*it).data).name()); const QString mimeName {QMimeDatabase().mimeTypeForFileNameAndData(path, (*it).data).name()};
print((*it).data, mimeName);
header(Http::HEADER_CACHE_CONTROL, getCachingInterval(mimeName));
return; return;
} }
@ -474,8 +489,8 @@ void WebApplication::sendFile(const QString &path)
QByteArray data {file.readAll()}; QByteArray data {file.readAll()};
file.close(); file.close();
const QMimeType type {QMimeDatabase().mimeTypeForFileNameAndData(path, data)}; const QMimeType mimeType {QMimeDatabase().mimeTypeForFileNameAndData(path, data)};
const bool isTranslatable {type.inherits(QLatin1String("text/plain"))}; const bool isTranslatable {mimeType.inherits(QLatin1String("text/plain"))};
// Translate the file // Translate the file
if (isTranslatable) { if (isTranslatable) {
@ -486,7 +501,8 @@ void WebApplication::sendFile(const QString &path)
m_translatedFiles[path] = {data, lastModified}; // caching translated file m_translatedFiles[path] = {data, lastModified}; // caching translated file
} }
print(data, type.name()); print(data, mimeType.name());
header(Http::HEADER_CACHE_CONTROL, getCachingInterval(mimeType.name()));
} }
Http::Response WebApplication::processRequest(const Http::Request &request, const Http::Environment &env) Http::Response WebApplication::processRequest(const Http::Request &request, const Http::Environment &env)

2
src/webui/webui.qrc

@ -42,7 +42,7 @@
<file>www/private/transferlist.html</file> <file>www/private/transferlist.html</file>
<file>www/private/upload.html</file> <file>www/private/upload.html</file>
<file>www/private/uploadlimit.html</file> <file>www/private/uploadlimit.html</file>
<file>www/public/css/style.css</file> <file>www/public/css/login.css</file>
<file>www/public/login.html</file> <file>www/public/login.html</file>
<file>www/public/scripts/lib/mootools-1.2-core-yc.js</file> <file>www/public/scripts/lib/mootools-1.2-core-yc.js</file>
</qresource> </qresource>

0
src/webui/www/public/css/style.css → src/webui/www/public/css/login.css

2
src/webui/www/public/login.html

@ -5,7 +5,7 @@
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<title>qBittorrent QBT_TR(Web UI)QBT_TR[CONTEXT=OptionsDialog]</title> <title>qBittorrent QBT_TR(Web UI)QBT_TR[CONTEXT=OptionsDialog]</title>
<link rel="icon" type="image/png" href="images/skin/qbittorrent32.png" /> <link rel="icon" type="image/png" href="images/skin/qbittorrent32.png" />
<link rel="stylesheet" type="text/css" href="css/style.css" /> <link rel="stylesheet" type="text/css" href="css/login.css" />
<script src="scripts/lib/mootools-1.2-core-yc.js"></script> <script src="scripts/lib/mootools-1.2-core-yc.js"></script>
<script> <script>
window.onload = function() { window.onload = function() {

Loading…
Cancel
Save