2016-03-31 01:48:34 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* 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/.
|
|
|
|
*/
|
|
|
|
|
2020-12-06 15:55:22 -06:00
|
|
|
#include <chrono>
|
2017-12-20 07:06:26 -06:00
|
|
|
#include <config.h>
|
2016-04-08 06:49:33 -05:00
|
|
|
|
2021-01-10 11:20:00 -06:00
|
|
|
#include "HttpRequest.hpp"
|
2017-03-08 10:38:22 -06:00
|
|
|
#include "Storage.hpp"
|
|
|
|
|
2016-11-11 08:32:29 -06:00
|
|
|
#include <algorithm>
|
2020-06-11 09:54:27 -05:00
|
|
|
#include <memory>
|
2016-03-31 01:48:34 -05:00
|
|
|
#include <cassert>
|
2018-06-10 10:42:15 -05:00
|
|
|
#include <errno.h>
|
2016-03-31 01:48:34 -05:00
|
|
|
#include <fstream>
|
2017-10-24 04:31:39 -05:00
|
|
|
#include <iconv.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
|
|
|
#include <string>
|
2016-03-31 01:48:34 -05:00
|
|
|
|
2016-12-16 07:19:27 -06:00
|
|
|
#include <Poco/Exception.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
|
|
|
#include <Poco/JSON/Object.h>
|
|
|
|
#include <Poco/JSON/Parser.h>
|
2018-09-05 07:11:05 -05:00
|
|
|
|
2019-02-12 05:16:40 -06:00
|
|
|
#if !MOBILEAPP
|
2018-09-05 07:11:05 -05:00
|
|
|
|
|
|
|
#include <Poco/Net/AcceptCertificateHandler.h>
|
|
|
|
#include <Poco/Net/Context.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
|
|
|
#include <Poco/Net/DNS.h>
|
2016-04-08 04:24:52 -05:00
|
|
|
#include <Poco/Net/HTTPClientSession.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
|
|
|
#include <Poco/Net/HTTPRequest.h>
|
|
|
|
#include <Poco/Net/HTTPResponse.h>
|
2016-03-31 01:48:34 -05:00
|
|
|
#include <Poco/Net/HTTPSClientSession.h>
|
2018-09-05 07:11:05 -05:00
|
|
|
#include <Poco/Net/KeyConsoleHandler.h>
|
2017-05-17 03:48:51 -05:00
|
|
|
#include <Poco/Net/NameValueCollection.h>
|
2016-03-31 01:48:34 -05:00
|
|
|
#include <Poco/Net/SSLManager.h>
|
2018-09-05 07:11:05 -05:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2016-03-31 01:48:34 -05:00
|
|
|
#include <Poco/StreamCopier.h>
|
2018-07-17 01:01:05 -05:00
|
|
|
#include <Poco/URI.h>
|
2016-03-31 01:48:34 -05:00
|
|
|
|
|
|
|
#include "Auth.hpp"
|
2017-12-20 07:06:26 -06:00
|
|
|
#include <Common.hpp>
|
2016-04-16 11:55:56 -05:00
|
|
|
#include "Exceptions.hpp"
|
2017-12-20 07:06:26 -06:00
|
|
|
#include <Log.hpp>
|
|
|
|
#include <Unit.hpp>
|
|
|
|
#include <Util.hpp>
|
2019-11-07 09:24:37 -06:00
|
|
|
#include "ProofKey.hpp"
|
2017-12-20 07:06:26 -06:00
|
|
|
#include <common/FileUtil.hpp>
|
2018-02-17 16:32:41 -06:00
|
|
|
#include <common/JsonUtil.hpp>
|
2021-06-01 05:21:09 -05:00
|
|
|
#include <common/TraceEvent.hpp>
|
2021-07-06 22:10:01 -05:00
|
|
|
#include <NetUtil.hpp>
|
2021-09-13 14:24:50 -05:00
|
|
|
#include <CommandControl.hpp>
|
2022-03-24 05:23:34 -05:00
|
|
|
#include "HostUtil.hpp"
|
2016-03-31 01:48:34 -05:00
|
|
|
|
2021-04-07 07:56:04 -05:00
|
|
|
#ifdef IOS
|
|
|
|
#include <ios.h>
|
2021-05-31 01:54:34 -05:00
|
|
|
#elif defined(__ANDROID__)
|
|
|
|
#include "androidapp.hpp"
|
2022-11-24 05:33:20 -06:00
|
|
|
#elif defined(GTKAPP)
|
|
|
|
#include "gtk.hpp"
|
2022-11-11 07:29:07 -06:00
|
|
|
#elif defined(__EMSCRIPTEN__)
|
|
|
|
#include "wasmapp.hpp"
|
2021-04-07 07:56:04 -05:00
|
|
|
#endif
|
|
|
|
|
2016-06-07 02:18:49 -05:00
|
|
|
bool StorageBase::FilesystemEnabled;
|
2020-04-29 14:24:33 -05:00
|
|
|
bool StorageBase::SSLAsScheme = true;
|
|
|
|
bool StorageBase::SSLEnabled = false;
|
2016-04-16 07:13:59 -05:00
|
|
|
|
2019-02-12 05:16:40 -06:00
|
|
|
#if !MOBILEAPP
|
2018-09-13 11:16:00 -05:00
|
|
|
|
2016-03-31 01:48:34 -05:00
|
|
|
std::string StorageBase::getLocalRootPath() const
|
|
|
|
{
|
2018-02-07 03:17:59 -06:00
|
|
|
std::string localPath = _jailPath;
|
2016-03-31 01:48:34 -05:00
|
|
|
if (localPath[0] == '/')
|
|
|
|
{
|
|
|
|
// Remove the leading /
|
|
|
|
localPath.erase(0, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// /chroot/jailId/user/doc/childId
|
2018-02-07 03:17:59 -06:00
|
|
|
const Poco::Path rootPath = Poco::Path(_localStorePath, localPath);
|
2016-03-31 01:48:34 -05:00
|
|
|
Poco::File(rootPath).createDirectories();
|
|
|
|
|
|
|
|
return rootPath.toString();
|
|
|
|
}
|
2022-02-07 23:50:09 -06:00
|
|
|
#endif
|
|
|
|
|
|
|
|
void StorageBase::initialize()
|
|
|
|
{
|
|
|
|
#if !MOBILEAPP
|
|
|
|
const auto& app = Poco::Util::Application::instance();
|
|
|
|
FilesystemEnabled = app.config().getBool("storage.filesystem[@allow]", false);
|
|
|
|
|
2022-06-20 05:20:32 -05:00
|
|
|
//parse wopi.storage.host only when there is no storage.wopi.alias_groups entry in config
|
|
|
|
if (!app.config().has("storage.wopi.alias_groups"))
|
|
|
|
{
|
|
|
|
HostUtil::parseWopiHost(app.config());
|
|
|
|
}
|
2022-03-07 07:37:14 -06:00
|
|
|
|
2022-08-02 21:04:49 -05:00
|
|
|
#if ENABLE_FEATURE_LOCK
|
2022-02-07 23:50:09 -06:00
|
|
|
CommandControl::LockManager::parseLockedHost(app.config());
|
|
|
|
#endif
|
2017-04-27 12:47:35 -05:00
|
|
|
|
2022-03-24 05:23:34 -05:00
|
|
|
HostUtil::parseAliases(app.config());
|
|
|
|
|
2017-04-27 12:47:35 -05:00
|
|
|
#if ENABLE_SSL
|
|
|
|
// FIXME: should use our own SSL socket implementation here.
|
|
|
|
Poco::Crypto::initializeCrypto();
|
|
|
|
Poco::Net::initializeSSL();
|
|
|
|
|
|
|
|
// Init client
|
|
|
|
Poco::Net::Context::Params sslClientParams;
|
|
|
|
|
2020-04-29 14:24:33 -05:00
|
|
|
// false default for upgrade to preserve legacy configuration
|
|
|
|
// in-config-file defaults are true.
|
2021-11-18 06:08:14 -06:00
|
|
|
SSLAsScheme = COOLWSD::getConfigValue<bool>("storage.ssl.as_scheme", false);
|
2020-04-29 14:24:33 -05:00
|
|
|
|
2019-10-24 11:00:07 -05:00
|
|
|
// Fallback to ssl.enable if not set - for back compatibility & simplicity.
|
2021-11-18 06:08:14 -06:00
|
|
|
SSLEnabled = COOLWSD::getConfigValue<bool>(
|
|
|
|
"storage.ssl.enable", COOLWSD::getConfigValue<bool>("ssl.enable", true));
|
2019-10-24 11:00:07 -05:00
|
|
|
|
2019-10-07 06:51:30 -05:00
|
|
|
#if ENABLE_DEBUG
|
|
|
|
char *StorageSSLEnabled = getenv("STORAGE_SSL_ENABLE");
|
|
|
|
if (StorageSSLEnabled != NULL)
|
|
|
|
{
|
|
|
|
if (!strcasecmp(StorageSSLEnabled, "true"))
|
|
|
|
SSLEnabled = true;
|
|
|
|
else if (!strcasecmp(StorageSSLEnabled, "false"))
|
|
|
|
SSLEnabled = false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (SSLEnabled)
|
|
|
|
{
|
2021-11-18 06:08:14 -06:00
|
|
|
sslClientParams.certificateFile = COOLWSD::getPathFromConfigWithFallback("storage.ssl.cert_file_path", "ssl.cert_file_path");
|
|
|
|
sslClientParams.privateKeyFile = COOLWSD::getPathFromConfigWithFallback("storage.ssl.key_file_path", "ssl.key_file_path");
|
|
|
|
sslClientParams.caLocation = COOLWSD::getPathFromConfigWithFallback("storage.ssl.ca_file_path", "ssl.ca_file_path");
|
|
|
|
sslClientParams.cipherList = COOLWSD::getPathFromConfigWithFallback("storage.ssl.cipher_list", "ssl.cipher_list");
|
2019-10-07 06:51:30 -05:00
|
|
|
|
|
|
|
sslClientParams.verificationMode = (sslClientParams.caLocation.empty() ? Poco::Net::Context::VERIFY_NONE : Poco::Net::Context::VERIFY_STRICT);
|
2019-10-18 12:21:09 -05:00
|
|
|
sslClientParams.loadDefaultCAs = true;
|
2019-10-07 06:51:30 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
sslClientParams.verificationMode = Poco::Net::Context::VERIFY_NONE;
|
2017-04-27 12:47:35 -05:00
|
|
|
|
|
|
|
Poco::SharedPtr<Poco::Net::PrivateKeyPassphraseHandler> consoleClientHandler = new Poco::Net::KeyConsoleHandler(false);
|
|
|
|
Poco::SharedPtr<Poco::Net::InvalidCertificateHandler> invalidClientCertHandler = new Poco::Net::AcceptCertificateHandler(false);
|
|
|
|
|
|
|
|
Poco::Net::Context::Ptr sslClientContext = new Poco::Net::Context(Poco::Net::Context::CLIENT_USE, sslClientParams);
|
2019-10-18 12:21:09 -05:00
|
|
|
sslClientContext->disableProtocols(Poco::Net::Context::Protocols::PROTO_SSLV2 |
|
|
|
|
Poco::Net::Context::Protocols::PROTO_SSLV3 |
|
|
|
|
Poco::Net::Context::Protocols::PROTO_TLSV1);
|
2017-04-27 12:47:35 -05:00
|
|
|
Poco::Net::SSLManager::instance().initializeClient(consoleClientHandler, invalidClientCertHandler, sslClientContext);
|
2021-06-23 20:30:28 -05:00
|
|
|
|
|
|
|
// Initialize our client SSL context.
|
2021-06-27 14:22:17 -05:00
|
|
|
ssl::Manager::initializeClientContext(
|
|
|
|
sslClientParams.certificateFile, sslClientParams.privateKeyFile, sslClientParams.caLocation,
|
|
|
|
sslClientParams.cipherList,
|
|
|
|
sslClientParams.caLocation.empty() ? ssl::CertificateVerification::Disabled
|
|
|
|
: ssl::CertificateVerification::Required);
|
2021-06-23 20:30:28 -05:00
|
|
|
if (!ssl::Manager::isClientContextInitialized())
|
|
|
|
LOG_ERR("Failed to initialize Client SSL.");
|
|
|
|
else
|
|
|
|
LOG_INF("Initialized Client SSL.");
|
2017-04-27 12:47:35 -05:00
|
|
|
#endif
|
2018-09-13 11:16:00 -05:00
|
|
|
#else
|
|
|
|
FilesystemEnabled = true;
|
|
|
|
#endif
|
2016-04-16 07:13:59 -05:00
|
|
|
}
|
|
|
|
|
2019-02-12 05:16:40 -06:00
|
|
|
#if !MOBILEAPP
|
2018-09-05 07:11:05 -05:00
|
|
|
|
2017-05-19 03:32:24 -05:00
|
|
|
bool isLocalhost(const std::string& targetHost)
|
2016-04-19 03:17:50 -05:00
|
|
|
{
|
2021-07-06 22:10:01 -05:00
|
|
|
const std::string targetAddress = net::resolveHostAddress(targetHost);
|
2016-04-19 08:32:22 -05:00
|
|
|
|
2021-07-06 22:10:01 -05:00
|
|
|
if (net::isLocalhost(targetHost))
|
2016-04-19 03:17:50 -05:00
|
|
|
{
|
2021-07-06 22:10:01 -05:00
|
|
|
LOG_INF("WOPI host [" << targetHost << "] is on the same host as the WOPI client: \""
|
|
|
|
<< targetAddress << "\". Connection is allowed.");
|
|
|
|
return true;
|
2016-04-19 03:17:50 -05:00
|
|
|
}
|
2016-11-03 19:26:40 -05:00
|
|
|
|
2021-07-06 22:10:01 -05:00
|
|
|
LOG_INF("WOPI host [" << targetHost << "] is not on the same host as the WOPI client: \""
|
|
|
|
<< targetAddress << "\". Connection is not allowed.");
|
2016-04-19 03:17:50 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-09-05 07:11:05 -05:00
|
|
|
#endif
|
|
|
|
|
2022-03-08 16:56:18 -06:00
|
|
|
bool isTemplate(const std::string& filename)
|
|
|
|
{
|
|
|
|
std::vector<std::string> templateExtensions {".stw", ".ott", ".dot", ".dotx", ".dotm", ".otm", ".stc", ".ots", ".xltx", ".xltm", ".sti", ".otp", ".potx", ".potm", ".std", ".otg"};
|
|
|
|
for (auto & extension : templateExtensions)
|
|
|
|
if (Util::endsWith(filename, extension))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-11-22 16:36:44 -06:00
|
|
|
std::unique_ptr<StorageBase> StorageBase::create(const Poco::URI& uri, const std::string& jailRoot,
|
|
|
|
const std::string& jailPath, bool takeOwnership)
|
2016-04-07 15:59:27 -05:00
|
|
|
{
|
Attempt to handle unauthorized WOPI usage better
Use the previously unused UnauthorizedRequestException for this, and
throw a such in StorageBase::create() when the WOPI host doesn't match
any of those configured.
In a developer debug build, without access to any real WOPI
functionality, you can test by setting the FAKE_UNAUTHORIZED
environment variable and attempting to edit a plain local file:
URI. That will cause such an exception to be thrown in that function.
Catch that UnauthorizedRequestException in
ClientRequestHandler::handleGetRequest(), and send an 'error:
cmd=internal kind=unauthorized' message to the client. Handle that in
loleaflet in the same place where the 'error: cmd=internal
kild=diskfull' message is handled, and in the same fashion, giving up
on the document.
Actually, using exceptions for relatively non-exceptional situations
like this is lame and makes understanding the code harder, but that is
just my personal preference...
FIXME: By the time StorageBase::create() gets called we have already
sent three 'statusindicator:' messages ('find', 'connect', and
'ready') to the client. We should ideally do the checks we do in
StorageBase::create() much earlier.
Also consider that ClientRequestHandler::handleClientRequest() has
code that catches UnauthorizedRequestException and
BadRequestException, and tries to set the HTTP response in those
cases. I am not sure if that functionality has ever been exercised,
though. Currently, we upgrade the HTTP connection to WebSocket early,
and only after that we check whether the WOPI host is authorized
etc. By that time it is too late to return an HTTP response to the
user. If that even is what we ideally should do? If not, then we
probably should drop the code that constructs HTTP responses and
attempts to send them.
Also, if I, as a test, force an HTTPResponse::HTTP_BAD_REQUEST to be
sent before the HTTP connection is upgraded to WebSocket, loleaflet
throws up the generic "Well, this is embarrassing" dialog anyway. At
least in Firefox on Linux. (Instead of the browser showing some own
dialog, which I was half-expecting to happen.)
2016-10-17 08:55:20 -05:00
|
|
|
// FIXME: By the time this gets called we have already sent to the client three
|
|
|
|
// 'statusindicator:' messages: 'find', 'connect' and 'ready'. We should ideally do the checks
|
|
|
|
// here much earlier. Also, using exceptions is lame and makes understanding the code harder,
|
|
|
|
// but that is just my personal preference.
|
|
|
|
|
2016-04-07 15:59:27 -05:00
|
|
|
std::unique_ptr<StorageBase> storage;
|
|
|
|
|
2016-10-14 05:09:43 -05:00
|
|
|
if (UnitWSD::get().createStorage(uri, jailRoot, jailPath, storage))
|
2016-04-16 07:13:59 -05:00
|
|
|
{
|
2023-03-05 07:15:29 -06:00
|
|
|
LOG_INF("Storage create hooked");
|
2016-08-31 10:05:10 -05:00
|
|
|
if (storage)
|
2016-11-03 19:26:40 -05:00
|
|
|
{
|
2016-08-31 10:05:10 -05:00
|
|
|
return storage;
|
2016-11-03 19:26:40 -05:00
|
|
|
}
|
2016-04-16 07:13:59 -05:00
|
|
|
}
|
2016-04-07 15:59:27 -05:00
|
|
|
else if (uri.isRelative() || uri.getScheme() == "file")
|
|
|
|
{
|
2023-03-05 07:15:29 -06:00
|
|
|
LOG_INF("Public URI [" << COOLWSD::anonymizeUrl(uri.toString()) << "] is a file");
|
2016-11-03 19:26:40 -05:00
|
|
|
|
Attempt to handle unauthorized WOPI usage better
Use the previously unused UnauthorizedRequestException for this, and
throw a such in StorageBase::create() when the WOPI host doesn't match
any of those configured.
In a developer debug build, without access to any real WOPI
functionality, you can test by setting the FAKE_UNAUTHORIZED
environment variable and attempting to edit a plain local file:
URI. That will cause such an exception to be thrown in that function.
Catch that UnauthorizedRequestException in
ClientRequestHandler::handleGetRequest(), and send an 'error:
cmd=internal kind=unauthorized' message to the client. Handle that in
loleaflet in the same place where the 'error: cmd=internal
kild=diskfull' message is handled, and in the same fashion, giving up
on the document.
Actually, using exceptions for relatively non-exceptional situations
like this is lame and makes understanding the code harder, but that is
just my personal preference...
FIXME: By the time StorageBase::create() gets called we have already
sent three 'statusindicator:' messages ('find', 'connect', and
'ready') to the client. We should ideally do the checks we do in
StorageBase::create() much earlier.
Also consider that ClientRequestHandler::handleClientRequest() has
code that catches UnauthorizedRequestException and
BadRequestException, and tries to set the HTTP response in those
cases. I am not sure if that functionality has ever been exercised,
though. Currently, we upgrade the HTTP connection to WebSocket early,
and only after that we check whether the WOPI host is authorized
etc. By that time it is too late to return an HTTP response to the
user. If that even is what we ideally should do? If not, then we
probably should drop the code that constructs HTTP responses and
attempts to send them.
Also, if I, as a test, force an HTTPResponse::HTTP_BAD_REQUEST to be
sent before the HTTP connection is upgraded to WebSocket, loleaflet
throws up the generic "Well, this is embarrassing" dialog anyway. At
least in Firefox on Linux. (Instead of the browser showing some own
dialog, which I was half-expecting to happen.)
2016-10-17 08:55:20 -05:00
|
|
|
#if ENABLE_DEBUG
|
|
|
|
if (std::getenv("FAKE_UNAUTHORIZED"))
|
|
|
|
{
|
2016-11-03 19:26:40 -05:00
|
|
|
LOG_FTL("Faking an UnauthorizedRequestException");
|
2023-03-05 07:15:29 -06:00
|
|
|
throw UnauthorizedRequestException(
|
|
|
|
"No acceptable WOPI hosts found matching the target host in config");
|
Attempt to handle unauthorized WOPI usage better
Use the previously unused UnauthorizedRequestException for this, and
throw a such in StorageBase::create() when the WOPI host doesn't match
any of those configured.
In a developer debug build, without access to any real WOPI
functionality, you can test by setting the FAKE_UNAUTHORIZED
environment variable and attempting to edit a plain local file:
URI. That will cause such an exception to be thrown in that function.
Catch that UnauthorizedRequestException in
ClientRequestHandler::handleGetRequest(), and send an 'error:
cmd=internal kind=unauthorized' message to the client. Handle that in
loleaflet in the same place where the 'error: cmd=internal
kild=diskfull' message is handled, and in the same fashion, giving up
on the document.
Actually, using exceptions for relatively non-exceptional situations
like this is lame and makes understanding the code harder, but that is
just my personal preference...
FIXME: By the time StorageBase::create() gets called we have already
sent three 'statusindicator:' messages ('find', 'connect', and
'ready') to the client. We should ideally do the checks we do in
StorageBase::create() much earlier.
Also consider that ClientRequestHandler::handleClientRequest() has
code that catches UnauthorizedRequestException and
BadRequestException, and tries to set the HTTP response in those
cases. I am not sure if that functionality has ever been exercised,
though. Currently, we upgrade the HTTP connection to WebSocket early,
and only after that we check whether the WOPI host is authorized
etc. By that time it is too late to return an HTTP response to the
user. If that even is what we ideally should do? If not, then we
probably should drop the code that constructs HTTP responses and
attempts to send them.
Also, if I, as a test, force an HTTPResponse::HTTP_BAD_REQUEST to be
sent before the HTTP connection is upgraded to WebSocket, loleaflet
throws up the generic "Well, this is embarrassing" dialog anyway. At
least in Firefox on Linux. (Instead of the browser showing some own
dialog, which I was half-expecting to happen.)
2016-10-17 08:55:20 -05:00
|
|
|
}
|
|
|
|
#endif
|
2020-11-22 16:36:44 -06:00
|
|
|
if (FilesystemEnabled || takeOwnership)
|
2016-04-07 15:59:27 -05:00
|
|
|
{
|
2020-11-22 16:36:44 -06:00
|
|
|
return std::unique_ptr<StorageBase>(
|
|
|
|
new LocalStorage(uri, jailRoot, jailPath, takeOwnership));
|
2017-08-02 11:55:43 -05:00
|
|
|
}
|
2016-04-07 15:59:27 -05:00
|
|
|
|
2023-03-05 07:15:29 -06:00
|
|
|
LOG_ERR("Local Storage is disabled by default. Enable in the config file or on the "
|
|
|
|
"command-line to enable");
|
2016-04-07 15:59:27 -05:00
|
|
|
}
|
2019-02-12 05:16:40 -06:00
|
|
|
#if !MOBILEAPP
|
2022-03-24 05:23:34 -05:00
|
|
|
else if (HostUtil::isWopiEnabled())
|
2016-04-07 15:59:27 -05:00
|
|
|
{
|
2023-03-05 07:15:29 -06:00
|
|
|
LOG_INF("Public URI [" << COOLWSD::anonymizeUrl(uri.toString()) << "] considered WOPI");
|
2016-04-26 02:34:32 -05:00
|
|
|
const auto& targetHost = uri.getHost();
|
2020-10-14 13:52:49 -05:00
|
|
|
bool allowed(false);
|
2022-03-31 05:58:44 -05:00
|
|
|
HostUtil::setFirstHost(uri);
|
2022-04-07 01:51:46 -05:00
|
|
|
if (HostUtil::allowedWopiHost(targetHost) ||
|
2022-03-17 03:38:33 -05:00
|
|
|
isLocalhost(targetHost))
|
2016-04-16 11:55:56 -05:00
|
|
|
{
|
2020-10-14 13:52:49 -05:00
|
|
|
allowed = true;
|
|
|
|
}
|
|
|
|
if (!allowed)
|
|
|
|
{
|
|
|
|
// check if the IP address is in the list of allowed hosts
|
|
|
|
const auto hostAddresses(Poco::Net::DNS::resolve(targetHost));
|
|
|
|
for (auto &address : hostAddresses.addresses())
|
|
|
|
{
|
2022-04-07 01:51:46 -05:00
|
|
|
if (HostUtil::allowedWopiHost(address.toString()))
|
2020-10-14 13:52:49 -05:00
|
|
|
{
|
|
|
|
allowed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-04-16 11:55:56 -05:00
|
|
|
}
|
2020-10-14 13:52:49 -05:00
|
|
|
if (allowed)
|
|
|
|
return std::unique_ptr<StorageBase>(new WopiStorage(uri, jailRoot, jailPath));
|
2023-03-05 07:15:29 -06:00
|
|
|
|
|
|
|
LOG_ERR("No acceptable WOPI hosts found matching the target host [" << targetHost
|
|
|
|
<< "] in config");
|
|
|
|
throw UnauthorizedRequestException(
|
|
|
|
"No acceptable WOPI hosts found matching the target host [" + targetHost +
|
|
|
|
"] in config");
|
2016-04-16 07:13:59 -05:00
|
|
|
}
|
2018-09-05 07:11:05 -05:00
|
|
|
#endif
|
2023-03-05 07:15:29 -06:00
|
|
|
|
|
|
|
throw BadRequestException("No Storage configured or invalid URI " +
|
|
|
|
COOLWSD::anonymizeUrl(uri.toString()) + ']');
|
2016-04-07 15:59:27 -05:00
|
|
|
}
|
|
|
|
|
2016-10-26 07:47:42 -05:00
|
|
|
std::atomic<unsigned> LocalStorage::LastLocalStorageId;
|
2016-09-30 04:02:09 -05:00
|
|
|
|
2017-05-12 10:42:03 -05:00
|
|
|
std::unique_ptr<LocalStorage::LocalFileInfo> LocalStorage::getLocalFileInfo()
|
2016-03-31 01:48:34 -05:00
|
|
|
{
|
2020-11-15 08:25:24 -06:00
|
|
|
const Poco::Path path = getUri().getPath();
|
2021-11-18 06:08:14 -06:00
|
|
|
LOG_DBG("Getting info for local uri [" << COOLWSD::anonymizeUrl(getUri().toString()) << "], path [" << COOLWSD::anonymizeUrl(path.toString()) << "].");
|
2016-10-26 06:15:28 -05:00
|
|
|
|
2020-11-15 08:25:24 -06:00
|
|
|
const FileUtil::Stat stat(path.toString());
|
|
|
|
const std::chrono::system_clock::time_point lastModified = stat.modifiedTimepoint();
|
2016-10-26 06:15:28 -05:00
|
|
|
|
2022-02-06 20:15:09 -06:00
|
|
|
setFileInfo(FileInfo(path.getFileName(), "LocalOwner",
|
|
|
|
Util::getIso8601FracformatTime(lastModified)));
|
2016-10-26 06:15:28 -05:00
|
|
|
|
2020-11-15 08:25:24 -06:00
|
|
|
// Set automatic userid and username.
|
|
|
|
const std::string userId = std::to_string(LastLocalStorageId++);
|
2021-04-07 07:56:04 -05:00
|
|
|
std::string userNameString;
|
|
|
|
|
2021-05-31 01:54:34 -05:00
|
|
|
#if MOBILEAPP
|
2021-04-07 07:56:04 -05:00
|
|
|
if (user_name != nullptr)
|
|
|
|
userNameString = std::string(user_name);
|
|
|
|
#endif
|
2023-03-04 14:53:45 -06:00
|
|
|
if (userNameString.empty())
|
2021-04-07 07:56:04 -05:00
|
|
|
userNameString = "LocalUser#" + userId;
|
|
|
|
|
2020-11-15 08:25:24 -06:00
|
|
|
return std::unique_ptr<LocalStorage::LocalFileInfo>(
|
2021-04-07 07:56:04 -05:00
|
|
|
new LocalFileInfo({"LocalUser" + userId, userNameString}));
|
2016-03-31 01:48:34 -05:00
|
|
|
}
|
|
|
|
|
2020-11-29 18:11:59 -06:00
|
|
|
std::string LocalStorage::downloadStorageFileToLocal(const Authorization& /*auth*/,
|
|
|
|
LockContext& /*lockCtx*/,
|
|
|
|
const std::string& /*templateUri*/)
|
2016-03-31 01:48:34 -05:00
|
|
|
{
|
2019-02-12 05:16:40 -06:00
|
|
|
#if !MOBILEAPP
|
2016-03-31 01:48:34 -05:00
|
|
|
// /chroot/jailId/user/doc/childId/file.ext
|
2018-11-28 02:07:29 -06:00
|
|
|
const std::string filename = Poco::Path(getUri().getPath()).getFileName();
|
|
|
|
setRootFilePath(Poco::Path(getLocalRootPath(), filename).toString());
|
2021-11-18 06:08:14 -06:00
|
|
|
setRootFilePathAnonym(COOLWSD::anonymizeUrl(getRootFilePath()));
|
|
|
|
LOG_INF("Public URI [" << COOLWSD::anonymizeUrl(getUri().getPath()) <<
|
2018-11-28 02:07:29 -06:00
|
|
|
"] jailed to [" << getRootFilePathAnonym() << "].");
|
2016-03-31 01:48:34 -05:00
|
|
|
|
2016-09-30 08:09:53 -05:00
|
|
|
// Despite the talk about URIs it seems that _uri is actually just a pathname here
|
2018-11-28 02:07:29 -06:00
|
|
|
const std::string publicFilePath = getUri().getPath();
|
2020-11-22 16:36:44 -06:00
|
|
|
if (!Poco::File(publicFilePath).exists())
|
|
|
|
{
|
|
|
|
LOG_ERR("Local file URI [" << publicFilePath << "] invalid or doesn't exist.");
|
|
|
|
throw BadRequestException("Invalid URI: " + getUri().toString());
|
|
|
|
}
|
2016-09-30 08:09:53 -05:00
|
|
|
|
2018-11-28 02:07:29 -06:00
|
|
|
if (!FileUtil::checkDiskSpace(getRootFilePath()))
|
2016-11-03 19:26:40 -05:00
|
|
|
{
|
2018-11-28 02:07:29 -06:00
|
|
|
throw StorageSpaceLowException("Low disk space for " + getRootFilePathAnonym());
|
2016-11-03 19:26:40 -05:00
|
|
|
}
|
2016-09-30 08:09:53 -05:00
|
|
|
|
2020-11-22 16:36:44 -06:00
|
|
|
if (_isTemporaryFile)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Neither link nor copy, just move, it's a temporary file.
|
|
|
|
Poco::File(publicFilePath).moveTo(getRootFilePath());
|
2020-11-22 20:39:39 -06:00
|
|
|
|
|
|
|
// Cleanup the directory after moving.
|
|
|
|
const std::string dir = Poco::Path(publicFilePath).parent().toString();
|
|
|
|
if (FileUtil::isEmptyDirectory(dir))
|
|
|
|
FileUtil::removeFile(dir);
|
2020-11-22 16:36:44 -06:00
|
|
|
}
|
|
|
|
catch (const Poco::Exception& exc)
|
|
|
|
{
|
2021-11-18 06:08:14 -06:00
|
|
|
LOG_ERR("Failed to move [" << COOLWSD::anonymizeUrl(publicFilePath) << "] to ["
|
2020-11-22 16:36:44 -06:00
|
|
|
<< getRootFilePathAnonym() << "]: " << exc.displayText());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!FileUtil::Stat(getRootFilePath()).exists())
|
2016-03-31 01:48:34 -05:00
|
|
|
{
|
2020-11-22 16:36:44 -06:00
|
|
|
// Try to link.
|
2021-11-18 06:08:14 -06:00
|
|
|
LOG_INF("Linking " << COOLWSD::anonymizeUrl(publicFilePath) << " to "
|
2020-11-22 16:36:44 -06:00
|
|
|
<< getRootFilePathAnonym());
|
|
|
|
if (!Poco::File(getRootFilePath()).exists()
|
|
|
|
&& link(publicFilePath.c_str(), getRootFilePath().c_str()) == -1)
|
|
|
|
{
|
|
|
|
// Failed
|
2021-11-18 06:08:14 -06:00
|
|
|
LOG_INF("link(\"" << COOLWSD::anonymizeUrl(publicFilePath) << "\", \""
|
2020-11-22 16:36:44 -06:00
|
|
|
<< getRootFilePathAnonym() << "\") failed. Will copy. Linking error: "
|
|
|
|
<< Util::symbolicErrno(errno) << ' ' << strerror(errno));
|
|
|
|
}
|
2016-03-31 01:48:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Fallback to copying.
|
2020-11-22 16:36:44 -06:00
|
|
|
if (!FileUtil::Stat(getRootFilePath()).exists())
|
2016-03-31 01:48:34 -05:00
|
|
|
{
|
2020-01-18 09:56:01 -06:00
|
|
|
FileUtil::copyFileTo(publicFilePath, getRootFilePath());
|
2016-03-31 01:48:34 -05:00
|
|
|
_isCopy = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const Poco::Exception& exc)
|
|
|
|
{
|
2021-11-18 06:08:14 -06:00
|
|
|
LOG_ERR("copyTo(\"" << COOLWSD::anonymizeUrl(publicFilePath) << "\", \""
|
2020-11-15 08:25:24 -06:00
|
|
|
<< getRootFilePathAnonym() << "\") failed: " << exc.displayText());
|
2016-03-31 01:48:34 -05:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2021-02-15 09:03:04 -06:00
|
|
|
setDownloaded(true);
|
|
|
|
|
2016-03-31 01:48:34 -05:00
|
|
|
// Now return the jailed path.
|
2017-02-06 16:26:38 -06:00
|
|
|
#ifndef KIT_IN_PROCESS
|
2021-11-18 06:08:14 -06:00
|
|
|
if (COOLWSD::NoCapsForKit)
|
2018-11-28 02:07:29 -06:00
|
|
|
return getRootFilePath();
|
2017-04-05 13:20:47 -05:00
|
|
|
else
|
2018-11-28 02:07:29 -06:00
|
|
|
return Poco::Path(getJailPath(), filename).toString();
|
2017-02-06 16:26:38 -06:00
|
|
|
#else
|
2018-11-28 02:07:29 -06:00
|
|
|
return getRootFilePath();
|
2017-02-06 16:26:38 -06:00
|
|
|
#endif
|
2018-09-13 11:16:00 -05:00
|
|
|
|
|
|
|
#else // MOBILEAPP
|
|
|
|
|
|
|
|
// In the mobile app we use no jail
|
2018-11-28 04:40:52 -06:00
|
|
|
setRootFilePath(getUri().getPath());
|
2018-09-13 11:16:00 -05:00
|
|
|
|
2018-11-28 04:40:52 -06:00
|
|
|
return getRootFilePath();
|
2018-09-13 11:16:00 -05:00
|
|
|
#endif
|
2016-03-31 01:48:34 -05:00
|
|
|
}
|
|
|
|
|
2022-07-24 18:53:11 -05:00
|
|
|
void LocalStorage::uploadLocalFileToStorageAsync(const Authorization& /*auth*/,
|
|
|
|
LockContext& /*lockCtx*/,
|
|
|
|
const std::string& /*saveAsPath*/,
|
|
|
|
const std::string& /*saveAsFilename*/,
|
wsd: move storage attributes to DocBroker
There are a number of races with having Storage
track the attributes. To fix them, we move all
attributes to DocBroker and correct a number
of issues.
The idea of the design is based on the fact that
we want to capture the attributes between
uploads, but based on the saved document.
That is, when we upload a document version, we
want to pass to the storage whether from the
perspective of the *Storage* there has been
any user-modifications or not. Since saving
to disk may happen multiple times between
uploads (not least because of failures), and
since saving resets the modified flag, we need
to capture the modified flag at each save and
propagate it until we upload successfully.
Upon uploading successfully, we reset the
attributes.
For this reason we have two attribute instances;
one is the 'current' attributes as being uploaded
and the other the 'next' one. We capture the
current state at saving into 'next' and we merge
with 'current' when saving succeeds and we
aren't already uploading (otherwise, we update
it and then discard it when uploading succeeds,
losing the last attributes).
Furthermore, because the modified flag is
reset after each save, and because we might
save and upload immediately after a
modification, we may not have the modified flag.
This means that we need some heuristics to
decide if there has been user-issued
modifications. (It is better to be conservative
here.) We try to detect this by introspecting
the commands we receive from users.
In effect, we capture the attributes when issuing
an internal save, we transfer the captured
attributes only when saving succeeds and we aren't
uploading, and from then on, uploading has to
succeed to reset the 'current' attributes. In the
meantime, if we fail to upload and issue another
save, the new attributes will be captured and
merged with the 'current' and the next upload
will not have any lost attributes.
Change-Id: I8c5e75d25ac235c6232318343678bf5c0933c31e
Signed-off-by: Ashod Nakashian <ashod.nakashian@collabora.co.uk>
2022-08-04 21:02:11 -05:00
|
|
|
bool /*isRename*/, const Attributes&, SocketPoll&,
|
2022-07-24 18:53:11 -05:00
|
|
|
const AsyncUploadCallback& asyncUploadCallback)
|
2016-03-31 01:48:34 -05:00
|
|
|
{
|
2020-11-15 08:25:24 -06:00
|
|
|
const std::string path = getUri().getPath();
|
|
|
|
|
2022-07-24 18:53:11 -05:00
|
|
|
// Assume failure by default.
|
|
|
|
UploadResult res = UploadResult(UploadResult::Result::FAILED, "Internal error");
|
2016-03-31 01:48:34 -05:00
|
|
|
try
|
|
|
|
{
|
2020-11-29 18:11:59 -06:00
|
|
|
LOG_TRC("Copying local file to local file storage (isCopy: " << _isCopy << ") for "
|
|
|
|
<< getRootFilePathAnonym());
|
2020-11-15 08:25:24 -06:00
|
|
|
|
2016-03-31 01:48:34 -05:00
|
|
|
// Copy the file back.
|
2021-05-24 13:11:40 -05:00
|
|
|
if (_isCopy && Poco::File(getRootFilePathUploading()).exists())
|
|
|
|
FileUtil::copyFileTo(getRootFilePathUploading(), path);
|
2017-06-10 09:45:00 -05:00
|
|
|
|
|
|
|
// update its fileinfo object. This is used later to check if someone else changed the
|
|
|
|
// document while we are/were editing it
|
2022-02-06 20:15:09 -06:00
|
|
|
getFileInfo().setLastModifiedTime(
|
|
|
|
Util::getIso8601FracformatTime(FileUtil::Stat(path).modifiedTimepoint()));
|
|
|
|
LOG_TRC("New FileInfo modified time in storage " << getFileInfo().getLastModifiedTime());
|
2022-07-24 18:53:11 -05:00
|
|
|
res = UploadResult(UploadResult::Result::OK);
|
2016-03-31 01:48:34 -05:00
|
|
|
}
|
|
|
|
catch (const Poco::Exception& exc)
|
|
|
|
{
|
2021-11-18 06:08:14 -06:00
|
|
|
LOG_ERR("copyTo(\"" << getRootFilePathAnonym() << "\", \"" << COOLWSD::anonymizeUrl(path)
|
2020-11-15 08:33:26 -06:00
|
|
|
<< "\") failed: " << exc.displayText());
|
2022-07-24 18:53:11 -05:00
|
|
|
// Default UploadResult is failure.
|
2016-03-31 01:48:34 -05:00
|
|
|
}
|
|
|
|
|
2022-07-24 18:53:11 -05:00
|
|
|
if (asyncUploadCallback)
|
|
|
|
asyncUploadCallback(AsyncUpload(AsyncUpload::State::Complete, res));
|
2016-03-31 01:48:34 -05:00
|
|
|
}
|
|
|
|
|
2019-02-12 05:16:40 -06:00
|
|
|
#if !MOBILEAPP
|
Start on a gtk+-based workalike to the iOS app
The idea is that it would work sufficiently identically, so that even
people without a Mac and without an iOS device could participate in
development of the non-iOS-specific bits, like the JavaScript, or the
online MOBILEAPP-specific plumbing. Which would be great.
No, this doesn't do anything sane yet. It does compile the same online
C++ files as the iOS app, though. (Some minor tweaks were needed in a
couple of them to silence gcc warnings.)
There is a plain Makefile, but I should change to using autofoo, too.
Eventually, this will need to be built in a separate tree from a
normal online, just like when using the --enable-iosapp configure
switch. (But for now, doesn't matter.)
Change-Id: I13e4d921acb99d802d2f9da4b0df4a237ca60ad6
2018-10-16 16:40:49 -05:00
|
|
|
|
2019-10-07 06:51:30 -05:00
|
|
|
Poco::Net::HTTPClientSession* StorageBase::getHTTPClientSession(const Poco::URI& uri)
|
2020-04-29 14:24:33 -05:00
|
|
|
{
|
|
|
|
bool useSSL = false;
|
|
|
|
if (SSLAsScheme)
|
|
|
|
{
|
|
|
|
// the WOPI URI itself should control whether we use SSL or not
|
|
|
|
// for whether we verify vs. certificates, cf. above
|
|
|
|
useSSL = uri.getScheme() != "http";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// We decoupled the Wopi communication from client communication because
|
|
|
|
// the Wopi communication must have an independent policy.
|
|
|
|
// So, we will use here only Storage settings.
|
2021-11-18 06:08:14 -06:00
|
|
|
useSSL = SSLEnabled || COOLWSD::isSSLTermination();
|
2020-04-29 14:24:33 -05:00
|
|
|
}
|
2019-10-09 07:22:57 -05:00
|
|
|
// We decoupled the Wopi communication from client communication because
|
2019-10-07 06:51:30 -05:00
|
|
|
// the Wopi communication must have an independent policy.
|
|
|
|
// So, we will use here only Storage settings.
|
2020-07-16 09:44:41 -05:00
|
|
|
Poco::Net::HTTPClientSession* session = useSSL
|
2020-04-29 14:24:33 -05:00
|
|
|
? new Poco::Net::HTTPSClientSession(uri.getHost(), uri.getPort(),
|
|
|
|
Poco::Net::SSLManager::instance().defaultClientContext())
|
|
|
|
: new Poco::Net::HTTPClientSession(uri.getHost(), uri.getPort());
|
2020-07-16 09:44:41 -05:00
|
|
|
|
|
|
|
// Set the timeout to the configured value.
|
2021-11-18 06:08:14 -06:00
|
|
|
static int timeoutSec = COOLWSD::getConfigValue<int>("net.connection_timeout_secs", 30);
|
2020-07-16 09:44:41 -05:00
|
|
|
session->setTimeout(Poco::Timespan(timeoutSec, 0));
|
|
|
|
|
|
|
|
return session;
|
2020-04-29 14:24:33 -05:00
|
|
|
}
|
2019-10-09 05:17:33 -05:00
|
|
|
|
2021-01-10 11:20:00 -06:00
|
|
|
std::shared_ptr<http::Session> StorageBase::getHttpSession(const Poco::URI& uri)
|
|
|
|
{
|
|
|
|
bool useSSL = false;
|
|
|
|
if (SSLAsScheme)
|
|
|
|
{
|
|
|
|
// the WOPI URI itself should control whether we use SSL or not
|
|
|
|
// for whether we verify vs. certificates, cf. above
|
|
|
|
useSSL = uri.getScheme() != "http";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// We decoupled the Wopi communication from client communication because
|
|
|
|
// the Wopi communication must have an independent policy.
|
|
|
|
// So, we will use here only Storage settings.
|
2021-11-18 06:08:14 -06:00
|
|
|
useSSL = SSLEnabled || COOLWSD::isSSLTermination();
|
2021-01-10 11:20:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
const auto protocol
|
|
|
|
= useSSL ? http::Session::Protocol::HttpSsl : http::Session::Protocol::HttpUnencrypted;
|
|
|
|
|
|
|
|
// Create the session.
|
2021-03-10 08:19:14 -06:00
|
|
|
auto httpSession = http::Session::create(uri.getHost(), protocol, uri.getPort());
|
2021-01-10 11:20:00 -06:00
|
|
|
|
2021-11-18 06:08:14 -06:00
|
|
|
static int timeoutSec = COOLWSD::getConfigValue<int>("net.connection_timeout_secs", 30);
|
2021-01-10 11:20:00 -06:00
|
|
|
httpSession->setTimeout(std::chrono::seconds(timeoutSec));
|
|
|
|
|
|
|
|
return httpSession;
|
|
|
|
}
|
|
|
|
|
2018-02-17 16:32:41 -06:00
|
|
|
namespace
|
|
|
|
{
|
2016-07-18 06:45:36 -05:00
|
|
|
|
2019-08-26 20:16:54 -05:00
|
|
|
static void addStorageDebugCookie(Poco::Net::HTTPRequest& request)
|
2017-05-17 03:48:51 -05:00
|
|
|
{
|
2017-05-17 06:13:24 -05:00
|
|
|
(void) request;
|
2017-05-17 03:48:51 -05:00
|
|
|
#if ENABLE_DEBUG
|
2021-11-18 06:08:14 -06:00
|
|
|
if (std::getenv("COOL_STORAGE_COOKIE"))
|
2017-05-17 03:48:51 -05:00
|
|
|
{
|
|
|
|
Poco::Net::NameValueCollection nvcCookies;
|
2022-03-29 20:37:57 -05:00
|
|
|
StringVector cookieTokens = StringVector::tokenize(std::string(std::getenv("COOL_STORAGE_COOKIE")), ':');
|
2017-05-17 03:48:51 -05:00
|
|
|
if (cookieTokens.size() == 2)
|
|
|
|
{
|
|
|
|
nvcCookies.add(cookieTokens[0], cookieTokens[1]);
|
|
|
|
request.setCookies(nvcCookies);
|
2020-05-24 08:10:18 -05:00
|
|
|
LOG_TRC("Added storage debug cookie [" << cookieTokens[0] << '=' << cookieTokens[1] << "].");
|
2017-05-17 03:48:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-02-14 12:01:06 -06:00
|
|
|
// access_token must be decoded
|
2020-02-19 07:38:47 -06:00
|
|
|
void addWopiProof(Poco::Net::HTTPRequest& request, const Poco::URI& uri,
|
2020-02-14 12:01:06 -06:00
|
|
|
const std::string& access_token)
|
2016-03-31 01:48:34 -05:00
|
|
|
{
|
2020-02-19 07:38:47 -06:00
|
|
|
assert(!uri.isRelative());
|
|
|
|
for (const auto& header : GetProofHeaders(access_token, uri.toString()))
|
2019-11-07 09:24:37 -06:00
|
|
|
request.set(header.first, header.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::map<std::string, std::string> GetQueryParams(const Poco::URI& uri)
|
|
|
|
{
|
|
|
|
std::map<std::string, std::string> result;
|
|
|
|
for (const auto& param : uri.getQueryParameters())
|
|
|
|
result.emplace(param);
|
|
|
|
return result;
|
2019-11-19 15:23:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2019-11-28 10:09:58 -06:00
|
|
|
#endif // !MOBILEAPP
|
|
|
|
|
2019-11-19 16:51:45 -06:00
|
|
|
void LockContext::initSupportsLocks()
|
|
|
|
{
|
2019-11-28 10:09:58 -06:00
|
|
|
#if MOBILEAPP
|
|
|
|
_supportsLocks = false;
|
|
|
|
#else
|
2019-11-19 16:51:45 -06:00
|
|
|
if (_supportsLocks)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// first time token setup
|
|
|
|
_supportsLocks = true;
|
2021-11-15 10:23:40 -06:00
|
|
|
_lockToken = "cool-lock" + Util::rng::getHexString(8);
|
2019-11-28 10:09:58 -06:00
|
|
|
#endif
|
2019-11-19 16:51:45 -06:00
|
|
|
}
|
|
|
|
|
2019-11-27 02:43:05 -06:00
|
|
|
bool LockContext::needsRefresh(const std::chrono::steady_clock::time_point &now) const
|
|
|
|
{
|
2023-02-18 08:26:29 -06:00
|
|
|
return _supportsLocks && _isLocked && _refreshSeconds > std::chrono::seconds::zero() &&
|
|
|
|
(now - _lastLockTime) >= _refreshSeconds;
|
2019-11-27 02:43:05 -06:00
|
|
|
}
|
|
|
|
|
2019-12-16 01:33:16 -06:00
|
|
|
void LockContext::dumpState(std::ostream& os) const
|
2019-11-27 02:43:05 -06:00
|
|
|
{
|
|
|
|
if (!_supportsLocks)
|
|
|
|
return;
|
2022-02-14 06:08:27 -06:00
|
|
|
|
|
|
|
os << "\n LockContext:";
|
|
|
|
os << "\n locked: " << _isLocked;
|
|
|
|
os << "\n token: " << _lockToken;
|
|
|
|
os << "\n last locked: " << Util::getSteadyClockAsString(_lastLockTime);
|
2019-11-27 02:43:05 -06:00
|
|
|
}
|
|
|
|
|
2019-11-28 10:09:58 -06:00
|
|
|
#if !MOBILEAPP
|
|
|
|
|
2020-06-14 11:44:10 -05:00
|
|
|
void WopiStorage::initHttpRequest(Poco::Net::HTTPRequest& request, const Poco::URI& uri,
|
2021-10-31 18:39:11 -05:00
|
|
|
const Authorization& auth) const
|
2020-06-14 11:44:10 -05:00
|
|
|
{
|
|
|
|
request.set("User-Agent", WOPI_AGENT_STRING);
|
|
|
|
|
|
|
|
auth.authorizeRequest(request);
|
|
|
|
|
|
|
|
addStorageDebugCookie(request);
|
|
|
|
|
|
|
|
// TODO: Avoid repeated parsing.
|
|
|
|
std::map<std::string, std::string> params = GetQueryParams(uri);
|
2021-02-06 16:11:06 -06:00
|
|
|
const auto it = params.find("access_token");
|
|
|
|
if (it != params.end())
|
|
|
|
addWopiProof(request, uri, it->second);
|
2020-06-14 11:44:10 -05:00
|
|
|
|
2020-10-08 12:20:41 -05:00
|
|
|
// Helps wrt. debugging cluster cases from the logs
|
2021-11-18 06:08:14 -06:00
|
|
|
request.set("X-COOL-WOPI-ServerId", Util::getProcessIdentifier());
|
2020-06-14 11:44:10 -05:00
|
|
|
}
|
|
|
|
|
2021-10-31 18:39:11 -05:00
|
|
|
http::Request WopiStorage::initHttpRequest(const Poco::URI& uri, const Authorization& auth) const
|
2021-01-10 11:20:00 -06:00
|
|
|
{
|
|
|
|
http::Request httpRequest(uri.getPathAndQuery());
|
|
|
|
|
|
|
|
//FIXME: Hack Hack Hack! Use own version.
|
|
|
|
Poco::Net::HTTPRequest request;
|
2021-10-31 18:39:11 -05:00
|
|
|
initHttpRequest(request, uri, auth);
|
2021-01-10 11:20:00 -06:00
|
|
|
|
|
|
|
// Copy the headers, including the cookies.
|
|
|
|
for (const auto& pair : request)
|
|
|
|
{
|
|
|
|
httpRequest.header().set(pair.first, pair.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
return httpRequest;
|
|
|
|
}
|
|
|
|
|
2021-10-31 18:39:11 -05:00
|
|
|
std::unique_ptr<WopiStorage::WOPIFileInfo>
|
|
|
|
WopiStorage::getWOPIFileInfoForUri(Poco::URI uriObject, const Authorization& auth,
|
|
|
|
LockContext& lockCtx, unsigned redirectLimit)
|
2019-11-19 15:23:50 -06:00
|
|
|
{
|
2021-05-31 07:02:47 -05:00
|
|
|
ProfileZone profileZone("WopiStorage::getWOPIFileInfo", { {"url", _fileUrl} });
|
2021-05-25 04:29:43 -05:00
|
|
|
|
2019-11-19 15:23:50 -06:00
|
|
|
// update the access_token to the one matching to the session
|
|
|
|
auth.authorizeURI(uriObject);
|
2021-11-18 06:08:14 -06:00
|
|
|
const std::string uriAnonym = COOLWSD::anonymizeUrl(uriObject.toString());
|
2019-08-26 20:16:54 -05:00
|
|
|
|
2018-06-10 19:55:52 -05:00
|
|
|
LOG_DBG("Getting info for wopi uri [" << uriAnonym << "].");
|
2016-10-14 05:09:43 -05:00
|
|
|
|
2018-06-10 10:42:15 -05:00
|
|
|
std::string wopiResponse;
|
2020-12-06 15:55:22 -06:00
|
|
|
std::chrono::milliseconds callDurationMs;
|
2016-12-23 01:53:50 -06:00
|
|
|
try
|
|
|
|
{
|
2020-12-05 17:41:16 -06:00
|
|
|
std::shared_ptr<http::Session> httpSession = getHttpSession(uriObject);
|
2021-10-31 18:39:11 -05:00
|
|
|
http::Request httpRequest = initHttpRequest(uriObject, auth);
|
2020-06-14 11:44:10 -05:00
|
|
|
|
2018-06-10 10:42:15 -05:00
|
|
|
const auto startTime = std::chrono::steady_clock::now();
|
|
|
|
|
2020-04-20 02:07:25 -05:00
|
|
|
Log::StreamLogger logger = Log::trace();
|
|
|
|
if (logger.enabled())
|
|
|
|
{
|
|
|
|
logger << "WOPI::CheckFileInfo request header for URI [" << uriAnonym << "]:\n";
|
2020-12-05 17:41:16 -06:00
|
|
|
for (const auto& pair : httpRequest.header())
|
2020-04-20 02:07:25 -05:00
|
|
|
{
|
|
|
|
logger << '\t' << pair.first << ": " << pair.second << " / ";
|
|
|
|
}
|
|
|
|
|
2022-08-28 09:29:47 -05:00
|
|
|
LOG_END_FLUSH(logger);
|
2020-04-20 02:07:25 -05:00
|
|
|
}
|
|
|
|
|
2021-04-05 16:51:33 -05:00
|
|
|
const std::shared_ptr<const http::Response> httpResponse
|
|
|
|
= httpSession->syncRequest(httpRequest);
|
2016-07-18 06:45:36 -05:00
|
|
|
|
2020-12-06 15:55:22 -06:00
|
|
|
callDurationMs = std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
|
|
std::chrono::steady_clock::now() - startTime);
|
2016-03-31 01:48:34 -05:00
|
|
|
|
2021-06-18 09:50:40 -05:00
|
|
|
if (httpResponse->statusLine().statusCode() == Poco::Net::HTTPResponse::HTTP_FOUND ||
|
2021-06-22 10:41:13 -05:00
|
|
|
httpResponse->statusLine().statusCode() == Poco::Net::HTTPResponse::HTTP_MOVED_PERMANENTLY ||
|
|
|
|
httpResponse->statusLine().statusCode() == Poco::Net::HTTPResponse::HTTP_TEMPORARY_REDIRECT ||
|
|
|
|
httpResponse->statusLine().statusCode() == Poco::Net::HTTPResponse::HTTP_PERMANENT_REDIRECT)
|
2021-06-18 09:50:40 -05:00
|
|
|
{
|
2021-06-22 10:45:12 -05:00
|
|
|
if (redirectLimit)
|
|
|
|
{
|
|
|
|
const std::string& location = httpResponse->get("Location");
|
2021-11-18 06:08:14 -06:00
|
|
|
LOG_TRC("WOPI::CheckFileInfo redirect to URI [" << COOLWSD::anonymizeUrl(location) << "]");
|
2021-06-18 09:50:40 -05:00
|
|
|
|
2021-06-22 10:45:12 -05:00
|
|
|
Poco::URI redirectUriObject(location);
|
|
|
|
setUri(redirectUriObject);
|
2021-10-31 18:39:11 -05:00
|
|
|
return getWOPIFileInfoForUri(redirectUriObject, auth, lockCtx, redirectLimit - 1);
|
2021-06-22 10:45:12 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-06-28 07:04:22 -05:00
|
|
|
LOG_WRN("WOPI::CheckFileInfo redirected too many times - URI [" << uriAnonym << "]");
|
2021-06-22 10:45:12 -05:00
|
|
|
}
|
2021-06-18 09:50:40 -05:00
|
|
|
}
|
|
|
|
|
2020-12-05 17:41:16 -06:00
|
|
|
// Note: we don't log the response if obfuscation is enabled, except for failures.
|
|
|
|
wopiResponse = httpResponse->getBody();
|
|
|
|
const bool failed
|
2021-04-05 16:51:33 -05:00
|
|
|
= (httpResponse->statusLine().statusCode() != Poco::Net::HTTPResponse::HTTP_OK);
|
2020-12-05 17:41:16 -06:00
|
|
|
|
|
|
|
Log::StreamLogger logRes = failed ? Log::error() : Log::trace();
|
2020-06-20 13:07:13 -05:00
|
|
|
if (logRes.enabled())
|
2016-12-23 01:53:50 -06:00
|
|
|
{
|
2020-12-05 17:41:16 -06:00
|
|
|
logRes << "WOPI::CheckFileInfo " << (failed ? "failed" : "returned") << " for URI ["
|
|
|
|
<< uriAnonym << "]: " << httpResponse->statusLine().statusCode() << ' '
|
|
|
|
<< httpResponse->statusLine().reasonPhrase() << ". Headers: ";
|
|
|
|
for (const auto& pair : httpResponse->header())
|
2016-12-22 15:41:05 -06:00
|
|
|
{
|
2020-06-20 13:07:13 -05:00
|
|
|
logRes << '\t' << pair.first << ": " << pair.second << " / ";
|
2016-12-22 15:41:05 -06:00
|
|
|
}
|
|
|
|
|
2020-12-05 17:41:16 -06:00
|
|
|
if (failed)
|
|
|
|
logRes << "\tBody: [" << wopiResponse << "]";
|
|
|
|
|
2022-08-28 09:29:47 -05:00
|
|
|
LOG_END_FLUSH(logRes);
|
2016-12-23 01:53:50 -06:00
|
|
|
}
|
2016-03-31 01:48:34 -05:00
|
|
|
|
2020-12-05 17:41:16 -06:00
|
|
|
if (failed)
|
2017-05-18 13:05:45 -05:00
|
|
|
{
|
2021-06-30 09:37:31 -05:00
|
|
|
if (httpResponse->statusLine().statusCode() == Poco::Net::HTTPResponse::HTTP_FORBIDDEN)
|
|
|
|
throw UnauthorizedRequestException(
|
|
|
|
"Access denied, 403. WOPI::CheckFileInfo failed on: " + uriAnonym);
|
|
|
|
|
2020-12-05 17:41:16 -06:00
|
|
|
throw StorageConnectionException("WOPI::CheckFileInfo failed: " + wopiResponse);
|
2017-05-18 13:05:45 -05:00
|
|
|
}
|
2016-12-23 01:53:50 -06:00
|
|
|
}
|
2018-06-10 19:55:52 -05:00
|
|
|
catch (const Poco::Exception& pexc)
|
2016-03-31 01:48:34 -05:00
|
|
|
{
|
2018-06-10 19:55:52 -05:00
|
|
|
LOG_ERR("Cannot get file info from WOPI storage uri [" << uriAnonym << "]. Error: " <<
|
2020-05-24 08:10:18 -05:00
|
|
|
pexc.displayText() << (pexc.nested() ? " (" + pexc.nested()->displayText() + ')' : ""));
|
2016-12-23 01:53:50 -06:00
|
|
|
throw;
|
2016-03-31 01:48:34 -05:00
|
|
|
}
|
2019-12-23 07:45:38 -06:00
|
|
|
catch (const BadRequestException& exc)
|
|
|
|
{
|
2020-10-06 22:29:56 -05:00
|
|
|
LOG_ERR("Cannot get file info from WOPI storage uri [" << uriAnonym << "]. Error: " << exc.what());
|
2019-12-23 07:45:38 -06:00
|
|
|
}
|
2016-03-31 01:48:34 -05:00
|
|
|
|
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
|
|
|
Poco::JSON::Object::Ptr object;
|
2018-06-10 10:42:15 -05:00
|
|
|
if (JsonUtil::parseJSON(wopiResponse, object))
|
2016-03-31 01:48:34 -05:00
|
|
|
{
|
2021-11-18 06:08:14 -06:00
|
|
|
if (COOLWSD::AnonymizeUserData)
|
2020-12-06 21:45:46 -06:00
|
|
|
LOG_DBG("WOPI::CheckFileInfo (" << callDurationMs << "): anonymizing...");
|
2018-08-01 21:04:33 -05:00
|
|
|
else
|
2020-12-06 21:45:46 -06:00
|
|
|
LOG_DBG("WOPI::CheckFileInfo (" << callDurationMs << "): " << wopiResponse);
|
2018-08-01 21:04:33 -05:00
|
|
|
|
2020-11-15 11:03:45 -06:00
|
|
|
std::size_t size = 0;
|
2020-06-11 09:54:27 -05:00
|
|
|
std::string filename, ownerId, lastModifiedTime;
|
|
|
|
|
|
|
|
JsonUtil::findJSONValue(object, "Size", size);
|
2018-02-17 16:32:41 -06:00
|
|
|
JsonUtil::findJSONValue(object, "OwnerId", ownerId);
|
2020-06-11 09:54:27 -05:00
|
|
|
JsonUtil::findJSONValue(object, "BaseFileName", filename);
|
|
|
|
JsonUtil::findJSONValue(object, "LastModifiedTime", lastModifiedTime);
|
|
|
|
|
2022-02-06 20:15:09 -06:00
|
|
|
FileInfo fileInfo = FileInfo({filename, ownerId, lastModifiedTime});
|
2020-06-11 09:54:27 -05:00
|
|
|
setFileInfo(fileInfo);
|
2018-06-10 10:42:15 -05:00
|
|
|
|
2021-11-18 06:08:14 -06:00
|
|
|
if (COOLWSD::AnonymizeUserData)
|
2018-11-28 02:07:29 -06:00
|
|
|
Util::mapAnonymized(Util::getFilenameFromURL(filename), Util::getFilenameFromURL(getUri().toString()));
|
2018-07-10 22:09:27 -05:00
|
|
|
|
2022-02-07 23:50:09 -06:00
|
|
|
auto wopiInfo = Util::make_unique<WopiStorage::WOPIFileInfo>(fileInfo, callDurationMs, object, uriObject);
|
2020-06-11 09:54:27 -05:00
|
|
|
if (wopiInfo->getSupportsLocks())
|
|
|
|
lockCtx.initSupportsLocks();
|
2018-06-10 10:42:15 -05:00
|
|
|
|
2021-02-06 16:11:06 -06:00
|
|
|
// If FileUrl is set, we use it for GetFile.
|
|
|
|
_fileUrl = wopiInfo->getFileUrl();
|
|
|
|
|
2020-06-11 09:54:27 -05:00
|
|
|
return wopiInfo;
|
2016-03-31 01:48:34 -05:00
|
|
|
}
|
2016-10-21 03:57:17 -05:00
|
|
|
else
|
2016-12-22 15:41:05 -06:00
|
|
|
{
|
2021-11-18 06:08:14 -06:00
|
|
|
if (COOLWSD::AnonymizeUserData)
|
2018-08-01 21:04:33 -05:00
|
|
|
wopiResponse = "obfuscated";
|
|
|
|
|
2020-12-06 15:55:22 -06:00
|
|
|
LOG_ERR("WOPI::CheckFileInfo ("
|
2020-12-06 21:45:46 -06:00
|
|
|
<< callDurationMs
|
|
|
|
<< ") failed or no valid JSON payload returned. Access denied. Original response: ["
|
2020-12-06 15:55:22 -06:00
|
|
|
<< wopiResponse << "].");
|
2018-08-01 21:04:33 -05:00
|
|
|
|
2018-06-10 19:55:52 -05:00
|
|
|
throw UnauthorizedRequestException("Access denied. WOPI::CheckFileInfo failed on: " + uriAnonym);
|
2016-12-22 15:41:05 -06:00
|
|
|
}
|
2020-06-11 09:54:27 -05:00
|
|
|
}
|
|
|
|
|
2021-06-18 09:50:40 -05:00
|
|
|
std::unique_ptr<WopiStorage::WOPIFileInfo> WopiStorage::getWOPIFileInfo(const Authorization& auth,
|
|
|
|
LockContext& lockCtx)
|
|
|
|
{
|
|
|
|
Poco::URI uriObject(getUri());
|
2021-10-31 18:39:11 -05:00
|
|
|
return getWOPIFileInfoForUri(uriObject, auth, lockCtx, RedirectionLimit);
|
2021-06-18 09:50:40 -05:00
|
|
|
}
|
|
|
|
|
2020-06-11 09:54:27 -05:00
|
|
|
void WopiStorage::WOPIFileInfo::init()
|
|
|
|
{
|
|
|
|
_userCanWrite = false;
|
|
|
|
_enableOwnerTermination = false;
|
|
|
|
_hidePrintOption = false;
|
|
|
|
_hideSaveOption = false;
|
|
|
|
_hideExportOption = false;
|
2022-10-13 11:50:20 -05:00
|
|
|
_hideRepairOption = false;
|
2020-06-11 09:54:27 -05:00
|
|
|
_disablePrint = false;
|
|
|
|
_disableExport = false;
|
|
|
|
_disableCopy = false;
|
|
|
|
_disableInactiveMessages = false;
|
|
|
|
_downloadAsPostMessage = false;
|
|
|
|
_userCanNotWriteRelative = true;
|
|
|
|
_enableInsertRemoteImage = false;
|
2023-02-14 09:48:11 -06:00
|
|
|
_enableRemoteLinkPicker = false;
|
2020-06-11 09:54:27 -05:00
|
|
|
_enableShare = false;
|
|
|
|
_supportsLocks = false;
|
|
|
|
_supportsRename = false;
|
|
|
|
_userCanRename = false;
|
|
|
|
_hideUserList = "false";
|
|
|
|
_disableChangeTrackingRecord = WOPIFileInfo::TriState::Unset;
|
|
|
|
_disableChangeTrackingShow = WOPIFileInfo::TriState::Unset;
|
|
|
|
_hideChangeTrackingControls = WOPIFileInfo::TriState::Unset;
|
|
|
|
}
|
|
|
|
|
|
|
|
WopiStorage::WOPIFileInfo::WOPIFileInfo(const FileInfo &fileInfo,
|
2020-12-06 15:55:22 -06:00
|
|
|
std::chrono::milliseconds callDurationMs,
|
2022-02-07 23:50:09 -06:00
|
|
|
Poco::JSON::Object::Ptr &object, Poco::URI &uriObject)
|
2020-06-11 09:54:27 -05:00
|
|
|
{
|
|
|
|
init();
|
|
|
|
|
|
|
|
const std::string &filename = fileInfo.getFilename();
|
|
|
|
const std::string &ownerId = fileInfo.getOwnerId();
|
|
|
|
|
|
|
|
JsonUtil::findJSONValue(object, "UserId", _userId);
|
|
|
|
JsonUtil::findJSONValue(object, "UserFriendlyName", _username);
|
|
|
|
JsonUtil::findJSONValue(object, "TemplateSaveAs", _templateSaveAs);
|
|
|
|
JsonUtil::findJSONValue(object, "TemplateSource", _templateSource);
|
2016-03-31 01:48:34 -05:00
|
|
|
|
2020-12-19 09:06:01 -06:00
|
|
|
// UserFriendlyName is used as the Author when loading the document.
|
|
|
|
// If it's missing, document loading fails. Since the UserFriendlyName
|
|
|
|
// field is optional in WOPI specs, it's often left out by integrators.
|
|
|
|
if (_username.empty())
|
|
|
|
{
|
|
|
|
_username = "UnknownUser"; // Default to something sensible yet friendly.
|
|
|
|
if (!_userId.empty())
|
|
|
|
_username += '_' + _userId;
|
|
|
|
|
2021-02-16 06:45:28 -06:00
|
|
|
LOG_ERR("WOPI::CheckFileInfo does not specify a valid UserFriendlyName for the current "
|
2020-12-19 09:06:01 -06:00
|
|
|
"user. Temporarily ["
|
|
|
|
<< _username << "] will be used until a valid name is specified.");
|
|
|
|
}
|
|
|
|
|
2020-06-11 09:54:27 -05:00
|
|
|
std::ostringstream wopiResponse;
|
2017-05-19 03:32:24 -05:00
|
|
|
|
2020-06-11 09:54:27 -05:00
|
|
|
// Anonymize key values.
|
2021-11-18 06:08:14 -06:00
|
|
|
if (COOLWSD::AnonymizeUserData)
|
2020-06-11 09:54:27 -05:00
|
|
|
{
|
|
|
|
JsonUtil::findJSONValue(object, "ObfuscatedUserId", _obfuscatedUserId, false);
|
|
|
|
if (!_obfuscatedUserId.empty())
|
|
|
|
{
|
|
|
|
Util::mapAnonymized(ownerId, _obfuscatedUserId);
|
|
|
|
Util::mapAnonymized(_userId, _obfuscatedUserId);
|
|
|
|
Util::mapAnonymized(_username, _obfuscatedUserId);
|
|
|
|
}
|
|
|
|
|
|
|
|
Poco::JSON::Object::Ptr anonObject(object);
|
|
|
|
|
|
|
|
// Set anonymized version of the above fields before logging.
|
|
|
|
// Note: anonymization caches the result, so we don't need to store here.
|
2021-11-18 06:08:14 -06:00
|
|
|
if (COOLWSD::AnonymizeUserData)
|
|
|
|
anonObject->set("BaseFileName", COOLWSD::anonymizeUrl(filename));
|
2020-06-11 09:54:27 -05:00
|
|
|
|
|
|
|
// If obfuscatedUserId is provided, then don't log the originals and use it.
|
2021-11-18 06:08:14 -06:00
|
|
|
if (COOLWSD::AnonymizeUserData && _obfuscatedUserId.empty())
|
2020-06-11 09:54:27 -05:00
|
|
|
{
|
2021-11-18 06:08:14 -06:00
|
|
|
anonObject->set("OwnerId", COOLWSD::anonymizeUsername(ownerId));
|
|
|
|
anonObject->set("UserId", COOLWSD::anonymizeUsername(_userId));
|
|
|
|
anonObject->set("UserFriendlyName", COOLWSD::anonymizeUsername(_username));
|
2020-06-11 09:54:27 -05:00
|
|
|
}
|
|
|
|
anonObject->stringify(wopiResponse);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
object->stringify(wopiResponse);
|
|
|
|
|
2020-12-06 21:45:46 -06:00
|
|
|
LOG_DBG("WOPI::CheckFileInfo (" << callDurationMs << "): " << wopiResponse.str());
|
2020-06-11 09:54:27 -05:00
|
|
|
|
|
|
|
JsonUtil::findJSONValue(object, "UserExtraInfo", _userExtraInfo);
|
2023-01-04 03:09:21 -06:00
|
|
|
JsonUtil::findJSONValue(object, "UserPrivateInfo", _userPrivateInfo);
|
2020-06-11 09:54:27 -05:00
|
|
|
JsonUtil::findJSONValue(object, "WatermarkText", _watermarkText);
|
|
|
|
JsonUtil::findJSONValue(object, "UserCanWrite", _userCanWrite);
|
|
|
|
JsonUtil::findJSONValue(object, "PostMessageOrigin", _postMessageOrigin);
|
|
|
|
JsonUtil::findJSONValue(object, "HidePrintOption", _hidePrintOption);
|
|
|
|
JsonUtil::findJSONValue(object, "HideSaveOption", _hideSaveOption);
|
|
|
|
JsonUtil::findJSONValue(object, "HideExportOption", _hideExportOption);
|
2022-10-13 11:50:20 -05:00
|
|
|
JsonUtil::findJSONValue(object, "HideRepairOption", _hideRepairOption);
|
2020-06-11 09:54:27 -05:00
|
|
|
JsonUtil::findJSONValue(object, "EnableOwnerTermination", _enableOwnerTermination);
|
|
|
|
JsonUtil::findJSONValue(object, "DisablePrint", _disablePrint);
|
|
|
|
JsonUtil::findJSONValue(object, "DisableExport", _disableExport);
|
|
|
|
JsonUtil::findJSONValue(object, "DisableCopy", _disableCopy);
|
|
|
|
JsonUtil::findJSONValue(object, "DisableInactiveMessages", _disableInactiveMessages);
|
|
|
|
JsonUtil::findJSONValue(object, "DownloadAsPostMessage", _downloadAsPostMessage);
|
|
|
|
JsonUtil::findJSONValue(object, "UserCanNotWriteRelative", _userCanNotWriteRelative);
|
|
|
|
JsonUtil::findJSONValue(object, "EnableInsertRemoteImage", _enableInsertRemoteImage);
|
2023-02-14 09:48:11 -06:00
|
|
|
JsonUtil::findJSONValue(object, "EnableRemoteLinkPicker", _enableRemoteLinkPicker);
|
2020-06-11 09:54:27 -05:00
|
|
|
JsonUtil::findJSONValue(object, "EnableShare", _enableShare);
|
|
|
|
JsonUtil::findJSONValue(object, "HideUserList", _hideUserList);
|
|
|
|
JsonUtil::findJSONValue(object, "SupportsLocks", _supportsLocks);
|
|
|
|
JsonUtil::findJSONValue(object, "SupportsRename", _supportsRename);
|
|
|
|
JsonUtil::findJSONValue(object, "UserCanRename", _userCanRename);
|
2020-06-09 11:43:58 -05:00
|
|
|
JsonUtil::findJSONValue(object, "BreadcrumbDocName", _breadcrumbDocName);
|
2021-02-06 16:11:06 -06:00
|
|
|
JsonUtil::findJSONValue(object, "FileUrl", _fileUrl);
|
2021-05-29 07:31:33 -05:00
|
|
|
|
2022-08-02 21:04:49 -05:00
|
|
|
#if ENABLE_FEATURE_LOCK
|
2022-02-07 23:50:09 -06:00
|
|
|
bool isUserLocked = false;
|
|
|
|
JsonUtil::findJSONValue(object, "IsUserLocked", isUserLocked);
|
|
|
|
|
|
|
|
if (config::getBool("feature_lock.locked_hosts[@allow]", false))
|
|
|
|
{
|
|
|
|
bool isReadOnly = false;
|
|
|
|
isUserLocked = false;
|
2022-07-14 05:26:58 -05:00
|
|
|
CommandControl::LockManager::setUnlockLink(uriObject.getHost());
|
2022-03-24 05:33:40 -05:00
|
|
|
Poco::URI newUri(HostUtil::getNewLockedUri(uriObject));
|
|
|
|
const std::string host = newUri.getHost();
|
2022-02-07 23:50:09 -06:00
|
|
|
|
|
|
|
if (CommandControl::LockManager::hostExist(host))
|
|
|
|
{
|
|
|
|
isReadOnly = CommandControl::LockManager::isHostReadOnly(host);
|
|
|
|
isUserLocked = CommandControl::LockManager::isHostCommandDisabled(host);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-03-24 05:23:34 -05:00
|
|
|
LOG_INF("Could not find matching locked_host: " << host << ",applying fallback settings");
|
2022-02-07 23:50:09 -06:00
|
|
|
isReadOnly = config::getBool("feature_lock.locked_hosts.fallback[@read_only]", false);
|
|
|
|
isUserLocked =
|
|
|
|
config::getBool("feature_lock.locked_hosts.fallback[@disabled_commands]", false);
|
|
|
|
}
|
2021-05-29 07:31:33 -05:00
|
|
|
|
2022-02-07 23:50:09 -06:00
|
|
|
if (isReadOnly)
|
|
|
|
{
|
|
|
|
isUserLocked = true;
|
2022-11-23 02:25:44 -06:00
|
|
|
_userCanWrite = false;
|
|
|
|
LOG_DBG("Feature lock is enabled and " << host << " is in the list of read-only members. Therefore, document is set to read-only.");
|
2022-02-07 23:50:09 -06:00
|
|
|
}
|
|
|
|
CommandControl::LockManager::setHostReadOnly(isReadOnly);
|
|
|
|
}
|
|
|
|
CommandControl::LockManager::setLockedUser(isUserLocked);
|
|
|
|
#else
|
|
|
|
(void)uriObject;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bool booleanFlag = false;
|
2021-09-13 15:02:43 -05:00
|
|
|
JsonUtil::findJSONValue(object, "IsUserRestricted", booleanFlag);
|
|
|
|
CommandControl::RestrictionManager::setRestrictedUser(booleanFlag);
|
|
|
|
|
2020-06-11 09:54:27 -05:00
|
|
|
if (JsonUtil::findJSONValue(object, "DisableChangeTrackingRecord", booleanFlag))
|
|
|
|
_disableChangeTrackingRecord = (booleanFlag ? WOPIFileInfo::TriState::True : WOPIFileInfo::TriState::False);
|
|
|
|
if (JsonUtil::findJSONValue(object, "DisableChangeTrackingShow", booleanFlag))
|
|
|
|
_disableChangeTrackingShow = (booleanFlag ? WOPIFileInfo::TriState::True : WOPIFileInfo::TriState::False);
|
|
|
|
if (JsonUtil::findJSONValue(object, "HideChangeTrackingControls", booleanFlag))
|
|
|
|
_hideChangeTrackingControls = (booleanFlag ? WOPIFileInfo::TriState::True : WOPIFileInfo::TriState::False);
|
2019-11-19 16:51:45 -06:00
|
|
|
|
2020-12-03 20:14:30 -06:00
|
|
|
static const std::string overrideWatermarks
|
2021-11-18 06:08:14 -06:00
|
|
|
= COOLWSD::getConfigValue<std::string>("watermark.text", "");
|
2019-11-25 06:03:49 -06:00
|
|
|
if (!overrideWatermarks.empty())
|
2020-06-11 09:54:27 -05:00
|
|
|
_watermarkText = overrideWatermarks;
|
2022-03-08 16:56:18 -06:00
|
|
|
if (isTemplate(filename))
|
|
|
|
_disableExport = true;
|
2016-03-31 01:48:34 -05:00
|
|
|
}
|
|
|
|
|
2023-02-19 14:17:39 -06:00
|
|
|
StorageBase::LockUpdateResult WopiStorage::updateLockState(const Authorization& auth,
|
|
|
|
LockContext& lockCtx, bool lock,
|
|
|
|
const Attributes& attribs)
|
2019-11-19 16:51:45 -06:00
|
|
|
{
|
2020-07-01 03:34:08 -05:00
|
|
|
lockCtx._lockFailureReason.clear();
|
2019-11-19 16:51:45 -06:00
|
|
|
if (!lockCtx._supportsLocks)
|
2023-02-19 14:17:39 -06:00
|
|
|
return LockUpdateResult::UNSUPPORTED;
|
2019-11-19 16:51:45 -06:00
|
|
|
|
|
|
|
Poco::URI uriObject(getUri());
|
|
|
|
auth.authorizeURI(uriObject);
|
|
|
|
|
|
|
|
Poco::URI uriObjectAnonym(getUri());
|
2021-11-18 06:08:14 -06:00
|
|
|
uriObjectAnonym.setPath(COOLWSD::anonymizeUrl(uriObjectAnonym.getPath()));
|
2019-11-19 16:51:45 -06:00
|
|
|
const std::string uriAnonym = uriObjectAnonym.toString();
|
|
|
|
|
|
|
|
const std::string wopiLog(lock ? "WOPI::Lock" : "WOPI::Unlock");
|
|
|
|
LOG_DBG(wopiLog << " requesting: " << uriAnonym);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
std::unique_ptr<Poco::Net::HTTPClientSession> psession(getHTTPClientSession(uriObject));
|
|
|
|
|
2020-06-14 11:44:10 -05:00
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST,
|
|
|
|
uriObject.getPathAndQuery(),
|
|
|
|
Poco::Net::HTTPMessage::HTTP_1_1);
|
2021-10-31 18:39:11 -05:00
|
|
|
initHttpRequest(request, uriObject, auth);
|
2019-11-19 16:51:45 -06:00
|
|
|
|
|
|
|
request.set("X-WOPI-Override", lock ? "LOCK" : "UNLOCK");
|
|
|
|
request.set("X-WOPI-Lock", lockCtx._lockToken);
|
wsd: move storage attributes to DocBroker
There are a number of races with having Storage
track the attributes. To fix them, we move all
attributes to DocBroker and correct a number
of issues.
The idea of the design is based on the fact that
we want to capture the attributes between
uploads, but based on the saved document.
That is, when we upload a document version, we
want to pass to the storage whether from the
perspective of the *Storage* there has been
any user-modifications or not. Since saving
to disk may happen multiple times between
uploads (not least because of failures), and
since saving resets the modified flag, we need
to capture the modified flag at each save and
propagate it until we upload successfully.
Upon uploading successfully, we reset the
attributes.
For this reason we have two attribute instances;
one is the 'current' attributes as being uploaded
and the other the 'next' one. We capture the
current state at saving into 'next' and we merge
with 'current' when saving succeeds and we
aren't already uploading (otherwise, we update
it and then discard it when uploading succeeds,
losing the last attributes).
Furthermore, because the modified flag is
reset after each save, and because we might
save and upload immediately after a
modification, we may not have the modified flag.
This means that we need some heuristics to
decide if there has been user-issued
modifications. (It is better to be conservative
here.) We try to detect this by introspecting
the commands we receive from users.
In effect, we capture the attributes when issuing
an internal save, we transfer the captured
attributes only when saving succeeds and we aren't
uploading, and from then on, uploading has to
succeed to reset the 'current' attributes. In the
meantime, if we fail to upload and issue another
save, the new attributes will be captured and
merged with the 'current' and the next upload
will not have any lost attributes.
Change-Id: I8c5e75d25ac235c6232318343678bf5c0933c31e
Signed-off-by: Ashod Nakashian <ashod.nakashian@collabora.co.uk>
2022-08-04 21:02:11 -05:00
|
|
|
if (!attribs.getExtendedData().empty())
|
2021-11-19 00:39:37 -06:00
|
|
|
{
|
wsd: move storage attributes to DocBroker
There are a number of races with having Storage
track the attributes. To fix them, we move all
attributes to DocBroker and correct a number
of issues.
The idea of the design is based on the fact that
we want to capture the attributes between
uploads, but based on the saved document.
That is, when we upload a document version, we
want to pass to the storage whether from the
perspective of the *Storage* there has been
any user-modifications or not. Since saving
to disk may happen multiple times between
uploads (not least because of failures), and
since saving resets the modified flag, we need
to capture the modified flag at each save and
propagate it until we upload successfully.
Upon uploading successfully, we reset the
attributes.
For this reason we have two attribute instances;
one is the 'current' attributes as being uploaded
and the other the 'next' one. We capture the
current state at saving into 'next' and we merge
with 'current' when saving succeeds and we
aren't already uploading (otherwise, we update
it and then discard it when uploading succeeds,
losing the last attributes).
Furthermore, because the modified flag is
reset after each save, and because we might
save and upload immediately after a
modification, we may not have the modified flag.
This means that we need some heuristics to
decide if there has been user-issued
modifications. (It is better to be conservative
here.) We try to detect this by introspecting
the commands we receive from users.
In effect, we capture the attributes when issuing
an internal save, we transfer the captured
attributes only when saving succeeds and we aren't
uploading, and from then on, uploading has to
succeed to reset the 'current' attributes. In the
meantime, if we fail to upload and issue another
save, the new attributes will be captured and
merged with the 'current' and the next upload
will not have any lost attributes.
Change-Id: I8c5e75d25ac235c6232318343678bf5c0933c31e
Signed-off-by: Ashod Nakashian <ashod.nakashian@collabora.co.uk>
2022-08-04 21:02:11 -05:00
|
|
|
request.set("X-COOL-WOPI-ExtendedData", attribs.getExtendedData());
|
|
|
|
request.set("X-LOOL-WOPI-ExtendedData", attribs.getExtendedData());
|
2021-11-19 00:39:37 -06:00
|
|
|
}
|
2019-11-19 16:51:45 -06:00
|
|
|
|
2020-06-30 08:37:40 -05:00
|
|
|
// IIS requires content-length for POST requests: see https://forums.iis.net/t/1119456.aspx
|
|
|
|
request.setContentLength(0);
|
|
|
|
|
2019-11-19 16:51:45 -06:00
|
|
|
psession->sendRequest(request);
|
|
|
|
Poco::Net::HTTPResponse response;
|
|
|
|
std::istream& rs = psession->receiveResponse(response);
|
|
|
|
|
|
|
|
std::ostringstream oss;
|
|
|
|
Poco::StreamCopier::copyStream(rs, oss);
|
|
|
|
std::string responseString = oss.str();
|
|
|
|
|
|
|
|
LOG_INF(wopiLog << " response: " << responseString <<
|
|
|
|
" status " << response.getStatus());
|
|
|
|
|
|
|
|
if (response.getStatus() == Poco::Net::HTTPResponse::HTTP_OK)
|
|
|
|
{
|
|
|
|
lockCtx._isLocked = lock;
|
2023-03-10 14:03:05 -06:00
|
|
|
lockCtx.bumpTimer();
|
2023-02-19 14:17:39 -06:00
|
|
|
return LockUpdateResult::OK;
|
2019-11-19 16:51:45 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-06-30 17:29:58 -05:00
|
|
|
std::string sMoreInfo = response.get("X-WOPI-LockFailureReason", "");
|
|
|
|
if (!sMoreInfo.empty())
|
2020-07-01 03:34:08 -05:00
|
|
|
{
|
|
|
|
lockCtx._lockFailureReason = sMoreInfo;
|
2020-06-30 17:29:58 -05:00
|
|
|
sMoreInfo = ", failure reason: \"" + sMoreInfo + "\"";
|
2020-07-01 03:34:08 -05:00
|
|
|
}
|
2023-02-19 14:17:39 -06:00
|
|
|
|
|
|
|
if (response.getStatus() == Poco::Net::HTTPResponse::HTTP_UNAUTHORIZED ||
|
|
|
|
response.getStatus() == Poco::Net::HTTPResponse::HTTP_FORBIDDEN ||
|
|
|
|
response.getStatus() == Poco::Net::HTTPResponse::HTTP_NOT_FOUND)
|
|
|
|
{
|
|
|
|
LOG_ERR("Un-successful " << wopiLog << " with expired token, HTTP status "
|
|
|
|
<< response.getStatus() << sMoreInfo
|
|
|
|
<< " and response: " << responseString);
|
|
|
|
|
|
|
|
return LockUpdateResult::UNAUTHORIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_ERR("Un-successful " << wopiLog << " with HTTP status " << response.getStatus()
|
|
|
|
<< sMoreInfo << " and response: " << responseString);
|
|
|
|
return LockUpdateResult::FAILED;
|
2019-11-19 16:51:45 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const Poco::Exception& pexc)
|
|
|
|
{
|
|
|
|
LOG_ERR("Cannot " << wopiLog << " uri [" << uriAnonym << "]. Error: " <<
|
2020-05-24 08:10:18 -05:00
|
|
|
pexc.displayText() << (pexc.nested() ? " (" + pexc.nested()->displayText() + ')' : ""));
|
2019-11-19 16:51:45 -06:00
|
|
|
}
|
2019-12-23 07:45:38 -06:00
|
|
|
catch (const BadRequestException& exc)
|
|
|
|
{
|
2020-10-06 22:29:56 -05:00
|
|
|
LOG_ERR("Cannot " << wopiLog << " uri [" << uriAnonym << "]. Error: " << exc.what());
|
2019-12-23 07:45:38 -06:00
|
|
|
}
|
2023-02-19 14:17:39 -06:00
|
|
|
|
|
|
|
lockCtx._lockFailureReason = "Request failed";
|
|
|
|
return LockUpdateResult::FAILED;
|
2019-11-19 16:51:45 -06:00
|
|
|
}
|
|
|
|
|
2016-03-31 01:48:34 -05:00
|
|
|
/// uri format: http://server/<...>/wopi*/files/<id>/content
|
2020-11-29 18:11:59 -06:00
|
|
|
std::string WopiStorage::downloadStorageFileToLocal(const Authorization& auth,
|
|
|
|
LockContext& /*lockCtx*/,
|
|
|
|
const std::string& templateUri)
|
2016-03-31 01:48:34 -05:00
|
|
|
{
|
2021-05-31 07:02:47 -05:00
|
|
|
ProfileZone profileZone("WopiStorage::downloadStorageFileToLocal", { {"url", _fileUrl} });
|
|
|
|
|
2019-05-21 22:33:26 -05:00
|
|
|
if (!templateUri.empty())
|
|
|
|
{
|
2021-02-13 17:33:51 -06:00
|
|
|
// Download the template file and load it normally.
|
|
|
|
// The document will get saved once loading in Core is complete.
|
2021-11-18 06:08:14 -06:00
|
|
|
const std::string templateUriAnonym = COOLWSD::anonymizeUrl(templateUri);
|
2021-02-13 17:33:51 -06:00
|
|
|
try
|
|
|
|
{
|
|
|
|
LOG_INF("WOPI::GetFile template source: " << templateUriAnonym);
|
2021-10-31 18:39:11 -05:00
|
|
|
return downloadDocument(Poco::URI(templateUri), templateUriAnonym, auth,
|
|
|
|
RedirectionLimit);
|
2021-02-13 17:33:51 -06:00
|
|
|
}
|
|
|
|
catch (const std::exception& ex)
|
|
|
|
{
|
|
|
|
LOG_ERR("Could not download template from [" + templateUriAnonym + "]. Error: "
|
|
|
|
<< ex.what());
|
2022-03-06 12:07:47 -06:00
|
|
|
throw; // Bubble-up the exception.
|
2021-02-13 17:33:51 -06:00
|
|
|
}
|
2019-05-21 22:33:26 -05:00
|
|
|
|
2021-02-13 17:33:51 -06:00
|
|
|
return std::string();
|
2019-05-21 22:33:26 -05:00
|
|
|
}
|
|
|
|
|
2021-02-06 16:11:06 -06:00
|
|
|
// First try the FileUrl, if provided.
|
|
|
|
if (!_fileUrl.empty())
|
|
|
|
{
|
2021-11-18 06:08:14 -06:00
|
|
|
const std::string fileUrlAnonym = COOLWSD::anonymizeUrl(_fileUrl);
|
2021-02-06 16:11:06 -06:00
|
|
|
try
|
|
|
|
{
|
|
|
|
LOG_INF("WOPI::GetFile using FileUrl: " << fileUrlAnonym);
|
2021-10-31 18:39:11 -05:00
|
|
|
return downloadDocument(Poco::URI(_fileUrl), fileUrlAnonym, auth, RedirectionLimit);
|
2021-02-06 16:11:06 -06:00
|
|
|
}
|
2022-03-06 12:07:47 -06:00
|
|
|
catch (const StorageSpaceLowException&)
|
|
|
|
{
|
|
|
|
throw; // Bubble-up the exception.
|
|
|
|
}
|
2021-02-06 16:11:06 -06:00
|
|
|
catch (const std::exception& ex)
|
|
|
|
{
|
|
|
|
LOG_ERR("Could not download document from WOPI FileUrl [" + fileUrlAnonym
|
|
|
|
+ "]. Will use default URL. Error: "
|
|
|
|
<< ex.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try the default URL, we either don't have FileUrl, or it failed.
|
2021-02-07 20:53:46 -06:00
|
|
|
// WOPI URI to download files ends in '/contents'.
|
2021-10-31 18:39:11 -05:00
|
|
|
// Add it here to get the payload instead of file info.
|
2021-02-07 20:53:46 -06:00
|
|
|
Poco::URI uriObject(getUri());
|
|
|
|
uriObject.setPath(uriObject.getPath() + "/contents");
|
|
|
|
auth.authorizeURI(uriObject);
|
|
|
|
|
|
|
|
Poco::URI uriObjectAnonym(getUri());
|
2021-11-18 06:08:14 -06:00
|
|
|
uriObjectAnonym.setPath(COOLWSD::anonymizeUrl(uriObjectAnonym.getPath()) + "/contents");
|
2021-02-07 20:53:46 -06:00
|
|
|
const std::string uriAnonym = uriObjectAnonym.toString();
|
|
|
|
|
2016-12-23 01:53:50 -06:00
|
|
|
try
|
|
|
|
{
|
2021-02-06 16:11:06 -06:00
|
|
|
LOG_INF("WOPI::GetFile using default URI: " << uriAnonym);
|
2021-10-31 18:39:11 -05:00
|
|
|
return downloadDocument(uriObject, uriAnonym, auth, RedirectionLimit);
|
2016-12-23 01:53:50 -06:00
|
|
|
}
|
2021-02-06 16:11:06 -06:00
|
|
|
catch (const std::exception& ex)
|
2019-12-23 07:45:38 -06:00
|
|
|
{
|
2020-11-29 18:11:59 -06:00
|
|
|
LOG_ERR("Cannot download document from WOPI storage uri [" + uriAnonym + "]. Error: "
|
2021-02-06 16:11:06 -06:00
|
|
|
<< ex.what());
|
2022-03-06 12:07:47 -06:00
|
|
|
throw; // Bubble-up the exception.
|
2019-12-23 07:45:38 -06:00
|
|
|
}
|
2016-03-31 01:48:34 -05:00
|
|
|
|
2020-11-29 18:11:59 -06:00
|
|
|
return std::string();
|
2016-03-31 01:48:34 -05:00
|
|
|
}
|
|
|
|
|
2021-02-07 20:53:46 -06:00
|
|
|
std::string WopiStorage::downloadDocument(const Poco::URI& uriObject, const std::string& uriAnonym,
|
2021-10-31 18:39:11 -05:00
|
|
|
const Authorization& auth, unsigned redirectLimit)
|
2021-02-07 20:53:46 -06:00
|
|
|
{
|
|
|
|
const auto startTime = std::chrono::steady_clock::now();
|
2021-01-10 11:20:00 -06:00
|
|
|
std::shared_ptr<http::Session> httpSession = getHttpSession(uriObject);
|
|
|
|
|
2021-10-31 18:39:11 -05:00
|
|
|
http::Request httpRequest = initHttpRequest(uriObject, auth);
|
2021-02-07 20:53:46 -06:00
|
|
|
|
2021-01-10 11:20:00 -06:00
|
|
|
setRootFilePath(Poco::Path(getLocalRootPath(), getFileInfo().getFilename()).toString());
|
2021-11-18 06:08:14 -06:00
|
|
|
setRootFilePathAnonym(COOLWSD::anonymizeUrl(getRootFilePath()));
|
2021-01-10 11:20:00 -06:00
|
|
|
|
2022-03-06 12:07:47 -06:00
|
|
|
if (!FileUtil::checkDiskSpace(getRootFilePath()))
|
|
|
|
{
|
|
|
|
throw StorageSpaceLowException("Low disk space for " + getRootFilePathAnonym());
|
|
|
|
}
|
|
|
|
|
2021-01-10 11:20:00 -06:00
|
|
|
LOG_TRC("Downloading from [" << uriAnonym << "] to [" << getRootFilePath()
|
|
|
|
<< "]: " << httpRequest.header().toString());
|
2021-04-05 16:51:33 -05:00
|
|
|
const std::shared_ptr<const http::Response> httpResponse
|
|
|
|
= httpSession->syncDownload(httpRequest, getRootFilePath());
|
2021-02-07 20:53:46 -06:00
|
|
|
|
|
|
|
const std::chrono::milliseconds diff = std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
|
|
std::chrono::steady_clock::now() - startTime);
|
|
|
|
|
2021-04-05 16:51:33 -05:00
|
|
|
if (httpResponse->statusLine().statusCode() == Poco::Net::HTTPResponse::HTTP_OK)
|
2021-02-07 20:53:46 -06:00
|
|
|
{
|
2021-02-20 15:17:45 -06:00
|
|
|
// Log the response header.
|
|
|
|
Log::StreamLogger logger = Log::trace();
|
|
|
|
if (logger.enabled())
|
2021-02-07 20:53:46 -06:00
|
|
|
{
|
2021-02-20 15:17:45 -06:00
|
|
|
logger << "WOPI::GetFile response header for URI [" << uriAnonym << "]:\n";
|
|
|
|
for (const auto& pair : httpResponse->header())
|
|
|
|
{
|
|
|
|
logger << '\t' << pair.first << ": " << pair.second << " / ";
|
|
|
|
}
|
|
|
|
|
2022-08-28 09:29:47 -05:00
|
|
|
LOG_END_FLUSH(logger);
|
2021-02-07 20:53:46 -06:00
|
|
|
}
|
|
|
|
}
|
2021-06-22 10:38:45 -05:00
|
|
|
else if (httpResponse->statusLine().statusCode() == Poco::Net::HTTPResponse::HTTP_FOUND ||
|
2021-06-22 10:41:13 -05:00
|
|
|
httpResponse->statusLine().statusCode() == Poco::Net::HTTPResponse::HTTP_MOVED_PERMANENTLY ||
|
|
|
|
httpResponse->statusLine().statusCode() == Poco::Net::HTTPResponse::HTTP_TEMPORARY_REDIRECT ||
|
|
|
|
httpResponse->statusLine().statusCode() == Poco::Net::HTTPResponse::HTTP_PERMANENT_REDIRECT)
|
2021-06-22 10:38:45 -05:00
|
|
|
{
|
2021-06-22 10:45:12 -05:00
|
|
|
if (redirectLimit)
|
|
|
|
{
|
|
|
|
const std::string& location = httpResponse->get("Location");
|
2021-11-18 06:08:14 -06:00
|
|
|
LOG_TRC("WOPI::GetFile redirect to URI [" << COOLWSD::anonymizeUrl(location) << "]");
|
2021-06-22 10:38:45 -05:00
|
|
|
|
2021-06-22 10:45:12 -05:00
|
|
|
Poco::URI redirectUriObject(location);
|
2021-10-31 18:39:11 -05:00
|
|
|
return downloadDocument(redirectUriObject, uriAnonym, auth, redirectLimit - 1);
|
2021-06-22 10:45:12 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw StorageConnectionException("WOPI::GetFile [" + uriAnonym
|
|
|
|
+ "] failed: redirected too many times");
|
|
|
|
}
|
2021-06-22 10:38:45 -05:00
|
|
|
}
|
2021-04-05 16:51:33 -05:00
|
|
|
else
|
2021-02-07 20:53:46 -06:00
|
|
|
{
|
2021-01-10 11:20:00 -06:00
|
|
|
const std::string responseString = httpResponse->getBody();
|
|
|
|
LOG_ERR("WOPI::GetFile [" << uriAnonym << "] failed with Status Code: "
|
|
|
|
<< httpResponse->statusLine().statusCode());
|
|
|
|
throw StorageConnectionException("WOPI::GetFile [" + uriAnonym
|
|
|
|
+ "] failed: " + responseString);
|
2021-02-07 20:53:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Successful
|
|
|
|
const FileUtil::Stat fileStat(getRootFilePath());
|
|
|
|
const std::size_t filesize = (fileStat.good() ? fileStat.size() : 0);
|
|
|
|
LOG_INF("WOPI::GetFile downloaded " << filesize << " bytes from [" << uriAnonym << "] -> "
|
2021-02-20 15:30:24 -06:00
|
|
|
<< getRootFilePathAnonym() << " in " << diff);
|
2021-02-15 09:03:04 -06:00
|
|
|
setDownloaded(true);
|
2021-02-07 20:53:46 -06:00
|
|
|
|
|
|
|
// Now return the jailed path.
|
2021-11-18 06:08:14 -06:00
|
|
|
if (COOLWSD::NoCapsForKit)
|
2021-02-07 20:53:46 -06:00
|
|
|
return getRootFilePath();
|
|
|
|
else
|
|
|
|
return Poco::Path(getJailPath(), getFileInfo().getFilename()).toString();
|
|
|
|
}
|
|
|
|
|
2020-12-28 13:58:07 -06:00
|
|
|
/// A helper class to invoke the AsyncUploadCallback
|
|
|
|
/// when it exits its scope.
|
|
|
|
/// By default it invokes the callback with a failure state.
|
|
|
|
class ScopedInvokeAsyncUploadCallback
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ScopedInvokeAsyncUploadCallback(StorageBase::AsyncUploadCallback asyncUploadCallback)
|
|
|
|
: _asyncUploadCallback(std::move(asyncUploadCallback))
|
|
|
|
, _arg(StorageBase::AsyncUpload(
|
|
|
|
StorageBase::AsyncUpload::State::Error,
|
|
|
|
StorageBase::UploadResult(StorageBase::UploadResult::Result::FAILED)))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~ScopedInvokeAsyncUploadCallback()
|
|
|
|
{
|
|
|
|
if (_asyncUploadCallback)
|
|
|
|
_asyncUploadCallback(_arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set a new callback argument.
|
|
|
|
void setArg(StorageBase::AsyncUpload arg) { _arg = std::move(arg); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
StorageBase::AsyncUploadCallback _asyncUploadCallback;
|
|
|
|
StorageBase::AsyncUpload _arg;
|
|
|
|
};
|
|
|
|
|
2021-10-31 18:39:11 -05:00
|
|
|
void WopiStorage::uploadLocalFileToStorageAsync(const Authorization& auth, LockContext& lockCtx,
|
2020-12-28 13:58:07 -06:00
|
|
|
const std::string& saveAsPath,
|
|
|
|
const std::string& saveAsFilename,
|
wsd: move storage attributes to DocBroker
There are a number of races with having Storage
track the attributes. To fix them, we move all
attributes to DocBroker and correct a number
of issues.
The idea of the design is based on the fact that
we want to capture the attributes between
uploads, but based on the saved document.
That is, when we upload a document version, we
want to pass to the storage whether from the
perspective of the *Storage* there has been
any user-modifications or not. Since saving
to disk may happen multiple times between
uploads (not least because of failures), and
since saving resets the modified flag, we need
to capture the modified flag at each save and
propagate it until we upload successfully.
Upon uploading successfully, we reset the
attributes.
For this reason we have two attribute instances;
one is the 'current' attributes as being uploaded
and the other the 'next' one. We capture the
current state at saving into 'next' and we merge
with 'current' when saving succeeds and we
aren't already uploading (otherwise, we update
it and then discard it when uploading succeeds,
losing the last attributes).
Furthermore, because the modified flag is
reset after each save, and because we might
save and upload immediately after a
modification, we may not have the modified flag.
This means that we need some heuristics to
decide if there has been user-issued
modifications. (It is better to be conservative
here.) We try to detect this by introspecting
the commands we receive from users.
In effect, we capture the attributes when issuing
an internal save, we transfer the captured
attributes only when saving succeeds and we aren't
uploading, and from then on, uploading has to
succeed to reset the 'current' attributes. In the
meantime, if we fail to upload and issue another
save, the new attributes will be captured and
merged with the 'current' and the next upload
will not have any lost attributes.
Change-Id: I8c5e75d25ac235c6232318343678bf5c0933c31e
Signed-off-by: Ashod Nakashian <ashod.nakashian@collabora.co.uk>
2022-08-04 21:02:11 -05:00
|
|
|
const bool isRename, const Attributes& attribs,
|
|
|
|
SocketPoll& socketPoll,
|
2020-12-28 13:58:07 -06:00
|
|
|
const AsyncUploadCallback& asyncUploadCallback)
|
2016-03-31 01:48:34 -05:00
|
|
|
{
|
2021-05-31 07:02:47 -05:00
|
|
|
ProfileZone profileZone("WopiStorage::uploadLocalFileToStorage", { {"url", _fileUrl} });
|
|
|
|
|
2016-10-19 09:52:53 -05:00
|
|
|
// TODO: Check if this URI has write permission (canWrite = true)
|
2016-12-22 15:41:05 -06:00
|
|
|
|
2020-12-28 13:58:07 -06:00
|
|
|
// Always invoke the callback with the result of the async upload.
|
|
|
|
ScopedInvokeAsyncUploadCallback scopedInvokeCallback(asyncUploadCallback);
|
|
|
|
|
|
|
|
//TODO: replace with state machine.
|
|
|
|
if (_uploadHttpSession)
|
|
|
|
{
|
2021-06-16 15:06:02 -05:00
|
|
|
LOG_WRN("Upload is already in progress.");
|
2020-12-28 13:58:07 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-24 04:31:39 -05:00
|
|
|
const bool isSaveAs = !saveAsPath.empty() && !saveAsFilename.empty();
|
2021-05-24 13:11:40 -05:00
|
|
|
const std::string filePath(isSaveAs ? saveAsPath : getRootFilePathUploading());
|
2021-11-18 06:08:14 -06:00
|
|
|
const std::string filePathAnonym = COOLWSD::anonymizeUrl(filePath);
|
2017-11-06 05:44:37 -06:00
|
|
|
|
2020-10-22 18:20:29 -05:00
|
|
|
const FileUtil::Stat fileStat(filePath);
|
|
|
|
if (!fileStat.good())
|
|
|
|
{
|
|
|
|
LOG_ERR("Cannot access file [" << filePathAnonym << "] to upload to wopi storage.");
|
2020-12-28 13:58:07 -06:00
|
|
|
scopedInvokeCallback.setArg(
|
|
|
|
AsyncUpload(AsyncUpload::State::Error,
|
|
|
|
UploadResult(UploadResult::Result::FAILED, "File not found.")));
|
|
|
|
return;
|
2020-10-22 18:20:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const std::size_t size = (fileStat.good() ? fileStat.size() : 0);
|
2017-10-20 11:12:05 -05:00
|
|
|
|
2018-11-28 02:07:29 -06:00
|
|
|
Poco::URI uriObject(getUri());
|
2020-12-05 16:43:17 -06:00
|
|
|
uriObject.setPath(isSaveAs || isRename ? uriObject.getPath()
|
|
|
|
: uriObject.getPath() + "/contents");
|
2017-08-16 09:38:00 -05:00
|
|
|
auth.authorizeURI(uriObject);
|
2019-08-26 20:16:54 -05:00
|
|
|
|
2021-11-18 06:08:14 -06:00
|
|
|
const std::string uriAnonym = COOLWSD::anonymizeUrl(uriObject.toString());
|
2017-05-12 10:42:03 -05:00
|
|
|
|
2020-10-22 18:20:29 -05:00
|
|
|
LOG_INF("Uploading " << size << " bytes from [" << filePathAnonym << "] to URI via WOPI ["
|
|
|
|
<< uriAnonym << "].");
|
2016-03-31 01:48:34 -05:00
|
|
|
|
2019-11-12 03:50:33 -06:00
|
|
|
const auto startTime = std::chrono::steady_clock::now();
|
2016-12-23 01:53:50 -06:00
|
|
|
try
|
2016-11-23 06:09:54 -06:00
|
|
|
{
|
2021-05-24 12:47:41 -05:00
|
|
|
assert(!_uploadHttpSession && "Unexpected to have an upload http::session");
|
2020-12-28 13:58:07 -06:00
|
|
|
_uploadHttpSession = getHttpSession(uriObject);
|
2016-12-23 01:53:50 -06:00
|
|
|
|
2021-10-31 18:39:11 -05:00
|
|
|
http::Request httpRequest = initHttpRequest(uriObject, auth);
|
2020-12-05 16:43:17 -06:00
|
|
|
httpRequest.setVerb(http::Request::VERB_POST);
|
|
|
|
|
|
|
|
http::Header& httpHeader = httpRequest.header();
|
2017-10-20 11:12:05 -05:00
|
|
|
|
2021-10-12 07:08:00 -05:00
|
|
|
// must include this header except for SaveAs
|
|
|
|
if (!isSaveAs && lockCtx._supportsLocks)
|
|
|
|
httpHeader.set("X-WOPI-Lock", lockCtx._lockToken);
|
|
|
|
|
2019-04-30 09:21:44 -05:00
|
|
|
if (!isSaveAs && !isRename)
|
2017-10-20 11:12:05 -05:00
|
|
|
{
|
|
|
|
// normal save
|
2020-12-05 16:43:17 -06:00
|
|
|
httpHeader.set("X-WOPI-Override", "PUT");
|
wsd: move storage attributes to DocBroker
There are a number of races with having Storage
track the attributes. To fix them, we move all
attributes to DocBroker and correct a number
of issues.
The idea of the design is based on the fact that
we want to capture the attributes between
uploads, but based on the saved document.
That is, when we upload a document version, we
want to pass to the storage whether from the
perspective of the *Storage* there has been
any user-modifications or not. Since saving
to disk may happen multiple times between
uploads (not least because of failures), and
since saving resets the modified flag, we need
to capture the modified flag at each save and
propagate it until we upload successfully.
Upon uploading successfully, we reset the
attributes.
For this reason we have two attribute instances;
one is the 'current' attributes as being uploaded
and the other the 'next' one. We capture the
current state at saving into 'next' and we merge
with 'current' when saving succeeds and we
aren't already uploading (otherwise, we update
it and then discard it when uploading succeeds,
losing the last attributes).
Furthermore, because the modified flag is
reset after each save, and because we might
save and upload immediately after a
modification, we may not have the modified flag.
This means that we need some heuristics to
decide if there has been user-issued
modifications. (It is better to be conservative
here.) We try to detect this by introspecting
the commands we receive from users.
In effect, we capture the attributes when issuing
an internal save, we transfer the captured
attributes only when saving succeeds and we aren't
uploading, and from then on, uploading has to
succeed to reset the 'current' attributes. In the
meantime, if we fail to upload and issue another
save, the new attributes will be captured and
merged with the 'current' and the next upload
will not have any lost attributes.
Change-Id: I8c5e75d25ac235c6232318343678bf5c0933c31e
Signed-off-by: Ashod Nakashian <ashod.nakashian@collabora.co.uk>
2022-08-04 21:02:11 -05:00
|
|
|
httpHeader.set("X-COOL-WOPI-IsModifiedByUser", attribs.isUserModified() ? "true" : "false");
|
|
|
|
httpHeader.set("X-LOOL-WOPI-IsModifiedByUser", attribs.isUserModified() ? "true" : "false");
|
|
|
|
httpHeader.set("X-COOL-WOPI-IsAutosave", attribs.isAutosave() ? "true" : "false");
|
|
|
|
httpHeader.set("X-LOOL-WOPI-IsAutosave", attribs.isAutosave() ? "true" : "false");
|
|
|
|
httpHeader.set("X-COOL-WOPI-IsExitSave", attribs.isExitSave() ? "true" : "false");
|
|
|
|
httpHeader.set("X-LOOL-WOPI-IsExitSave", attribs.isExitSave() ? "true" : "false");
|
|
|
|
if (attribs.isExitSave())
|
2021-07-29 16:49:52 -05:00
|
|
|
httpHeader.set("Connection", "close"); // Don't maintain the socket if we are exiting.
|
wsd: move storage attributes to DocBroker
There are a number of races with having Storage
track the attributes. To fix them, we move all
attributes to DocBroker and correct a number
of issues.
The idea of the design is based on the fact that
we want to capture the attributes between
uploads, but based on the saved document.
That is, when we upload a document version, we
want to pass to the storage whether from the
perspective of the *Storage* there has been
any user-modifications or not. Since saving
to disk may happen multiple times between
uploads (not least because of failures), and
since saving resets the modified flag, we need
to capture the modified flag at each save and
propagate it until we upload successfully.
Upon uploading successfully, we reset the
attributes.
For this reason we have two attribute instances;
one is the 'current' attributes as being uploaded
and the other the 'next' one. We capture the
current state at saving into 'next' and we merge
with 'current' when saving succeeds and we
aren't already uploading (otherwise, we update
it and then discard it when uploading succeeds,
losing the last attributes).
Furthermore, because the modified flag is
reset after each save, and because we might
save and upload immediately after a
modification, we may not have the modified flag.
This means that we need some heuristics to
decide if there has been user-issued
modifications. (It is better to be conservative
here.) We try to detect this by introspecting
the commands we receive from users.
In effect, we capture the attributes when issuing
an internal save, we transfer the captured
attributes only when saving succeeds and we aren't
uploading, and from then on, uploading has to
succeed to reset the 'current' attributes. In the
meantime, if we fail to upload and issue another
save, the new attributes will be captured and
merged with the 'current' and the next upload
will not have any lost attributes.
Change-Id: I8c5e75d25ac235c6232318343678bf5c0933c31e
Signed-off-by: Ashod Nakashian <ashod.nakashian@collabora.co.uk>
2022-08-04 21:02:11 -05:00
|
|
|
if (!attribs.getExtendedData().empty())
|
2021-11-19 00:39:37 -06:00
|
|
|
{
|
wsd: move storage attributes to DocBroker
There are a number of races with having Storage
track the attributes. To fix them, we move all
attributes to DocBroker and correct a number
of issues.
The idea of the design is based on the fact that
we want to capture the attributes between
uploads, but based on the saved document.
That is, when we upload a document version, we
want to pass to the storage whether from the
perspective of the *Storage* there has been
any user-modifications or not. Since saving
to disk may happen multiple times between
uploads (not least because of failures), and
since saving resets the modified flag, we need
to capture the modified flag at each save and
propagate it until we upload successfully.
Upon uploading successfully, we reset the
attributes.
For this reason we have two attribute instances;
one is the 'current' attributes as being uploaded
and the other the 'next' one. We capture the
current state at saving into 'next' and we merge
with 'current' when saving succeeds and we
aren't already uploading (otherwise, we update
it and then discard it when uploading succeeds,
losing the last attributes).
Furthermore, because the modified flag is
reset after each save, and because we might
save and upload immediately after a
modification, we may not have the modified flag.
This means that we need some heuristics to
decide if there has been user-issued
modifications. (It is better to be conservative
here.) We try to detect this by introspecting
the commands we receive from users.
In effect, we capture the attributes when issuing
an internal save, we transfer the captured
attributes only when saving succeeds and we aren't
uploading, and from then on, uploading has to
succeed to reset the 'current' attributes. In the
meantime, if we fail to upload and issue another
save, the new attributes will be captured and
merged with the 'current' and the next upload
will not have any lost attributes.
Change-Id: I8c5e75d25ac235c6232318343678bf5c0933c31e
Signed-off-by: Ashod Nakashian <ashod.nakashian@collabora.co.uk>
2022-08-04 21:02:11 -05:00
|
|
|
httpHeader.set("X-COOL-WOPI-ExtendedData", attribs.getExtendedData());
|
|
|
|
httpHeader.set("X-LOOL-WOPI-ExtendedData", attribs.getExtendedData());
|
2021-11-19 00:39:37 -06:00
|
|
|
}
|
2017-10-20 11:12:05 -05:00
|
|
|
|
wsd: move storage attributes to DocBroker
There are a number of races with having Storage
track the attributes. To fix them, we move all
attributes to DocBroker and correct a number
of issues.
The idea of the design is based on the fact that
we want to capture the attributes between
uploads, but based on the saved document.
That is, when we upload a document version, we
want to pass to the storage whether from the
perspective of the *Storage* there has been
any user-modifications or not. Since saving
to disk may happen multiple times between
uploads (not least because of failures), and
since saving resets the modified flag, we need
to capture the modified flag at each save and
propagate it until we upload successfully.
Upon uploading successfully, we reset the
attributes.
For this reason we have two attribute instances;
one is the 'current' attributes as being uploaded
and the other the 'next' one. We capture the
current state at saving into 'next' and we merge
with 'current' when saving succeeds and we
aren't already uploading (otherwise, we update
it and then discard it when uploading succeeds,
losing the last attributes).
Furthermore, because the modified flag is
reset after each save, and because we might
save and upload immediately after a
modification, we may not have the modified flag.
This means that we need some heuristics to
decide if there has been user-issued
modifications. (It is better to be conservative
here.) We try to detect this by introspecting
the commands we receive from users.
In effect, we capture the attributes when issuing
an internal save, we transfer the captured
attributes only when saving succeeds and we aren't
uploading, and from then on, uploading has to
succeed to reset the 'current' attributes. In the
meantime, if we fail to upload and issue another
save, the new attributes will be captured and
merged with the 'current' and the next upload
will not have any lost attributes.
Change-Id: I8c5e75d25ac235c6232318343678bf5c0933c31e
Signed-off-by: Ashod Nakashian <ashod.nakashian@collabora.co.uk>
2022-08-04 21:02:11 -05:00
|
|
|
if (!attribs.isForced())
|
2017-10-20 11:12:05 -05:00
|
|
|
{
|
|
|
|
// Request WOPI host to not overwrite if timestamps mismatch
|
2022-02-06 20:15:09 -06:00
|
|
|
httpHeader.set("X-COOL-WOPI-Timestamp", getFileInfo().getLastModifiedTime());
|
|
|
|
httpHeader.set("X-LOOL-WOPI-Timestamp", getFileInfo().getLastModifiedTime());
|
2017-10-20 11:12:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2017-06-01 07:56:54 -05:00
|
|
|
{
|
2017-10-24 04:31:39 -05:00
|
|
|
// the suggested target has to be in UTF-7; default to extension
|
|
|
|
// only when the conversion fails
|
2019-09-21 20:49:58 -05:00
|
|
|
std::string suggestedTarget = '.' + Poco::Path(saveAsFilename).getExtension();
|
2017-10-24 04:31:39 -05:00
|
|
|
|
2019-09-21 20:49:58 -05:00
|
|
|
//TODO: Perhaps we should cache this descriptor and reuse, as iconv_open might be expensive.
|
|
|
|
const iconv_t cd = iconv_open("UTF-7", "UTF-8");
|
2017-10-24 04:31:39 -05:00
|
|
|
if (cd == (iconv_t) -1)
|
|
|
|
LOG_ERR("Failed to initialize iconv for UTF-7 conversion, using '" << suggestedTarget << "'.");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::vector<char> input(saveAsFilename.begin(), saveAsFilename.end());
|
|
|
|
std::vector<char> buffer(8 * saveAsFilename.size());
|
|
|
|
|
|
|
|
char* in = &input[0];
|
2020-11-15 11:03:45 -06:00
|
|
|
std::size_t in_left = input.size();
|
2017-10-24 04:31:39 -05:00
|
|
|
char* out = &buffer[0];
|
2020-11-15 11:03:45 -06:00
|
|
|
std::size_t out_left = buffer.size();
|
2017-10-24 04:31:39 -05:00
|
|
|
|
|
|
|
if (iconv(cd, &in, &in_left, &out, &out_left) == (size_t) -1)
|
|
|
|
LOG_ERR("Failed to convert '" << saveAsFilename << "' to UTF-7, using '" << suggestedTarget << "'.");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// conversion succeeded
|
|
|
|
suggestedTarget = std::string(&buffer[0], buffer.size() - out_left);
|
|
|
|
LOG_TRC("Converted '" << saveAsFilename << "' to UTF-7 as '" << suggestedTarget << "'.");
|
|
|
|
}
|
2019-09-21 20:49:58 -05:00
|
|
|
|
|
|
|
iconv_close(cd);
|
2017-10-24 04:31:39 -05:00
|
|
|
}
|
|
|
|
|
2019-04-30 09:21:44 -05:00
|
|
|
if (isRename)
|
|
|
|
{
|
|
|
|
// rename file
|
2020-12-05 16:43:17 -06:00
|
|
|
httpHeader.set("X-WOPI-Override", "RENAME_FILE");
|
|
|
|
httpHeader.set("X-WOPI-RequestedName", suggestedTarget);
|
2019-04-30 09:21:44 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// save as
|
2020-12-05 16:43:17 -06:00
|
|
|
httpHeader.set("X-WOPI-Override", "PUT_RELATIVE");
|
|
|
|
httpHeader.set("X-WOPI-Size", std::to_string(size));
|
2021-11-24 09:45:31 -06:00
|
|
|
LOG_TRC("Save as: suggested target is '" << suggestedTarget << "'.");
|
2020-12-05 16:43:17 -06:00
|
|
|
httpHeader.set("X-WOPI-SuggestedTarget", suggestedTarget);
|
2019-04-30 09:21:44 -05:00
|
|
|
}
|
2017-06-01 07:56:54 -05:00
|
|
|
}
|
2017-06-01 09:16:03 -05:00
|
|
|
|
2020-12-05 16:43:17 -06:00
|
|
|
httpHeader.setContentType("application/octet-stream");
|
|
|
|
httpHeader.setContentLength(size);
|
2020-06-14 11:44:10 -05:00
|
|
|
|
2020-12-05 16:43:17 -06:00
|
|
|
httpRequest.setBodyFile(filePath);
|
2017-10-20 11:12:05 -05:00
|
|
|
|
2020-12-28 13:58:07 -06:00
|
|
|
http::Session::FinishedCallback finishedCallback =
|
|
|
|
[=](const std::shared_ptr<http::Session>& httpSession)
|
|
|
|
{
|
2021-05-24 12:47:41 -05:00
|
|
|
// Retire.
|
|
|
|
_uploadHttpSession.reset();
|
|
|
|
|
|
|
|
assert(httpSession && "Expected a valid http::Session");
|
2020-12-28 13:58:07 -06:00
|
|
|
const std::shared_ptr<const http::Response> httpResponse = httpSession->response();
|
2018-06-10 21:40:32 -05:00
|
|
|
|
2020-12-28 13:58:07 -06:00
|
|
|
_wopiSaveDuration = std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
|
|
std::chrono::steady_clock::now() - startTime);
|
wsd: reduce debug and generally redundant logs
In 'debug' log-level we expect a detailed, but
still readable output. Having one area with
disproportionately large number of logs reduces
the overall utility of the log output.
This patch reduces a number of redundant log
entries, including errors that are already
logged. It also reduces the level of some
others from 'information' to 'debug' and
from 'debug' to 'trace'.
The goal is to make 'debug' level as useful as
possible to read the progress and be able to
understand what was going on, such that one is
able to decide which area to dig deeper into.
Then, trace level could be used to get more
insight into that area, if necessary. For
example, when investigating a test failure,
one first enables 'debug' logs and reads through.
Once a section between two debug entries is
identified as being of interest, enabling 'trace'
level logs becomes more productive as it's
now possible to easily reach the first DBG
entry and read through until the second one.
It's unfortunate that we don't have per-area
control for enabling/disabling logs, so it
is common to see more and more 'debug' log
entries added all around, making logs
less and less readable.
It is also a limitation of the levels we have
that we really only have 3 usable levels:
one, two, many. That is, 'information' for
the most important events, 'debug' for
technical details needed to investigate issues,
and 'trace' for everything else. ('warning'
and 'error' aren't really 'levels'; they have
semantics that makes them special-cases.)
So we have to avoid degrading one into the
other, or have differences without distinction.
If any of these entries are needed to be
displayed more frequently, changing them
back to 'debug' or even 'information' should
be done. Though for me they seem special
cases that don't benefit most log readings.
Change-Id: Id2c6a9dc027483b81a066b0b4b50a298c5eff449
Signed-off-by: Ashod Nakashian <ashod.nakashian@collabora.co.uk>
2022-02-19 07:30:26 -06:00
|
|
|
LOG_TRC("Finished async uploading in " << _wopiSaveDuration);
|
2020-12-28 13:58:07 -06:00
|
|
|
|
|
|
|
WopiUploadDetails details = { filePathAnonym,
|
|
|
|
uriAnonym,
|
|
|
|
httpResponse->statusLine().reasonPhrase(),
|
|
|
|
httpResponse->statusLine().statusCode(),
|
|
|
|
size,
|
|
|
|
isSaveAs,
|
|
|
|
isRename };
|
|
|
|
|
|
|
|
// Handle the response.
|
|
|
|
const StorageBase::UploadResult res =
|
|
|
|
handleUploadToStorageResponse(details, httpResponse->getBody());
|
|
|
|
|
|
|
|
// Fire the callback to our client (DocBroker, typically).
|
|
|
|
asyncUploadCallback(AsyncUpload(AsyncUpload::State::Complete, res));
|
|
|
|
};
|
2020-11-15 14:40:21 -06:00
|
|
|
|
2020-12-28 13:58:07 -06:00
|
|
|
_uploadHttpSession->setFinishedHandler(finishedCallback);
|
|
|
|
|
|
|
|
LOG_DBG("Async upload request: " << httpRequest.header().toString());
|
|
|
|
|
|
|
|
// Make the request.
|
|
|
|
_uploadHttpSession->asyncRequest(httpRequest, socketPoll);
|
|
|
|
|
|
|
|
scopedInvokeCallback.setArg(
|
|
|
|
AsyncUpload(AsyncUpload::State::Running, UploadResult(UploadResult::Result::OK)));
|
|
|
|
return;
|
2020-11-15 14:40:21 -06:00
|
|
|
}
|
2020-12-28 12:05:50 -06:00
|
|
|
catch (const Poco::Exception& ex)
|
2020-11-15 14:40:21 -06:00
|
|
|
{
|
2020-12-28 12:05:50 -06:00
|
|
|
LOG_ERR("Cannot upload file to WOPI storage uri ["
|
|
|
|
<< uriAnonym << "]. Error: " << ex.displayText()
|
|
|
|
<< (ex.nested() ? " (" + ex.nested()->displayText() + ')' : ""));
|
2020-11-15 14:40:21 -06:00
|
|
|
}
|
2020-12-28 12:05:50 -06:00
|
|
|
catch (const std::exception& ex)
|
2020-11-15 14:40:21 -06:00
|
|
|
{
|
2020-12-28 12:05:50 -06:00
|
|
|
LOG_ERR("Cannot upload file to WOPI storage uri [" + uriAnonym + "]. Error: " << ex.what());
|
2020-11-15 14:40:21 -06:00
|
|
|
}
|
|
|
|
|
2020-12-28 13:58:07 -06:00
|
|
|
scopedInvokeCallback.setArg(AsyncUpload(
|
|
|
|
AsyncUpload::State::Error, UploadResult(UploadResult::Result::FAILED, "Internal error.")));
|
|
|
|
}
|
|
|
|
|
2020-11-29 18:11:59 -06:00
|
|
|
StorageBase::UploadResult
|
|
|
|
WopiStorage::handleUploadToStorageResponse(const WopiUploadDetails& details,
|
|
|
|
std::string responseString)
|
2020-11-15 14:40:21 -06:00
|
|
|
{
|
|
|
|
// Assume we failed, unless we have confirmation of success.
|
2020-12-28 12:05:50 -06:00
|
|
|
StorageBase::UploadResult result(UploadResult::Result::FAILED, responseString);
|
2020-11-15 14:40:21 -06:00
|
|
|
try
|
|
|
|
{
|
2020-12-28 12:05:50 -06:00
|
|
|
// Save a copy of the response because we might need to anonymize.
|
2020-11-15 14:40:21 -06:00
|
|
|
const std::string origResponseString = responseString;
|
|
|
|
|
|
|
|
const std::string wopiLog(details.isSaveAs
|
|
|
|
? "WOPI::PutRelativeFile"
|
|
|
|
: (details.isRename ? "WOPI::RenameFile" : "WOPI::PutFile"));
|
2018-06-10 21:40:32 -05:00
|
|
|
|
|
|
|
if (Log::infoEnabled())
|
|
|
|
{
|
2021-11-18 06:08:14 -06:00
|
|
|
if (COOLWSD::AnonymizeUserData)
|
2018-06-10 21:40:32 -05:00
|
|
|
{
|
|
|
|
Poco::JSON::Object::Ptr object;
|
|
|
|
if (JsonUtil::parseJSON(responseString, object))
|
|
|
|
{
|
|
|
|
// Anonymize the filename
|
2018-07-17 01:01:05 -05:00
|
|
|
std::string url;
|
2018-06-10 21:40:32 -05:00
|
|
|
std::string filename;
|
2020-11-15 14:40:21 -06:00
|
|
|
if (JsonUtil::findJSONValue(object, "Url", url)
|
|
|
|
&& JsonUtil::findJSONValue(object, "Name", filename))
|
2018-07-21 14:58:35 -05:00
|
|
|
{
|
|
|
|
// Get the FileId form the URL, which we use as the anonymized filename.
|
|
|
|
std::string decodedUrl;
|
|
|
|
Poco::URI::decode(url, decodedUrl);
|
|
|
|
const std::string obfuscatedFileId = Util::getFilenameFromURL(decodedUrl);
|
2020-11-15 14:40:21 -06:00
|
|
|
Util::mapAnonymized(obfuscatedFileId,
|
|
|
|
obfuscatedFileId); // Identity, to avoid re-anonymizing.
|
2018-07-21 14:58:35 -05:00
|
|
|
|
|
|
|
const std::string filenameOnly = Util::getFilenameFromURL(filename);
|
|
|
|
Util::mapAnonymized(filenameOnly, obfuscatedFileId);
|
2021-11-18 06:08:14 -06:00
|
|
|
object->set("Name", COOLWSD::anonymizeUrl(filename));
|
2018-07-21 14:58:35 -05:00
|
|
|
}
|
|
|
|
|
2018-06-10 21:40:32 -05:00
|
|
|
// Stringify to log.
|
|
|
|
std::ostringstream ossResponse;
|
|
|
|
object->stringify(ossResponse);
|
|
|
|
responseString = ossResponse.str();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-28 13:58:07 -06:00
|
|
|
LOG_INF(wopiLog << " uploaded " << details.size << " bytes in " << _wopiSaveDuration
|
|
|
|
<< " from [" << details.filePathAnonym << "] -> [" << details.uriAnonym
|
2020-11-15 14:40:21 -06:00
|
|
|
<< "]: " << details.httpResponseCode << ' '
|
|
|
|
<< details.httpResponseReason << ": " << responseString);
|
2018-06-10 21:40:32 -05:00
|
|
|
}
|
2016-12-23 01:53:50 -06:00
|
|
|
|
2020-11-15 14:40:21 -06:00
|
|
|
if (details.httpResponseCode == Poco::Net::HTTPResponse::HTTP_OK)
|
2016-12-23 01:53:50 -06:00
|
|
|
{
|
2020-11-29 18:11:59 -06:00
|
|
|
result.setResult(StorageBase::UploadResult::Result::OK);
|
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
|
|
|
Poco::JSON::Object::Ptr object;
|
2020-11-15 14:40:21 -06:00
|
|
|
if (JsonUtil::parseJSON(origResponseString, object))
|
2017-05-31 12:22:54 -05:00
|
|
|
{
|
2020-11-15 14:40:21 -06:00
|
|
|
const std::string lastModifiedTime
|
|
|
|
= JsonUtil::getJSONValue<std::string>(object, "LastModifiedTime");
|
2017-10-20 11:12:05 -05:00
|
|
|
LOG_TRC(wopiLog << " returns LastModifiedTime [" << lastModifiedTime << "].");
|
2022-02-06 20:15:09 -06:00
|
|
|
getFileInfo().setLastModifiedTime(lastModifiedTime);
|
2017-06-01 09:16:03 -05:00
|
|
|
|
2020-11-15 14:40:21 -06:00
|
|
|
if (details.isSaveAs || details.isRename)
|
2017-10-25 07:09:27 -05:00
|
|
|
{
|
2018-02-17 16:32:41 -06:00
|
|
|
const std::string name = JsonUtil::getJSONValue<std::string>(object, "Name");
|
2021-11-18 06:08:14 -06:00
|
|
|
LOG_TRC(wopiLog << " returns Name [" << COOLWSD::anonymizeUrl(name) << "].");
|
2017-10-25 07:09:27 -05:00
|
|
|
|
2018-02-17 16:32:41 -06:00
|
|
|
const std::string url = JsonUtil::getJSONValue<std::string>(object, "Url");
|
2021-11-18 06:08:14 -06:00
|
|
|
LOG_TRC(wopiLog << " returns Url [" << COOLWSD::anonymizeUrl(url) << "].");
|
2017-10-25 07:09:27 -05:00
|
|
|
|
2020-11-29 18:11:59 -06:00
|
|
|
result.setSaveAsResult(name, url);
|
2017-10-25 07:09:27 -05:00
|
|
|
}
|
2017-05-31 12:22:54 -05:00
|
|
|
}
|
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
|
|
|
else
|
|
|
|
{
|
2021-02-16 06:45:28 -06:00
|
|
|
LOG_ERR("Invalid or missing JSON in " << wopiLog << " HTTP_OK response.");
|
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-12-23 01:53:50 -06:00
|
|
|
}
|
2020-11-15 14:40:21 -06:00
|
|
|
else if (details.httpResponseCode == Poco::Net::HTTPResponse::HTTP_REQUEST_ENTITY_TOO_LARGE)
|
2016-12-23 01:53:50 -06:00
|
|
|
{
|
2022-05-21 08:03:43 -05:00
|
|
|
result.setResult(StorageBase::UploadResult::Result::TOO_LARGE);
|
2016-12-23 01:53:50 -06:00
|
|
|
}
|
2022-11-26 07:26:33 -06:00
|
|
|
else if (details.httpResponseCode == Poco::Net::HTTPResponse::HTTP_UNAUTHORIZED ||
|
|
|
|
details.httpResponseCode == Poco::Net::HTTPResponse::HTTP_FORBIDDEN ||
|
|
|
|
details.httpResponseCode == Poco::Net::HTTPResponse::HTTP_NOT_FOUND)
|
2017-05-31 01:21:12 -05:00
|
|
|
{
|
2022-11-26 07:26:33 -06:00
|
|
|
// The ms-wopi specs recognizes 401 and 404 for invalid token
|
|
|
|
// and file unknown/user unauthorized, respectively.
|
|
|
|
// We also handle 403 that some implementation use.
|
2020-11-29 18:11:59 -06:00
|
|
|
result.setResult(StorageBase::UploadResult::Result::UNAUTHORIZED);
|
2017-05-31 01:21:12 -05:00
|
|
|
}
|
2020-11-15 14:40:21 -06:00
|
|
|
else if (details.httpResponseCode == Poco::Net::HTTPResponse::HTTP_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
|
|
|
{
|
2020-11-29 18:11:59 -06:00
|
|
|
result.setResult(StorageBase::UploadResult::Result::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
|
|
|
Poco::JSON::Object::Ptr object;
|
2020-11-15 14:40:21 -06:00
|
|
|
if (JsonUtil::parseJSON(origResponseString, object))
|
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
|
|
|
{
|
2021-11-18 06:08:14 -06:00
|
|
|
const unsigned coolStatusCode
|
|
|
|
= JsonUtil::getJSONValue<unsigned>(object, "COOLStatusCode");
|
2021-11-19 00:39:37 -06:00
|
|
|
if (coolStatusCode == static_cast<unsigned>(COOLStatusCode::DOC_CHANGED) ||
|
2022-04-03 16:38:03 -05:00
|
|
|
JsonUtil::getJSONValue<unsigned>(object, "LOOLStatusCode") ==
|
|
|
|
static_cast<unsigned>(COOLStatusCode::DOC_CHANGED))
|
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
|
|
|
{
|
2020-11-29 18:11:59 -06:00
|
|
|
result.setResult(StorageBase::UploadResult::Result::DOC_CHANGED);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-02-16 06:45:28 -06:00
|
|
|
LOG_ERR("Invalid or missing JSON in " << wopiLog << " HTTP_CONFLICT response.");
|
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
|
|
|
}
|
|
|
|
}
|
2019-11-21 12:43:46 -06:00
|
|
|
else
|
|
|
|
{
|
2019-12-02 07:09:24 -06:00
|
|
|
// Internal server error, and other failures.
|
2022-04-07 16:39:09 -05:00
|
|
|
if (responseString.empty())
|
|
|
|
{
|
|
|
|
responseString = "No response received. Connection terminated or timed-out.";
|
|
|
|
}
|
|
|
|
|
2020-10-22 21:24:38 -05:00
|
|
|
LOG_ERR("Unexpected response to "
|
2020-11-15 14:40:21 -06:00
|
|
|
<< wopiLog << ". Cannot upload file to WOPI storage uri [" << details.uriAnonym
|
|
|
|
<< "]: " << details.httpResponseCode << ' ' << details.httpResponseReason
|
|
|
|
<< ": " << responseString);
|
2020-11-29 18:11:59 -06:00
|
|
|
result.setResult(StorageBase::UploadResult::Result::FAILED);
|
2019-11-21 12:43:46 -06:00
|
|
|
}
|
2016-11-23 06:09:54 -06:00
|
|
|
}
|
2018-07-08 21:50:09 -05:00
|
|
|
catch (const Poco::Exception& pexc)
|
2016-11-23 06:09:54 -06:00
|
|
|
{
|
2020-11-29 18:11:59 -06:00
|
|
|
LOG_ERR("Cannot upload file to WOPI storage uri ["
|
2020-11-15 14:40:21 -06:00
|
|
|
<< details.uriAnonym << "]. Error: " << pexc.displayText()
|
|
|
|
<< (pexc.nested() ? " (" + pexc.nested()->displayText() + ')' : ""));
|
2020-11-29 18:11:59 -06:00
|
|
|
result.setResult(StorageBase::UploadResult::Result::FAILED);
|
2016-11-23 06:09:54 -06:00
|
|
|
}
|
2019-12-23 07:45:38 -06:00
|
|
|
catch (const BadRequestException& exc)
|
|
|
|
{
|
2020-11-29 18:11:59 -06:00
|
|
|
LOG_ERR("Cannot upload file to WOPI storage uri [" + details.uriAnonym + "]. Error: "
|
2020-11-15 14:40:21 -06:00
|
|
|
<< exc.what());
|
2020-11-29 18:11:59 -06:00
|
|
|
result.setResult(StorageBase::UploadResult::Result::FAILED);
|
2019-12-23 07:45:38 -06:00
|
|
|
}
|
2016-11-23 06:09:54 -06:00
|
|
|
|
2020-11-29 18:11:59 -06:00
|
|
|
return result;
|
2016-03-31 01:48:34 -05:00
|
|
|
}
|
|
|
|
|
2019-11-28 10:09:58 -06:00
|
|
|
#endif // !MOBILEAPP
|
2018-09-05 07:11:05 -05:00
|
|
|
|
2016-03-31 01:48:34 -05:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|