From f012772fa37323ac18f87a728c8d38dc95ca38f9 Mon Sep 17 00:00:00 2001 From: humbletim Date: Thu, 29 Oct 2020 11:29:26 -0400 Subject: [PATCH 1/4] add toHttpDateString helper method --- libraries/networking/src/ResourceRequest.cpp | 6 ++++++ libraries/networking/src/ResourceRequest.h | 1 + 2 files changed, 7 insertions(+) diff --git a/libraries/networking/src/ResourceRequest.cpp b/libraries/networking/src/ResourceRequest.cpp index c63bd4c563..0f475579d6 100644 --- a/libraries/networking/src/ResourceRequest.cpp +++ b/libraries/networking/src/ResourceRequest.cpp @@ -15,8 +15,14 @@ #include #include +#include #include +QString ResourceRequest::toHttpDateString(uint64_t msecsSinceEpoch) { + return QLocale::c() + .toString(QDateTime::fromMSecsSinceEpoch(msecsSinceEpoch), QLatin1String("ddd, dd MMM yyyy hh:mm:ss 'GMT'")) + .toLatin1(); +} void ResourceRequest::send() { if (QThread::currentThread() != thread()) { diff --git a/libraries/networking/src/ResourceRequest.h b/libraries/networking/src/ResourceRequest.h index 550294d79b..042e706291 100644 --- a/libraries/networking/src/ResourceRequest.h +++ b/libraries/networking/src/ResourceRequest.h @@ -90,6 +90,7 @@ public: void setCacheEnabled(bool value) { _cacheEnabled = value; } void setByteRange(ByteRange byteRange) { _byteRange = byteRange; } + static QString toHttpDateString(uint64_t msecsSinceEpoch); public slots: void send(); From ae7aa3eb883a0a1235773cd522848a79fc5b1054 Mon Sep 17 00:00:00 2001 From: humbletim Date: Thu, 29 Oct 2020 11:30:33 -0400 Subject: [PATCH 2/4] update ScriptCache to detect local script modifications --- .../networking/src/FileResourceRequest.cpp | 2 ++ libraries/script-engine/src/ScriptCache.cpp | 31 ++++++++++++++----- libraries/script-engine/src/ScriptCache.h | 2 +- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/libraries/networking/src/FileResourceRequest.cpp b/libraries/networking/src/FileResourceRequest.cpp index f9ec7797be..ebeb5936c2 100644 --- a/libraries/networking/src/FileResourceRequest.cpp +++ b/libraries/networking/src/FileResourceRequest.cpp @@ -13,6 +13,7 @@ #include #include +#include #include #include @@ -54,6 +55,7 @@ void FileResourceRequest::doSend() { } else { QFile file(filename); if (file.exists()) { + setProperty("last-modified", toHttpDateString(QFileInfo(file).lastModified().toMSecsSinceEpoch())); if (file.open(QFile::ReadOnly)) { if (file.size() < _byteRange.fromInclusive || file.size() < _byteRange.toExclusive) { diff --git a/libraries/script-engine/src/ScriptCache.cpp b/libraries/script-engine/src/ScriptCache.cpp index b23aa48762..58c5047802 100644 --- a/libraries/script-engine/src/ScriptCache.cpp +++ b/libraries/script-engine/src/ScriptCache.cpp @@ -85,11 +85,25 @@ void ScriptCache::getScriptContents(const QString& scriptOrURL, contentAvailable Lock lock(_containerLock); if (_scriptCache.contains(url) && !forceDownload) { - auto scriptContent = _scriptCache[url]; - lock.unlock(); - qCDebug(scriptengine) << "Found script in cache:" << url.fileName(); - contentAvailable(url.toString(), scriptContent, true, true, STATUS_CACHED); - } else { + auto entry = _scriptCache[url]; + if (url.isLocalFile() || url.scheme().isEmpty()) { + auto mtime = QFileInfo(url.toLocalFile()).lastModified(); + QString localTime = ResourceRequest::toHttpDateString(mtime.toMSecsSinceEpoch()); + QString cachedTime = entry["last-modified"].toString(); + if (cachedTime != localTime) { + forceDownload = true; + qCDebug(scriptengine) << "Found script in cache, but local file modified; reloading:" << url.fileName() + << "(memory:" << cachedTime << "disk:" << localTime << ")"; + } + } + if (!forceDownload) { + lock.unlock(); + qCDebug(scriptengine) << "Found script in cache:" << url.fileName(); + contentAvailable(url.toString(), entry["data"].toString(), true, true, STATUS_CACHED); + return; + } + } + { auto& scriptRequest = _activeScriptRequests[url]; bool alreadyWaiting = scriptRequest.scriptUsers.size() > 0; scriptRequest.scriptUsers.push_back(contentAvailable); @@ -140,7 +154,10 @@ void ScriptCache::scriptContentAvailable(int maxRetries) { _activeScriptRequests.remove(url); - _scriptCache[url] = scriptContent = req->getData(); + _scriptCache[url] = { + { "data", scriptContent = req->getData() }, + { "last-modified", req->property("last-modified") }, + }; } else { auto result = req->getResult(); bool irrecoverable = @@ -177,7 +194,7 @@ void ScriptCache::scriptContentAvailable(int maxRetries) { allCallbacks = scriptRequest.scriptUsers; if (_scriptCache.contains(url)) { - scriptContent = _scriptCache[url]; + scriptContent = _scriptCache[url]["data"].toString(); } _activeScriptRequests.remove(url); qCWarning(scriptengine) << "Error loading script from URL (" << status <<")"; diff --git a/libraries/script-engine/src/ScriptCache.h b/libraries/script-engine/src/ScriptCache.h index 511d392409..4ab4458ee9 100644 --- a/libraries/script-engine/src/ScriptCache.h +++ b/libraries/script-engine/src/ScriptCache.h @@ -60,7 +60,7 @@ private: Mutex _containerLock; QMap _activeScriptRequests; - QHash _scriptCache; + QHash _scriptCache; QMultiMap _scriptUsers; }; From a7fb294a1b5d9dec5d8ced5584243e60483cfc62 Mon Sep 17 00:00:00 2001 From: humbletim Date: Sat, 31 Oct 2020 13:39:29 -0400 Subject: [PATCH 3/4] Update libraries/networking/src/ResourceRequest.cpp Co-authored-by: David Rowe --- libraries/networking/src/ResourceRequest.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/networking/src/ResourceRequest.cpp b/libraries/networking/src/ResourceRequest.cpp index 0f475579d6..8739cb61da 100644 --- a/libraries/networking/src/ResourceRequest.cpp +++ b/libraries/networking/src/ResourceRequest.cpp @@ -19,8 +19,8 @@ #include QString ResourceRequest::toHttpDateString(uint64_t msecsSinceEpoch) { - return QLocale::c() - .toString(QDateTime::fromMSecsSinceEpoch(msecsSinceEpoch), QLatin1String("ddd, dd MMM yyyy hh:mm:ss 'GMT'")) + return QDateTime::fromMSecsSinceEpoch(msecsSinceEpoch) + .toString("ddd, dd MMM yyyy hh:mm:ss 'GMT'") .toLatin1(); } From f41ef31ef59e9e441746929978aac74b832cefa9 Mon Sep 17 00:00:00 2001 From: humbletim Date: Sat, 31 Oct 2020 13:50:47 -0400 Subject: [PATCH 4/4] changes per CR --- libraries/script-engine/src/ScriptCache.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/libraries/script-engine/src/ScriptCache.cpp b/libraries/script-engine/src/ScriptCache.cpp index 58c5047802..0b63803a33 100644 --- a/libraries/script-engine/src/ScriptCache.cpp +++ b/libraries/script-engine/src/ScriptCache.cpp @@ -87,8 +87,8 @@ void ScriptCache::getScriptContents(const QString& scriptOrURL, contentAvailable if (_scriptCache.contains(url) && !forceDownload) { auto entry = _scriptCache[url]; if (url.isLocalFile() || url.scheme().isEmpty()) { - auto mtime = QFileInfo(url.toLocalFile()).lastModified(); - QString localTime = ResourceRequest::toHttpDateString(mtime.toMSecsSinceEpoch()); + auto modifiedTime = QFileInfo(url.toLocalFile()).lastModified(); + QString localTime = ResourceRequest::toHttpDateString(modifiedTime.toMSecsSinceEpoch()); QString cachedTime = entry["last-modified"].toString(); if (cachedTime != localTime) { forceDownload = true; @@ -97,10 +97,10 @@ void ScriptCache::getScriptContents(const QString& scriptOrURL, contentAvailable } } if (!forceDownload) { - lock.unlock(); - qCDebug(scriptengine) << "Found script in cache:" << url.fileName(); - contentAvailable(url.toString(), entry["data"].toString(), true, true, STATUS_CACHED); - return; + lock.unlock(); + qCDebug(scriptengine) << "Found script in cache:" << url.fileName(); + contentAvailable(url.toString(), entry["data"].toString(), true, true, STATUS_CACHED); + return; } } { @@ -155,8 +155,8 @@ void ScriptCache::scriptContentAvailable(int maxRetries) { _activeScriptRequests.remove(url); _scriptCache[url] = { - { "data", scriptContent = req->getData() }, - { "last-modified", req->property("last-modified") }, + { "data", scriptContent = req->getData() }, + { "last-modified", req->property("last-modified") }, }; } else { auto result = req->getResult();