2016-03-23 07:41:18 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
2016-03-31 01:48:34 -05:00
|
|
|
#include <cassert>
|
|
|
|
|
2016-03-23 07:41:18 -05:00
|
|
|
#include <Poco/Path.h>
|
2016-03-26 06:50:13 -05:00
|
|
|
#include <Poco/SHA1Engine.h>
|
2016-03-23 07:41:18 -05:00
|
|
|
|
|
|
|
#include "DocumentBroker.hpp"
|
2016-04-17 10:05:39 -05:00
|
|
|
#include "Exceptions.hpp"
|
|
|
|
#include "LOOLWSD.hpp"
|
2016-03-25 21:56:18 -05:00
|
|
|
#include "Storage.hpp"
|
|
|
|
#include "TileCache.hpp"
|
2016-03-23 07:41:18 -05:00
|
|
|
|
2016-03-26 06:50:13 -05:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
/// Returns the cache path for a given document URI.
|
|
|
|
std::string getCachePath(const std::string& uri)
|
|
|
|
{
|
|
|
|
Poco::SHA1Engine digestEngine;
|
|
|
|
|
|
|
|
digestEngine.update(uri.c_str(), uri.size());
|
|
|
|
|
|
|
|
return (LOOLWSD::Cache + "/" +
|
|
|
|
Poco::DigestEngine::digestToHex(digestEngine.digest()).insert(3, "/").insert(2, "/").insert(1, "/"));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-04-13 03:05:00 -05:00
|
|
|
Poco::URI DocumentBroker::sanitizeURI(const std::string& uri)
|
2016-03-23 07:41:18 -05:00
|
|
|
{
|
|
|
|
// The URI of the document should be url-encoded.
|
|
|
|
std::string decodedUri;
|
|
|
|
Poco::URI::decode(uri, decodedUri);
|
|
|
|
auto uriPublic = Poco::URI(decodedUri);
|
|
|
|
|
|
|
|
if (uriPublic.isRelative() || uriPublic.getScheme() == "file")
|
|
|
|
{
|
|
|
|
// TODO: Validate and limit access to local paths!
|
|
|
|
uriPublic.normalize();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uriPublic.getPath().empty())
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Invalid URI.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return uriPublic;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string DocumentBroker::getDocKey(const Poco::URI& uri)
|
|
|
|
{
|
|
|
|
// Keep the host as part of the key to close a potential security hole.
|
|
|
|
std::string docKey;
|
|
|
|
Poco::URI::encode(uri.getHost() + uri.getPath(), "", docKey);
|
|
|
|
return docKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
DocumentBroker::DocumentBroker(const Poco::URI& uriPublic,
|
|
|
|
const std::string& docKey,
|
2016-04-03 20:40:14 -05:00
|
|
|
const std::string& childRoot,
|
|
|
|
std::shared_ptr<ChildProcess> childProcess) :
|
2016-03-23 07:41:18 -05:00
|
|
|
_uriPublic(uriPublic),
|
|
|
|
_docKey(docKey),
|
|
|
|
_childRoot(childRoot),
|
2016-03-26 06:50:13 -05:00
|
|
|
_cacheRoot(getCachePath(uriPublic.toString())),
|
2016-04-17 22:29:03 -05:00
|
|
|
_childProcess(childProcess),
|
2016-04-24 10:08:08 -05:00
|
|
|
_lastSaveTime(std::chrono::steady_clock::now()),
|
2016-04-21 23:11:24 -05:00
|
|
|
_markToDestroy(false),
|
|
|
|
_isModified(false)
|
2016-03-23 07:41:18 -05:00
|
|
|
{
|
|
|
|
assert(!_docKey.empty());
|
|
|
|
assert(!_childRoot.empty());
|
|
|
|
Log::info("DocumentBroker [" + _uriPublic.toString() + "] created. DocKey: [" + _docKey + "]");
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentBroker::validate(const Poco::URI& uri)
|
|
|
|
{
|
|
|
|
Log::info("Validating: " + uri.toString());
|
2016-04-17 10:05:39 -05:00
|
|
|
try
|
|
|
|
{
|
|
|
|
auto storage = StorageBase::create("", "", uri);
|
|
|
|
if (storage == nullptr || !storage->getFileInfo(uri).isValid())
|
|
|
|
{
|
|
|
|
throw BadRequestException("Invalid URI or access denied.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const std::exception&)
|
2016-04-06 23:32:28 -05:00
|
|
|
{
|
2016-04-17 10:05:39 -05:00
|
|
|
throw BadRequestException("Invalid URI or access denied.");
|
2016-04-06 23:32:28 -05:00
|
|
|
}
|
2016-03-23 07:41:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DocumentBroker::load(const std::string& jailId)
|
|
|
|
{
|
|
|
|
Log::debug("Loading from URI: " + _uriPublic.toString());
|
|
|
|
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
|
2016-04-17 22:29:03 -05:00
|
|
|
if (_markToDestroy)
|
|
|
|
{
|
|
|
|
// Tearing down.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-23 07:41:18 -05:00
|
|
|
if (_storage)
|
|
|
|
{
|
|
|
|
// Already loaded. Nothing to do.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
_jailId = jailId;
|
|
|
|
|
|
|
|
// The URL is the publicly visible one, not visible in the chroot jail.
|
|
|
|
// We need to map it to a jailed path and copy the file there.
|
|
|
|
|
|
|
|
// user/doc/jailId
|
2016-03-28 06:01:19 -05:00
|
|
|
const auto jailPath = Poco::Path(JAILED_DOCUMENT_ROOT, jailId);
|
2016-03-23 07:41:18 -05:00
|
|
|
const std::string jailRoot = getJailRoot();
|
|
|
|
|
|
|
|
Log::info("jailPath: " + jailPath.toString() + ", jailRoot: " + jailRoot);
|
|
|
|
|
2016-04-07 15:59:27 -05:00
|
|
|
auto storage = StorageBase::create("", "", _uriPublic);
|
|
|
|
if (storage)
|
|
|
|
{
|
|
|
|
const auto fileInfo = storage->getFileInfo(_uriPublic);
|
2016-04-19 02:10:07 -05:00
|
|
|
_tileCache.reset(new TileCache(_uriPublic.toString(), fileInfo._modifiedTime, _cacheRoot));
|
|
|
|
_filename = fileInfo._filename;
|
2016-04-07 15:59:27 -05:00
|
|
|
_storage = StorageBase::create(jailRoot, jailPath.toString(), _uriPublic);
|
2016-03-25 21:56:18 -05:00
|
|
|
|
2016-04-07 15:59:27 -05:00
|
|
|
const auto localPath = _storage->loadStorageFileToLocal();
|
|
|
|
_uriJailed = Poco::URI(Poco::URI("file://"), localPath);
|
2016-03-23 07:41:18 -05:00
|
|
|
|
2016-04-07 15:59:27 -05:00
|
|
|
return true;
|
|
|
|
}
|
2016-04-10 12:03:57 -05:00
|
|
|
|
|
|
|
return false;
|
2016-03-23 07:41:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DocumentBroker::save()
|
|
|
|
{
|
2016-04-10 21:07:09 -05:00
|
|
|
std::unique_lock<std::mutex> lock(_saveMutex);
|
|
|
|
|
2016-04-10 12:03:57 -05:00
|
|
|
const auto uri = _uriPublic.toString();
|
|
|
|
Log::debug("Saving to URI [" + uri + "].");
|
2016-03-23 07:41:18 -05:00
|
|
|
|
2016-03-26 08:10:53 -05:00
|
|
|
assert(_storage && _tileCache);
|
|
|
|
if (_storage->saveLocalFileToStorage())
|
|
|
|
{
|
2016-04-21 23:11:24 -05:00
|
|
|
_isModified = false;
|
2016-04-09 22:20:20 -05:00
|
|
|
_lastSaveTime = std::chrono::steady_clock::now();
|
2016-04-22 06:00:11 -05:00
|
|
|
_tileCache->setUnsavedChanges(false);
|
2016-04-10 12:03:57 -05:00
|
|
|
Log::debug("Saved to URI [" + uri + "] and updated tile cache.");
|
2016-04-10 21:07:09 -05:00
|
|
|
_saveCV.notify_all();
|
2016-04-22 06:00:11 -05:00
|
|
|
const auto fileInfo = _storage->getFileInfo(_uriPublic);
|
|
|
|
_tileCache->saveLastModified(fileInfo._modifiedTime);
|
2016-03-26 08:10:53 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-10 12:03:57 -05:00
|
|
|
Log::error("Failed to save to URI [" + uri + "].");
|
2016-03-26 08:10:53 -05:00
|
|
|
return false;
|
2016-03-23 07:41:18 -05:00
|
|
|
}
|
|
|
|
|
2016-04-10 21:07:09 -05:00
|
|
|
bool DocumentBroker::autoSave(const bool force)
|
2016-04-09 22:20:20 -05:00
|
|
|
{
|
2016-04-16 16:18:51 -05:00
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
if (_sessions.empty())
|
2016-04-09 22:20:20 -05:00
|
|
|
{
|
|
|
|
// Shouldn't happen.
|
2016-04-10 21:07:09 -05:00
|
|
|
return false;
|
2016-04-09 22:20:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find the most recent activity.
|
|
|
|
double inactivityTimeMs = std::numeric_limits<double>::max();
|
2016-04-16 16:18:51 -05:00
|
|
|
for (auto& sessionIt: _sessions)
|
2016-04-09 22:20:20 -05:00
|
|
|
{
|
|
|
|
inactivityTimeMs = std::min(sessionIt.second->getInactivityMS(), inactivityTimeMs);
|
|
|
|
}
|
|
|
|
|
2016-04-11 01:25:32 -05:00
|
|
|
Log::trace("Most recent activity was " + std::to_string((int)inactivityTimeMs) + " ms ago.");
|
2016-04-09 22:20:20 -05:00
|
|
|
const auto timeSinceLastSaveMs = getTimeSinceLastSaveMs();
|
2016-04-11 01:25:32 -05:00
|
|
|
Log::trace("Time since last save is " + std::to_string((int)timeSinceLastSaveMs) + " ms.");
|
2016-04-09 22:20:20 -05:00
|
|
|
|
|
|
|
// There has been some editing since we saved last?
|
|
|
|
if (inactivityTimeMs < timeSinceLastSaveMs)
|
|
|
|
{
|
|
|
|
// Either we've been idle long enough, or it's auto-save time.
|
2016-04-10 21:07:09 -05:00
|
|
|
// Or we are asked to save anyway.
|
2016-04-09 22:20:20 -05:00
|
|
|
if (inactivityTimeMs >= IdleSaveDurationMs ||
|
2016-04-10 21:07:09 -05:00
|
|
|
timeSinceLastSaveMs >= AutoSaveDurationMs ||
|
|
|
|
force)
|
2016-04-09 22:20:20 -05:00
|
|
|
{
|
|
|
|
Log::info("Auto-save triggered for doc [" + _docKey + "].");
|
|
|
|
|
2016-04-12 05:14:11 -05:00
|
|
|
// Save using session holding the edit-lock
|
2016-04-10 11:30:33 -05:00
|
|
|
bool sent = false;
|
2016-04-16 16:18:51 -05:00
|
|
|
for (auto& sessionIt: _sessions)
|
2016-04-10 11:30:33 -05:00
|
|
|
{
|
2016-04-12 05:14:11 -05:00
|
|
|
if (!sessionIt.second->isEditLocked())
|
|
|
|
continue;
|
|
|
|
|
2016-04-10 11:30:33 -05:00
|
|
|
auto queue = sessionIt.second->getQueue();
|
|
|
|
if (queue)
|
|
|
|
{
|
|
|
|
queue->put("uno .uno:Save");
|
|
|
|
sent = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sent)
|
|
|
|
{
|
2016-04-10 12:13:05 -05:00
|
|
|
Log::error("Failed to auto-save doc [" + _docKey + "]: No valid sessions.");
|
2016-04-10 11:30:33 -05:00
|
|
|
}
|
2016-04-10 21:07:09 -05:00
|
|
|
|
|
|
|
return sent;
|
2016-04-09 22:20:20 -05:00
|
|
|
}
|
|
|
|
}
|
2016-04-10 21:07:09 -05:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DocumentBroker::waitSave(const size_t timeoutMs)
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(_saveMutex);
|
|
|
|
|
|
|
|
// Remeber the last save time, since this is the predicate.
|
|
|
|
const auto lastSaveTime = _lastSaveTime;
|
|
|
|
|
|
|
|
if (_saveCV.wait_for(lock, std::chrono::milliseconds(timeoutMs)) == std::cv_status::no_timeout)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (lastSaveTime != _lastSaveTime);
|
2016-04-09 22:20:20 -05:00
|
|
|
}
|
|
|
|
|
2016-03-23 07:41:18 -05:00
|
|
|
std::string DocumentBroker::getJailRoot() const
|
|
|
|
{
|
|
|
|
assert(!_jailId.empty());
|
|
|
|
return Poco::Path(_childRoot, _jailId).toString();
|
|
|
|
}
|
|
|
|
|
2016-04-13 03:05:00 -05:00
|
|
|
void DocumentBroker::takeEditLock(const std::string& id)
|
2016-03-23 11:55:28 -05:00
|
|
|
{
|
2016-04-16 16:18:51 -05:00
|
|
|
std::lock_guard<std::mutex> lock(_mutex);
|
|
|
|
for (auto& it: _sessions)
|
2016-03-23 11:55:28 -05:00
|
|
|
{
|
|
|
|
if (it.first != id)
|
|
|
|
{
|
|
|
|
it.second->setEditLock(false);
|
2016-04-08 15:16:03 -05:00
|
|
|
it.second->sendTextFrame("editlock: 0");
|
2016-03-23 11:55:28 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
it.second->setEditLock(true);
|
2016-04-08 15:16:03 -05:00
|
|
|
it.second->sendTextFrame("editlock: 1");
|
2016-03-23 11:55:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-16 16:18:51 -05:00
|
|
|
size_t DocumentBroker::addSession(std::shared_ptr<MasterProcessSession>& session)
|
2016-03-23 11:55:28 -05:00
|
|
|
{
|
2016-04-16 16:18:51 -05:00
|
|
|
const auto id = session->getId();
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(_mutex);
|
2016-04-10 12:03:57 -05:00
|
|
|
|
2016-04-16 16:18:51 -05:00
|
|
|
auto ret = _sessions.emplace(id, session);
|
2016-03-23 11:55:28 -05:00
|
|
|
if (!ret.second)
|
|
|
|
{
|
2016-04-24 10:08:08 -05:00
|
|
|
Log::warn("DocumentBroker: Trying to add already existing session.");
|
2016-03-23 11:55:28 -05:00
|
|
|
}
|
2016-04-03 20:40:14 -05:00
|
|
|
|
2016-04-16 16:18:51 -05:00
|
|
|
if (_sessions.size() == 1)
|
2016-04-10 12:35:06 -05:00
|
|
|
{
|
2016-04-16 16:18:51 -05:00
|
|
|
session->setEditLock(true);
|
|
|
|
session->sendTextFrame("editlock: 1");
|
2016-04-10 12:35:06 -05:00
|
|
|
}
|
|
|
|
|
2016-04-03 20:40:14 -05:00
|
|
|
// Request a new session from the child kit.
|
|
|
|
const std::string aMessage = "session " + id + " " + _docKey + "\n";
|
|
|
|
Log::debug("DocBroker to Child: " + aMessage.substr(0, aMessage.length() - 1));
|
2016-04-23 15:00:09 -05:00
|
|
|
//FIXME: The socket could be broken, child dead, etc. Must recover!
|
2016-04-03 20:40:14 -05:00
|
|
|
_childProcess->getWebSocket()->sendFrame(aMessage.data(), aMessage.size());
|
2016-04-16 16:18:51 -05:00
|
|
|
|
|
|
|
return _sessions.size();
|
2016-03-23 11:55:28 -05:00
|
|
|
}
|
|
|
|
|
2016-04-16 16:18:51 -05:00
|
|
|
size_t DocumentBroker::removeSession(const std::string& id)
|
2016-03-23 11:55:28 -05:00
|
|
|
{
|
2016-04-16 16:18:51 -05:00
|
|
|
std::lock_guard<std::mutex> lock(_mutex);
|
2016-04-10 12:06:35 -05:00
|
|
|
|
2016-04-16 16:18:51 -05:00
|
|
|
auto it = _sessions.find(id);
|
|
|
|
if (it != _sessions.end())
|
2016-03-23 11:55:28 -05:00
|
|
|
{
|
2016-04-16 16:18:51 -05:00
|
|
|
const auto haveEditLock = it->second->isEditLocked();
|
2016-04-10 12:06:35 -05:00
|
|
|
it->second->setEditLock(false);
|
2016-04-16 16:18:51 -05:00
|
|
|
_sessions.erase(it);
|
2016-03-23 11:55:28 -05:00
|
|
|
|
2016-04-16 16:18:51 -05:00
|
|
|
if (haveEditLock)
|
2016-03-23 11:55:28 -05:00
|
|
|
{
|
2016-04-16 16:18:51 -05:00
|
|
|
// pass the edit lock to first session in map
|
|
|
|
it = _sessions.begin();
|
|
|
|
if (it != _sessions.end())
|
|
|
|
{
|
|
|
|
it->second->setEditLock(true);
|
|
|
|
it->second->sendTextFrame("editlock: 1");
|
|
|
|
}
|
2016-03-23 11:55:28 -05:00
|
|
|
}
|
|
|
|
}
|
2016-04-16 16:18:51 -05:00
|
|
|
|
|
|
|
return _sessions.size();
|
2016-03-23 11:55:28 -05:00
|
|
|
}
|
|
|
|
|
2016-04-17 22:29:03 -05:00
|
|
|
bool DocumentBroker::canDestroy()
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
|
|
|
|
if (_sessions.size() == 1)
|
|
|
|
{
|
|
|
|
// Last view going away, can destroy.
|
|
|
|
_markToDestroy = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return _markToDestroy;
|
|
|
|
}
|
|
|
|
|
2016-04-22 06:00:11 -05:00
|
|
|
void DocumentBroker::setModified(const bool value)
|
|
|
|
{
|
|
|
|
_tileCache->setUnsavedChanges(value);
|
|
|
|
_isModified = value;
|
|
|
|
}
|
|
|
|
|
2016-03-23 07:41:18 -05:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|