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/.
|
|
|
|
*/
|
|
|
|
|
2017-12-20 07:06:26 -06:00
|
|
|
#include <config.h>
|
loolwsd: include cleanup and organization
A source file (.cpp) must include its own header first.
This insures that the header is self-contained and
doesn't depend on arbitrary (and accidental) includes
before it to compile.
Furthermore, system headers should go next, followed by
C then C++ headers, then libraries (Poco, etc) and, finally,
project headers come last.
This makes sure that headers and included in the same dependency
order to avoid side-effects. For example, Poco should never rely on
anything from our project in the same way that a C header should
never rely on anything in C++, Poco, or project headers.
Also, includes ought to be sorted where possible, to improve
readability and avoid accidental duplicates (of which there
were a few).
Change-Id: I62cc1343e4a091d69195e37ed659dba20cfcb1ef
Reviewed-on: https://gerrit.libreoffice.org/25262
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Tested-by: Ashod Nakashian <ashnakash@gmail.com>
2016-05-21 09:23:07 -05:00
|
|
|
|
2017-03-08 10:38:22 -06:00
|
|
|
#include "DocumentBroker.hpp"
|
|
|
|
|
2017-05-20 12:28:43 -05:00
|
|
|
#include <atomic>
|
2016-03-31 01:48:34 -05:00
|
|
|
#include <cassert>
|
2017-04-18 23:54:42 -05:00
|
|
|
#include <chrono>
|
2016-12-14 10:12:57 -06:00
|
|
|
#include <ctime>
|
loolwsd: include cleanup and organization
A source file (.cpp) must include its own header first.
This insures that the header is self-contained and
doesn't depend on arbitrary (and accidental) includes
before it to compile.
Furthermore, system headers should go next, followed by
C then C++ headers, then libraries (Poco, etc) and, finally,
project headers come last.
This makes sure that headers and included in the same dependency
order to avoid side-effects. For example, Poco should never rely on
anything from our project in the same way that a C header should
never rely on anything in C++, Poco, or project headers.
Also, includes ought to be sorted where possible, to improve
readability and avoid accidental duplicates (of which there
were a few).
Change-Id: I62cc1343e4a091d69195e37ed659dba20cfcb1ef
Reviewed-on: https://gerrit.libreoffice.org/25262
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Tested-by: Ashod Nakashian <ashnakash@gmail.com>
2016-05-21 09:23:07 -05:00
|
|
|
#include <fstream>
|
2016-11-10 05:55:38 -06:00
|
|
|
#include <sstream>
|
2016-03-31 01:48:34 -05:00
|
|
|
|
2017-11-08 02:34:37 -06:00
|
|
|
#include <Poco/DigestStream.h>
|
2017-11-07 07:15:36 -06:00
|
|
|
#include <Poco/Exception.h>
|
2016-11-10 05:55:38 -06:00
|
|
|
#include <Poco/JSON/Object.h>
|
2016-03-23 07:41:18 -05:00
|
|
|
#include <Poco/Path.h>
|
2016-03-26 06:50:13 -05:00
|
|
|
#include <Poco/SHA1Engine.h>
|
2017-01-22 22:12:51 -06:00
|
|
|
#include <Poco/StreamCopier.h>
|
2016-03-23 07:41:18 -05:00
|
|
|
|
2016-10-22 09:25:57 -05:00
|
|
|
#include "Admin.hpp"
|
loolwsd: include cleanup and organization
A source file (.cpp) must include its own header first.
This insures that the header is self-contained and
doesn't depend on arbitrary (and accidental) includes
before it to compile.
Furthermore, system headers should go next, followed by
C then C++ headers, then libraries (Poco, etc) and, finally,
project headers come last.
This makes sure that headers and included in the same dependency
order to avoid side-effects. For example, Poco should never rely on
anything from our project in the same way that a C header should
never rely on anything in C++, Poco, or project headers.
Also, includes ought to be sorted where possible, to improve
readability and avoid accidental duplicates (of which there
were a few).
Change-Id: I62cc1343e4a091d69195e37ed659dba20cfcb1ef
Reviewed-on: https://gerrit.libreoffice.org/25262
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Tested-by: Ashod Nakashian <ashnakash@gmail.com>
2016-05-21 09:23:07 -05:00
|
|
|
#include "ClientSession.hpp"
|
2016-04-17 10:05:39 -05:00
|
|
|
#include "Exceptions.hpp"
|
|
|
|
#include "LOOLWSD.hpp"
|
2017-05-20 12:28:43 -05:00
|
|
|
#include "SenderQueue.hpp"
|
2016-03-25 21:56:18 -05:00
|
|
|
#include "Storage.hpp"
|
|
|
|
#include "TileCache.hpp"
|
2017-12-20 07:06:26 -06:00
|
|
|
#include <common/Log.hpp>
|
|
|
|
#include <common/Message.hpp>
|
2019-07-04 04:50:33 -05:00
|
|
|
#include <common/Clipboard.hpp>
|
2017-12-20 07:06:26 -06:00
|
|
|
#include <common/Protocol.hpp>
|
|
|
|
#include <common/Unit.hpp>
|
2019-03-01 15:25:44 -06:00
|
|
|
#include <common/FileUtil.hpp>
|
2016-05-01 19:50:11 -05:00
|
|
|
|
2018-01-29 09:13:54 -06:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
2018-07-31 08:42:58 -05:00
|
|
|
#define TILES_ON_FLY_MIN_UPPER_LIMIT 10.0f
|
2018-07-10 07:05:36 -05:00
|
|
|
|
2016-05-01 19:50:11 -05:00
|
|
|
using namespace LOOLProtocol;
|
|
|
|
|
2016-11-10 05:55:38 -06:00
|
|
|
using Poco::JSON::Object;
|
2016-09-28 14:07:07 -05:00
|
|
|
|
2019-04-20 13:58:48 -05:00
|
|
|
#if ENABLE_DEBUG
|
|
|
|
# define ADD_DEBUG_RENDERID (" renderid=cached\n")
|
|
|
|
#else
|
|
|
|
# define ADD_DEBUG_RENDERID ("\n")
|
|
|
|
#endif
|
|
|
|
|
2017-03-07 22:46:02 -06:00
|
|
|
void ChildProcess::setDocumentBroker(const std::shared_ptr<DocumentBroker>& docBroker)
|
|
|
|
{
|
|
|
|
assert(docBroker && "Invalid DocumentBroker instance.");
|
|
|
|
_docBroker = docBroker;
|
|
|
|
|
|
|
|
// Add the prisoner socket to the docBroker poll.
|
|
|
|
docBroker->addSocketToPoll(_socket);
|
|
|
|
}
|
|
|
|
|
2016-03-26 06:50:13 -05:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2018-11-08 09:41:53 -06:00
|
|
|
void sendLastModificationTime(const std::shared_ptr<Session>& session,
|
|
|
|
DocumentBroker* documentBroker,
|
2019-08-30 12:37:55 -05:00
|
|
|
const std::chrono::system_clock::time_point& documentLastModifiedTime)
|
2018-11-08 09:41:53 -06:00
|
|
|
{
|
|
|
|
if (!session)
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::stringstream stream;
|
|
|
|
stream << "lastmodtime: " << documentLastModifiedTime;
|
|
|
|
std::string message = stream.str();
|
|
|
|
session->sendTextFrame(message);
|
|
|
|
if (documentBroker)
|
|
|
|
documentBroker->broadcastMessage(message);
|
|
|
|
}
|
|
|
|
|
2016-03-26 06:50:13 -05:00
|
|
|
}
|
|
|
|
|
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.
|
2019-11-28 12:05:04 -06:00
|
|
|
#if !MOBILEAPP
|
|
|
|
std::string decodedUri;
|
|
|
|
Poco::URI::decode(uri, decodedUri);
|
|
|
|
Poco::URI uriPublic(decodedUri);
|
|
|
|
#else
|
2019-11-21 07:29:51 -06:00
|
|
|
Poco::URI uriPublic(uri);
|
2019-11-28 12:05:04 -06:00
|
|
|
#endif
|
2016-03-23 07:41:18 -05:00
|
|
|
|
|
|
|
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.");
|
|
|
|
}
|
|
|
|
|
2016-11-17 01:06:48 -06:00
|
|
|
// We decoded access token before embedding it in loleaflet.html
|
|
|
|
// So, we need to decode it now to get its actual value
|
|
|
|
Poco::URI::QueryParameters queryParams = uriPublic.getQueryParameters();
|
|
|
|
for (auto& param: queryParams)
|
|
|
|
{
|
|
|
|
// look for encoded query params (access token as of now)
|
|
|
|
if (param.first == "access_token")
|
|
|
|
{
|
|
|
|
std::string decodedToken;
|
|
|
|
Poco::URI::decode(param.second, decodedToken);
|
|
|
|
param.second = decodedToken;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-21 21:43:12 -06:00
|
|
|
uriPublic.setQueryParameters(queryParams);
|
2016-03-23 07:41:18 -05:00
|
|
|
return uriPublic;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string DocumentBroker::getDocKey(const Poco::URI& uri)
|
|
|
|
{
|
2017-05-19 03:32:24 -05:00
|
|
|
// If multiple host-names are used to access us, then
|
2017-05-23 06:35:07 -05:00
|
|
|
// they must be aliases. Permission to access aliased hosts
|
|
|
|
// is checked at the point of accepting incoming connections.
|
|
|
|
// At this point storing the hostname artificially discriminates
|
|
|
|
// between aliases and forces same document (when opened from
|
2017-05-19 03:32:24 -05:00
|
|
|
// alias hosts) to load as separate documents and sharing doesn't
|
|
|
|
// work. Worse, saving overwrites one another.
|
|
|
|
std::string docKey;
|
2017-05-23 06:35:07 -05:00
|
|
|
Poco::URI::encode(uri.getPath(), "", docKey);
|
2017-05-19 03:32:24 -05:00
|
|
|
return docKey;
|
2016-03-23 07:41:18 -05:00
|
|
|
}
|
|
|
|
|
2017-03-07 11:34:01 -06:00
|
|
|
/// The Document Broker Poll - one of these in a thread per document
|
2017-03-12 12:56:42 -05:00
|
|
|
class DocumentBroker::DocumentBrokerPoll final : public TerminatingPoll
|
2017-03-07 11:34:01 -06:00
|
|
|
{
|
2017-03-12 12:56:42 -05:00
|
|
|
/// The DocumentBroker owning us.
|
|
|
|
DocumentBroker& _docBroker;
|
|
|
|
|
2017-03-07 11:34:01 -06:00
|
|
|
public:
|
2017-03-12 12:56:42 -05:00
|
|
|
DocumentBrokerPoll(const std::string &threadName, DocumentBroker& docBroker) :
|
|
|
|
TerminatingPoll(threadName),
|
|
|
|
_docBroker(docBroker)
|
2017-03-07 11:34:01 -06:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-05-20 17:28:12 -05:00
|
|
|
bool continuePolling() override
|
|
|
|
{
|
|
|
|
#if MOBILEAPP
|
|
|
|
if (MobileTerminationFlag)
|
|
|
|
{
|
|
|
|
LOG_TRC("Noticed MobileTerminationFlag.");
|
|
|
|
MobileTerminationFlag = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return TerminatingPoll::continuePolling();
|
|
|
|
}
|
|
|
|
|
2019-05-21 02:13:13 -05:00
|
|
|
void pollingThread() override
|
2017-03-07 11:34:01 -06:00
|
|
|
{
|
2017-03-12 12:56:42 -05:00
|
|
|
// Delegate to the docBroker.
|
|
|
|
_docBroker.pollThread();
|
2017-03-07 11:34:01 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-04-02 14:55:56 -05:00
|
|
|
std::atomic<unsigned> DocumentBroker::DocBrokerId(1);
|
|
|
|
|
2017-02-05 20:59:08 -06:00
|
|
|
DocumentBroker::DocumentBroker(const std::string& uri,
|
|
|
|
const Poco::URI& uriPublic,
|
2019-03-01 15:03:35 -06:00
|
|
|
const std::string& docKey) :
|
2019-12-11 21:38:50 -06:00
|
|
|
_limitLifeSeconds(0),
|
2017-02-05 20:59:08 -06:00
|
|
|
_uriOrig(uri),
|
2016-03-23 07:41:18 -05:00
|
|
|
_uriPublic(uriPublic),
|
|
|
|
_docKey(docKey),
|
2017-04-02 14:55:56 -05:00
|
|
|
_docId(Util::encodeId(DocBrokerId++, 3)),
|
2017-06-01 07:56:54 -05:00
|
|
|
_documentChangedInStorage(false),
|
2016-04-24 10:08:08 -05:00
|
|
|
_lastSaveTime(std::chrono::steady_clock::now()),
|
2017-03-25 23:52:34 -05:00
|
|
|
_lastSaveRequestTime(std::chrono::steady_clock::now() - std::chrono::milliseconds(COMMAND_TIMEOUT_MS)),
|
2016-04-21 23:11:24 -05:00
|
|
|
_markToDestroy(false),
|
2018-02-01 22:28:20 -06:00
|
|
|
_closeRequest(false),
|
2016-10-08 09:31:35 -05:00
|
|
|
_isLoaded(false),
|
|
|
|
_isModified(false),
|
2016-05-22 15:47:22 -05:00
|
|
|
_cursorPosX(0),
|
|
|
|
_cursorPosY(0),
|
2016-09-01 15:15:13 -05:00
|
|
|
_cursorWidth(0),
|
|
|
|
_cursorHeight(0),
|
2017-04-02 16:50:17 -05:00
|
|
|
_poll(new DocumentBrokerPoll("docbroker_" + _docId, *this)),
|
2017-03-12 21:11:25 -05:00
|
|
|
_stop(false),
|
2017-07-07 06:42:19 -05:00
|
|
|
_closeReason("stopped"),
|
2019-11-19 16:51:45 -06:00
|
|
|
_lockCtx(new LockContext()),
|
2016-10-11 07:39:56 -05:00
|
|
|
_tileVersion(0),
|
2019-11-12 03:50:33 -06:00
|
|
|
_debugRenderedTileCount(0),
|
|
|
|
_wopiLoadDuration(0)
|
2016-03-23 07:41:18 -05:00
|
|
|
{
|
|
|
|
assert(!_docKey.empty());
|
2019-03-01 15:03:35 -06:00
|
|
|
assert(!LOOLWSD::ChildRoot.empty());
|
2016-05-02 06:21:30 -05:00
|
|
|
|
2018-06-15 09:26:52 -05:00
|
|
|
LOG_INF("DocumentBroker [" << LOOLWSD::anonymizeUrl(_uriPublic.toString()) <<
|
2019-03-01 15:03:35 -06:00
|
|
|
"] created with docKey [" << _docKey << "]");
|
2017-03-04 17:07:17 -06:00
|
|
|
}
|
|
|
|
|
2017-03-13 07:00:31 -05:00
|
|
|
void DocumentBroker::startThread()
|
|
|
|
{
|
|
|
|
_poll->startThread();
|
|
|
|
}
|
|
|
|
|
2018-01-14 10:16:10 -06:00
|
|
|
void DocumentBroker::assertCorrectThread() const
|
2017-03-15 07:07:17 -05:00
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
_poll->assertCorrectThread();
|
2017-03-15 07:07:17 -05:00
|
|
|
}
|
|
|
|
|
2017-03-09 12:19:53 -06:00
|
|
|
// The inner heart of the DocumentBroker - our poll loop.
|
2017-03-07 19:46:16 -06:00
|
|
|
void DocumentBroker::pollThread()
|
2017-03-04 17:07:17 -06:00
|
|
|
{
|
2017-03-09 22:50:36 -06:00
|
|
|
LOG_INF("Starting docBroker polling thread for docKey [" << _docKey << "].");
|
|
|
|
|
2017-03-11 16:01:27 -06:00
|
|
|
_threadStart = std::chrono::steady_clock::now();
|
|
|
|
|
2017-03-04 17:07:17 -06:00
|
|
|
// Request a kit process for this doc.
|
2019-02-12 05:16:40 -06:00
|
|
|
#if !MOBILEAPP
|
2017-03-29 22:11:34 -05:00
|
|
|
do
|
|
|
|
{
|
|
|
|
static const int timeoutMs = COMMAND_TIMEOUT_MS * 5;
|
|
|
|
_childProcess = getNewChild_Blocks();
|
|
|
|
if (_childProcess ||
|
|
|
|
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() -
|
|
|
|
_threadStart).count() > timeoutMs)
|
|
|
|
break;
|
2019-02-13 16:42:05 -06:00
|
|
|
|
2018-01-14 19:49:11 -06:00
|
|
|
// Nominal time between retries, lest we busy-loop. getNewChild could also wait, so don't double that here.
|
2017-06-21 20:49:08 -05:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(CHILD_REBALANCE_INTERVAL_MS / 10));
|
2017-03-29 22:11:34 -05:00
|
|
|
}
|
2019-08-12 02:03:16 -05:00
|
|
|
while (!_stop && _poll->continuePolling() && !SigUtil::getTerminationFlag() && !SigUtil::getShutdownRequestFlag());
|
2018-09-13 11:16:00 -05:00
|
|
|
#else
|
|
|
|
_childProcess = getNewChild_Blocks(getPublicUri().getPath());
|
|
|
|
#endif
|
2017-03-29 22:11:34 -05:00
|
|
|
|
2017-03-07 19:46:16 -06:00
|
|
|
if (!_childProcess)
|
2017-03-04 17:07:17 -06:00
|
|
|
{
|
|
|
|
// Let the client know we can't serve now.
|
|
|
|
LOG_ERR("Failed to get new child.");
|
|
|
|
|
|
|
|
// FIXME: need to notify all clients and shut this down ...
|
2018-01-14 19:49:11 -06:00
|
|
|
// FIXME: return something good down the websocket ...
|
2017-03-04 17:07:17 -06:00
|
|
|
#if 0
|
|
|
|
const std::string msg = SERVICE_UNAVAILABLE_INTERNAL_ERROR;
|
2017-03-29 19:03:01 -05:00
|
|
|
ws.sendMessage(msg);
|
2017-03-04 17:07:17 -06:00
|
|
|
// abnormal close frame handshake
|
|
|
|
ws.shutdown(WebSocketHandler::StatusCodes::ENDPOINT_GOING_AWAY);
|
|
|
|
#endif
|
2018-01-14 19:49:11 -06:00
|
|
|
stop("Failed to get new child.");
|
|
|
|
|
|
|
|
// Stop to mark it done and cleanup.
|
|
|
|
_poll->stop();
|
|
|
|
_poll->removeSockets();
|
|
|
|
|
|
|
|
// Async cleanup.
|
|
|
|
LOOLWSD::doHousekeeping();
|
2017-03-29 22:11:34 -05:00
|
|
|
|
|
|
|
LOG_INF("Finished docBroker polling thread for docKey [" << _docKey << "].");
|
2017-03-07 22:46:02 -06:00
|
|
|
return;
|
2017-03-04 17:07:17 -06:00
|
|
|
}
|
2017-03-07 19:46:16 -06:00
|
|
|
|
|
|
|
_childProcess->setDocumentBroker(shared_from_this());
|
2017-03-19 13:49:52 -05:00
|
|
|
LOG_INF("Doc [" << _docKey << "] attached to child [" << _childProcess->getPid() << "].");
|
2017-03-04 17:07:17 -06:00
|
|
|
|
2017-04-18 23:54:42 -05:00
|
|
|
static const bool AutoSaveEnabled = !std::getenv("LOOL_NO_AUTOSAVE");
|
2019-03-21 07:25:15 -05:00
|
|
|
|
|
|
|
#if !MOBILEAPP
|
2017-05-07 12:28:57 -05:00
|
|
|
static const size_t IdleDocTimeoutSecs = LOOLWSD::getConfigValue<int>(
|
|
|
|
"per_document.idle_timeout_secs", 3600);
|
2018-01-14 10:16:10 -06:00
|
|
|
|
2017-06-03 16:53:57 -05:00
|
|
|
// Used to accumulate B/W deltas.
|
|
|
|
uint64_t adminSent = 0;
|
|
|
|
uint64_t adminRecv = 0;
|
|
|
|
auto lastBWUpdateTime = std::chrono::steady_clock::now();
|
2019-06-21 06:35:17 -05:00
|
|
|
auto lastClipboardHashUpdateTime = std::chrono::steady_clock::now();
|
2018-11-29 01:52:57 -06:00
|
|
|
|
2019-12-18 14:28:14 -06:00
|
|
|
const int limit_load_secs =
|
|
|
|
#ifdef ENABLE_DEBUG
|
|
|
|
// paused waiting for a debugger to attach
|
|
|
|
// ignore load time out
|
|
|
|
std::getenv("PAUSEFORDEBUGGER") ? -1 :
|
|
|
|
#endif
|
|
|
|
LOOLWSD::getConfigValue<int>("per_document.limit_load_secs", 100);
|
|
|
|
|
2019-09-19 22:42:13 -05:00
|
|
|
const auto loadDeadline = std::chrono::steady_clock::now() + std::chrono::seconds(limit_load_secs);
|
2018-09-05 07:11:05 -05:00
|
|
|
#endif
|
2019-08-07 06:40:27 -05:00
|
|
|
auto last30SecCheckTime = std::chrono::steady_clock::now();
|
2017-06-03 16:53:57 -05:00
|
|
|
|
2017-03-04 17:07:17 -06:00
|
|
|
// Main polling loop goodness.
|
2019-08-08 02:10:59 -05:00
|
|
|
while (!_stop && _poll->continuePolling() && !SigUtil::getTerminationFlag())
|
2017-03-04 17:07:17 -06:00
|
|
|
{
|
2018-09-05 07:11:05 -05:00
|
|
|
_poll->poll(SocketPoll::DefaultPollTimeoutMs);
|
|
|
|
|
2018-11-07 16:11:21 -06:00
|
|
|
const auto now = std::chrono::steady_clock::now();
|
|
|
|
|
2019-02-12 05:16:40 -06:00
|
|
|
#if !MOBILEAPP
|
2019-10-28 08:26:15 -05:00
|
|
|
// a tile's data is ~8k, a 4k screen is ~128 256x256 tiles
|
2019-11-05 18:33:37 -06:00
|
|
|
if (_tileCache)
|
|
|
|
_tileCache->setMaxCacheSize(8 * 1024 * 128 * _sessions.size());
|
2019-10-28 08:26:15 -05:00
|
|
|
|
2018-11-07 17:00:32 -06:00
|
|
|
if (!_isLoaded && (limit_load_secs > 0) && (now > loadDeadline))
|
|
|
|
{
|
2019-10-02 07:38:45 -05:00
|
|
|
LOG_WRN("Doc [" << _docKey << "] is taking too long to load. Will kill process ["
|
|
|
|
<< _childProcess->getPid() << "]. per_document.limit_load_secs set to "
|
|
|
|
<< limit_load_secs << " secs.");
|
|
|
|
broadcastMessage("error: cmd=load kind=docloadtimeout");
|
|
|
|
|
2018-11-07 17:00:32 -06:00
|
|
|
// Brutal but effective.
|
|
|
|
if (_childProcess)
|
|
|
|
_childProcess->terminate();
|
2019-09-19 22:42:13 -05:00
|
|
|
|
2018-11-07 17:00:32 -06:00
|
|
|
stop("Load timed out");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-12-11 21:38:50 -06:00
|
|
|
if (_limitLifeSeconds > 0 &&
|
|
|
|
std::chrono::duration_cast<std::chrono::seconds>(now - _threadStart).count() > _limitLifeSeconds)
|
|
|
|
{
|
|
|
|
LOG_WRN("Doc [" << _docKey << "] is taking too long to convert. Will kill process ["
|
|
|
|
<< _childProcess->getPid() << "]. per_document.limit_convert_secs set to "
|
|
|
|
<< _limitLifeSeconds << " secs.");
|
|
|
|
broadcastMessage("error: cmd=load kind=docexpired");
|
|
|
|
|
|
|
|
// Brutal but effective.
|
|
|
|
if (_childProcess)
|
|
|
|
_childProcess->terminate();
|
|
|
|
|
|
|
|
stop("Load timed out");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-06-03 16:53:57 -05:00
|
|
|
if (std::chrono::duration_cast<std::chrono::milliseconds>
|
2019-10-02 07:38:45 -05:00
|
|
|
(now - lastBWUpdateTime).count() >= COMMAND_TIMEOUT_MS)
|
2017-06-03 16:53:57 -05:00
|
|
|
{
|
|
|
|
lastBWUpdateTime = now;
|
|
|
|
uint64_t sent, recv;
|
|
|
|
getIOStats(sent, recv);
|
|
|
|
// send change since last notification.
|
|
|
|
Admin::instance().addBytes(getDocKey(),
|
|
|
|
// connection drop transiently reduces this.
|
2017-06-09 20:28:16 -05:00
|
|
|
(sent > adminSent ? (sent - adminSent): uint64_t(0)),
|
|
|
|
(recv > adminRecv ? (recv - adminRecv): uint64_t(0)));
|
2017-06-03 16:53:57 -05:00
|
|
|
adminSent = sent;
|
|
|
|
adminRecv = recv;
|
2019-10-02 07:38:45 -05:00
|
|
|
LOG_TRC("Doc [" << _docKey << "] added stats sent: " << sent << ", recv: " << recv << " bytes to totals.");
|
2017-06-03 16:53:57 -05:00
|
|
|
}
|
2019-11-27 02:43:05 -06:00
|
|
|
|
|
|
|
if (_storage && _lockCtx->needsRefresh(now))
|
|
|
|
refreshLock();
|
2018-09-05 07:11:05 -05:00
|
|
|
#endif
|
2017-06-03 16:53:57 -05:00
|
|
|
|
2018-01-14 17:59:11 -06:00
|
|
|
if (isSaving() &&
|
2017-04-18 23:54:42 -05:00
|
|
|
std::chrono::duration_cast<std::chrono::milliseconds>
|
|
|
|
(now - _lastSaveRequestTime).count() <= COMMAND_TIMEOUT_MS)
|
|
|
|
{
|
2018-01-14 10:16:10 -06:00
|
|
|
// We are saving, nothing more to do but wait (until we save or we timeout).
|
2017-04-18 23:54:42 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-08-12 02:03:16 -05:00
|
|
|
if (SigUtil::getShutdownRequestFlag() || _closeRequest)
|
2017-04-18 23:54:42 -05:00
|
|
|
{
|
2019-08-12 02:03:16 -05:00
|
|
|
const std::string reason = SigUtil::getShutdownRequestFlag() ? "recycling" : _closeReason;
|
2018-02-01 22:28:20 -06:00
|
|
|
LOG_INF("Autosaving DocumentBroker for docKey [" << getDocKey() << "] for " << reason);
|
2018-01-14 23:49:14 -06:00
|
|
|
if (!autoSave(isPossiblyModified()))
|
|
|
|
{
|
2018-02-01 22:28:20 -06:00
|
|
|
LOG_INF("Terminating DocumentBroker for docKey [" << getDocKey() << "].");
|
|
|
|
stop(reason);
|
2018-01-14 23:49:14 -06:00
|
|
|
}
|
2017-04-18 23:54:42 -05:00
|
|
|
}
|
|
|
|
else if (AutoSaveEnabled && !_stop &&
|
|
|
|
std::chrono::duration_cast<std::chrono::seconds>(now - last30SecCheckTime).count() >= 30)
|
2017-03-09 12:19:53 -06:00
|
|
|
{
|
2017-04-18 23:54:42 -05:00
|
|
|
LOG_TRC("Triggering an autosave.");
|
2017-03-09 12:19:53 -06:00
|
|
|
autoSave(false);
|
|
|
|
last30SecCheckTime = std::chrono::steady_clock::now();
|
|
|
|
}
|
2017-03-19 18:01:03 -05:00
|
|
|
|
2019-06-21 06:35:17 -05:00
|
|
|
#if !MOBILEAPP
|
|
|
|
if (std::chrono::duration_cast<std::chrono::minutes>(now - lastClipboardHashUpdateTime).count() >= 2)
|
2019-07-04 04:50:33 -05:00
|
|
|
for (auto &it : _sessions)
|
|
|
|
{
|
|
|
|
if (it.second->staleWaitDisconnect(now))
|
|
|
|
{
|
|
|
|
std::string id = it.second->getId();
|
|
|
|
LOG_WRN("Unusual, Kit session " + id + " failed its disconnect handshake, killing");
|
|
|
|
finalRemoveSession(id);
|
|
|
|
break; // it invalid.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (std::chrono::duration_cast<std::chrono::minutes>(now - lastClipboardHashUpdateTime).count() >= 5)
|
2019-06-21 06:35:17 -05:00
|
|
|
{
|
2019-06-22 15:23:12 -05:00
|
|
|
LOG_TRC("Rotating clipboard keys");
|
2019-07-04 04:50:33 -05:00
|
|
|
for (auto &it : _sessions)
|
2019-06-22 15:23:12 -05:00
|
|
|
it.second->rotateClipboardKey(true);
|
2019-07-04 04:50:33 -05:00
|
|
|
|
2019-06-24 15:44:07 -05:00
|
|
|
lastClipboardHashUpdateTime = now;
|
2019-06-21 06:35:17 -05:00
|
|
|
}
|
|
|
|
|
2019-03-21 07:25:15 -05:00
|
|
|
if (false)
|
|
|
|
;
|
2017-04-06 11:58:41 -05:00
|
|
|
// Remove idle documents after 1 hour.
|
2019-03-21 07:25:15 -05:00
|
|
|
else if (isLoaded() && getIdleTimeSecs() >= IdleDocTimeoutSecs)
|
2018-01-14 23:49:14 -06:00
|
|
|
{
|
|
|
|
// Stop if there is nothing to save.
|
|
|
|
LOG_INF("Autosaving idle DocumentBroker for docKey [" << getDocKey() << "] to kill.");
|
|
|
|
if (!autoSave(isPossiblyModified()))
|
|
|
|
{
|
|
|
|
LOG_INF("Terminating idle DocumentBroker for docKey [" << getDocKey() << "].");
|
|
|
|
stop("idle");
|
|
|
|
}
|
|
|
|
}
|
2019-03-21 07:25:15 -05:00
|
|
|
#endif
|
2018-01-14 23:49:14 -06:00
|
|
|
else if (_sessions.empty() && (isLoaded() || _markToDestroy))
|
2017-04-06 11:58:41 -05:00
|
|
|
{
|
2018-01-14 23:49:14 -06:00
|
|
|
// If all sessions have been removed, no reason to linger.
|
|
|
|
LOG_INF("Terminating dead DocumentBroker for docKey [" << getDocKey() << "].");
|
|
|
|
stop("dead");
|
2017-03-19 18:01:03 -05:00
|
|
|
}
|
2017-03-09 12:19:53 -06:00
|
|
|
}
|
|
|
|
|
2017-04-04 23:26:09 -05:00
|
|
|
LOG_INF("Finished polling doc [" << _docKey << "]. stop: " << _stop << ", continuePolling: " <<
|
2019-08-12 02:03:16 -05:00
|
|
|
_poll->continuePolling() << ", ShutdownRequestFlag: " << SigUtil::getShutdownRequestFlag() <<
|
2019-08-08 02:10:59 -05:00
|
|
|
", TerminationFlag: " << SigUtil::getTerminationFlag() << ", closeReason: " << _closeReason << ". Flushing socket.");
|
2017-04-09 18:41:29 -05:00
|
|
|
|
2018-01-12 10:16:56 -06:00
|
|
|
if (_isModified)
|
|
|
|
{
|
|
|
|
std::stringstream state;
|
|
|
|
dumpState(state);
|
|
|
|
LOG_ERR("DocumentBroker stopping although modified " << state.str());
|
|
|
|
}
|
|
|
|
|
2017-05-21 18:13:55 -05:00
|
|
|
// Flush socket data first.
|
2019-10-18 07:10:12 -05:00
|
|
|
constexpr int flushTimeoutMs = POLL_TIMEOUT_MS * 2; // ~1000ms
|
|
|
|
LOG_INF("Flushing socket for doc ["
|
|
|
|
<< _docKey << "] for " << flushTimeoutMs << " ms. stop: " << _stop
|
|
|
|
<< ", continuePolling: " << _poll->continuePolling()
|
|
|
|
<< ", ShutdownRequestFlag: " << SigUtil::getShutdownRequestFlag()
|
|
|
|
<< ", TerminationFlag: " << SigUtil::getTerminationFlag()
|
|
|
|
<< ". Terminating child with reason: [" << _closeReason << "].");
|
2017-03-27 19:50:30 -05:00
|
|
|
const auto flushStartTime = std::chrono::steady_clock::now();
|
|
|
|
while (_poll->getSocketCount())
|
|
|
|
{
|
|
|
|
const auto now = std::chrono::steady_clock::now();
|
|
|
|
const int elapsedMs = std::chrono::duration_cast<std::chrono::milliseconds>(now - flushStartTime).count();
|
|
|
|
if (elapsedMs > flushTimeoutMs)
|
|
|
|
break;
|
|
|
|
|
|
|
|
_poll->poll(std::min(flushTimeoutMs - elapsedMs, POLL_TIMEOUT_MS / 5));
|
|
|
|
}
|
2017-03-26 22:10:24 -05:00
|
|
|
|
2018-01-11 00:29:08 -06:00
|
|
|
LOG_INF("Finished flushing socket for doc [" << _docKey << "]. stop: " << _stop << ", continuePolling: " <<
|
2019-08-12 02:03:16 -05:00
|
|
|
_poll->continuePolling() << ", ShutdownRequestFlag: " << SigUtil::getShutdownRequestFlag() <<
|
2019-08-08 02:10:59 -05:00
|
|
|
", TerminationFlag: " << SigUtil::getTerminationFlag() << ". Terminating child with reason: [" << _closeReason << "].");
|
2018-01-11 00:29:08 -06:00
|
|
|
|
2017-05-21 18:13:55 -05:00
|
|
|
// Terminate properly while we can.
|
2017-07-07 06:42:19 -05:00
|
|
|
terminateChild(_closeReason);
|
2017-05-21 18:13:55 -05:00
|
|
|
|
2017-04-06 00:32:39 -05:00
|
|
|
// Stop to mark it done and cleanup.
|
|
|
|
_poll->stop();
|
2017-04-06 01:56:21 -05:00
|
|
|
_poll->removeSockets();
|
2017-04-06 00:32:39 -05:00
|
|
|
|
2019-02-12 05:16:40 -06:00
|
|
|
#if !MOBILEAPP
|
2017-04-05 15:31:15 -05:00
|
|
|
// Async cleanup.
|
2017-04-02 18:56:42 -05:00
|
|
|
LOOLWSD::doHousekeeping();
|
2018-09-05 07:11:05 -05:00
|
|
|
#endif
|
2017-04-02 18:56:42 -05:00
|
|
|
|
2019-02-14 15:40:33 -06:00
|
|
|
if (_tileCache)
|
|
|
|
_tileCache->clear();
|
2017-04-18 05:13:04 -05:00
|
|
|
|
2017-03-09 22:50:36 -06:00
|
|
|
LOG_INF("Finished docBroker polling thread for docKey [" << _docKey << "].");
|
2017-03-04 17:07:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DocumentBroker::isAlive() const
|
|
|
|
{
|
2017-04-12 20:19:51 -05:00
|
|
|
if (!_stop || _poll->isAlive())
|
|
|
|
return true; // Polling thread not started or still running.
|
2017-03-04 17:07:17 -06:00
|
|
|
|
2017-03-12 21:11:25 -05:00
|
|
|
// Shouldn't have live child process outside of the polling thread.
|
|
|
|
return _childProcess && _childProcess->isAlive();
|
2016-03-23 07:41:18 -05:00
|
|
|
}
|
|
|
|
|
2016-10-22 09:25:57 -05:00
|
|
|
DocumentBroker::~DocumentBroker()
|
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2017-04-03 23:11:06 -05:00
|
|
|
|
2019-02-12 05:16:40 -06:00
|
|
|
#if !MOBILEAPP
|
2016-10-22 09:25:57 -05:00
|
|
|
Admin::instance().rmDoc(_docKey);
|
2018-09-05 07:11:05 -05:00
|
|
|
#endif
|
2016-10-22 09:25:57 -05:00
|
|
|
|
2017-04-18 23:54:42 -05:00
|
|
|
LOG_INF("~DocumentBroker [" << _docKey <<
|
2016-11-06 10:59:59 -06:00
|
|
|
"] destroyed with " << _sessions.size() << " sessions left.");
|
2016-10-30 13:31:34 -05:00
|
|
|
|
2017-03-31 11:28:20 -05:00
|
|
|
// Do this early - to avoid operating on _childProcess from two threads.
|
|
|
|
_poll->joinThread();
|
|
|
|
|
2016-11-03 22:05:48 -05:00
|
|
|
if (!_sessions.empty())
|
2017-04-18 23:54:42 -05:00
|
|
|
LOG_WRN("DocumentBroker [" << _docKey << "] still has unremoved sessions.");
|
2016-11-05 22:00:16 -05:00
|
|
|
|
|
|
|
// Need to first make sure the child exited, socket closed,
|
|
|
|
// and thread finished before we are destroyed.
|
|
|
|
_childProcess.reset();
|
2016-10-22 09:25:57 -05:00
|
|
|
}
|
|
|
|
|
2017-03-31 14:58:33 -05:00
|
|
|
void DocumentBroker::joinThread()
|
|
|
|
{
|
|
|
|
_poll->joinThread();
|
|
|
|
}
|
|
|
|
|
2018-01-14 19:49:11 -06:00
|
|
|
void DocumentBroker::stop(const std::string& reason)
|
2017-04-05 15:31:15 -05:00
|
|
|
{
|
2019-11-03 14:03:11 -06:00
|
|
|
LOG_DBG("Stopping DocumentBroker for docKey [" << _docKey << "] with reason: " << reason);
|
2018-01-14 19:49:11 -06:00
|
|
|
_closeReason = reason; // used later in the polling loop
|
2017-04-05 15:31:15 -05:00
|
|
|
_stop = true;
|
|
|
|
_poll->wakeup();
|
|
|
|
}
|
|
|
|
|
2017-03-25 12:56:17 -05:00
|
|
|
bool DocumentBroker::load(const std::shared_ptr<ClientSession>& session, const std::string& jailId)
|
2016-03-23 07:41:18 -05:00
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2016-11-06 22:14:23 -06:00
|
|
|
|
|
|
|
const std::string sessionId = session->getId();
|
|
|
|
|
2016-11-06 12:54:00 -06:00
|
|
|
LOG_INF("Loading [" << _docKey << "] for session [" << sessionId << "] and jail [" << jailId << "].");
|
|
|
|
|
2016-10-20 16:09:00 -05:00
|
|
|
{
|
|
|
|
bool result;
|
|
|
|
if (UnitWSD::get().filterLoad(sessionId, jailId, result))
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-10-16 11:40:52 -05:00
|
|
|
if (_markToDestroy)
|
2016-04-06 23:32:28 -05:00
|
|
|
{
|
2016-10-16 11:40:52 -05:00
|
|
|
// Tearing down.
|
2016-11-06 12:54:00 -06:00
|
|
|
LOG_WRN("Will not load document marked to destroy. DocKey: [" << _docKey << "].");
|
2016-10-16 11:40:52 -05:00
|
|
|
return false;
|
2016-04-06 23:32:28 -05:00
|
|
|
}
|
2016-03-23 07:41:18 -05:00
|
|
|
|
|
|
|
_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
|
2018-02-07 03:17:59 -06:00
|
|
|
const Poco::Path jailPath(JAILED_DOCUMENT_ROOT, jailId);
|
2016-08-01 03:05:37 -05:00
|
|
|
std::string jailRoot = getJailRoot();
|
2016-11-06 12:54:00 -06:00
|
|
|
|
|
|
|
LOG_INF("jailPath: " << jailPath.toString() << ", jailRoot: " << jailRoot);
|
2016-08-01 03:05:37 -05:00
|
|
|
|
2016-12-19 05:21:42 -06:00
|
|
|
bool firstInstance = false;
|
2016-10-14 05:09:43 -05:00
|
|
|
if (_storage == nullptr)
|
|
|
|
{
|
2017-01-21 21:43:12 -06:00
|
|
|
// Pass the public URI to storage as it needs to load using the token
|
|
|
|
// and other storage-specific data provided in the URI.
|
2017-05-12 10:42:03 -05:00
|
|
|
const Poco::URI& uriPublic = session->getPublicUri();
|
2018-06-15 09:26:52 -05:00
|
|
|
LOG_DBG("Loading, and creating new storage instance for URI [" << LOOLWSD::anonymizeUrl(uriPublic.toString()) << "].");
|
2017-05-12 10:42:03 -05:00
|
|
|
|
2019-04-26 05:06:17 -05:00
|
|
|
try
|
|
|
|
{
|
|
|
|
_storage = StorageBase::create(uriPublic, jailRoot, jailPath.toString());
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
session->sendMessage("loadstorage: failed");
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2016-11-06 12:54:00 -06:00
|
|
|
if (_storage == nullptr)
|
2016-10-26 06:15:28 -05:00
|
|
|
{
|
2016-11-06 12:54:00 -06:00
|
|
|
// We should get an exception, not null.
|
|
|
|
LOG_ERR("Failed to create Storage instance for [" << _docKey << "] in " << jailPath.toString());
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-19 05:21:42 -06:00
|
|
|
firstInstance = true;
|
2016-11-06 12:54:00 -06:00
|
|
|
}
|
2016-10-26 06:15:28 -05:00
|
|
|
|
2016-11-06 12:54:00 -06:00
|
|
|
assert(_storage != nullptr);
|
2016-10-26 06:15:28 -05:00
|
|
|
|
2016-12-13 03:13:58 -06:00
|
|
|
// Call the storage specific fileinfo functions
|
2018-07-08 21:50:09 -05:00
|
|
|
std::string userId, username;
|
2017-05-28 11:20:49 -05:00
|
|
|
std::string userExtraInfo;
|
2017-09-04 08:40:04 -05:00
|
|
|
std::string watermarkText;
|
2019-05-21 22:33:26 -05:00
|
|
|
std::string templateSource;
|
2018-09-05 07:11:05 -05:00
|
|
|
|
2019-02-12 05:16:40 -06:00
|
|
|
#if !MOBILEAPP
|
2018-10-30 14:52:26 -05:00
|
|
|
std::chrono::duration<double> getInfoCallDuration(0);
|
2017-01-21 21:43:12 -06:00
|
|
|
WopiStorage* wopiStorage = dynamic_cast<WopiStorage*>(_storage.get());
|
|
|
|
if (wopiStorage != nullptr)
|
2016-11-06 12:54:00 -06:00
|
|
|
{
|
2019-11-19 16:51:45 -06:00
|
|
|
std::unique_ptr<WopiStorage::WOPIFileInfo> wopifileinfo =
|
|
|
|
wopiStorage->getWOPIFileInfo(session->getAuthorization(), *_lockCtx);
|
2018-11-21 02:07:52 -06:00
|
|
|
userId = wopifileinfo->getUserId();
|
|
|
|
username = wopifileinfo->getUsername();
|
|
|
|
userExtraInfo = wopifileinfo->getUserExtraInfo();
|
|
|
|
watermarkText = wopifileinfo->getWatermarkText();
|
2019-05-21 22:33:26 -05:00
|
|
|
templateSource = wopifileinfo->getTemplateSource();
|
2016-10-25 02:13:00 -05:00
|
|
|
|
2018-11-21 02:07:52 -06:00
|
|
|
if (!wopifileinfo->getUserCanWrite() ||
|
2017-06-06 22:43:48 -05:00
|
|
|
LOOLWSD::IsViewFileExtension(wopiStorage->getFileExtension()))
|
2016-10-26 06:15:28 -05:00
|
|
|
{
|
2016-11-06 12:54:00 -06:00
|
|
|
LOG_DBG("Setting the session as readonly");
|
2016-11-06 21:11:35 -06:00
|
|
|
session->setReadOnly();
|
2016-10-26 06:15:28 -05:00
|
|
|
}
|
|
|
|
|
2016-11-10 06:21:39 -06:00
|
|
|
// Construct a JSON containing relevant WOPI host properties
|
|
|
|
Object::Ptr wopiInfo = new Object();
|
2018-11-21 02:07:52 -06:00
|
|
|
if (!wopifileinfo->getPostMessageOrigin().empty())
|
2016-10-16 11:40:52 -05:00
|
|
|
{
|
2017-06-22 06:34:59 -05:00
|
|
|
// Update the scheme to https if ssl or ssl termination is on
|
2018-11-21 02:07:52 -06:00
|
|
|
if (wopifileinfo->getPostMessageOrigin().substr(0, 7) == "http://" &&
|
2017-06-22 06:34:59 -05:00
|
|
|
(LOOLWSD::isSSLEnabled() || LOOLWSD::isSSLTermination()))
|
|
|
|
{
|
2018-11-21 02:07:52 -06:00
|
|
|
wopifileinfo->getPostMessageOrigin().replace(0, 4, "https");
|
2019-02-13 16:42:05 -06:00
|
|
|
LOG_DBG("Updating PostMessageOrigin scheme to HTTPS. Updated origin is [" << wopifileinfo->getPostMessageOrigin() << "].");
|
2017-06-22 06:34:59 -05:00
|
|
|
}
|
|
|
|
|
2018-11-21 02:07:52 -06:00
|
|
|
wopiInfo->set("PostMessageOrigin", wopifileinfo->getPostMessageOrigin());
|
2016-10-16 11:40:52 -05:00
|
|
|
}
|
|
|
|
|
2016-12-13 06:31:50 -06:00
|
|
|
// If print, export are disabled, order client to hide these options in the UI
|
2018-11-21 02:07:52 -06:00
|
|
|
if (wopifileinfo->getDisablePrint())
|
|
|
|
wopifileinfo->setHidePrintOption(true);
|
|
|
|
if (wopifileinfo->getDisableExport())
|
|
|
|
wopifileinfo->setHideExportOption(true);
|
2016-12-13 06:31:50 -06:00
|
|
|
|
2018-11-29 01:42:43 -06:00
|
|
|
wopiInfo->set("BaseFileName", wopiStorage->getFileInfo().getFilename());
|
2018-12-18 06:14:38 -06:00
|
|
|
|
|
|
|
if (!wopifileinfo->getTemplateSaveAs().empty())
|
|
|
|
wopiInfo->set("TemplateSaveAs", wopifileinfo->getTemplateSaveAs());
|
|
|
|
|
2019-05-21 22:33:26 -05:00
|
|
|
if (!templateSource.empty())
|
|
|
|
wopiInfo->set("TemplateSource", templateSource);
|
|
|
|
|
2018-11-21 02:07:52 -06:00
|
|
|
wopiInfo->set("HidePrintOption", wopifileinfo->getHidePrintOption());
|
|
|
|
wopiInfo->set("HideSaveOption", wopifileinfo->getHideSaveOption());
|
|
|
|
wopiInfo->set("HideExportOption", wopifileinfo->getHideExportOption());
|
|
|
|
wopiInfo->set("DisablePrint", wopifileinfo->getDisablePrint());
|
|
|
|
wopiInfo->set("DisableExport", wopifileinfo->getDisableExport());
|
|
|
|
wopiInfo->set("DisableCopy", wopifileinfo->getDisableCopy());
|
|
|
|
wopiInfo->set("DisableInactiveMessages", wopifileinfo->getDisableInactiveMessages());
|
2019-06-04 06:57:53 -05:00
|
|
|
wopiInfo->set("DownloadAsPostMessage", wopifileinfo->getDownloadAsPostMessage());
|
2018-11-21 02:07:52 -06:00
|
|
|
wopiInfo->set("UserCanNotWriteRelative", wopifileinfo->getUserCanNotWriteRelative());
|
|
|
|
wopiInfo->set("EnableInsertRemoteImage", wopifileinfo->getEnableInsertRemoteImage());
|
|
|
|
wopiInfo->set("EnableShare", wopifileinfo->getEnableShare());
|
2018-12-15 08:23:58 -06:00
|
|
|
wopiInfo->set("HideUserList", wopifileinfo->getHideUserList());
|
2019-04-30 09:21:44 -05:00
|
|
|
wopiInfo->set("SupportsRename", wopifileinfo->getSupportsRename());
|
|
|
|
wopiInfo->set("UserCanRename", wopifileinfo->getUserCanRename());
|
2018-11-21 02:07:52 -06:00
|
|
|
if (wopifileinfo->getHideChangeTrackingControls() != WopiStorage::WOPIFileInfo::TriState::Unset)
|
|
|
|
wopiInfo->set("HideChangeTrackingControls", wopifileinfo->getHideChangeTrackingControls() == WopiStorage::WOPIFileInfo::TriState::True);
|
2016-11-10 06:21:39 -06:00
|
|
|
|
|
|
|
std::ostringstream ossWopiInfo;
|
|
|
|
wopiInfo->stringify(ossWopiInfo);
|
2018-07-17 01:01:05 -05:00
|
|
|
const std::string wopiInfoString = ossWopiInfo.str();
|
|
|
|
LOG_TRC("Sending wopi info to client: " << wopiInfoString);
|
|
|
|
|
2017-05-18 13:05:45 -05:00
|
|
|
// Contains PostMessageOrigin property which is necessary to post messages to parent
|
|
|
|
// frame. Important to send this message immediately and not enqueue it so that in case
|
|
|
|
// document load fails, loleaflet is able to tell its parent frame via PostMessage API.
|
2018-07-17 01:01:05 -05:00
|
|
|
session->sendMessage("wopi: " + wopiInfoString);
|
2016-11-10 06:21:39 -06:00
|
|
|
|
2016-11-08 07:37:28 -06:00
|
|
|
// Mark the session as 'Document owner' if WOPI hosts supports it
|
2018-11-29 01:42:43 -06:00
|
|
|
if (userId == _storage->getFileInfo().getOwnerId())
|
2016-11-08 07:37:28 -06:00
|
|
|
{
|
2017-05-20 12:28:09 -05:00
|
|
|
LOG_DBG("Session [" << sessionId << "] is the document owner");
|
2016-11-08 07:37:28 -06:00
|
|
|
session->setDocumentOwner(true);
|
|
|
|
}
|
|
|
|
|
2018-11-21 02:07:52 -06:00
|
|
|
getInfoCallDuration = wopifileinfo->getCallDuration();
|
2016-12-13 03:13:58 -06:00
|
|
|
|
|
|
|
// Pass the ownership to client session
|
|
|
|
session->setWopiFileInfo(wopifileinfo);
|
2016-11-06 12:54:00 -06:00
|
|
|
}
|
2017-01-21 21:43:12 -06:00
|
|
|
else
|
2018-09-05 07:11:05 -05:00
|
|
|
#endif
|
2016-11-06 12:54:00 -06:00
|
|
|
{
|
2017-01-21 21:43:12 -06:00
|
|
|
LocalStorage* localStorage = dynamic_cast<LocalStorage*>(_storage.get());
|
|
|
|
if (localStorage != nullptr)
|
|
|
|
{
|
2017-05-12 10:42:03 -05:00
|
|
|
std::unique_ptr<LocalStorage::LocalFileInfo> localfileinfo = localStorage->getLocalFileInfo();
|
2018-11-27 02:09:21 -06:00
|
|
|
userId = localfileinfo->getUserId();
|
|
|
|
username = localfileinfo->getUsername();
|
2017-06-14 06:51:53 -05:00
|
|
|
|
|
|
|
if (LOOLWSD::IsViewFileExtension(localStorage->getFileExtension()))
|
|
|
|
{
|
|
|
|
LOG_DBG("Setting the session as readonly");
|
|
|
|
session->setReadOnly();
|
|
|
|
}
|
2017-01-21 21:43:12 -06:00
|
|
|
}
|
2016-11-06 12:54:00 -06:00
|
|
|
}
|
2016-10-14 05:09:43 -05:00
|
|
|
|
2018-11-30 08:10:28 -06:00
|
|
|
|
2017-10-03 22:54:05 -05:00
|
|
|
#if ENABLE_SUPPORT_KEY
|
|
|
|
if (!LOOLWSD::OverrideWatermark.empty())
|
|
|
|
watermarkText = LOOLWSD::OverrideWatermark;
|
|
|
|
#endif
|
|
|
|
|
2018-06-10 10:42:15 -05:00
|
|
|
LOG_DBG("Setting username [" << LOOLWSD::anonymizeUsername(username) << "] and userId [" <<
|
2018-07-08 21:50:09 -05:00
|
|
|
LOOLWSD::anonymizeUsername(userId) << "] for session [" << sessionId << "]");
|
|
|
|
|
|
|
|
session->setUserId(userId);
|
2016-11-06 21:11:35 -06:00
|
|
|
session->setUserName(username);
|
2017-05-28 11:20:49 -05:00
|
|
|
session->setUserExtraInfo(userExtraInfo);
|
2017-09-04 08:40:04 -05:00
|
|
|
session->setWatermarkText(watermarkText);
|
2020-01-02 15:11:54 -06:00
|
|
|
session->recalcCanonicalViewId(_sessions);
|
2019-10-15 07:35:35 -05:00
|
|
|
|
2016-12-22 06:48:20 -06:00
|
|
|
// Basic file information was stored by the above getWOPIFileInfo() or getLocalFileInfo() calls
|
2018-02-07 03:17:59 -06:00
|
|
|
const StorageBase::FileInfo fileInfo = _storage->getFileInfo();
|
2016-11-06 12:54:00 -06:00
|
|
|
if (!fileInfo.isValid())
|
|
|
|
{
|
2017-05-12 10:42:03 -05:00
|
|
|
LOG_ERR("Invalid fileinfo for URI [" << session->getPublicUri().toString() << "].");
|
2016-11-06 12:54:00 -06:00
|
|
|
return false;
|
|
|
|
}
|
2016-04-25 19:44:24 -05:00
|
|
|
|
2016-12-19 05:21:42 -06:00
|
|
|
if (firstInstance)
|
WIP: Check if the document has been modified behind our back
For now, do the check only when a new session connects to the
document, because at that point we fetch the document information (in
separate function for WOPI and local files) and construct the
FileInfo, including timestamp.
For now, just log an ERR message if we notice that the document in its
storage system (WOPI or local file system) has an unexpected last
modified time. What should we do? If we don't have unsaved changes,
most likely we should just silently reload the document and force all
sessions to refresh. But if we have unsaved changes, and the document
has changed underneath, we have a problem.
We need to fetch the timestamp also also after saving ("persisting")
as we can't assume that the clock on the machine running loolwsd and
that of the storage (as reported in the WOPI case in CheckFileInfo)
are in synch. (Assuming separate machines, they certainly won't ever
exactly in synch, but aren't necessarily even just a few seconds apart
(think incorrectly set up timezone etc), so no amount of tolerance in
the comparison would be good enough, because after all, it might be
that in the problematic cases we are looking for the timestamps also
are separated by a quite short time.)
Yes, this means there is a race condition; what if the document is
modified behind out back right after we have persisted it, before we
ask for its timestamp? It would be much better if the persisting
operation atomically also told what the timestamp of the document in
the storage is after persisting, but alas, WOPI doesn't do that.
Rename the DocumentBroker::origDocumentLastModifiedTime field to
_documentLastModifiedTime as that is less misleading. It is not the
"original" document timestamp but the timestamp of the document in its
storage system.
This needs much more work: Ideally the timestamp of the document in
its storage system should be retrieved and checked against the
expected value also before we are about to save it.
But unfortunately experience has shown that the WOPI CheckFileInfo
operation can be expensive, so we'll see what can be done. Ideally
WOPI should contain the optional functionality to return an error if,
when saving a document, its timestamp (and size?) in storage are not
what the saving client expects.
Also add a few FIXME comments.
Change-Id: I5a9b55d4b55a8db0c9ee8638edd368dc0aa325d5
2016-12-20 10:01:03 -06:00
|
|
|
{
|
2018-11-29 01:42:43 -06:00
|
|
|
_documentLastModifiedTime = fileInfo.getModifiedTime();
|
2018-01-14 19:39:06 -06:00
|
|
|
LOG_DBG("Document timestamp: " << _documentLastModifiedTime);
|
WIP: Check if the document has been modified behind our back
For now, do the check only when a new session connects to the
document, because at that point we fetch the document information (in
separate function for WOPI and local files) and construct the
FileInfo, including timestamp.
For now, just log an ERR message if we notice that the document in its
storage system (WOPI or local file system) has an unexpected last
modified time. What should we do? If we don't have unsaved changes,
most likely we should just silently reload the document and force all
sessions to refresh. But if we have unsaved changes, and the document
has changed underneath, we have a problem.
We need to fetch the timestamp also also after saving ("persisting")
as we can't assume that the clock on the machine running loolwsd and
that of the storage (as reported in the WOPI case in CheckFileInfo)
are in synch. (Assuming separate machines, they certainly won't ever
exactly in synch, but aren't necessarily even just a few seconds apart
(think incorrectly set up timezone etc), so no amount of tolerance in
the comparison would be good enough, because after all, it might be
that in the problematic cases we are looking for the timestamps also
are separated by a quite short time.)
Yes, this means there is a race condition; what if the document is
modified behind out back right after we have persisted it, before we
ask for its timestamp? It would be much better if the persisting
operation atomically also told what the timestamp of the document in
the storage is after persisting, but alas, WOPI doesn't do that.
Rename the DocumentBroker::origDocumentLastModifiedTime field to
_documentLastModifiedTime as that is less misleading. It is not the
"original" document timestamp but the timestamp of the document in its
storage system.
This needs much more work: Ideally the timestamp of the document in
its storage system should be retrieved and checked against the
expected value also before we are about to save it.
But unfortunately experience has shown that the WOPI CheckFileInfo
operation can be expensive, so we'll see what can be done. Ideally
WOPI should contain the optional functionality to return an error if,
when saving a document, its timestamp (and size?) in storage are not
what the saving client expects.
Also add a few FIXME comments.
Change-Id: I5a9b55d4b55a8db0c9ee8638edd368dc0aa325d5
2016-12-20 10:01:03 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Check if document has been modified by some external action
|
2018-11-29 01:42:43 -06:00
|
|
|
LOG_TRC("Document modified time: " << fileInfo.getModifiedTime());
|
2019-08-30 12:37:55 -05:00
|
|
|
static const std::chrono::system_clock::time_point Zero;
|
2017-04-09 17:37:27 -05:00
|
|
|
if (_documentLastModifiedTime != Zero &&
|
2018-11-29 01:42:43 -06:00
|
|
|
fileInfo.getModifiedTime() != Zero &&
|
|
|
|
_documentLastModifiedTime != fileInfo.getModifiedTime())
|
WIP: Check if the document has been modified behind our back
For now, do the check only when a new session connects to the
document, because at that point we fetch the document information (in
separate function for WOPI and local files) and construct the
FileInfo, including timestamp.
For now, just log an ERR message if we notice that the document in its
storage system (WOPI or local file system) has an unexpected last
modified time. What should we do? If we don't have unsaved changes,
most likely we should just silently reload the document and force all
sessions to refresh. But if we have unsaved changes, and the document
has changed underneath, we have a problem.
We need to fetch the timestamp also also after saving ("persisting")
as we can't assume that the clock on the machine running loolwsd and
that of the storage (as reported in the WOPI case in CheckFileInfo)
are in synch. (Assuming separate machines, they certainly won't ever
exactly in synch, but aren't necessarily even just a few seconds apart
(think incorrectly set up timezone etc), so no amount of tolerance in
the comparison would be good enough, because after all, it might be
that in the problematic cases we are looking for the timestamps also
are separated by a quite short time.)
Yes, this means there is a race condition; what if the document is
modified behind out back right after we have persisted it, before we
ask for its timestamp? It would be much better if the persisting
operation atomically also told what the timestamp of the document in
the storage is after persisting, but alas, WOPI doesn't do that.
Rename the DocumentBroker::origDocumentLastModifiedTime field to
_documentLastModifiedTime as that is less misleading. It is not the
"original" document timestamp but the timestamp of the document in its
storage system.
This needs much more work: Ideally the timestamp of the document in
its storage system should be retrieved and checked against the
expected value also before we are about to save it.
But unfortunately experience has shown that the WOPI CheckFileInfo
operation can be expensive, so we'll see what can be done. Ideally
WOPI should contain the optional functionality to return an error if,
when saving a document, its timestamp (and size?) in storage are not
what the saving client expects.
Also add a few FIXME comments.
Change-Id: I5a9b55d4b55a8db0c9ee8638edd368dc0aa325d5
2016-12-20 10:01:03 -06:00
|
|
|
{
|
2018-06-15 09:26:52 -05:00
|
|
|
LOG_DBG("Document " << _docKey << "] has been modified behind our back. " <<
|
2018-01-14 19:39:06 -06:00
|
|
|
"Informing all clients. Expected: " << _documentLastModifiedTime <<
|
2018-11-29 01:42:43 -06:00
|
|
|
", Actual: " << fileInfo.getModifiedTime());
|
2017-06-10 09:45:00 -05:00
|
|
|
|
2017-06-01 07:56:54 -05:00
|
|
|
_documentChangedInStorage = true;
|
2017-09-18 12:30:19 -05:00
|
|
|
std::string message = "close: documentconflict";
|
2017-06-19 09:30:37 -05:00
|
|
|
if (_isModified)
|
2017-09-18 12:30:19 -05:00
|
|
|
message = "error: cmd=storage kind=documentconflict";
|
|
|
|
|
|
|
|
session->sendTextFrame(message);
|
|
|
|
broadcastMessage(message);
|
WIP: Check if the document has been modified behind our back
For now, do the check only when a new session connects to the
document, because at that point we fetch the document information (in
separate function for WOPI and local files) and construct the
FileInfo, including timestamp.
For now, just log an ERR message if we notice that the document in its
storage system (WOPI or local file system) has an unexpected last
modified time. What should we do? If we don't have unsaved changes,
most likely we should just silently reload the document and force all
sessions to refresh. But if we have unsaved changes, and the document
has changed underneath, we have a problem.
We need to fetch the timestamp also also after saving ("persisting")
as we can't assume that the clock on the machine running loolwsd and
that of the storage (as reported in the WOPI case in CheckFileInfo)
are in synch. (Assuming separate machines, they certainly won't ever
exactly in synch, but aren't necessarily even just a few seconds apart
(think incorrectly set up timezone etc), so no amount of tolerance in
the comparison would be good enough, because after all, it might be
that in the problematic cases we are looking for the timestamps also
are separated by a quite short time.)
Yes, this means there is a race condition; what if the document is
modified behind out back right after we have persisted it, before we
ask for its timestamp? It would be much better if the persisting
operation atomically also told what the timestamp of the document in
the storage is after persisting, but alas, WOPI doesn't do that.
Rename the DocumentBroker::origDocumentLastModifiedTime field to
_documentLastModifiedTime as that is less misleading. It is not the
"original" document timestamp but the timestamp of the document in its
storage system.
This needs much more work: Ideally the timestamp of the document in
its storage system should be retrieved and checked against the
expected value also before we are about to save it.
But unfortunately experience has shown that the WOPI CheckFileInfo
operation can be expensive, so we'll see what can be done. Ideally
WOPI should contain the optional functionality to return an error if,
when saving a document, its timestamp (and size?) in storage are not
what the saving client expects.
Also add a few FIXME comments.
Change-Id: I5a9b55d4b55a8db0c9ee8638edd368dc0aa325d5
2016-12-20 10:01:03 -06:00
|
|
|
}
|
|
|
|
}
|
2016-12-19 05:21:42 -06:00
|
|
|
|
2018-11-08 09:41:53 -06:00
|
|
|
sendLastModificationTime(session, this, _documentLastModifiedTime);
|
|
|
|
|
2017-01-21 21:43:12 -06:00
|
|
|
// Let's load the document now, if not loaded.
|
|
|
|
if (!_storage->isLoaded())
|
2016-11-06 12:54:00 -06:00
|
|
|
{
|
2019-11-19 16:51:45 -06:00
|
|
|
std::string localPath = _storage->loadStorageFileToLocal(
|
|
|
|
session->getAuthorization(), *_lockCtx, templateSource);
|
|
|
|
|
|
|
|
if (!_storage->updateLockState(session->getAuthorization(), *_lockCtx, true))
|
|
|
|
LOG_ERR("Failed to lock!");
|
2017-11-07 07:15:36 -06:00
|
|
|
|
2019-02-12 05:16:40 -06:00
|
|
|
#if !MOBILEAPP
|
2017-11-07 07:15:36 -06:00
|
|
|
// Check if we have a prefilter "plugin" for this document format
|
|
|
|
for (const auto& plugin : LOOLWSD::PluginConfigurations)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2018-01-14 19:39:06 -06:00
|
|
|
const std::string extension(plugin->getString("prefilter.extension"));
|
|
|
|
const std::string newExtension(plugin->getString("prefilter.newextension"));
|
|
|
|
const std::string commandLine(plugin->getString("prefilter.commandline"));
|
2017-11-07 07:15:36 -06:00
|
|
|
|
|
|
|
if (localPath.length() > extension.length()+1 &&
|
|
|
|
strcasecmp(localPath.substr(localPath.length() - extension.length() -1).data(), (std::string(".") + extension).data()) == 0)
|
|
|
|
{
|
|
|
|
// Extension matches, try the conversion. We convert the file to another one in
|
|
|
|
// the same (jail) directory, with just the new extension tacked on.
|
|
|
|
|
2018-01-14 19:39:06 -06:00
|
|
|
const std::string newRootPath = _storage->getRootFilePath() + "." + newExtension;
|
2017-11-07 07:15:36 -06:00
|
|
|
|
2019-11-08 13:52:35 -06:00
|
|
|
std::vector<std::string> args(LOOLProtocol::tokenize(commandLine, ' '));
|
|
|
|
std::string command(args[0]);
|
2019-12-06 03:22:54 -06:00
|
|
|
args.erase(args.begin()); // strip the command
|
2019-11-08 13:52:35 -06:00
|
|
|
|
2017-11-07 07:15:36 -06:00
|
|
|
// The commandline must contain the space-separated substring @INPUT@ that is
|
|
|
|
// replaced with the input file name, and @OUTPUT@ for the output file name.
|
2019-11-08 13:52:35 -06:00
|
|
|
int inputs(0), outputs(0);
|
|
|
|
for (auto it = args.begin(); it != args.end(); ++it)
|
|
|
|
{
|
|
|
|
if (*it == "@INPUT@")
|
|
|
|
{
|
|
|
|
*it = _storage->getRootFilePath();
|
|
|
|
++inputs;
|
|
|
|
}
|
|
|
|
else if (*it == "@OUTPUT@")
|
|
|
|
{
|
|
|
|
*it = newRootPath;
|
|
|
|
++outputs;
|
|
|
|
}
|
|
|
|
}
|
2018-01-14 19:39:06 -06:00
|
|
|
|
2019-11-08 13:52:35 -06:00
|
|
|
if (inputs != 1 || outputs != 1)
|
|
|
|
throw std::exception();
|
2017-11-07 07:15:36 -06:00
|
|
|
|
2019-11-08 13:52:35 -06:00
|
|
|
int process = Util::spawnProcess(command, args);
|
2018-01-29 09:13:54 -06:00
|
|
|
int status = -1;
|
|
|
|
const int rc = ::waitpid(process, &status, 0);
|
2017-11-07 07:15:36 -06:00
|
|
|
if (rc != 0)
|
|
|
|
{
|
2018-01-14 19:39:06 -06:00
|
|
|
LOG_ERR("Conversion from " << extension << " to " << newExtension << " failed (" << rc << ").");
|
2017-11-07 07:15:36 -06:00
|
|
|
return false;
|
|
|
|
}
|
2018-01-14 19:39:06 -06:00
|
|
|
|
2017-11-07 07:15:36 -06:00
|
|
|
_storage->setRootFilePath(newRootPath);
|
|
|
|
localPath += "." + newExtension;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We successfully converted the file to something LO can use; break out of the for
|
|
|
|
// loop.
|
|
|
|
break;
|
|
|
|
}
|
2019-11-08 13:52:35 -06:00
|
|
|
catch (const std::exception&)
|
2017-11-07 07:15:36 -06:00
|
|
|
{
|
|
|
|
// This plugin is not a proper prefilter one
|
|
|
|
}
|
|
|
|
}
|
2018-09-05 07:11:05 -05:00
|
|
|
#endif
|
2017-01-22 22:12:51 -06:00
|
|
|
|
|
|
|
std::ifstream istr(localPath, std::ios::binary);
|
|
|
|
Poco::SHA1Engine sha1;
|
|
|
|
Poco::DigestOutputStream dos(sha1);
|
|
|
|
Poco::StreamCopier::copyStream(istr, dos);
|
|
|
|
dos.close();
|
2018-06-10 19:24:04 -05:00
|
|
|
LOG_INF("SHA1 for DocKey [" << _docKey << "] of [" << LOOLWSD::anonymizeUrl(localPath) << "]: " <<
|
2017-02-05 18:35:54 -06:00
|
|
|
Poco::DigestEngine::digestToHex(sha1.digest()));
|
2017-01-22 22:12:51 -06:00
|
|
|
|
2018-02-17 17:24:08 -06:00
|
|
|
std::string localPathEncoded;
|
2019-11-21 07:29:51 -06:00
|
|
|
Poco::URI::encode(localPath, "#?", localPathEncoded);
|
2018-06-10 10:41:17 -05:00
|
|
|
_uriJailed = Poco::URI(Poco::URI("file://"), localPathEncoded).toString();
|
2019-05-15 21:14:29 -05:00
|
|
|
_uriJailedAnonym = Poco::URI(Poco::URI("file://"), LOOLWSD::anonymizeUrl(localPathEncoded)).toString();
|
2018-02-17 17:24:08 -06:00
|
|
|
|
2018-11-29 01:42:43 -06:00
|
|
|
_filename = fileInfo.getFilename();
|
2016-11-06 12:54:00 -06:00
|
|
|
|
|
|
|
// Use the local temp file's timestamp.
|
2019-08-30 12:37:55 -05:00
|
|
|
_lastFileModifiedTime = templateSource.empty() ? Util::getFileTimestamp(_storage->getRootFilePath()) :
|
|
|
|
std::chrono::system_clock::time_point();
|
2019-02-14 15:40:33 -06:00
|
|
|
|
|
|
|
bool dontUseCache = false;
|
|
|
|
#if MOBILEAPP
|
|
|
|
// avoid memory consumption for single-user local bits.
|
|
|
|
// FIXME: arguably should/could do this for single user documents too.
|
|
|
|
dontUseCache = true;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
_tileCache.reset(new TileCache(_storage->getUriString(), _lastFileModifiedTime, dontUseCache));
|
2017-06-22 07:58:50 -05:00
|
|
|
_tileCache->setThreadOwner(std::this_thread::get_id());
|
2016-11-06 12:54:00 -06:00
|
|
|
}
|
|
|
|
|
2019-02-12 05:16:40 -06:00
|
|
|
#if !MOBILEAPP
|
2017-02-10 00:09:01 -06:00
|
|
|
LOOLWSD::dumpNewSessionTrace(getJailId(), sessionId, _uriOrig, _storage->getRootFilePath());
|
|
|
|
|
2016-11-06 12:54:00 -06:00
|
|
|
// Since document has been loaded, send the stats if its WOPI
|
2017-01-21 21:43:12 -06:00
|
|
|
if (wopiStorage != nullptr)
|
2016-11-06 12:54:00 -06:00
|
|
|
{
|
|
|
|
// Get the time taken to load the file from storage
|
2017-01-21 21:43:12 -06:00
|
|
|
auto callDuration = wopiStorage->getWopiLoadDuration();
|
2016-11-06 12:54:00 -06:00
|
|
|
// Add the time taken to check file info
|
|
|
|
callDuration += getInfoCallDuration;
|
2019-11-12 03:50:33 -06:00
|
|
|
_wopiLoadDuration = std::chrono::duration_cast<std::chrono::milliseconds>(callDuration);
|
2016-11-06 12:54:00 -06:00
|
|
|
const std::string msg = "stats: wopiloadduration " + std::to_string(callDuration.count());
|
|
|
|
LOG_TRC("Sending to Client [" << msg << "].");
|
2016-11-06 21:11:35 -06:00
|
|
|
session->sendTextFrame(msg);
|
2016-04-07 15:59:27 -05:00
|
|
|
}
|
2018-09-05 07:11:05 -05:00
|
|
|
#endif
|
2016-11-06 12:54:00 -06:00
|
|
|
return true;
|
2016-03-23 07:41:18 -05:00
|
|
|
}
|
|
|
|
|
2017-04-11 01:54:09 -05:00
|
|
|
bool DocumentBroker::saveToStorage(const std::string& sessionId,
|
2017-06-01 09:16:03 -05:00
|
|
|
bool success, const std::string& result, bool force)
|
2017-03-09 22:50:36 -06:00
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2017-03-29 21:50:29 -05:00
|
|
|
|
2019-10-22 09:28:57 -05:00
|
|
|
// Force saving on exit, if enabled.
|
|
|
|
if (!force && isMarkedToDestroy())
|
2017-06-01 09:16:03 -05:00
|
|
|
{
|
2019-10-22 09:28:57 -05:00
|
|
|
static const bool always_save = LOOLWSD::getConfigValue<bool>("per_document.always_save_on_exit", false);
|
|
|
|
if (always_save)
|
|
|
|
{
|
|
|
|
LOG_TRC("Enabling forced saving to storage per always_save_on_exit config.");
|
|
|
|
_storage->forceSave();
|
|
|
|
force = true;
|
|
|
|
}
|
2017-06-01 09:16:03 -05:00
|
|
|
}
|
2018-01-14 10:16:10 -06:00
|
|
|
|
2019-10-22 09:28:57 -05:00
|
|
|
constexpr bool isRename = false;
|
|
|
|
const bool res = saveToStorageInternal(sessionId, success, result, /*saveAsPath*/ std::string(),
|
|
|
|
/*saveAsFilename*/ std::string(), isRename, force);
|
2017-03-09 22:50:36 -06:00
|
|
|
|
2017-04-20 21:43:27 -05:00
|
|
|
// If marked to destroy, or session is disconnected, remove.
|
|
|
|
const auto it = _sessions.find(sessionId);
|
|
|
|
if (_markToDestroy || (it != _sessions.end() && it->second->isCloseFrame()))
|
2019-07-04 04:50:33 -05:00
|
|
|
disconnectSessionInternal(sessionId);
|
2017-04-19 23:10:30 -05:00
|
|
|
|
2017-03-09 22:50:36 -06:00
|
|
|
// If marked to destroy, then this was the last session.
|
2017-04-19 23:10:30 -05:00
|
|
|
if (_markToDestroy || _sessions.empty())
|
2017-03-09 22:50:36 -06:00
|
|
|
{
|
|
|
|
// Stop so we get cleaned up and removed.
|
|
|
|
_stop = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2019-04-30 09:21:44 -05:00
|
|
|
bool DocumentBroker::saveAsToStorage(const std::string& sessionId, const std::string& saveAsPath, const std::string& saveAsFilename, const bool isRename)
|
2017-10-20 11:12:05 -05:00
|
|
|
{
|
|
|
|
assertCorrectThread();
|
|
|
|
|
2019-04-30 09:21:44 -05:00
|
|
|
return saveToStorageInternal(sessionId, true, "", saveAsPath, saveAsFilename, isRename);
|
2017-10-20 11:12:05 -05:00
|
|
|
}
|
|
|
|
|
2019-10-22 09:28:57 -05:00
|
|
|
bool DocumentBroker::saveToStorageInternal(const std::string& sessionId, bool success,
|
|
|
|
const std::string& result, const std::string& saveAsPath,
|
|
|
|
const std::string& saveAsFilename, const bool isRename,
|
|
|
|
const bool force)
|
2016-03-23 07:41:18 -05:00
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2016-04-10 21:07:09 -05:00
|
|
|
|
2019-10-08 04:23:29 -05:00
|
|
|
// Record that we got a response to avoid timing out on saving.
|
2018-01-14 17:59:11 -06:00
|
|
|
_lastSaveResponseTime = std::chrono::steady_clock::now();
|
|
|
|
|
2017-01-01 11:08:12 -06:00
|
|
|
// If save requested, but core didn't save because document was unmodified
|
|
|
|
// notify the waiting thread, if any.
|
2019-10-22 09:28:57 -05:00
|
|
|
LOG_TRC("Uploading to storage docKey [" << _docKey << "] for session [" << sessionId <<
|
|
|
|
"]. Success: " << success << ", result: " << result << ", force: " << force);
|
|
|
|
if (!success && result == "unmodified" && !isRename && !force)
|
2017-01-01 11:08:12 -06:00
|
|
|
{
|
2017-01-01 14:39:23 -06:00
|
|
|
LOG_DBG("Save skipped as document [" << _docKey << "] was not modified.");
|
2017-01-01 18:42:19 -06:00
|
|
|
_lastSaveTime = std::chrono::steady_clock::now();
|
2017-03-09 12:19:53 -06:00
|
|
|
_poll->wakeup();
|
2017-01-01 11:08:12 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-16 11:40:52 -05:00
|
|
|
const auto it = _sessions.find(sessionId);
|
|
|
|
if (it == _sessions.end())
|
|
|
|
{
|
2019-10-22 09:28:57 -05:00
|
|
|
LOG_ERR("Session with sessionId ["
|
|
|
|
<< sessionId << "] not found while storing document docKey [" << _docKey
|
|
|
|
<< "]. The document will not be uploaded to storage at this time.");
|
2016-10-16 11:40:52 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-07 06:12:23 -06:00
|
|
|
// Check that we are actually about to upload a successfully saved document.
|
2019-10-22 09:28:57 -05:00
|
|
|
if (!success && !force)
|
2017-11-07 06:12:23 -06:00
|
|
|
{
|
2019-10-22 09:28:57 -05:00
|
|
|
LOG_ERR("Cannot store docKey [" << _docKey << "] as .uno:Save has failed in LOK.");
|
2017-11-07 06:12:23 -06:00
|
|
|
it->second->sendTextFrame("error: cmd=storage kind=savefailed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-22 09:28:57 -05:00
|
|
|
if (force)
|
|
|
|
{
|
|
|
|
LOG_DBG("Document docKey [" << _docKey << "] will be forcefully uploaded to storage.");
|
|
|
|
}
|
|
|
|
|
|
|
|
const bool isSaveAs = !saveAsPath.empty();
|
2017-08-16 09:38:00 -05:00
|
|
|
const Authorization auth = it->second->getAuthorization();
|
2018-06-10 19:55:52 -05:00
|
|
|
const std::string uri = isSaveAs ? saveAsPath : it->second->getPublicUri().toString();
|
2018-07-21 14:58:35 -05:00
|
|
|
|
|
|
|
// Map the FileId from the docKey to the new filename to anonymize the new filename as the FileId.
|
|
|
|
const std::string newFilename = Util::getFilenameFromURL(uri);
|
|
|
|
const std::string fileId = Util::getFilenameFromURL(_docKey);
|
2019-04-14 11:24:45 -05:00
|
|
|
if (LOOLWSD::AnonymizeUserData)
|
2018-07-21 14:58:35 -05:00
|
|
|
LOG_DBG("New filename [" << LOOLWSD::anonymizeUrl(newFilename) << "] will be known by its fileId [" << fileId << "]");
|
|
|
|
|
|
|
|
Util::mapAnonymized(newFilename, fileId);
|
2018-06-10 19:55:52 -05:00
|
|
|
const std::string uriAnonym = LOOLWSD::anonymizeUrl(uri);
|
2016-04-27 19:52:32 -05:00
|
|
|
|
2018-01-14 16:17:00 -06:00
|
|
|
// If the file timestamp hasn't changed, skip saving.
|
2019-08-30 12:37:55 -05:00
|
|
|
const std::chrono::system_clock::time_point newFileModifiedTime = Util::getFileTimestamp(_storage->getRootFilePath());
|
2019-04-30 09:21:44 -05:00
|
|
|
if (!isSaveAs && newFileModifiedTime == _lastFileModifiedTime && !isRename)
|
2016-04-25 19:44:24 -05:00
|
|
|
{
|
|
|
|
// Nothing to do.
|
2019-08-30 12:37:55 -05:00
|
|
|
auto timeInSec = std::chrono::duration_cast<std::chrono::seconds>
|
|
|
|
(std::chrono::system_clock::now() - _lastFileModifiedTime);
|
2018-06-10 19:55:52 -05:00
|
|
|
LOG_DBG("Skipping unnecessary saving to URI [" << uriAnonym << "] with docKey [" << _docKey <<
|
2019-08-30 12:37:55 -05:00
|
|
|
"]. File last modified " << timeInSec.count() << " seconds ago.");
|
2017-03-09 12:19:53 -06:00
|
|
|
_poll->wakeup();
|
2016-04-25 19:44:24 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-10 19:55:52 -05:00
|
|
|
LOG_DBG("Persisting [" << _docKey << "] after saving to URI [" << uriAnonym << "].");
|
2016-03-23 07:41:18 -05:00
|
|
|
|
2016-03-26 08:10:53 -05:00
|
|
|
assert(_storage && _tileCache);
|
2019-11-19 16:51:45 -06:00
|
|
|
StorageBase::SaveResult storageSaveResult = _storage->saveLocalFileToStorage(
|
|
|
|
auth, *_lockCtx, saveAsPath, saveAsFilename, isRename);
|
2017-10-25 07:09:27 -05:00
|
|
|
if (storageSaveResult.getResult() == StorageBase::SaveResult::OK)
|
2016-03-26 08:10:53 -05:00
|
|
|
{
|
2019-11-12 03:50:33 -06:00
|
|
|
#if !MOBILEAPP
|
|
|
|
WopiStorage* wopiStorage = dynamic_cast<WopiStorage*>(_storage.get());
|
|
|
|
if (wopiStorage != nullptr)
|
|
|
|
Admin::instance().setDocWopiUploadDuration(_docKey, std::chrono::duration_cast<std::chrono::milliseconds>(wopiStorage->getWopiSaveDuration()));
|
|
|
|
#endif
|
|
|
|
|
2019-04-30 09:21:44 -05:00
|
|
|
if (!isSaveAs && !isRename)
|
2017-10-20 11:12:05 -05:00
|
|
|
{
|
2018-01-14 17:59:11 -06:00
|
|
|
// Saved and stored; update flags.
|
2017-10-20 11:12:05 -05:00
|
|
|
setModified(false);
|
|
|
|
_lastFileModifiedTime = newFileModifiedTime;
|
|
|
|
_lastSaveTime = std::chrono::steady_clock::now();
|
WIP: Check if the document has been modified behind our back
For now, do the check only when a new session connects to the
document, because at that point we fetch the document information (in
separate function for WOPI and local files) and construct the
FileInfo, including timestamp.
For now, just log an ERR message if we notice that the document in its
storage system (WOPI or local file system) has an unexpected last
modified time. What should we do? If we don't have unsaved changes,
most likely we should just silently reload the document and force all
sessions to refresh. But if we have unsaved changes, and the document
has changed underneath, we have a problem.
We need to fetch the timestamp also also after saving ("persisting")
as we can't assume that the clock on the machine running loolwsd and
that of the storage (as reported in the WOPI case in CheckFileInfo)
are in synch. (Assuming separate machines, they certainly won't ever
exactly in synch, but aren't necessarily even just a few seconds apart
(think incorrectly set up timezone etc), so no amount of tolerance in
the comparison would be good enough, because after all, it might be
that in the problematic cases we are looking for the timestamps also
are separated by a quite short time.)
Yes, this means there is a race condition; what if the document is
modified behind out back right after we have persisted it, before we
ask for its timestamp? It would be much better if the persisting
operation atomically also told what the timestamp of the document in
the storage is after persisting, but alas, WOPI doesn't do that.
Rename the DocumentBroker::origDocumentLastModifiedTime field to
_documentLastModifiedTime as that is less misleading. It is not the
"original" document timestamp but the timestamp of the document in its
storage system.
This needs much more work: Ideally the timestamp of the document in
its storage system should be retrieved and checked against the
expected value also before we are about to save it.
But unfortunately experience has shown that the WOPI CheckFileInfo
operation can be expensive, so we'll see what can be done. Ideally
WOPI should contain the optional functionality to return an error if,
when saving a document, its timestamp (and size?) in storage are not
what the saving client expects.
Also add a few FIXME comments.
Change-Id: I5a9b55d4b55a8db0c9ee8638edd368dc0aa325d5
2016-12-20 10:01:03 -06:00
|
|
|
|
2018-01-14 17:59:11 -06:00
|
|
|
// Save the storage timestamp.
|
2018-11-29 01:42:43 -06:00
|
|
|
_documentLastModifiedTime = _storage->getFileInfo().getModifiedTime();
|
2017-06-01 11:11:11 -05:00
|
|
|
|
2017-10-20 11:12:05 -05:00
|
|
|
// After a successful save, we are sure that document in the storage is same as ours
|
|
|
|
_documentChangedInStorage = false;
|
WIP: Check if the document has been modified behind our back
For now, do the check only when a new session connects to the
document, because at that point we fetch the document information (in
separate function for WOPI and local files) and construct the
FileInfo, including timestamp.
For now, just log an ERR message if we notice that the document in its
storage system (WOPI or local file system) has an unexpected last
modified time. What should we do? If we don't have unsaved changes,
most likely we should just silently reload the document and force all
sessions to refresh. But if we have unsaved changes, and the document
has changed underneath, we have a problem.
We need to fetch the timestamp also also after saving ("persisting")
as we can't assume that the clock on the machine running loolwsd and
that of the storage (as reported in the WOPI case in CheckFileInfo)
are in synch. (Assuming separate machines, they certainly won't ever
exactly in synch, but aren't necessarily even just a few seconds apart
(think incorrectly set up timezone etc), so no amount of tolerance in
the comparison would be good enough, because after all, it might be
that in the problematic cases we are looking for the timestamps also
are separated by a quite short time.)
Yes, this means there is a race condition; what if the document is
modified behind out back right after we have persisted it, before we
ask for its timestamp? It would be much better if the persisting
operation atomically also told what the timestamp of the document in
the storage is after persisting, but alas, WOPI doesn't do that.
Rename the DocumentBroker::origDocumentLastModifiedTime field to
_documentLastModifiedTime as that is less misleading. It is not the
"original" document timestamp but the timestamp of the document in its
storage system.
This needs much more work: Ideally the timestamp of the document in
its storage system should be retrieved and checked against the
expected value also before we are about to save it.
But unfortunately experience has shown that the WOPI CheckFileInfo
operation can be expensive, so we'll see what can be done. Ideally
WOPI should contain the optional functionality to return an error if,
when saving a document, its timestamp (and size?) in storage are not
what the saving client expects.
Also add a few FIXME comments.
Change-Id: I5a9b55d4b55a8db0c9ee8638edd368dc0aa325d5
2016-12-20 10:01:03 -06:00
|
|
|
|
2018-06-10 19:55:52 -05:00
|
|
|
LOG_DBG("Saved docKey [" << _docKey << "] to URI [" << uriAnonym << "] and updated timestamps. " <<
|
2018-01-14 19:39:06 -06:00
|
|
|
" Document modified timestamp: " << _documentLastModifiedTime);
|
2018-01-14 17:59:11 -06:00
|
|
|
|
|
|
|
// Resume polling.
|
|
|
|
_poll->wakeup();
|
2017-10-20 11:12:05 -05:00
|
|
|
}
|
2019-04-30 09:21:44 -05:00
|
|
|
else if (isRename)
|
|
|
|
{
|
|
|
|
// encode the name
|
2019-05-30 01:43:39 -05:00
|
|
|
const std::string& filename = storageSaveResult.getSaveAsName();
|
2019-04-30 09:21:44 -05:00
|
|
|
const std::string url = Poco::URI(storageSaveResult.getSaveAsUrl()).toString();
|
|
|
|
std::string encodedName;
|
|
|
|
Poco::URI::encode(filename, "", encodedName);
|
|
|
|
const std::string filenameAnonym = LOOLWSD::anonymizeUrl(filename);
|
|
|
|
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << "renamefile: " << "filename=" << encodedName << " url=" << url;
|
|
|
|
broadcastMessage(oss.str());
|
|
|
|
}
|
2017-10-20 11:12:05 -05:00
|
|
|
else
|
|
|
|
{
|
2017-10-25 07:09:27 -05:00
|
|
|
// normalize the url (mainly to " " -> "%20")
|
2018-06-10 19:55:52 -05:00
|
|
|
const std::string url = Poco::URI(storageSaveResult.getSaveAsUrl()).toString();
|
|
|
|
|
2019-01-02 02:07:06 -06:00
|
|
|
const std::string& filename = storageSaveResult.getSaveAsName();
|
2017-10-25 07:09:27 -05:00
|
|
|
|
|
|
|
// encode the name
|
|
|
|
std::string encodedName;
|
2018-06-10 19:55:52 -05:00
|
|
|
Poco::URI::encode(filename, "", encodedName);
|
|
|
|
const std::string filenameAnonym = LOOLWSD::anonymizeUrl(filename);
|
2017-10-25 07:09:27 -05:00
|
|
|
|
2018-06-10 19:55:52 -05:00
|
|
|
std::ostringstream oss;
|
|
|
|
oss << "saveas: url=" << url << " filename=" << encodedName
|
|
|
|
<< " xfilename=" << filenameAnonym;
|
|
|
|
it->second->sendTextFrame(oss.str());
|
2017-10-25 07:09:27 -05:00
|
|
|
|
2018-06-15 09:26:52 -05:00
|
|
|
LOG_DBG("Saved As docKey [" << _docKey << "] to URI [" << LOOLWSD::anonymizeUrl(url) <<
|
2018-06-10 19:55:52 -05:00
|
|
|
"] with name [" << filenameAnonym << "] successfully.");
|
2017-10-20 11:12:05 -05:00
|
|
|
}
|
2018-06-10 19:55:52 -05:00
|
|
|
|
2018-11-08 09:41:53 -06:00
|
|
|
sendLastModificationTime(it->second, this, _documentLastModifiedTime);
|
|
|
|
|
2016-03-26 08:10:53 -05:00
|
|
|
return true;
|
|
|
|
}
|
2017-10-25 07:09:27 -05:00
|
|
|
else if (storageSaveResult.getResult() == StorageBase::SaveResult::DISKFULL)
|
2016-11-23 06:09:54 -06:00
|
|
|
{
|
2018-06-10 19:55:52 -05:00
|
|
|
LOG_WRN("Disk full while saving docKey [" << _docKey << "] to URI [" << uriAnonym <<
|
2017-01-01 11:06:05 -06:00
|
|
|
"]. Making all sessions on doc read-only and notifying clients.");
|
|
|
|
|
|
|
|
// Make everyone readonly and tell everyone that storage is low on diskspace.
|
|
|
|
for (const auto& sessionIt : _sessions)
|
2016-11-23 06:09:54 -06:00
|
|
|
{
|
|
|
|
sessionIt.second->sendTextFrame("error: cmd=storage kind=savediskfull");
|
|
|
|
}
|
|
|
|
}
|
2017-10-25 07:09:27 -05:00
|
|
|
else if (storageSaveResult.getResult() == StorageBase::SaveResult::UNAUTHORIZED)
|
2017-05-31 01:21:12 -05:00
|
|
|
{
|
2018-06-10 19:55:52 -05:00
|
|
|
LOG_ERR("Cannot save docKey [" << _docKey << "] to storage URI [" << uriAnonym <<
|
|
|
|
"]. Invalid or expired access token. Notifying client.");
|
2017-05-31 01:21:12 -05:00
|
|
|
it->second->sendTextFrame("error: cmd=storage kind=saveunauthorized");
|
|
|
|
}
|
2017-10-25 07:09:27 -05:00
|
|
|
else if (storageSaveResult.getResult() == StorageBase::SaveResult::FAILED)
|
2016-11-23 06:09:54 -06:00
|
|
|
{
|
2017-01-01 11:06:05 -06:00
|
|
|
//TODO: Should we notify all clients?
|
2018-06-10 19:55:52 -05:00
|
|
|
LOG_ERR("Failed to save docKey [" << _docKey << "] to URI [" << uriAnonym << "]. Notifying client.");
|
2019-04-30 09:21:44 -05:00
|
|
|
std::ostringstream oss;
|
|
|
|
oss << "error: cmd=storage kind=" << (isRename ? "renamefailed" : "savefailed");
|
|
|
|
it->second->sendTextFrame(oss.str());
|
2016-11-23 06:09:54 -06:00
|
|
|
}
|
2019-11-19 08:19:21 -06:00
|
|
|
else if (storageSaveResult.getResult() == StorageBase::SaveResult::DOC_CHANGED
|
|
|
|
|| storageSaveResult.getResult() == StorageBase::SaveResult::CONFLICT)
|
Inform all clients when document changed behind our back
Introduce a new header X-LOOL-WOPI-Timestamp
This is a WOPI header extension to detect any external document change. For
example, when the file that is already opened by LOOL is changed
in storage.
The WOPI host sends LastModifiedTime field (in WOPI specs) as part
of the CheckFileInfo response. It also expects wsd to send the
same timestamp in X-LOOL-WOPI-Timestamp header during WOPI::PutFile. If
this header is present, then WOPI host checks, before saving the
document, if the timestamp in the header is equal to the timestamp of
the file in its storage. Only upon meeting this condition, it saves the
file back to storage, otherwise it informs us about some change
to the document.
We are supposed to inform the user accordingly. If user is okay
with over-writing the document, then we can omit sending
X-LOOL-WOPI-Timestamp header, in which case, no check as mentioned above
would be performed while saving the file and document will be
overwritten.
Also, use a separate list of LOOL status codes to denote such a change.
It would be wrong to use HTTP_CONFLICT status code for denoting doc
changed in storage scenario. WOPI specs reserves that for WOPI locks
which are not yet implemented. Better to use a separate LOOL specific
status codes synced across WOPI hosts and us to denote scenario that we
expect and are not covered in WOPI specs.
Change-Id: I61539dfae672bc104b8008f030f96e90f9ff48a5
2017-05-31 12:48:33 -05:00
|
|
|
{
|
|
|
|
LOG_ERR("PutFile says that Document changed in storage");
|
2017-06-01 07:56:54 -05:00
|
|
|
_documentChangedInStorage = true;
|
2018-02-01 11:13:47 -06:00
|
|
|
std::string message = "close: documentconflict";
|
2017-06-19 09:30:37 -05:00
|
|
|
if (_isModified)
|
2018-02-01 11:13:47 -06:00
|
|
|
message = "error: cmd=storage kind=documentconflict";
|
|
|
|
|
|
|
|
broadcastMessage(message);
|
Inform all clients when document changed behind our back
Introduce a new header X-LOOL-WOPI-Timestamp
This is a WOPI header extension to detect any external document change. For
example, when the file that is already opened by LOOL is changed
in storage.
The WOPI host sends LastModifiedTime field (in WOPI specs) as part
of the CheckFileInfo response. It also expects wsd to send the
same timestamp in X-LOOL-WOPI-Timestamp header during WOPI::PutFile. If
this header is present, then WOPI host checks, before saving the
document, if the timestamp in the header is equal to the timestamp of
the file in its storage. Only upon meeting this condition, it saves the
file back to storage, otherwise it informs us about some change
to the document.
We are supposed to inform the user accordingly. If user is okay
with over-writing the document, then we can omit sending
X-LOOL-WOPI-Timestamp header, in which case, no check as mentioned above
would be performed while saving the file and document will be
overwritten.
Also, use a separate list of LOOL status codes to denote such a change.
It would be wrong to use HTTP_CONFLICT status code for denoting doc
changed in storage scenario. WOPI specs reserves that for WOPI locks
which are not yet implemented. Better to use a separate LOOL specific
status codes synced across WOPI hosts and us to denote scenario that we
expect and are not covered in WOPI specs.
Change-Id: I61539dfae672bc104b8008f030f96e90f9ff48a5
2017-05-31 12:48:33 -05:00
|
|
|
}
|
2016-03-26 08:10:53 -05:00
|
|
|
|
|
|
|
return false;
|
2016-03-23 07:41:18 -05:00
|
|
|
}
|
|
|
|
|
2017-03-11 16:01:27 -06:00
|
|
|
void DocumentBroker::setLoaded()
|
|
|
|
{
|
2017-03-25 12:57:47 -05:00
|
|
|
if (!_isLoaded)
|
|
|
|
{
|
|
|
|
_isLoaded = true;
|
|
|
|
_loadDuration = std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
|
|
std::chrono::steady_clock::now() - _threadStart);
|
|
|
|
LOG_TRC("Document loaded in " << _loadDuration.count() << "ms");
|
|
|
|
}
|
2017-03-11 16:01:27 -06:00
|
|
|
}
|
|
|
|
|
2019-11-27 02:43:05 -06:00
|
|
|
std::string DocumentBroker::getWriteableSessionId() const
|
2016-04-09 22:20:20 -05:00
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2017-04-03 23:11:06 -05:00
|
|
|
|
2017-05-15 00:59:04 -05:00
|
|
|
std::string savingSessionId;
|
|
|
|
for (auto& sessionIt : _sessions)
|
|
|
|
{
|
2019-10-22 09:28:57 -05:00
|
|
|
// Save the document using an editable and loaded session, or first ...
|
2019-10-16 08:05:04 -05:00
|
|
|
if (savingSessionId.empty()
|
|
|
|
|| (!sessionIt.second->isReadOnly() && sessionIt.second->isViewLoaded()
|
|
|
|
&& !sessionIt.second->inWaitDisconnected()))
|
2017-05-15 00:59:04 -05:00
|
|
|
{
|
|
|
|
savingSessionId = sessionIt.second->getId();
|
|
|
|
}
|
|
|
|
|
|
|
|
// or if any of the sessions is document owner, use that.
|
|
|
|
if (sessionIt.second->isDocumentOwner())
|
|
|
|
{
|
|
|
|
savingSessionId = sessionIt.second->getId();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-11-27 02:43:05 -06:00
|
|
|
return savingSessionId;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentBroker::refreshLock()
|
|
|
|
{
|
|
|
|
assertCorrectThread();
|
|
|
|
|
|
|
|
std::string savingSessionId = getWriteableSessionId();
|
|
|
|
LOG_TRC("Refresh lock " << _lockCtx->_lockToken << " with session " << savingSessionId);
|
|
|
|
|
|
|
|
auto it = _sessions.find(savingSessionId);
|
|
|
|
if (it == _sessions.end())
|
|
|
|
LOG_ERR("No write-able session to refresh lock with");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::shared_ptr<ClientSession> session = it->second;
|
|
|
|
if (!session || !_storage->updateLockState(session->getAuthorization(), *_lockCtx, true))
|
|
|
|
LOG_ERR("Failed to refresh lock");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DocumentBroker::autoSave(const bool force, const bool dontSaveIfUnmodified)
|
|
|
|
{
|
|
|
|
assertCorrectThread();
|
|
|
|
|
|
|
|
LOG_TRC("autoSave(): forceful? " << force);
|
|
|
|
if (_sessions.empty() || _storage == nullptr || !_isLoaded ||
|
|
|
|
!_childProcess->isAlive() || (!_isModified && !force))
|
|
|
|
{
|
|
|
|
// Nothing to do.
|
|
|
|
LOG_TRC("Nothing to autosave [" << _docKey << "].");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remember the last save time, since this is the predicate.
|
|
|
|
LOG_TRC("Checking to autosave [" << _docKey << "].");
|
|
|
|
|
|
|
|
// Which session to use when auto saving ?
|
|
|
|
std::string savingSessionId = getWriteableSessionId();
|
2017-05-15 00:59:04 -05:00
|
|
|
|
2016-04-25 19:48:02 -05:00
|
|
|
bool sent = false;
|
2016-05-02 21:58:53 -05:00
|
|
|
if (force)
|
2016-04-09 22:20:20 -05:00
|
|
|
{
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_TRC("Sending forced save command for [" << _docKey << "].");
|
2018-01-11 00:29:08 -06:00
|
|
|
// Don't terminate editing as this can be invoked by the admin OOM, but otherwise force saving anyway.
|
2018-11-30 08:10:28 -06:00
|
|
|
// Flag isAutosave=false so the WOPI host wouldn't think this is a regular checkpoint and
|
|
|
|
// potentially optimize it away. This is as good as user-issued save, since this is
|
|
|
|
// triggered when the document is closed. In the case of network disconnection or browser crash
|
|
|
|
// most users would want to have had the chance to hit save before the document unloaded.
|
2018-12-04 01:49:49 -06:00
|
|
|
sent = sendUnoSave(savingSessionId, /*dontTerminateEdit=*/true,
|
2019-06-06 04:48:54 -05:00
|
|
|
dontSaveIfUnmodified, /*isAutosave=*/false,
|
2018-12-04 01:49:49 -06:00
|
|
|
/*isExitSave=*/true);
|
2016-04-09 22:20:20 -05:00
|
|
|
}
|
2016-05-02 21:58:53 -05:00
|
|
|
else if (_isModified)
|
2016-04-09 22:20:20 -05:00
|
|
|
{
|
2018-02-07 03:17:59 -06:00
|
|
|
const std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
|
|
|
|
const std::chrono::milliseconds::rep inactivityTimeMs = std::chrono::duration_cast<std::chrono::milliseconds>(now - _lastActivityTime).count();
|
|
|
|
const std::chrono::milliseconds::rep timeSinceLastSaveMs = std::chrono::duration_cast<std::chrono::milliseconds>(now - _lastSaveTime).count();
|
2017-01-01 14:39:23 -06:00
|
|
|
LOG_TRC("Time since last save of docKey [" << _docKey << "] is " << timeSinceLastSaveMs <<
|
2017-01-01 18:42:19 -06:00
|
|
|
"ms and most recent activity was " << inactivityTimeMs << "ms ago.");
|
2016-04-10 11:30:33 -05:00
|
|
|
|
2018-02-07 03:17:59 -06:00
|
|
|
static const int idleSaveDurationMs = LOOLWSD::getConfigValue<int>("per_document.idlesave_duration_secs", 30) * 1000;
|
|
|
|
static const int autoSaveDurationMs = LOOLWSD::getConfigValue<int>("per_document.autosave_duration_secs", 300) * 1000;
|
2019-06-05 10:24:05 -05:00
|
|
|
bool save = false;
|
|
|
|
// Zero or negative config value disables save.
|
2016-05-02 21:58:53 -05:00
|
|
|
// Either we've been idle long enough, or it's auto-save time.
|
2019-06-05 10:24:05 -05:00
|
|
|
if (idleSaveDurationMs > 0 && inactivityTimeMs >= idleSaveDurationMs)
|
|
|
|
{
|
|
|
|
save = true;
|
|
|
|
}
|
|
|
|
if (autoSaveDurationMs > 0 && timeSinceLastSaveMs >= autoSaveDurationMs)
|
|
|
|
{
|
|
|
|
save = true;
|
|
|
|
}
|
|
|
|
if (save)
|
2016-04-25 19:48:02 -05:00
|
|
|
{
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_TRC("Sending timed save command for [" << _docKey << "].");
|
2018-12-04 01:49:49 -06:00
|
|
|
sent = sendUnoSave(savingSessionId, /*dontTerminateEdit=*/true,
|
|
|
|
/*dontSaveIfUnmodified=*/true, /*isAutosave=*/true,
|
|
|
|
/*isExitSave=*/false);
|
2016-04-25 19:48:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sent;
|
2016-04-10 21:07:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-04 01:49:49 -06:00
|
|
|
bool DocumentBroker::sendUnoSave(const std::string& sessionId, bool dontTerminateEdit,
|
2019-06-16 13:42:11 -05:00
|
|
|
bool dontSaveIfUnmodified, bool isAutosave, bool isExitSave,
|
|
|
|
const std::string& extendedData)
|
2016-04-10 21:07:09 -05:00
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2017-04-03 23:11:06 -05:00
|
|
|
|
2017-05-15 00:59:04 -05:00
|
|
|
LOG_INF("Saving doc [" << _docKey << "].");
|
2016-04-10 21:07:09 -05:00
|
|
|
|
2017-05-15 00:59:04 -05:00
|
|
|
if (_sessions.find(sessionId) != _sessions.end())
|
2016-04-10 21:07:09 -05:00
|
|
|
{
|
2016-09-19 02:03:31 -05:00
|
|
|
// Invalidate the timestamp to force persisting.
|
2019-08-30 12:37:55 -05:00
|
|
|
_lastFileModifiedTime = std::chrono::system_clock::time_point();
|
2016-09-19 02:03:31 -05:00
|
|
|
|
|
|
|
// We do not want save to terminate editing mode if we are in edit mode now
|
2016-05-09 20:15:09 -05:00
|
|
|
|
2016-09-19 02:03:31 -05:00
|
|
|
std::ostringstream oss;
|
|
|
|
// arguments init
|
|
|
|
oss << "{";
|
2016-09-01 07:25:32 -05:00
|
|
|
|
2017-05-15 00:59:04 -05:00
|
|
|
if (dontTerminateEdit)
|
|
|
|
{
|
|
|
|
oss << "\"DontTerminateEdit\":"
|
|
|
|
<< "{"
|
|
|
|
<< "\"type\":\"boolean\","
|
|
|
|
<< "\"value\":true"
|
|
|
|
<< "}";
|
|
|
|
}
|
2016-07-14 04:49:21 -05:00
|
|
|
|
2016-09-19 02:03:31 -05:00
|
|
|
if (dontSaveIfUnmodified)
|
|
|
|
{
|
2017-05-15 00:59:04 -05:00
|
|
|
if (dontTerminateEdit)
|
|
|
|
oss << ",";
|
|
|
|
|
|
|
|
oss << "\"DontSaveIfUnmodified\":"
|
2016-07-14 04:49:21 -05:00
|
|
|
<< "{"
|
|
|
|
<< "\"type\":\"boolean\","
|
|
|
|
<< "\"value\":true"
|
|
|
|
<< "}";
|
2016-09-19 02:03:31 -05:00
|
|
|
}
|
2016-07-14 04:49:21 -05:00
|
|
|
|
2016-09-19 02:03:31 -05:00
|
|
|
// arguments end
|
|
|
|
oss << "}";
|
2016-07-14 04:49:21 -05:00
|
|
|
|
2017-09-25 12:16:48 -05:00
|
|
|
assert(_storage);
|
2018-09-13 11:16:00 -05:00
|
|
|
_storage->setIsAutosave(isAutosave || UnitWSD::get().isAutosave());
|
2018-12-04 01:49:49 -06:00
|
|
|
_storage->setIsExitSave(isExitSave);
|
2019-06-16 13:42:11 -05:00
|
|
|
_storage->setExtendedData(extendedData);
|
2017-09-25 12:16:48 -05:00
|
|
|
|
2018-02-07 03:17:59 -06:00
|
|
|
const std::string saveArgs = oss.str();
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_TRC(".uno:Save arguments: " << saveArgs);
|
2016-08-31 22:34:41 -05:00
|
|
|
const auto command = "uno .uno:Save " + saveArgs;
|
2017-05-15 00:59:04 -05:00
|
|
|
forwardToChild(sessionId, command);
|
2017-03-20 20:57:21 -05:00
|
|
|
_lastSaveRequestTime = std::chrono::steady_clock::now();
|
2016-09-19 02:03:31 -05:00
|
|
|
return true;
|
2016-04-10 21:07:09 -05:00
|
|
|
}
|
|
|
|
|
2017-05-15 00:59:04 -05:00
|
|
|
LOG_ERR("Failed to save doc [" << _docKey << "]: No valid sessions.");
|
2016-04-25 19:48:02 -05:00
|
|
|
return false;
|
2016-04-09 22:20:20 -05:00
|
|
|
}
|
|
|
|
|
2016-03-23 07:41:18 -05:00
|
|
|
std::string DocumentBroker::getJailRoot() const
|
|
|
|
{
|
|
|
|
assert(!_jailId.empty());
|
2019-03-01 15:03:35 -06:00
|
|
|
return Poco::Path(LOOLWSD::ChildRoot, _jailId).toString();
|
2016-03-23 07:41:18 -05:00
|
|
|
}
|
|
|
|
|
2017-03-25 12:56:17 -05:00
|
|
|
size_t DocumentBroker::addSession(const std::shared_ptr<ClientSession>& session)
|
2017-05-22 14:04:35 -05:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return addSessionInternal(session);
|
|
|
|
}
|
|
|
|
catch (const std::exception& exc)
|
|
|
|
{
|
2018-06-15 09:26:52 -05:00
|
|
|
LOG_ERR("Failed to add session to [" << _docKey << "] with URI [" << LOOLWSD::anonymizeUrl(session->getPublicUri().toString()) << "]: " << exc.what());
|
2017-05-22 14:04:35 -05:00
|
|
|
if (_sessions.empty())
|
|
|
|
{
|
|
|
|
LOG_INF("Doc [" << _docKey << "] has no more sessions. Marking to destroy.");
|
|
|
|
_markToDestroy = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t DocumentBroker::addSessionInternal(const std::shared_ptr<ClientSession>& session)
|
2016-03-23 11:55:28 -05:00
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2017-01-10 16:10:10 -06:00
|
|
|
|
2016-11-06 21:11:59 -06:00
|
|
|
try
|
|
|
|
{
|
|
|
|
// First load the document, since this can fail.
|
2017-05-07 10:05:34 -05:00
|
|
|
if (!load(session, _childProcess->getJailId()))
|
2016-10-18 15:51:11 -05:00
|
|
|
{
|
2016-10-20 18:06:00 -05:00
|
|
|
const auto msg = "Failed to load document with URI [" + session->getPublicUri().toString() + "].";
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_ERR(msg);
|
2016-10-20 18:06:00 -05:00
|
|
|
throw std::runtime_error(msg);
|
2016-10-19 05:21:29 -05:00
|
|
|
}
|
2016-10-12 22:04:07 -05:00
|
|
|
}
|
|
|
|
catch (const StorageSpaceLowException&)
|
|
|
|
{
|
2016-11-06 21:11:59 -06:00
|
|
|
LOG_ERR("Out of storage while loading document with URI [" << session->getPublicUri().toString() << "].");
|
|
|
|
|
2016-10-12 22:04:07 -05:00
|
|
|
// We use the same message as is sent when some of lool's own locations are full,
|
|
|
|
// even if in this case it might be a totally different location (file system, or
|
|
|
|
// some other type of storage somewhere). This message is not sent to all clients,
|
|
|
|
// though, just to all sessions of this document.
|
2016-11-17 08:00:05 -06:00
|
|
|
alertAllUsers("internal", "diskfull");
|
2016-10-12 22:04:07 -05:00
|
|
|
throw;
|
|
|
|
}
|
2019-05-15 21:14:29 -05:00
|
|
|
catch (const std::exception& exc)
|
|
|
|
{
|
|
|
|
LOG_ERR("loading document exception: " << exc.what());
|
|
|
|
throw;
|
|
|
|
}
|
2016-10-12 22:04:07 -05:00
|
|
|
|
2018-02-07 03:17:59 -06:00
|
|
|
const std::string id = session->getId();
|
2016-11-06 22:14:23 -06:00
|
|
|
|
|
|
|
// Request a new session from the child kit.
|
2018-07-10 22:09:27 -05:00
|
|
|
const std::string aMessage = "session " + id + ' ' + _docKey + ' ' + _docId;
|
2016-11-06 22:14:23 -06:00
|
|
|
_childProcess->sendTextFrame(aMessage);
|
|
|
|
|
2019-02-12 05:16:40 -06:00
|
|
|
#if !MOBILEAPP
|
2016-10-22 09:25:57 -05:00
|
|
|
// Tell the admin console about this new doc
|
2017-06-23 07:32:44 -05:00
|
|
|
Admin::instance().addDoc(_docKey, getPid(), getFilename(), id, session->getUserName(), session->getUserId());
|
2019-11-12 03:50:33 -06:00
|
|
|
Admin::instance().setDocWopiDownloadDuration(_docKey, _wopiLoadDuration);
|
2018-09-05 07:11:05 -05:00
|
|
|
#endif
|
2016-10-22 09:25:57 -05:00
|
|
|
|
2017-05-22 14:04:35 -05:00
|
|
|
// Add and attach the session.
|
|
|
|
_sessions.emplace(session->getId(), session);
|
2019-07-04 04:50:33 -05:00
|
|
|
session->setState(ClientSession::SessionState::LOADING);
|
2017-05-22 14:04:35 -05:00
|
|
|
|
2018-02-07 03:17:59 -06:00
|
|
|
const size_t count = _sessions.size();
|
2017-01-02 23:12:15 -06:00
|
|
|
LOG_TRC("Added " << (session->isReadOnly() ? "readonly" : "non-readonly") <<
|
|
|
|
" session [" << id << "] to docKey [" <<
|
|
|
|
_docKey << "] to have " << count << " sessions.");
|
2016-10-12 22:04:07 -05:00
|
|
|
|
2016-11-06 22:14:23 -06:00
|
|
|
return count;
|
2016-03-23 11:55:28 -05:00
|
|
|
}
|
|
|
|
|
2018-01-14 16:17:00 -06:00
|
|
|
size_t DocumentBroker::removeSession(const std::string& id)
|
2016-03-23 11:55:28 -05:00
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2017-03-09 12:19:53 -06:00
|
|
|
|
2016-11-27 20:28:21 -06:00
|
|
|
try
|
2016-03-23 11:55:28 -05:00
|
|
|
{
|
2018-01-14 16:17:00 -06:00
|
|
|
const auto it = _sessions.find(id);
|
|
|
|
if (it == _sessions.end())
|
|
|
|
{
|
|
|
|
LOG_ERR("Invalid or unknown session [" << id << "] to remove.");
|
|
|
|
return _sessions.size();
|
|
|
|
}
|
2019-11-19 16:51:45 -06:00
|
|
|
std::shared_ptr<ClientSession> session = it->second;
|
2018-01-14 16:17:00 -06:00
|
|
|
|
|
|
|
// Last view going away, can destroy.
|
|
|
|
_markToDestroy = (_sessions.size() <= 1);
|
|
|
|
|
2019-11-19 16:51:45 -06:00
|
|
|
const bool lastEditableSession = !session->isReadOnly() && !haveAnotherEditableSession(id);
|
2019-10-22 09:28:57 -05:00
|
|
|
static const bool dontSaveIfUnmodified = !LOOLWSD::getConfigValue<bool>("per_document.always_save_on_exit", false);
|
2018-01-14 16:17:00 -06:00
|
|
|
|
2019-10-16 08:05:04 -05:00
|
|
|
LOG_INF("Removing session ["
|
|
|
|
<< id << "] on docKey [" << _docKey << "]. Have " << _sessions.size()
|
2019-11-19 16:51:45 -06:00
|
|
|
<< " sessions. IsReadOnly: " << session->isReadOnly()
|
|
|
|
<< ", IsViewLoaded: " << session->isViewLoaded() << ", IsWaitDisconnected: "
|
|
|
|
<< session->inWaitDisconnected() << ", MarkToDestroy: " << _markToDestroy
|
2019-10-22 09:28:57 -05:00
|
|
|
<< ", LastEditableSession: " << lastEditableSession
|
|
|
|
<< ", dontSaveIfUnmodified: " << dontSaveIfUnmodified);
|
2019-06-06 04:48:54 -05:00
|
|
|
|
2018-01-14 16:17:00 -06:00
|
|
|
// If last editable, save and don't remove until after uploading to storage.
|
2019-06-06 04:48:54 -05:00
|
|
|
if (!lastEditableSession || !autoSave(isPossiblyModified(), dontSaveIfUnmodified))
|
2019-07-04 04:50:33 -05:00
|
|
|
disconnectSessionInternal(id);
|
2017-03-09 22:50:36 -06:00
|
|
|
}
|
|
|
|
catch (const std::exception& ex)
|
|
|
|
{
|
|
|
|
LOG_ERR("Error while removing session [" << id << "]: " << ex.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
return _sessions.size();
|
|
|
|
}
|
|
|
|
|
2019-07-04 04:50:33 -05:00
|
|
|
void DocumentBroker::disconnectSessionInternal(const std::string& id)
|
2017-03-09 22:50:36 -06:00
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2017-03-09 22:50:36 -06:00
|
|
|
try
|
|
|
|
{
|
2019-02-12 05:16:40 -06:00
|
|
|
#if !MOBILEAPP
|
2016-11-27 20:28:21 -06:00
|
|
|
Admin::instance().rmDoc(_docKey, id);
|
2018-09-05 07:11:05 -05:00
|
|
|
#endif
|
2016-11-27 20:28:21 -06:00
|
|
|
auto it = _sessions.find(id);
|
|
|
|
if (it != _sessions.end())
|
|
|
|
{
|
2019-02-12 05:16:40 -06:00
|
|
|
#if !MOBILEAPP
|
2017-02-05 20:59:08 -06:00
|
|
|
LOOLWSD::dumpEndSessionTrace(getJailId(), id, _uriOrig);
|
2018-09-05 07:11:05 -05:00
|
|
|
#endif
|
2017-02-05 18:35:54 -06:00
|
|
|
|
2019-11-19 16:51:45 -06:00
|
|
|
LOG_TRC("Disconnect session internal " << id <<
|
|
|
|
" destroy? " << _markToDestroy <<
|
|
|
|
" locked? " << _lockCtx->_isLocked);
|
|
|
|
|
|
|
|
if (_markToDestroy && // last session to remove; FIXME: Editable?
|
|
|
|
_lockCtx->_isLocked && _storage)
|
|
|
|
{
|
|
|
|
if (!_storage->updateLockState(it->second->getAuthorization(), *_lockCtx, false))
|
|
|
|
LOG_ERR("Failed to unlock!");
|
|
|
|
}
|
2019-07-04 04:50:33 -05:00
|
|
|
|
|
|
|
bool hardDisconnect;
|
|
|
|
if (it->second->inWaitDisconnected())
|
|
|
|
{
|
|
|
|
LOG_TRC("hard disconnecting while waiting for disconnected handshake.");
|
|
|
|
hardDisconnect = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hardDisconnect = it->second->disconnectFromKit();
|
|
|
|
|
|
|
|
// Let the child know the client has disconnected.
|
|
|
|
const std::string msg("child-" + id + " disconnect");
|
|
|
|
_childProcess->sendTextFrame(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hardDisconnect)
|
|
|
|
finalRemoveSession(id);
|
|
|
|
// else wait for disconnected.
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOG_TRC("Session [" << id << "] not found to disconnect from docKey [" <<
|
|
|
|
_docKey << "]. Have " << _sessions.size() << " sessions.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const std::exception& ex)
|
|
|
|
{
|
|
|
|
LOG_ERR("Error while disconnecting session [" << id << "]: " << ex.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentBroker::finalRemoveSession(const std::string& id)
|
|
|
|
{
|
|
|
|
assertCorrectThread();
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto it = _sessions.find(id);
|
|
|
|
if (it != _sessions.end())
|
|
|
|
{
|
2018-02-07 03:17:59 -06:00
|
|
|
const bool readonly = (it->second ? it->second->isReadOnly() : false);
|
2017-04-06 12:49:44 -05:00
|
|
|
|
2017-06-19 20:53:58 -05:00
|
|
|
// Remove. The caller must have a reference to the session
|
2019-07-04 04:50:33 -05:00
|
|
|
// in question, lest we destroy from underneath them.
|
2016-11-27 20:28:21 -06:00
|
|
|
_sessions.erase(it);
|
2018-02-07 03:17:59 -06:00
|
|
|
const size_t count = _sessions.size();
|
2018-01-14 10:16:10 -06:00
|
|
|
|
2018-02-07 03:17:59 -06:00
|
|
|
Log::StreamLogger logger = Log::trace();
|
2018-01-14 10:16:10 -06:00
|
|
|
if (logger.enabled())
|
2017-04-04 23:26:09 -05:00
|
|
|
{
|
2018-01-14 10:16:10 -06:00
|
|
|
logger << "Removed " << (readonly ? "readonly" : "non-readonly")
|
|
|
|
<< " session [" << id << "] from docKey ["
|
|
|
|
<< _docKey << "] to have " << count << " sessions:";
|
|
|
|
for (const auto& pair : _sessions)
|
|
|
|
logger << pair.second->getId() << ' ';
|
|
|
|
|
2018-06-04 09:23:58 -05:00
|
|
|
LOG_END(logger, true);
|
2017-04-04 23:26:09 -05:00
|
|
|
}
|
2017-02-04 14:46:52 -06:00
|
|
|
|
2019-07-04 04:50:33 -05:00
|
|
|
return;
|
2017-01-02 23:12:15 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-08 11:22:21 -06:00
|
|
|
LOG_TRC("Session [" << id << "] not found to remove from docKey [" <<
|
|
|
|
_docKey << "]. Have " << _sessions.size() << " sessions.");
|
2016-11-27 20:28:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const std::exception& ex)
|
|
|
|
{
|
|
|
|
LOG_ERR("Error while removing session [" << id << "]: " << ex.what());
|
|
|
|
}
|
2016-03-23 11:55:28 -05:00
|
|
|
}
|
|
|
|
|
2017-04-11 01:54:09 -05:00
|
|
|
void DocumentBroker::addCallback(const SocketPoll::CallbackFn& fn)
|
2017-03-20 12:18:41 -05:00
|
|
|
{
|
|
|
|
_poll->addCallback(fn);
|
|
|
|
}
|
|
|
|
|
2017-03-07 11:34:01 -06:00
|
|
|
void DocumentBroker::addSocketToPoll(const std::shared_ptr<Socket>& socket)
|
|
|
|
{
|
|
|
|
_poll->insertNewSocket(socket);
|
|
|
|
}
|
|
|
|
|
2016-11-17 08:00:05 -06:00
|
|
|
void DocumentBroker::alertAllUsers(const std::string& msg)
|
2016-09-28 14:07:07 -05:00
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2016-09-28 14:07:07 -05:00
|
|
|
|
2017-06-16 11:55:23 -05:00
|
|
|
if (UnitWSD::get().filterAlertAllusers(msg))
|
|
|
|
return;
|
|
|
|
|
2017-01-21 18:51:02 -06:00
|
|
|
auto payload = std::make_shared<Message>(msg, Message::Dir::Out);
|
2016-12-13 18:20:05 -06:00
|
|
|
|
2016-11-22 21:06:05 -06:00
|
|
|
LOG_DBG("Alerting all users of [" << _docKey << "]: " << msg);
|
2016-10-29 20:15:00 -05:00
|
|
|
for (auto& it : _sessions)
|
2016-09-28 14:07:07 -05:00
|
|
|
{
|
2019-07-04 04:50:33 -05:00
|
|
|
if (!it.second->inWaitDisconnected())
|
|
|
|
it.second->enqueueSendMessage(payload);
|
2016-09-28 14:07:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-06 13:50:06 -06:00
|
|
|
/// Handles input from the prisoner / child kit process
|
2016-05-01 19:50:11 -05:00
|
|
|
bool DocumentBroker::handleInput(const std::vector<char>& payload)
|
|
|
|
{
|
2017-01-22 22:18:37 -06:00
|
|
|
auto message = std::make_shared<Message>(payload.data(), payload.size(), Message::Dir::Out);
|
|
|
|
const auto& msg = message->abbr();
|
2016-12-21 20:07:18 -06:00
|
|
|
LOG_TRC("DocumentBroker handling child message: [" << msg << "].");
|
2016-05-01 19:50:11 -05:00
|
|
|
|
2019-02-12 05:16:40 -06:00
|
|
|
#if !MOBILEAPP
|
2016-09-19 18:18:07 -05:00
|
|
|
LOOLWSD::dumpOutgoingTrace(getJailId(), "0", msg);
|
2018-09-05 07:11:05 -05:00
|
|
|
#endif
|
2016-07-31 06:54:47 -05:00
|
|
|
|
2017-01-22 22:18:37 -06:00
|
|
|
if (LOOLProtocol::getFirstToken(message->forwardToken(), '-') == "client")
|
2016-05-01 19:50:11 -05:00
|
|
|
{
|
2017-01-22 22:18:37 -06:00
|
|
|
forwardToClient(message);
|
2016-09-28 14:07:07 -05:00
|
|
|
}
|
2016-09-19 18:18:07 -05:00
|
|
|
else
|
|
|
|
{
|
2017-01-22 22:18:37 -06:00
|
|
|
const auto& command = message->firstToken();
|
|
|
|
if (command == "tile:")
|
|
|
|
{
|
|
|
|
handleTileResponse(payload);
|
|
|
|
}
|
|
|
|
else if (command == "tilecombine:")
|
|
|
|
{
|
|
|
|
handleTileCombinedResponse(payload);
|
|
|
|
}
|
|
|
|
else if (command == "errortoall:")
|
|
|
|
{
|
|
|
|
LOG_CHECK_RET(message->tokens().size() == 3, false);
|
|
|
|
std::string cmd, kind;
|
|
|
|
LOOLProtocol::getTokenString((*message)[1], "cmd", cmd);
|
|
|
|
LOG_CHECK_RET(cmd != "", false);
|
|
|
|
LOOLProtocol::getTokenString((*message)[2], "kind", kind);
|
|
|
|
LOG_CHECK_RET(kind != "", false);
|
|
|
|
Util::alertAllUsers(cmd, kind);
|
|
|
|
}
|
2019-02-12 05:16:40 -06:00
|
|
|
#if !MOBILEAPP
|
2017-02-03 00:29:53 -06:00
|
|
|
else if (command == "procmemstats:")
|
|
|
|
{
|
2017-02-07 08:39:56 -06:00
|
|
|
int dirty;
|
|
|
|
if (message->getTokenInteger("dirty", dirty))
|
2017-02-03 00:29:53 -06:00
|
|
|
{
|
2019-10-28 08:26:15 -05:00
|
|
|
Admin::instance().updateMemoryDirty(
|
|
|
|
_docKey, dirty + getMemorySize()/1024);
|
2017-02-03 00:29:53 -06:00
|
|
|
}
|
|
|
|
}
|
2018-09-05 07:11:05 -05:00
|
|
|
#endif
|
2017-01-22 22:18:37 -06:00
|
|
|
else
|
|
|
|
{
|
|
|
|
LOG_ERR("Unexpected message: [" << msg << "].");
|
|
|
|
return false;
|
|
|
|
}
|
2016-09-19 18:18:07 -05:00
|
|
|
}
|
2016-05-01 19:50:11 -05:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-10-28 08:26:15 -05:00
|
|
|
size_t DocumentBroker::getMemorySize() const
|
|
|
|
{
|
2019-11-05 18:33:37 -06:00
|
|
|
return sizeof(DocumentBroker) +
|
|
|
|
(!!_tileCache ? _tileCache->getMemorySize() : 0) +
|
2019-10-28 08:26:15 -05:00
|
|
|
_sessions.size() * sizeof(ClientSession);
|
|
|
|
}
|
|
|
|
|
2019-10-15 07:35:35 -05:00
|
|
|
void DocumentBroker::invalidateTiles(const std::string& tiles, int normalizedViewId)
|
2016-09-20 21:19:52 -05:00
|
|
|
{
|
|
|
|
// Remove from cache.
|
2019-10-15 07:35:35 -05:00
|
|
|
_tileCache->invalidateTiles(tiles, normalizedViewId);
|
2016-09-20 21:19:52 -05:00
|
|
|
}
|
|
|
|
|
2016-05-22 13:31:18 -05:00
|
|
|
void DocumentBroker::handleTileRequest(TileDesc& tile,
|
2016-05-16 19:49:36 -05:00
|
|
|
const std::shared_ptr<ClientSession>& session)
|
2016-05-01 19:50:11 -05:00
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2016-05-01 19:50:11 -05:00
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
|
2016-05-22 13:31:18 -05:00
|
|
|
tile.setVersion(++_tileVersion);
|
2018-02-07 03:17:59 -06:00
|
|
|
const std::string tileMsg = tile.serialize();
|
2017-01-01 23:30:27 -06:00
|
|
|
LOG_TRC("Tile request for " << tileMsg);
|
2016-05-01 19:50:11 -05:00
|
|
|
|
2019-02-14 13:01:43 -06:00
|
|
|
TileCache::Tile cachedTile = _tileCache->lookupTile(tile);
|
2016-05-01 19:50:11 -05:00
|
|
|
if (cachedTile)
|
|
|
|
{
|
2019-04-20 13:58:48 -05:00
|
|
|
const std::string response = tile.serialize("tile:", ADD_DEBUG_RENDERID);
|
2019-02-14 13:01:43 -06:00
|
|
|
session->sendTile(response, cachedTile);
|
2016-05-01 19:50:11 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-07 08:07:50 -05:00
|
|
|
if (tile.getBroadcast())
|
|
|
|
{
|
|
|
|
for (auto& it: _sessions)
|
|
|
|
{
|
2019-07-04 04:50:33 -05:00
|
|
|
if (!it.second->inWaitDisconnected())
|
|
|
|
tileCache().subscribeToTileRendering(tile, it.second);
|
2016-10-07 08:07:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tileCache().subscribeToTileRendering(tile, session);
|
|
|
|
}
|
2016-05-04 21:05:09 -05:00
|
|
|
|
2016-09-20 21:26:19 -05:00
|
|
|
// Forward to child to render.
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_DBG("Sending render request for tile (" << tile.getPart() << ',' <<
|
|
|
|
tile.getTilePosX() << ',' << tile.getTilePosY() << ").");
|
2017-01-01 23:30:27 -06:00
|
|
|
const std::string request = "tile " + tileMsg;
|
2016-10-11 17:22:51 -05:00
|
|
|
_childProcess->sendTextFrame(request);
|
2016-10-11 07:39:56 -05:00
|
|
|
_debugRenderedTileCount++;
|
2016-05-01 19:50:11 -05:00
|
|
|
}
|
|
|
|
|
2016-05-22 10:45:28 -05:00
|
|
|
void DocumentBroker::handleTileCombinedRequest(TileCombined& tileCombined,
|
|
|
|
const std::shared_ptr<ClientSession>& session)
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_TRC("TileCombined request for " << tileCombined.serialize());
|
2016-05-22 13:31:18 -05:00
|
|
|
|
2019-02-14 13:01:43 -06:00
|
|
|
// Check which newly requested tiles need rendering.
|
2018-06-07 06:13:36 -05:00
|
|
|
std::vector<TileDesc> tilesNeedsRendering;
|
2016-05-29 16:40:27 -05:00
|
|
|
for (auto& tile : tileCombined.getTiles())
|
2016-05-22 10:45:28 -05:00
|
|
|
{
|
2018-09-26 15:15:26 -05:00
|
|
|
tile.setVersion(++_tileVersion);
|
2019-02-14 13:01:43 -06:00
|
|
|
TileCache::Tile cachedTile = _tileCache->lookupTile(tile);
|
|
|
|
if(!cachedTile)
|
2016-05-22 10:45:28 -05:00
|
|
|
{
|
2016-08-18 09:28:55 -05:00
|
|
|
// Not cached, needs rendering.
|
2018-06-07 06:13:36 -05:00
|
|
|
tilesNeedsRendering.push_back(tile);
|
2016-10-11 07:39:56 -05:00
|
|
|
_debugRenderedTileCount++;
|
2018-09-26 15:15:26 -05:00
|
|
|
tileCache().registerTileBeingRendered(tile);
|
2016-05-22 10:45:28 -05:00
|
|
|
}
|
2016-05-29 16:40:27 -05:00
|
|
|
}
|
2016-09-25 15:04:27 -05:00
|
|
|
|
2018-06-12 07:51:38 -05:00
|
|
|
// Send rendering request, prerender before we actually send the tiles
|
2018-06-07 06:13:36 -05:00
|
|
|
if (!tilesNeedsRendering.empty())
|
2016-09-25 15:04:27 -05:00
|
|
|
{
|
2018-06-07 06:13:36 -05:00
|
|
|
TileCombined newTileCombined = TileCombined::create(tilesNeedsRendering);
|
2016-09-25 15:04:27 -05:00
|
|
|
|
|
|
|
// Forward to child to render.
|
2018-02-07 03:17:59 -06:00
|
|
|
const std::string req = newTileCombined.serialize("tilecombine");
|
2019-04-22 11:03:49 -05:00
|
|
|
LOG_TRC("Sending uncached residual tilecombine request to Kit: " << req);
|
2016-10-11 17:22:51 -05:00
|
|
|
_childProcess->sendTextFrame(req);
|
2016-09-25 15:04:27 -05:00
|
|
|
}
|
2018-06-07 06:13:36 -05:00
|
|
|
|
|
|
|
// Accumulate tiles
|
2018-08-03 07:25:21 -05:00
|
|
|
std::deque<TileDesc>& requestedTiles = session->getRequestedTiles();
|
|
|
|
if (requestedTiles.empty())
|
2018-06-07 06:13:36 -05:00
|
|
|
{
|
2018-08-03 07:25:21 -05:00
|
|
|
requestedTiles = std::deque<TileDesc>(tileCombined.getTiles().begin(), tileCombined.getTiles().end());
|
2018-06-07 06:13:36 -05:00
|
|
|
}
|
|
|
|
// Drop duplicated tiles, but use newer version number
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (const auto& newTile : tileCombined.getTiles())
|
|
|
|
{
|
2018-08-03 07:25:21 -05:00
|
|
|
const TileDesc& firstOldTile = *(requestedTiles.begin());
|
2018-09-21 08:49:30 -05:00
|
|
|
if(!session->isTextDocument() && newTile.getPart() != firstOldTile.getPart())
|
2018-06-07 06:13:36 -05:00
|
|
|
{
|
2018-09-21 08:49:30 -05:00
|
|
|
LOG_WRN("Different part numbers in tile requests");
|
|
|
|
}
|
|
|
|
else if (newTile.getTileWidth() != firstOldTile.getTileWidth() ||
|
|
|
|
newTile.getTileHeight() != firstOldTile.getTileHeight() )
|
|
|
|
{
|
|
|
|
LOG_WRN("Different tile sizes in tile requests");
|
2018-06-07 06:13:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bool tileFound = false;
|
2018-08-03 07:25:21 -05:00
|
|
|
for (auto& oldTile : requestedTiles)
|
2018-06-07 06:13:36 -05:00
|
|
|
{
|
|
|
|
if(oldTile.getTilePosX() == newTile.getTilePosX() &&
|
2019-10-15 07:35:35 -05:00
|
|
|
oldTile.getTilePosY() == newTile.getTilePosY() &&
|
|
|
|
oldTile.getNormalizedViewId() == newTile.getNormalizedViewId())
|
2018-06-07 06:13:36 -05:00
|
|
|
{
|
|
|
|
oldTile.setVersion(newTile.getVersion());
|
|
|
|
oldTile.setOldWireId(newTile.getOldWireId());
|
|
|
|
oldTile.setWireId(newTile.getWireId());
|
|
|
|
tileFound = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!tileFound)
|
2018-08-03 07:25:21 -05:00
|
|
|
requestedTiles.push_back(newTile);
|
2018-06-07 06:13:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lock.unlock();
|
|
|
|
lock.release();
|
|
|
|
sendRequestedTiles(session);
|
|
|
|
}
|
|
|
|
|
2019-07-04 04:50:33 -05:00
|
|
|
/// lookup in global clipboard cache and send response, send error if missing if @sendError
|
|
|
|
bool DocumentBroker::lookupSendClipboardTag(const std::shared_ptr<StreamSocket> &socket,
|
|
|
|
const std::string &tag, bool sendError)
|
2019-06-21 06:35:17 -05:00
|
|
|
{
|
2019-07-04 04:50:33 -05:00
|
|
|
LOG_TRC("Clipboard request " << tag << " not for a live session - check cache.");
|
2019-08-12 04:04:10 -05:00
|
|
|
#if !MOBILEAPP
|
2019-07-04 04:50:33 -05:00
|
|
|
std::shared_ptr<std::string> saved =
|
|
|
|
LOOLWSD::SavedClipboards->getClipboard(tag);
|
|
|
|
if (saved)
|
2019-06-21 06:35:17 -05:00
|
|
|
{
|
2019-07-04 04:50:33 -05:00
|
|
|
std::ostringstream oss;
|
|
|
|
oss << "HTTP/1.1 200 OK\r\n"
|
2019-08-30 12:37:55 -05:00
|
|
|
<< "Last-Modified: " << Util::getHttpTimeNow() << "\r\n"
|
2019-07-04 04:50:33 -05:00
|
|
|
<< "User-Agent: " << WOPI_AGENT_STRING << "\r\n"
|
|
|
|
<< "Content-Length: " << saved->length() << "\r\n"
|
|
|
|
<< "Content-Type: application/octet-stream\r\n"
|
|
|
|
<< "X-Content-Type-Options: nosniff\r\n"
|
|
|
|
<< "\r\n";
|
|
|
|
oss.write(saved->c_str(), saved->length());
|
|
|
|
socket->setSocketBufferSize(std::min(saved->length() + 256,
|
|
|
|
size_t(Socket::MaximumSendBufferSize)));
|
|
|
|
socket->send(oss.str());
|
|
|
|
socket->shutdown();
|
|
|
|
LOG_INF("Found and queued clipboard response for send of size " << saved->length());
|
|
|
|
return true;
|
2019-06-21 06:35:17 -05:00
|
|
|
}
|
2019-08-12 04:04:10 -05:00
|
|
|
#endif
|
2019-07-04 04:50:33 -05:00
|
|
|
|
|
|
|
if (!sendError)
|
|
|
|
return false;
|
2019-06-21 06:35:17 -05:00
|
|
|
|
2019-08-12 04:04:10 -05:00
|
|
|
#if !MOBILEAPP
|
2019-06-21 06:35:17 -05:00
|
|
|
// Bad request.
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << "HTTP/1.1 400\r\n"
|
2019-08-30 12:37:55 -05:00
|
|
|
<< "Date: " << Util::getHttpTimeNow() << "\r\n"
|
2019-06-21 06:35:17 -05:00
|
|
|
<< "User-Agent: LOOLWSD WOPI Agent\r\n"
|
|
|
|
<< "Content-Length: 0\r\n"
|
|
|
|
<< "\r\n"
|
|
|
|
<< "Failed to find this clipboard";
|
|
|
|
socket->send(oss.str());
|
2019-08-12 04:04:10 -05:00
|
|
|
#endif
|
2019-06-21 06:35:17 -05:00
|
|
|
socket->shutdown();
|
2019-07-04 04:50:33 -05:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentBroker::handleClipboardRequest(ClipboardRequest type, const std::shared_ptr<StreamSocket> &socket,
|
|
|
|
const std::string &viewId, const std::string &tag,
|
|
|
|
const std::shared_ptr<std::string> &data)
|
|
|
|
{
|
|
|
|
for (auto& it : _sessions)
|
|
|
|
{
|
|
|
|
if (it.second->matchesClipboardKeys(viewId, tag))
|
|
|
|
{
|
|
|
|
it.second->handleClipboardRequest(type, socket, tag, data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!lookupSendClipboardTag(socket, tag, true))
|
|
|
|
LOG_ERR("Could not find matching session to handle clipboard request for " << viewId << " tag: " << tag);
|
2019-06-21 06:35:17 -05:00
|
|
|
}
|
|
|
|
|
2018-06-07 06:13:36 -05:00
|
|
|
void DocumentBroker::sendRequestedTiles(const std::shared_ptr<ClientSession>& session)
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
|
2018-07-05 07:40:28 -05:00
|
|
|
// How many tiles we have on the visible area, set the upper limit accordingly
|
2018-08-30 10:40:42 -05:00
|
|
|
Util::Rectangle normalizedVisArea = session->getNormalizedVisibleArea();
|
2018-07-05 07:40:28 -05:00
|
|
|
|
2018-09-03 09:35:00 -05:00
|
|
|
float tilesOnFlyUpperLimit = 0;
|
|
|
|
if (normalizedVisArea.hasSurface() && session->getTileWidthInTwips() != 0 && session->getTileHeightInTwips() != 0)
|
|
|
|
{
|
2019-10-22 09:28:57 -05:00
|
|
|
|
2018-11-14 02:07:47 -06:00
|
|
|
const int tilesFitOnWidth = std::ceil(normalizedVisArea.getRight() / session->getTileWidthInTwips()) -
|
|
|
|
std::ceil(normalizedVisArea.getLeft() / session->getTileWidthInTwips()) + 1;
|
|
|
|
const int tilesFitOnHeight = std::ceil(normalizedVisArea.getBottom() / session->getTileHeightInTwips()) -
|
|
|
|
std::ceil(normalizedVisArea.getTop() / session->getTileHeightInTwips()) + 1;
|
2018-09-03 09:35:00 -05:00
|
|
|
const int tilesInVisArea = tilesFitOnWidth * tilesFitOnHeight;
|
|
|
|
|
2018-09-26 15:15:41 -05:00
|
|
|
tilesOnFlyUpperLimit = std::max(TILES_ON_FLY_MIN_UPPER_LIMIT, tilesInVisArea * 1.1f);
|
2018-09-03 09:35:00 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-10-08 04:23:29 -05:00
|
|
|
tilesOnFlyUpperLimit = 200; // Have a big number here to get all tiles requested by file opening
|
2018-09-03 09:35:00 -05:00
|
|
|
}
|
2018-08-30 10:40:42 -05:00
|
|
|
|
2018-08-23 05:46:49 -05:00
|
|
|
// Drop tiles which we are waiting for too long
|
|
|
|
session->removeOutdatedTilesOnFly();
|
2018-07-31 07:47:27 -05:00
|
|
|
|
2019-02-14 13:01:43 -06:00
|
|
|
// All tiles were processed on client side that we sent last time, so we can send
|
2019-10-08 04:23:29 -05:00
|
|
|
// a new batch of tiles which was invalidated / requested in the meantime
|
2018-08-03 07:25:21 -05:00
|
|
|
std::deque<TileDesc>& requestedTiles = session->getRequestedTiles();
|
|
|
|
if (!requestedTiles.empty())
|
2018-06-07 06:13:36 -05:00
|
|
|
{
|
2018-09-28 12:30:57 -05:00
|
|
|
size_t delayedTiles = 0;
|
2018-06-12 07:51:38 -05:00
|
|
|
std::vector<TileDesc> tilesNeedsRendering;
|
2019-03-02 12:26:08 -06:00
|
|
|
size_t beingRendered = _tileCache->countTilesBeingRenderedForSession(session);
|
|
|
|
while(session->getTilesOnFlyCount() + beingRendered < tilesOnFlyUpperLimit &&
|
2018-09-28 12:30:57 -05:00
|
|
|
!requestedTiles.empty() &&
|
|
|
|
// If we delayed all tiles we don't send any tile (we will when next tileprocessed message arrives)
|
|
|
|
delayedTiles < requestedTiles.size())
|
2018-06-07 06:13:36 -05:00
|
|
|
{
|
2018-08-03 07:25:21 -05:00
|
|
|
TileDesc& tile = *(requestedTiles.begin());
|
2018-07-10 07:05:36 -05:00
|
|
|
|
2018-09-28 12:30:57 -05:00
|
|
|
// We already sent out two versions of the same tile, let's not send the third one
|
|
|
|
// until we get a tileprocessed message for this specific tile.
|
|
|
|
if (session->countIdenticalTilesOnFly(tile) >= 2)
|
|
|
|
{
|
|
|
|
requestedTiles.push_back(requestedTiles.front());
|
|
|
|
requestedTiles.pop_front();
|
|
|
|
delayedTiles += 1;
|
|
|
|
LOG_INF("Requested tile was delayed!");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-07-10 07:05:36 -05:00
|
|
|
// Satisfy as many tiles from the cache.
|
2019-02-14 13:01:43 -06:00
|
|
|
TileCache::Tile cachedTile = _tileCache->lookupTile(tile);
|
2018-06-07 06:13:36 -05:00
|
|
|
if (cachedTile)
|
|
|
|
{
|
|
|
|
//TODO: Combine the response to reduce latency.
|
2019-04-20 13:58:48 -05:00
|
|
|
const std::string response = tile.serialize("tile:", ADD_DEBUG_RENDERID);
|
2019-02-14 13:01:43 -06:00
|
|
|
session->sendTile(response, cachedTile);
|
2018-06-07 06:13:36 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-06-12 07:51:38 -05:00
|
|
|
// Not cached, needs rendering.
|
2018-09-26 15:15:26 -05:00
|
|
|
if (!tileCache().hasTileBeingRendered(tile) || // There is no in progress rendering of the given tile
|
|
|
|
tileCache().getTileBeingRenderedVersion(tile) < tile.getVersion()) // We need a newer version
|
|
|
|
{
|
|
|
|
tile.setVersion(++_tileVersion);
|
|
|
|
tilesNeedsRendering.push_back(tile);
|
|
|
|
_debugRenderedTileCount++;
|
|
|
|
}
|
2018-06-07 06:13:36 -05:00
|
|
|
tileCache().subscribeToTileRendering(tile, session);
|
2019-03-02 12:26:08 -06:00
|
|
|
beingRendered++;
|
2018-06-07 06:13:36 -05:00
|
|
|
}
|
2018-08-03 07:25:21 -05:00
|
|
|
requestedTiles.pop_front();
|
2018-06-07 06:13:36 -05:00
|
|
|
}
|
2018-06-12 07:51:38 -05:00
|
|
|
|
|
|
|
// Send rendering request for those tiles which were not prerendered
|
|
|
|
if (!tilesNeedsRendering.empty())
|
|
|
|
{
|
|
|
|
TileCombined newTileCombined = TileCombined::create(tilesNeedsRendering);
|
|
|
|
|
|
|
|
// Forward to child to render.
|
|
|
|
const std::string req = newTileCombined.serialize("tilecombine");
|
2019-04-22 11:03:49 -05:00
|
|
|
LOG_TRC("Some of the tiles were not prerendered. Sending residual tilecombine: " << req);
|
2018-06-12 07:51:38 -05:00
|
|
|
_childProcess->sendTextFrame(req);
|
|
|
|
}
|
2018-06-07 06:13:36 -05:00
|
|
|
}
|
2016-05-22 10:45:28 -05:00
|
|
|
}
|
|
|
|
|
2016-08-30 22:15:44 -05:00
|
|
|
void DocumentBroker::cancelTileRequests(const std::shared_ptr<ClientSession>& session)
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
|
2018-06-07 06:13:36 -05:00
|
|
|
// Clear tile requests
|
2018-07-10 07:05:36 -05:00
|
|
|
session->clearTilesOnFly();
|
2018-08-03 07:25:21 -05:00
|
|
|
|
|
|
|
session->getRequestedTiles().clear();
|
2018-06-07 06:13:36 -05:00
|
|
|
|
2018-08-22 09:19:04 -05:00
|
|
|
session->resetWireIdMap();
|
|
|
|
|
wsd: avoid UB in DocumentBroker::cancelTileRequests()
With this, it's possible to open a document and type keys with
sanitizers enabled.
/home/vmiklos/lode/opt_private/gcc-7.3.0/lib/gcc/x86_64-pc-linux-gnu/7.3.0/../../../../include/c++/7.3.0/bits/unique_ptr.h:323:9: runtime error: reference binding to null pointer of type 'TileCache'
#0 0x911996 in std::unique_ptr<TileCache, std::default_delete<TileCache> >::operator*() const /home/vmiklos/lode/opt_private/gcc-7.3.0/lib/gcc/x86_64-pc-linux-gnu/7.3.0/../../../../include/c++/7.3.0/bits/unique_ptr.h:323:2
#1 0x8ecb2a in DocumentBroker::tileCache() /home/vmiklos/lode/dev/online/./wsd/DocumentBroker.hpp:265:37
#2 0x8c6a63 in DocumentBroker::cancelTileRequests(std::shared_ptr<ClientSession> const&) /home/vmiklos/lode/dev/online/wsd/DocumentBroker.cpp:1586:37
#3 0xb32b0e in ClientSession::_handleInput(char const*, int) /home/vmiklos/lode/dev/online/wsd/ClientSession.cpp:194:20
#4 0xd45d3c in Session::handleMessage(bool, WSOpCode, std::vector<char, std::allocator<char> >&) /home/vmiklos/lode/dev/online/common/Session.cpp:219:13
#5 0x768080 in WebSocketHandler::handleTCPStream(std::shared_ptr<StreamSocket> const&) /home/vmiklos/lode/dev/online/./net/WebSocketHandler.hpp:368:13
#6 0x6f800d in WebSocketHandler::handleIncomingMessage(SocketDisposition&) /home/vmiklos/lode/dev/online/./net/WebSocketHandler.hpp:419:20
#7 0xb2c644 in ClientSession::handleIncomingMessage(SocketDisposition&) /home/vmiklos/lode/dev/online/wsd/ClientSession.cpp:74:14
#8 0xa6f641 in StreamSocket::handlePoll(SocketDisposition&, std::chrono::time_point<std::chrono::_V2::steady_clock, std::chrono::duration<long, std::ratio<1l, 1000000000l> > >, int) /home/vmiklos/lode/dev/online/./net/Socket.hpp:1037:29
#9 0x6ec63d in SocketPoll::poll(int) /home/vmiklos/lode/dev/online/./net/Socket.hpp:570:34
#10 0x83af99 in DocumentBroker::pollThread() /home/vmiklos/lode/dev/online/wsd/DocumentBroker.cpp:387:16
#11 0x8fc778 in DocumentBroker::DocumentBrokerPoll::pollingThread() /home/vmiklos/lode/dev/online/wsd/DocumentBroker.cpp:165:20
#12 0xdff935 in SocketPoll::pollingThreadEntry() /home/vmiklos/lode/dev/online/net/Socket.cpp:184:9
#13 0xe487bd in void std::__invoke_impl<void, void (SocketPoll::*)(), SocketPoll*>(std::__invoke_memfun_deref, void (SocketPoll::*&&)(), SocketPoll*&&) /home/vmiklos/lode/opt_private/gcc-7.3.0/lib/gcc/x86_64-pc-linux-gnu/7.3.0/../../../../include/c++/7.3.0/bits/invoke.h:73:14
#14 0xe482ca in std::__invoke_result<void (SocketPoll::*)(), SocketPoll*>::type std::__invoke<void (SocketPoll::*)(), SocketPoll*>(void (SocketPoll::*&&)(), SocketPoll*&&) /home/vmiklos/lode/opt_private/gcc-7.3.0/lib/gcc/x86_64-pc-linux-gnu/7.3.0/../../../../include/c++/7.3.0/bits/invoke.h:95:14
#15 0xe4817d in decltype(std::__invoke(_S_declval<0ul>(), _S_declval<1ul>())) std::thread::_Invoker<std::tuple<void (SocketPoll::*)(), SocketPoll*> >::_M_invoke<0ul, 1ul>(std::_Index_tuple<0ul, 1ul>) /home/vmiklos/lode/opt_private/gcc-7.3.0/lib/gcc/x86_64-pc-linux-gnu/7.3.0/../../../../include/c++/7.3.0/thread:234:13
#16 0xe47f87 in std::thread::_Invoker<std::tuple<void (SocketPoll::*)(), SocketPoll*> >::operator()() /home/vmiklos/lode/opt_private/gcc-7.3.0/lib/gcc/x86_64-pc-linux-gnu/7.3.0/../../../../include/c++/7.3.0/thread:243:11
#17 0xe4734a in std::thread::_State_impl<std::thread::_Invoker<std::tuple<void (SocketPoll::*)(), SocketPoll*> > >::_M_run() /home/vmiklos/lode/opt_private/gcc-7.3.0/lib/gcc/x86_64-pc-linux-gnu/7.3.0/../../../../include/c++/7.3.0/thread:186:13
#18 0x7f5c2ce55e2e in execute_native_thread_routine /home/vmiklos/lode/packages/gccbuild/x86_64-pc-linux-gnu/libstdc++-v3/src/c++11/../../../../../gcc-7.3.0/libstdc++-v3/src/c++11/thread.cc:83
#19 0x7f5c2c832558 in start_thread (/lib64/libpthread.so.0+0x7558)
#20 0x7f5c2bf4682e in clone (/lib64/libc.so.6+0xf882e)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /home/vmiklos/lode/opt_private/gcc-7.3.0/lib/gcc/x86_64-pc-linux-gnu/7.3.0/../../../../include/c++/7.3.0/bits/unique_ptr.h:323:9 in
Change-Id: Ief574a11c838c77f7f159b1133beeef0deada201
2019-05-24 02:04:07 -05:00
|
|
|
if (!hasTileCache())
|
|
|
|
return;
|
|
|
|
|
2018-02-07 03:17:59 -06:00
|
|
|
const std::string canceltiles = tileCache().cancelTiles(session);
|
2016-09-19 21:16:45 -05:00
|
|
|
if (!canceltiles.empty())
|
|
|
|
{
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_DBG("Forwarding canceltiles request: " << canceltiles);
|
2016-10-11 17:22:51 -05:00
|
|
|
_childProcess->sendTextFrame(canceltiles);
|
2016-09-19 21:16:45 -05:00
|
|
|
}
|
2016-08-30 22:15:44 -05:00
|
|
|
}
|
|
|
|
|
2016-05-01 19:50:11 -05:00
|
|
|
void DocumentBroker::handleTileResponse(const std::vector<char>& payload)
|
|
|
|
{
|
|
|
|
const std::string firstLine = getFirstLine(payload);
|
2016-12-09 17:15:10 -06:00
|
|
|
LOG_DBG("Handling tile: " << firstLine);
|
2016-10-30 14:24:27 -05:00
|
|
|
|
2016-05-15 17:47:08 -05:00
|
|
|
try
|
2016-05-05 09:19:13 -05:00
|
|
|
{
|
2018-02-07 03:17:59 -06:00
|
|
|
const size_t length = payload.size();
|
2016-05-22 15:47:22 -05:00
|
|
|
if (firstLine.size() < static_cast<std::string::size_type>(length) - 1)
|
2016-05-15 17:47:08 -05:00
|
|
|
{
|
2018-02-07 03:17:59 -06:00
|
|
|
const TileDesc tile = TileDesc::parse(firstLine);
|
|
|
|
const char* buffer = payload.data();
|
|
|
|
const size_t offset = firstLine.size() + 1;
|
2017-01-25 19:55:39 -06:00
|
|
|
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
|
2016-10-29 20:15:00 -05:00
|
|
|
tileCache().saveTileAndNotify(tile, buffer + offset, length - offset);
|
2016-05-15 17:47:08 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-04-02 23:45:40 -05:00
|
|
|
LOG_WRN("Dropping empty tile response: " << firstLine);
|
2016-10-30 14:24:27 -05:00
|
|
|
// They will get re-issued if we don't forget them.
|
2016-05-15 17:47:08 -05:00
|
|
|
}
|
2016-05-05 01:37:40 -05:00
|
|
|
}
|
2016-05-15 17:47:08 -05:00
|
|
|
catch (const std::exception& exc)
|
2016-05-05 01:37:40 -05:00
|
|
|
{
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_ERR("Failed to process tile response [" << firstLine << "]: " << exc.what() << ".");
|
2016-05-05 01:37:40 -05:00
|
|
|
}
|
2016-05-01 19:50:11 -05:00
|
|
|
}
|
|
|
|
|
2016-05-22 10:45:28 -05:00
|
|
|
void DocumentBroker::handleTileCombinedResponse(const std::vector<char>& payload)
|
|
|
|
{
|
|
|
|
const std::string firstLine = getFirstLine(payload);
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_DBG("Handling tile combined: " << firstLine);
|
2016-05-22 10:45:28 -05:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2018-09-26 15:15:30 -05:00
|
|
|
const size_t length = payload.size();
|
|
|
|
if (firstLine.size() < static_cast<std::string::size_type>(length) - 1)
|
|
|
|
{
|
|
|
|
const TileCombined tileCombined = TileCombined::parse(firstLine);
|
|
|
|
const char* buffer = payload.data();
|
|
|
|
size_t offset = firstLine.size() + 1;
|
2017-01-25 19:55:39 -06:00
|
|
|
|
2018-09-26 15:15:30 -05:00
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
2017-01-25 19:55:39 -06:00
|
|
|
|
2018-09-26 15:15:30 -05:00
|
|
|
for (const auto& tile : tileCombined.getTiles())
|
|
|
|
{
|
|
|
|
tileCache().saveTileAndNotify(tile, buffer + offset, tile.getImgSize());
|
|
|
|
offset += tile.getImgSize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2016-05-22 10:45:28 -05:00
|
|
|
{
|
2019-01-06 09:44:40 -06:00
|
|
|
LOG_INF("Dropping empty tilecombine response: " << firstLine);
|
2018-09-26 15:15:30 -05:00
|
|
|
// They will get re-issued if we don't forget them.
|
2016-05-22 10:45:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const std::exception& exc)
|
|
|
|
{
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_ERR("Failed to process tile response [" << firstLine << "]: " << exc.what() << ".");
|
2016-05-22 10:45:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-14 16:17:00 -06:00
|
|
|
bool DocumentBroker::haveAnotherEditableSession(const std::string& id) const
|
2016-04-17 22:29:03 -05:00
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2016-04-17 22:29:03 -05:00
|
|
|
|
2018-01-14 16:17:00 -06:00
|
|
|
for (const auto& it : _sessions)
|
2017-03-07 20:17:24 -06:00
|
|
|
{
|
2018-01-14 16:17:00 -06:00
|
|
|
if (it.second->getId() != id &&
|
|
|
|
it.second->isViewLoaded() &&
|
2019-07-04 04:50:33 -05:00
|
|
|
!it.second->isReadOnly() &&
|
|
|
|
!it.second->inWaitDisconnected())
|
2016-07-10 23:50:25 -05:00
|
|
|
{
|
2018-01-14 16:17:00 -06:00
|
|
|
// This is a loaded session that is non-readonly.
|
|
|
|
return true;
|
2016-07-10 23:50:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-14 16:17:00 -06:00
|
|
|
// None found.
|
|
|
|
return false;
|
2016-04-17 22:29:03 -05:00
|
|
|
}
|
|
|
|
|
2016-04-22 06:00:11 -05:00
|
|
|
void DocumentBroker::setModified(const bool value)
|
|
|
|
{
|
2018-01-03 22:15:44 -06:00
|
|
|
if (_isModified != value)
|
2017-05-12 08:59:01 -05:00
|
|
|
{
|
|
|
|
_isModified = value;
|
2019-02-12 05:16:40 -06:00
|
|
|
#if !MOBILEAPP
|
2017-05-12 08:59:01 -05:00
|
|
|
Admin::instance().modificationAlert(_docKey, getPid(), value);
|
2018-09-05 07:11:05 -05:00
|
|
|
#endif
|
2017-05-12 08:59:01 -05:00
|
|
|
}
|
|
|
|
|
2018-01-03 23:16:40 -06:00
|
|
|
_storage->setUserModified(value);
|
2016-04-22 06:00:11 -05:00
|
|
|
}
|
|
|
|
|
2018-04-24 11:09:37 -05:00
|
|
|
bool DocumentBroker::isInitialSettingSet(const std::string& name) const
|
|
|
|
{
|
|
|
|
return _isInitialStateSet.find(name) != _isInitialStateSet.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentBroker::setInitialSetting(const std::string& name)
|
|
|
|
{
|
|
|
|
_isInitialStateSet.emplace(name);
|
|
|
|
}
|
|
|
|
|
2016-10-16 12:43:44 -05:00
|
|
|
bool DocumentBroker::forwardToChild(const std::string& viewId, const std::string& message)
|
2016-10-08 13:25:27 -05:00
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2017-04-03 23:11:06 -05:00
|
|
|
|
2017-08-11 12:40:34 -05:00
|
|
|
// Ignore userinactive, useractive message until document is loaded
|
|
|
|
if (!isLoaded() && (message == "userinactive" || message == "useractive"))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-28 16:57:34 -06:00
|
|
|
LOG_TRC("Forwarding payload to child [" << viewId << "]: " << getAbbreviatedMessage(message));
|
2016-10-08 13:25:27 -05:00
|
|
|
|
2019-05-16 14:38:36 -05:00
|
|
|
if (Log::traceEnabled() && Util::startsWith(message, "paste "))
|
|
|
|
LOG_TRC("Logging paste payload (" << message.size() << " bytes) '" << message << "' end paste");
|
|
|
|
|
2017-03-07 04:44:29 -06:00
|
|
|
std::string msg = "child-" + viewId + ' ' + message;
|
|
|
|
|
2016-10-08 13:25:27 -05:00
|
|
|
const auto it = _sessions.find(viewId);
|
|
|
|
if (it != _sessions.end())
|
|
|
|
{
|
2017-03-07 04:44:29 -06:00
|
|
|
assert(!_uriJailed.empty());
|
2017-03-20 19:42:09 -05:00
|
|
|
|
|
|
|
std::vector<std::string> tokens = LOOLProtocol::tokenize(msg);
|
|
|
|
if (tokens.size() > 1 && tokens[1] == "load")
|
|
|
|
{
|
|
|
|
// The json options must come last.
|
|
|
|
msg = tokens[0] + ' ' + tokens[1] + ' ' + tokens[2];
|
2018-06-10 10:41:17 -05:00
|
|
|
msg += " jail=" + _uriJailed;
|
|
|
|
msg += " xjail=" + _uriJailedAnonym;
|
|
|
|
msg += ' ' + Poco::cat(std::string(" "), tokens.begin() + 3, tokens.end());
|
2017-03-20 19:42:09 -05:00
|
|
|
}
|
2017-03-07 04:44:29 -06:00
|
|
|
|
2016-10-11 17:22:51 -05:00
|
|
|
_childProcess->sendTextFrame(msg);
|
2016-10-08 17:21:35 -05:00
|
|
|
return true;
|
2016-10-08 13:25:27 -05:00
|
|
|
}
|
2017-03-25 13:19:17 -05:00
|
|
|
|
|
|
|
// try the not yet created sessions
|
2018-11-28 16:57:34 -06:00
|
|
|
LOG_WRN("Child session [" << viewId << "] not found to forward message: " << getAbbreviatedMessage(message));
|
2016-10-08 13:25:27 -05:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-22 22:18:37 -06:00
|
|
|
bool DocumentBroker::forwardToClient(const std::shared_ptr<Message>& payload)
|
2016-10-08 12:13:23 -05:00
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2017-03-25 12:57:08 -05:00
|
|
|
|
2017-01-22 22:18:37 -06:00
|
|
|
const std::string& msg = payload->abbr();
|
|
|
|
const std::string& prefix = payload->forwardToken();
|
|
|
|
LOG_TRC("Forwarding payload to [" << prefix << "]: " << msg);
|
2016-10-08 12:13:23 -05:00
|
|
|
|
|
|
|
std::string name;
|
|
|
|
std::string sid;
|
2017-01-22 22:18:37 -06:00
|
|
|
if (LOOLProtocol::parseNameValuePair(payload->forwardToken(), name, sid, '-') && name == "client")
|
2016-10-08 12:13:23 -05:00
|
|
|
{
|
2017-02-06 19:10:08 -06:00
|
|
|
const auto& data = payload->data().data();
|
|
|
|
const auto& size = payload->size();
|
|
|
|
|
|
|
|
if (sid == "all")
|
2016-10-08 12:13:23 -05:00
|
|
|
{
|
2017-02-06 19:10:08 -06:00
|
|
|
// Broadcast to all.
|
2017-06-19 20:53:58 -05:00
|
|
|
// Events could cause the removal of sessions.
|
|
|
|
std::map<std::string, std::shared_ptr<ClientSession>> sessions(_sessions);
|
2019-07-04 04:50:33 -05:00
|
|
|
for (const auto& it : _sessions)
|
2017-02-06 19:10:08 -06:00
|
|
|
{
|
2019-07-04 04:50:33 -05:00
|
|
|
if (!it.second->inWaitDisconnected())
|
|
|
|
it.second->handleKitToClientMessage(data, size);
|
2017-02-06 19:10:08 -06:00
|
|
|
}
|
2016-10-08 12:13:23 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-06 19:10:08 -06:00
|
|
|
const auto it = _sessions.find(sid);
|
|
|
|
if (it != _sessions.end())
|
|
|
|
{
|
2017-06-19 20:53:58 -05:00
|
|
|
// Take a ref as the session could be removed from _sessions
|
|
|
|
// if it's the save confirmation keeping a stopped session alive.
|
|
|
|
std::shared_ptr<ClientSession> session = it->second;
|
|
|
|
return session->handleKitToClientMessage(data, size);
|
2017-02-06 19:10:08 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOG_WRN("Client session [" << sid << "] not found to forward message: " << msg);
|
|
|
|
}
|
2016-10-08 12:13:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-12-09 17:15:10 -06:00
|
|
|
LOG_ERR("Unexpected prefix of forward-to-client message: " << prefix);
|
2016-10-08 12:13:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-21 18:13:55 -05:00
|
|
|
void DocumentBroker::shutdownClients(const std::string& closeReason)
|
|
|
|
{
|
|
|
|
assertCorrectThread();
|
2017-06-01 07:56:54 -05:00
|
|
|
LOG_INF("Terminating " << _sessions.size() << " clients of doc [" << _docKey << "] with reason: " << closeReason);
|
2017-05-21 18:13:55 -05:00
|
|
|
|
|
|
|
// First copy into local container, since removeSession
|
|
|
|
// will erase from _sessions, but will leave the last.
|
|
|
|
std::map<std::string, std::shared_ptr<ClientSession>> sessions = _sessions;
|
|
|
|
for (const auto& pair : sessions)
|
|
|
|
{
|
|
|
|
std::shared_ptr<ClientSession> session = pair.second;
|
|
|
|
try
|
|
|
|
{
|
2019-07-04 04:50:33 -05:00
|
|
|
if (session->inWaitDisconnected())
|
|
|
|
finalRemoveSession(session->getId());
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Notify the client and disconnect.
|
|
|
|
session->shutdown(WebSocketHandler::StatusCodes::ENDPOINT_GOING_AWAY, closeReason);
|
2017-05-21 18:13:55 -05:00
|
|
|
|
2019-07-04 04:50:33 -05:00
|
|
|
// Remove session, save, and mark to destroy.
|
|
|
|
removeSession(session->getId());
|
|
|
|
}
|
2017-05-21 18:13:55 -05:00
|
|
|
}
|
|
|
|
catch (const std::exception& exc)
|
|
|
|
{
|
|
|
|
LOG_WRN("Error while shutting down client [" <<
|
|
|
|
session->getName() << "]: " << exc.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-15 16:07:40 -05:00
|
|
|
void DocumentBroker::childSocketTerminated()
|
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2016-11-01 18:38:25 -05:00
|
|
|
|
2016-10-15 16:07:40 -05:00
|
|
|
if (!_childProcess->isAlive())
|
|
|
|
{
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_ERR("Child for doc [" << _docKey << "] terminated prematurely.");
|
2016-10-15 16:07:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// We could restore the kit if this was unexpected.
|
|
|
|
// For now, close the connections to cleanup.
|
2017-05-21 18:13:55 -05:00
|
|
|
shutdownClients("terminated");
|
2016-10-15 16:07:40 -05:00
|
|
|
}
|
|
|
|
|
2017-06-21 05:07:38 -05:00
|
|
|
void DocumentBroker::terminateChild(const std::string& closeReason)
|
2016-11-05 22:00:16 -05:00
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2016-11-05 22:00:16 -05:00
|
|
|
|
2017-06-01 07:56:54 -05:00
|
|
|
LOG_INF("Terminating doc [" << _docKey << "] with reason: " << closeReason);
|
2016-11-05 22:00:16 -05:00
|
|
|
|
2018-01-14 19:49:11 -06:00
|
|
|
// Close all running sessions first.
|
2017-06-21 05:07:38 -05:00
|
|
|
shutdownClients(closeReason);
|
2016-11-08 07:37:28 -06:00
|
|
|
|
2017-03-07 20:18:59 -06:00
|
|
|
if (_childProcess)
|
|
|
|
{
|
|
|
|
LOG_INF("Terminating child [" << getPid() << "] of doc [" << _docKey << "].");
|
2016-11-05 22:00:16 -05:00
|
|
|
|
2018-05-18 06:00:16 -05:00
|
|
|
_childProcess->close();
|
2017-03-07 20:18:59 -06:00
|
|
|
}
|
2017-03-12 21:31:50 -05:00
|
|
|
|
2018-01-14 19:49:11 -06:00
|
|
|
stop(closeReason);
|
2016-11-05 22:00:16 -05:00
|
|
|
}
|
|
|
|
|
2016-11-08 07:37:28 -06:00
|
|
|
void DocumentBroker::closeDocument(const std::string& reason)
|
|
|
|
{
|
2017-04-05 07:48:49 -05:00
|
|
|
assertCorrectThread();
|
2016-11-08 07:37:28 -06:00
|
|
|
|
2017-03-09 21:38:25 -06:00
|
|
|
LOG_DBG("Closing DocumentBroker for docKey [" << _docKey << "] with reason: " << reason);
|
2018-02-01 22:28:20 -06:00
|
|
|
_closeReason = reason;
|
|
|
|
_closeRequest = true;
|
2016-11-08 07:37:28 -06:00
|
|
|
}
|
|
|
|
|
2017-06-01 11:11:11 -05:00
|
|
|
void DocumentBroker::broadcastMessage(const std::string& message)
|
|
|
|
{
|
|
|
|
assertCorrectThread();
|
|
|
|
|
|
|
|
LOG_DBG("Broadcasting message [" << message << "] to all sessions.");
|
|
|
|
for (const auto& sessionIt : _sessions)
|
|
|
|
{
|
|
|
|
sessionIt.second->sendTextFrame(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-05 14:11:07 -06:00
|
|
|
void DocumentBroker::updateLastActivityTime()
|
|
|
|
{
|
2017-01-01 18:42:19 -06:00
|
|
|
_lastActivityTime = std::chrono::steady_clock::now();
|
2019-02-12 05:16:40 -06:00
|
|
|
#if !MOBILEAPP
|
2016-12-05 14:11:07 -06:00
|
|
|
Admin::instance().updateLastActivityTime(_docKey);
|
2018-09-05 07:11:05 -05:00
|
|
|
#endif
|
2016-12-05 14:11:07 -06:00
|
|
|
}
|
|
|
|
|
2017-05-31 21:40:01 -05:00
|
|
|
void DocumentBroker::getIOStats(uint64_t &sent, uint64_t &recv)
|
|
|
|
{
|
|
|
|
sent = 0;
|
|
|
|
recv = 0;
|
|
|
|
assertCorrectThread();
|
|
|
|
for (const auto& sessionIt : _sessions)
|
|
|
|
{
|
|
|
|
uint64_t s, r;
|
|
|
|
sessionIt.second->getIOStats(s, r);
|
|
|
|
sent += s;
|
|
|
|
recv += r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-12 09:41:54 -05:00
|
|
|
static std::atomic<size_t> NumConverters;
|
|
|
|
|
|
|
|
size_t ConvertToBroker::getInstanceCount()
|
|
|
|
{
|
|
|
|
return NumConverters;
|
|
|
|
}
|
|
|
|
|
|
|
|
ConvertToBroker::ConvertToBroker(const std::string& uri,
|
|
|
|
const Poco::URI& uriPublic,
|
2019-12-11 23:09:35 -06:00
|
|
|
const std::string& docKey,
|
|
|
|
const std::string& format,
|
|
|
|
const std::string& sOptions) :
|
|
|
|
DocumentBroker(uri, uriPublic, docKey),
|
|
|
|
_format(format),
|
|
|
|
_sOptions(sOptions)
|
2019-03-12 09:41:54 -05:00
|
|
|
{
|
2019-12-11 21:38:50 -06:00
|
|
|
static const int limit_convert_secs = LOOLWSD::getConfigValue<int>("per_document.limit_convert_secs", 100);
|
2019-03-12 09:41:54 -05:00
|
|
|
NumConverters++;
|
2019-12-11 21:38:50 -06:00
|
|
|
_limitLifeSeconds = limit_convert_secs;
|
2019-03-12 09:41:54 -05:00
|
|
|
}
|
|
|
|
|
2019-12-12 02:56:43 -06:00
|
|
|
#if !MOBILEAPP
|
2019-12-11 23:09:35 -06:00
|
|
|
bool ConvertToBroker::startConversion(SocketDisposition &disposition, const std::string &id)
|
|
|
|
{
|
|
|
|
std::shared_ptr<ConvertToBroker> docBroker = std::static_pointer_cast<ConvertToBroker>(shared_from_this());
|
|
|
|
|
|
|
|
// Create a session to load the document.
|
|
|
|
const bool isReadOnly = true;
|
|
|
|
_clientSession = std::make_shared<ClientSession>(id, docBroker, getPublicUri(), isReadOnly, "nocliphost");
|
|
|
|
_clientSession->construct();
|
|
|
|
|
|
|
|
if (!_clientSession)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
disposition.setMove([docBroker] (const std::shared_ptr<Socket> &moveSocket)
|
|
|
|
{
|
|
|
|
// Perform all of this after removing the socket
|
|
|
|
|
|
|
|
// Make sure the thread is running before adding callback.
|
|
|
|
docBroker->startThread();
|
|
|
|
|
|
|
|
// We no longer own this socket.
|
|
|
|
moveSocket->setThreadOwner(std::thread::id(0));
|
|
|
|
|
|
|
|
docBroker->addCallback([docBroker, moveSocket]()
|
|
|
|
{
|
|
|
|
auto streamSocket = std::static_pointer_cast<StreamSocket>(moveSocket);
|
|
|
|
docBroker->_clientSession->setSaveAsSocket(streamSocket);
|
|
|
|
|
|
|
|
// Move the socket into DocBroker.
|
|
|
|
docBroker->addSocketToPoll(moveSocket);
|
|
|
|
|
|
|
|
// First add and load the session.
|
|
|
|
docBroker->addSession(docBroker->_clientSession);
|
|
|
|
|
|
|
|
// Load the document manually and request saving in the target format.
|
|
|
|
std::string encodedFrom;
|
|
|
|
Poco::URI::encode(docBroker->getPublicUri().getPath(), "", encodedFrom);
|
2019-12-13 09:03:26 -06:00
|
|
|
const std::string _load = "load url=" + encodedFrom;
|
|
|
|
std::vector<char> loadRequest(_load.begin(), _load.end());
|
2019-12-11 23:09:35 -06:00
|
|
|
docBroker->_clientSession->handleMessage(true, WSOpCode::Text, loadRequest);
|
|
|
|
|
|
|
|
// Save is done in the setLoaded
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
2019-12-12 02:56:43 -06:00
|
|
|
#endif
|
2019-12-11 23:09:35 -06:00
|
|
|
|
2019-10-04 04:30:49 -05:00
|
|
|
void ConvertToBroker::dispose()
|
|
|
|
{
|
|
|
|
if (!_uriOrig.empty())
|
|
|
|
{
|
|
|
|
NumConverters--;
|
|
|
|
removeFile(_uriOrig);
|
|
|
|
_uriOrig.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-01 15:25:44 -06:00
|
|
|
ConvertToBroker::~ConvertToBroker()
|
|
|
|
{
|
2019-10-04 04:30:49 -05:00
|
|
|
dispose();
|
2019-05-21 13:50:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConvertToBroker::removeFile(const std::string &uriOrig)
|
|
|
|
{
|
|
|
|
if (!uriOrig.empty())
|
2019-03-01 15:25:44 -06:00
|
|
|
{
|
2019-05-21 20:38:39 -05:00
|
|
|
try {
|
|
|
|
// Remove source file and directory
|
|
|
|
Poco::Path path = uriOrig;
|
|
|
|
Poco::File(path).remove();
|
|
|
|
Poco::File(path.makeParent()).remove();
|
|
|
|
} catch (const std::exception &ex) {
|
|
|
|
LOG_ERR("Error while removing conversion temporary: '" << uriOrig << "' - " << ex.what());
|
|
|
|
}
|
2019-03-01 15:25:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-11 23:09:35 -06:00
|
|
|
void ConvertToBroker::setLoaded()
|
|
|
|
{
|
|
|
|
DocumentBroker::setLoaded();
|
|
|
|
|
|
|
|
// FIXME: Check for security violations.
|
|
|
|
Poco::Path toPath(getPublicUri().getPath());
|
|
|
|
toPath.setExtension(_format);
|
|
|
|
const std::string toJailURL = "file://" + std::string(JAILED_DOCUMENT_ROOT) + toPath.getFileName();
|
|
|
|
std::string encodedTo;
|
|
|
|
Poco::URI::encode(toJailURL, "", encodedTo);
|
|
|
|
|
|
|
|
// Convert it to the requested format.
|
|
|
|
const std::string saveAsCmd = "saveas url=" + encodedTo + " format=" + _format + " options=" + _sOptions;
|
|
|
|
|
|
|
|
// Send the save request ...
|
|
|
|
std::vector<char> saveasRequest(saveAsCmd.begin(), saveAsCmd.end());
|
|
|
|
|
|
|
|
_clientSession->handleMessage(true, WSOpCode::Text, saveasRequest);
|
|
|
|
}
|
|
|
|
|
2019-06-21 05:47:07 -05:00
|
|
|
std::vector<std::shared_ptr<ClientSession>> DocumentBroker::getSessionsTestOnlyUnsafe()
|
|
|
|
{
|
|
|
|
std::vector<std::shared_ptr<ClientSession>> result;
|
|
|
|
for (auto& it : _sessions)
|
|
|
|
result.push_back(it.second);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-03-12 18:03:45 -05:00
|
|
|
void DocumentBroker::dumpState(std::ostream& os)
|
2017-03-06 09:45:34 -06:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
|
2017-05-31 21:40:01 -05:00
|
|
|
uint64_t sent, recv;
|
|
|
|
getIOStats(sent, recv);
|
|
|
|
|
2018-11-07 17:00:32 -06:00
|
|
|
auto now = std::chrono::steady_clock::now();
|
|
|
|
|
2018-07-10 22:09:27 -05:00
|
|
|
os << " Broker: " << LOOLWSD::anonymizeUrl(_filename) << " pid: " << getPid();
|
2017-03-10 09:37:09 -06:00
|
|
|
if (_markToDestroy)
|
2017-03-12 18:03:45 -05:00
|
|
|
os << " *** Marked to destroy ***";
|
2017-03-10 09:37:09 -06:00
|
|
|
else
|
2017-03-12 18:03:45 -05:00
|
|
|
os << " has live sessions";
|
2017-03-11 16:01:27 -06:00
|
|
|
if (_isLoaded)
|
2017-03-12 18:03:45 -05:00
|
|
|
os << "\n loaded in: " << _loadDuration.count() << "ms";
|
2017-03-11 16:01:27 -06:00
|
|
|
else
|
2018-11-07 17:00:32 -06:00
|
|
|
os << "\n still loading... " <<
|
|
|
|
std::chrono::duration_cast<std::chrono::seconds>(
|
|
|
|
now - _threadStart).count() << "s";
|
2017-05-31 21:40:01 -05:00
|
|
|
os << "\n sent: " << sent;
|
2017-06-03 16:53:57 -05:00
|
|
|
os << "\n recv: " << recv;
|
2017-03-12 18:03:45 -05:00
|
|
|
os << "\n modified?: " << _isModified;
|
|
|
|
os << "\n jail id: " << _jailId;
|
2018-07-10 22:09:27 -05:00
|
|
|
os << "\n filename: " << LOOLWSD::anonymizeUrl(_filename);
|
2017-03-12 18:03:45 -05:00
|
|
|
os << "\n public uri: " << _uriPublic.toString();
|
2018-07-10 22:09:27 -05:00
|
|
|
os << "\n jailed uri: " << LOOLWSD::anonymizeUrl(_uriJailed);
|
2017-03-12 18:03:45 -05:00
|
|
|
os << "\n doc key: " << _docKey;
|
2017-04-03 23:11:06 -05:00
|
|
|
os << "\n doc id: " << _docId;
|
2017-04-06 12:49:22 -05:00
|
|
|
os << "\n num sessions: " << _sessions.size();
|
2019-12-11 21:38:50 -06:00
|
|
|
os << "\n thread start: " << Util::getSteadyClockAsString(_threadStart);
|
2019-11-27 03:12:59 -06:00
|
|
|
os << "\n last saved: " << Util::getSteadyClockAsString(_lastSaveTime);
|
|
|
|
os << "\n last save request: " << Util::getSteadyClockAsString(_lastSaveRequestTime);
|
|
|
|
os << "\n last save response: " << Util::getSteadyClockAsString(_lastSaveResponseTime);
|
|
|
|
os << "\n last modifed: " << Util::getHttpTime(_documentLastModifiedTime);
|
|
|
|
os << "\n file last modifed: " << Util::getHttpTime(_lastFileModifiedTime);
|
2019-12-11 21:38:50 -06:00
|
|
|
if (_limitLifeSeconds)
|
|
|
|
os << "\n life limit in seconds: " << _limitLifeSeconds;
|
2019-11-27 03:12:59 -06:00
|
|
|
os << "\n idle time: " << getIdleTimeSecs();
|
2017-03-12 18:03:45 -05:00
|
|
|
os << "\n cursor " << _cursorPosX << ", " << _cursorPosY
|
|
|
|
<< "( " << _cursorWidth << "," << _cursorHeight << ")\n";
|
2019-11-27 02:43:05 -06:00
|
|
|
_lockCtx->dumpState(os);
|
2019-11-05 18:33:37 -06:00
|
|
|
if (_tileCache)
|
|
|
|
_tileCache->dumpState(os);
|
2017-03-12 18:03:45 -05:00
|
|
|
|
|
|
|
_poll->dumpState(os);
|
2017-03-06 09:45:34 -06:00
|
|
|
}
|
|
|
|
|
2016-03-23 07:41:18 -05:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|