Merge pull request #7786 from sethalves/web-scripts-use-local-libs

allow scripts on the web to refer to local libraries with /~/
This commit is contained in:
Brad Hefta-Gaub 2016-05-03 11:10:48 -07:00
commit 754ef071eb
4 changed files with 44 additions and 9 deletions

View file

@ -982,7 +982,18 @@ void ScriptEngine::include(const QStringList& includeFiles, QScriptValue callbac
// Do NOT use PreferLocalFile as its behavior is unpredictable (e.g., on defaultScriptsLocation()) // Do NOT use PreferLocalFile as its behavior is unpredictable (e.g., on defaultScriptsLocation())
const auto strippingFlags = QUrl::RemoveFilename | QUrl::RemoveQuery | QUrl::RemoveFragment; const auto strippingFlags = QUrl::RemoveFilename | QUrl::RemoveQuery | QUrl::RemoveFragment;
for (QString file : includeFiles) { for (QString file : includeFiles) {
QUrl thisURL { resolvePath(file) }; QUrl thisURL;
if (file.startsWith("/~/")) {
thisURL = expandScriptUrl(QUrl::fromLocalFile(expandScriptPath(file)));
QUrl defaultScriptsLoc = defaultScriptsLocation();
if (!defaultScriptsLoc.isParentOf(thisURL)) {
qDebug() << "ScriptEngine::include -- skipping" << file << "-- outside of standard libraries";
continue;
}
} else {
thisURL = resolvePath(file);
}
if (!_includedURLs.contains(thisURL)) { if (!_includedURLs.contains(thisURL)) {
if (!currentSandboxURL.isEmpty() && (thisURL.scheme() == "file") && if (!currentSandboxURL.isEmpty() && (thisURL.scheme() == "file") &&
( (

View file

@ -70,6 +70,12 @@ QUrl normalizeScriptURL(const QUrl& rawScriptURL) {
} }
} }
QString expandScriptPath(const QString& rawPath) {
QStringList splitPath = rawPath.split("/");
QUrl defaultScriptsLoc = defaultScriptsLocation();
return defaultScriptsLoc.path() + "/" + splitPath.mid(2).join("/"); // 2 to skip the slashes in /~/
}
QUrl expandScriptUrl(const QUrl& rawScriptURL) { QUrl expandScriptUrl(const QUrl& rawScriptURL) {
QUrl normalizedScriptURL = normalizeScriptURL(rawScriptURL); QUrl normalizedScriptURL = normalizeScriptURL(rawScriptURL);
if (normalizedScriptURL.scheme() == "http" || if (normalizedScriptURL.scheme() == "http" ||
@ -79,9 +85,23 @@ QUrl expandScriptUrl(const QUrl& rawScriptURL) {
} else if (normalizedScriptURL.scheme() == "file") { } else if (normalizedScriptURL.scheme() == "file") {
if (normalizedScriptURL.path().startsWith("/~/")) { if (normalizedScriptURL.path().startsWith("/~/")) {
QUrl url = normalizedScriptURL; QUrl url = normalizedScriptURL;
QStringList splitPath = url.path().split("/"); url.setPath(expandScriptPath(url.path()));
// stop something like Script.include(["/~/../Desktop/naughty.js"]); from working
QFileInfo fileInfo(url.toLocalFile());
url = QUrl::fromLocalFile(fileInfo.canonicalFilePath());
QUrl defaultScriptsLoc = defaultScriptsLocation(); QUrl defaultScriptsLoc = defaultScriptsLocation();
url.setPath(defaultScriptsLoc.path() + "/" + splitPath.mid(2).join("/")); // 2 to skip the slashes in /~/ if (!defaultScriptsLoc.isParentOf(url)) {
qCWarning(scriptengine) << "Script.include() ignoring file path" << rawScriptURL
<< "-- outside of standard libraries: "
<< url.path()
<< defaultScriptsLoc.path();
return rawScriptURL;
}
if (rawScriptURL.path().endsWith("/") && !url.path().endsWith("/")) {
url.setPath(url.path() + "/");
}
return url; return url;
} }
return normalizedScriptURL; return normalizedScriptURL;

View file

@ -101,6 +101,7 @@ protected:
}; };
QUrl normalizeScriptURL(const QUrl& rawScriptURL); QUrl normalizeScriptURL(const QUrl& rawScriptURL);
QString expandScriptPath(const QString& rawPath);
QUrl expandScriptUrl(const QUrl& rawScriptURL); QUrl expandScriptUrl(const QUrl& rawScriptURL);
#endif // hifi_ScriptEngine_h #endif // hifi_ScriptEngine_h

View file

@ -56,12 +56,15 @@ QString findMostRecentFileExtension(const QString& originalFileName, QVector<QSt
} }
QUrl defaultScriptsLocation() { QUrl defaultScriptsLocation() {
#ifdef Q_OS_WIN
return QUrl(("file:///" + QCoreApplication::applicationDirPath()).toLower() + "/scripts");
#elif defined(Q_OS_OSX)
return QUrl(("file://" + QCoreApplication::applicationDirPath() + "/../Resources/scripts").toLower());
#else
// return "http://s3.amazonaws.com/hifi-public"; // return "http://s3.amazonaws.com/hifi-public";
return QUrl("file://" + QCoreApplication::applicationDirPath() + "/scripts"); #ifdef Q_OS_WIN
QString path = QCoreApplication::applicationDirPath() + "/scripts";
#elif defined(Q_OS_OSX)
QString path = QCoreApplication::applicationDirPath() + "/../Resources/scripts";
#else
QString path = QCoreApplication::applicationDirPath() + "/scripts";
#endif #endif
QFileInfo fileInfo(path);
return QUrl::fromLocalFile(fileInfo.canonicalFilePath());
} }