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 file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
2017-12-20 07:06:26 -06:00
|
|
|
#include <config.h>
|
2016-04-08 06:49:33 -05:00
|
|
|
|
2017-03-08 10:38:22 -06:00
|
|
|
#include "Storage.hpp"
|
|
|
|
|
2016-11-11 08:32:29 -06:00
|
|
|
#include <algorithm>
|
2016-03-31 01:48:34 -05:00
|
|
|
#include <cassert>
|
|
|
|
#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/DateTime.h>
|
|
|
|
#include <Poco/DateTimeParser.h>
|
|
|
|
#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>
|
|
|
|
#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>
|
2017-05-17 03:48:51 -05:00
|
|
|
#include <Poco/Net/NameValueCollection.h>
|
2016-04-19 03:17:50 -05:00
|
|
|
#include <Poco/Net/NetworkInterface.h>
|
2016-03-31 01:48:34 -05:00
|
|
|
#include <Poco/Net/SSLManager.h>
|
|
|
|
#include <Poco/StreamCopier.h>
|
2016-12-16 07:19:27 -06:00
|
|
|
#include <Poco/Timestamp.h>
|
2016-03-31 01:48:34 -05:00
|
|
|
|
2017-04-27 12:47:35 -05:00
|
|
|
// For residual Poco SSL usage.
|
|
|
|
#include <Poco/Net/AcceptCertificateHandler.h>
|
2017-11-08 02:34:37 -06:00
|
|
|
#include <Poco/Net/Context.h>
|
2017-04-27 12:47:35 -05:00
|
|
|
#include <Poco/Net/KeyConsoleHandler.h>
|
2017-11-08 02:34:37 -06:00
|
|
|
#include <Poco/Net/SSLManager.h>
|
2017-04-27 12:47:35 -05:00
|
|
|
|
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"
|
2016-07-18 06:45:36 -05:00
|
|
|
#include "LOOLWSD.hpp"
|
2017-12-20 07:06:26 -06:00
|
|
|
#include <Log.hpp>
|
|
|
|
#include <Unit.hpp>
|
|
|
|
#include <Util.hpp>
|
|
|
|
#include <common/FileUtil.hpp>
|
2018-02-17 16:32:41 -06:00
|
|
|
#include <common/JsonUtil.hpp>
|
2016-03-31 01:48:34 -05:00
|
|
|
|
2016-06-07 02:18:49 -05:00
|
|
|
bool StorageBase::FilesystemEnabled;
|
|
|
|
bool StorageBase::WopiEnabled;
|
|
|
|
Util::RegexListMatcher StorageBase::WopiHosts;
|
2016-04-16 07:13:59 -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();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t StorageBase::getFileSize(const std::string& filename)
|
|
|
|
{
|
|
|
|
return std::ifstream(filename, std::ifstream::ate | std::ifstream::binary).tellg();
|
|
|
|
}
|
|
|
|
|
2016-04-16 07:13:59 -05:00
|
|
|
void StorageBase::initialize()
|
|
|
|
{
|
|
|
|
const auto& app = Poco::Util::Application::instance();
|
2016-06-07 02:18:49 -05:00
|
|
|
FilesystemEnabled = app.config().getBool("storage.filesystem[@allow]", false);
|
2016-04-16 07:13:59 -05:00
|
|
|
|
|
|
|
// Parse the WOPI settings.
|
2016-06-07 02:18:49 -05:00
|
|
|
WopiHosts.clear();
|
|
|
|
WopiEnabled = app.config().getBool("storage.wopi[@allow]", false);
|
|
|
|
if (WopiEnabled)
|
2016-04-16 07:13:59 -05:00
|
|
|
{
|
|
|
|
for (size_t i = 0; ; ++i)
|
|
|
|
{
|
|
|
|
const std::string path = "storage.wopi.host[" + std::to_string(i) + "]";
|
2018-02-07 03:17:59 -06:00
|
|
|
const std::string host = app.config().getString(path, "");
|
2016-04-16 17:12:10 -05:00
|
|
|
if (!host.empty())
|
2016-04-16 07:13:59 -05:00
|
|
|
{
|
2016-04-16 17:12:10 -05:00
|
|
|
if (app.config().getBool(path + "[@allow]", false))
|
2016-04-16 07:13:59 -05:00
|
|
|
{
|
2016-11-03 19:26:40 -05:00
|
|
|
LOG_INF("Adding trusted WOPI host: [" << host << "].");
|
2016-06-07 02:18:49 -05:00
|
|
|
WopiHosts.allow(host);
|
2016-04-16 17:12:10 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-11-03 19:26:40 -05:00
|
|
|
LOG_INF("Adding blocked WOPI host: [" << host << "].");
|
2016-06-07 02:18:49 -05:00
|
|
|
WopiHosts.deny(host);
|
2016-04-16 07:13:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!app.config().has(path))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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;
|
|
|
|
|
2018-02-14 05:29:50 -06:00
|
|
|
// TODO: Be more strict and setup SSL key/certs for remote server and us
|
2017-04-27 12:47:35 -05:00
|
|
|
sslClientParams.verificationMode = Poco::Net::Context::VERIFY_NONE;
|
|
|
|
|
|
|
|
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);
|
|
|
|
Poco::Net::SSLManager::instance().initializeClient(consoleClientHandler, invalidClientCertHandler, sslClientContext);
|
|
|
|
#endif
|
2016-04-16 07:13:59 -05:00
|
|
|
}
|
|
|
|
|
2017-05-19 03:32:24 -05:00
|
|
|
bool isLocalhost(const std::string& targetHost)
|
2016-04-19 03:17:50 -05:00
|
|
|
{
|
2016-04-19 08:32:22 -05:00
|
|
|
std::string targetAddress;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
targetAddress = Poco::Net::DNS::resolveOne(targetHost).toString();
|
|
|
|
}
|
|
|
|
catch (const Poco::Exception& exc)
|
|
|
|
{
|
2016-12-22 15:41:05 -06:00
|
|
|
LOG_WRN("Poco::Net::DNS::resolveOne(\"" << targetHost << "\") failed: " << exc.displayText());
|
2016-04-19 08:32:22 -05:00
|
|
|
try
|
|
|
|
{
|
|
|
|
targetAddress = Poco::Net::IPAddress(targetHost).toString();
|
|
|
|
}
|
|
|
|
catch (const Poco::Exception& exc1)
|
|
|
|
{
|
2016-12-22 15:41:05 -06:00
|
|
|
LOG_WRN("Poco::Net::IPAddress(\"" << targetHost << "\") failed: " << exc1.displayText());
|
2016-04-19 08:32:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-19 03:17:50 -05:00
|
|
|
Poco::Net::NetworkInterface::NetworkInterfaceList list = Poco::Net::NetworkInterface::list(true,true);
|
2016-04-28 01:43:19 -05:00
|
|
|
for (auto& netif : list)
|
2016-04-19 03:17:50 -05:00
|
|
|
{
|
|
|
|
std::string address = netif.address().toString();
|
2016-04-26 02:34:32 -05:00
|
|
|
address = address.substr(0, address.find('%', 0));
|
2016-04-19 03:17:50 -05:00
|
|
|
if (address == targetAddress)
|
2016-04-19 08:32:22 -05:00
|
|
|
{
|
2016-11-03 19:26:40 -05:00
|
|
|
LOG_INF("WOPI host is on the same host as the WOPI client: \"" <<
|
|
|
|
targetAddress << "\". Connection is allowed.");
|
2016-04-19 03:17:50 -05:00
|
|
|
return true;
|
2016-04-19 08:32:22 -05:00
|
|
|
}
|
2016-04-19 03:17:50 -05:00
|
|
|
}
|
2016-11-03 19:26:40 -05:00
|
|
|
|
|
|
|
LOG_INF("WOPI host 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;
|
|
|
|
}
|
|
|
|
|
2016-10-14 05:09:43 -05:00
|
|
|
std::unique_ptr<StorageBase> StorageBase::create(const Poco::URI& uri, const std::string& jailRoot, const std::string& jailPath)
|
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
|
|
|
{
|
2016-11-03 19:26:40 -05:00
|
|
|
LOG_INF("Storage load 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")
|
|
|
|
{
|
2016-11-03 19:26:40 -05:00
|
|
|
LOG_INF("Public URI [" << uri.toString() << "] is a file.");
|
|
|
|
|
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");
|
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
|
|
|
throw UnauthorizedRequestException("No acceptable WOPI hosts found matching the target host in config.");
|
|
|
|
}
|
|
|
|
#endif
|
2016-06-07 02:18:49 -05:00
|
|
|
if (FilesystemEnabled)
|
2016-04-07 15:59:27 -05:00
|
|
|
{
|
2016-10-14 05:09:43 -05:00
|
|
|
return std::unique_ptr<StorageBase>(new LocalStorage(uri, jailRoot, jailPath));
|
2016-04-07 15:59:27 -05:00
|
|
|
}
|
2017-08-02 11:55:43 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// guard against attempts to escape
|
|
|
|
Poco::URI normalizedUri(uri);
|
|
|
|
normalizedUri.normalize();
|
|
|
|
|
|
|
|
std::vector<std::string> pathSegments;
|
|
|
|
normalizedUri.getPathSegments(pathSegments);
|
|
|
|
|
|
|
|
if (pathSegments.size() == 4 && pathSegments[0] == "tmp" && pathSegments[1] == "convert-to")
|
|
|
|
{
|
|
|
|
LOG_INF("Public URI [" << normalizedUri.toString() << "] is actually a convert-to tempfile.");
|
|
|
|
return std::unique_ptr<StorageBase>(new LocalStorage(normalizedUri, jailRoot, jailPath));
|
|
|
|
}
|
|
|
|
}
|
2016-04-07 15:59:27 -05:00
|
|
|
|
2016-11-03 19:26:40 -05: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
|
|
|
}
|
2016-06-07 02:18:49 -05:00
|
|
|
else if (WopiEnabled)
|
2016-04-07 15:59:27 -05:00
|
|
|
{
|
2016-11-03 19:26:40 -05:00
|
|
|
LOG_INF("Public URI [" << uri.toString() << "] considered WOPI.");
|
2016-04-26 02:34:32 -05:00
|
|
|
const auto& targetHost = uri.getHost();
|
2017-05-19 03:32:24 -05:00
|
|
|
if (WopiHosts.match(targetHost) || isLocalhost(targetHost))
|
2016-04-16 11:55:56 -05:00
|
|
|
{
|
2016-10-14 05:09:43 -05:00
|
|
|
return std::unique_ptr<StorageBase>(new WopiStorage(uri, jailRoot, jailPath));
|
2016-04-16 11:55:56 -05:00
|
|
|
}
|
2018-02-01 06:49:28 -06:00
|
|
|
LOG_ERR("No acceptable WOPI hosts found matching the target host [" << targetHost << "] 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
|
|
|
throw UnauthorizedRequestException("No acceptable WOPI hosts found matching the target host [" + targetHost + "] in config.");
|
2016-04-16 07:13:59 -05:00
|
|
|
}
|
|
|
|
|
2016-04-16 11:55:56 -05:00
|
|
|
throw BadRequestException("No Storage configured or invalid URI.");
|
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
|
|
|
{
|
2018-02-07 03:17:59 -06:00
|
|
|
const Poco::Path path = Poco::Path(_uri.getPath());
|
2017-05-12 10:42:03 -05:00
|
|
|
LOG_DBG("Getting info for local uri [" << _uri.toString() << "], path [" << path.toString() << "].");
|
2016-10-26 06:15:28 -05:00
|
|
|
|
2016-12-20 09:53:11 -06:00
|
|
|
const auto& filename = path.getFileName();
|
2018-02-07 03:17:59 -06:00
|
|
|
const Poco::File file = Poco::File(path);
|
|
|
|
const Poco::Timestamp lastModified = file.getLastModified();
|
|
|
|
const size_t size = file.getSize();
|
2016-10-26 06:15:28 -05:00
|
|
|
|
2016-12-20 09:53:11 -06:00
|
|
|
_fileInfo = FileInfo({filename, "localhost", lastModified, size});
|
2016-10-26 06:15:28 -05:00
|
|
|
|
|
|
|
// Set automatic userid and username
|
2017-07-01 15:30:14 -05:00
|
|
|
return std::unique_ptr<LocalStorage::LocalFileInfo>(new LocalFileInfo({"localhost" + std::to_string(LastLocalStorageId), "Local Host #" + std::to_string(LastLocalStorageId++)}));
|
2016-03-31 01:48:34 -05:00
|
|
|
}
|
|
|
|
|
2017-08-16 09:38:00 -05:00
|
|
|
std::string LocalStorage::loadStorageFileToLocal(const Authorization& /*auth*/)
|
2016-03-31 01:48:34 -05:00
|
|
|
{
|
|
|
|
// /chroot/jailId/user/doc/childId/file.ext
|
2018-02-07 03:17:59 -06:00
|
|
|
const std::string filename = Poco::Path(_uri.getPath()).getFileName();
|
2017-03-21 21:56:16 -05:00
|
|
|
_jailedFilePath = Poco::Path(getLocalRootPath(), filename).toString();
|
2016-11-03 19:26:40 -05:00
|
|
|
LOG_INF("Public URI [" << _uri.getPath() <<
|
2016-12-22 15:41:05 -06:00
|
|
|
"] jailed to [" << _jailedFilePath << "].");
|
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-02-07 03:17:59 -06:00
|
|
|
const std::string publicFilePath = _uri.getPath();
|
2016-09-30 08:09:53 -05:00
|
|
|
|
2017-06-16 12:34:07 -05:00
|
|
|
if (!FileUtil::checkDiskSpace(_jailedFilePath))
|
2016-11-03 19:26:40 -05:00
|
|
|
{
|
2017-06-16 12:34:07 -05:00
|
|
|
throw StorageSpaceLowException("Low disk space for " + _jailedFilePath);
|
2016-11-03 19:26:40 -05:00
|
|
|
}
|
2016-09-30 08:09:53 -05:00
|
|
|
|
2016-11-03 19:26:40 -05:00
|
|
|
LOG_INF("Linking " << publicFilePath << " to " << _jailedFilePath);
|
2016-03-31 01:48:34 -05:00
|
|
|
if (!Poco::File(_jailedFilePath).exists() && link(publicFilePath.c_str(), _jailedFilePath.c_str()) == -1)
|
|
|
|
{
|
|
|
|
// Failed
|
2018-03-23 06:16:18 -05:00
|
|
|
LOG_WRN("link(\"" << publicFilePath << "\", \"" << _jailedFilePath << "\") failed. Will copy. "
|
2018-07-11 04:58:10 -05:00
|
|
|
"Linking error: " << Util::symbolicErrno(errno) << " " << strerror(errno));
|
2016-03-31 01:48:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Fallback to copying.
|
|
|
|
if (!Poco::File(_jailedFilePath).exists())
|
|
|
|
{
|
2016-11-03 19:26:40 -05:00
|
|
|
LOG_INF("Copying " << publicFilePath << " to " << _jailedFilePath);
|
2016-03-31 01:48:34 -05:00
|
|
|
Poco::File(publicFilePath).copyTo(_jailedFilePath);
|
|
|
|
_isCopy = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const Poco::Exception& exc)
|
|
|
|
{
|
2016-12-22 15:41:05 -06:00
|
|
|
LOG_ERR("copyTo(\"" << publicFilePath << "\", \"" << _jailedFilePath << "\") failed: " << exc.displayText());
|
2016-03-31 01:48:34 -05:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2016-10-14 05:09:43 -05:00
|
|
|
_isLoaded = 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
|
2017-04-05 13:20:47 -05:00
|
|
|
if (LOOLWSD::NoCapsForKit)
|
|
|
|
return _jailedFilePath;
|
|
|
|
else
|
|
|
|
return Poco::Path(_jailPath, filename).toString();
|
2017-02-06 16:26:38 -06:00
|
|
|
#else
|
|
|
|
return _jailedFilePath;
|
|
|
|
#endif
|
2016-03-31 01:48:34 -05:00
|
|
|
}
|
|
|
|
|
2017-10-20 11:12:05 -05:00
|
|
|
StorageBase::SaveResult LocalStorage::saveLocalFileToStorage(const Authorization& /*auth*/, const std::string& /*saveAsPath*/, const std::string& /*saveAsFilename*/)
|
2016-03-31 01:48:34 -05:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2018-03-23 06:16:18 -05:00
|
|
|
LOG_TRC("Saving local file to local file storage (isCopy: " << _isCopy << ") for " << _jailedFilePath);
|
2016-03-31 01:48:34 -05:00
|
|
|
// Copy the file back.
|
|
|
|
if (_isCopy && Poco::File(_jailedFilePath).exists())
|
|
|
|
{
|
2017-05-12 10:42:03 -05:00
|
|
|
LOG_INF("Copying " << _jailedFilePath << " to " << _uri.getPath());
|
|
|
|
Poco::File(_jailedFilePath).copyTo(_uri.getPath());
|
2016-03-31 01:48:34 -05:00
|
|
|
}
|
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
|
|
|
|
_fileInfo._modifiedTime = Poco::File(_uri.getPath()).getLastModified();
|
2018-01-14 19:39:06 -06:00
|
|
|
LOG_TRC("New FileInfo modified time in storage " << _fileInfo._modifiedTime);
|
2016-03-31 01:48:34 -05:00
|
|
|
}
|
|
|
|
catch (const Poco::Exception& exc)
|
|
|
|
{
|
2017-05-12 10:42:03 -05:00
|
|
|
LOG_ERR("copyTo(\"" << _jailedFilePath << "\", \"" << _uri.getPath() <<
|
2016-11-03 19:26:40 -05:00
|
|
|
"\") failed: " << exc.displayText());
|
2017-08-22 09:19:20 -05:00
|
|
|
return StorageBase::SaveResult::FAILED;
|
2016-03-31 01:48:34 -05:00
|
|
|
}
|
|
|
|
|
2017-10-25 07:09:27 -05:00
|
|
|
return StorageBase::SaveResult(StorageBase::SaveResult::OK);
|
2016-03-31 01:48:34 -05:00
|
|
|
}
|
|
|
|
|
2018-02-17 16:32:41 -06:00
|
|
|
namespace
|
|
|
|
{
|
2016-07-18 06:45:36 -05:00
|
|
|
|
2016-12-23 00:47:04 -06:00
|
|
|
inline
|
2016-10-04 07:10:45 -05:00
|
|
|
Poco::Net::HTTPClientSession* getHTTPClientSession(const Poco::URI& uri)
|
2016-07-18 06:45:36 -05:00
|
|
|
{
|
2017-04-27 12:47:35 -05:00
|
|
|
// FIXME: if we're configured for http - we can still use an https:// wopi
|
|
|
|
// host surely; of course - the converse is not true / sensible.
|
2017-03-30 04:15:28 -05:00
|
|
|
return (LOOLWSD::isSSLEnabled() || LOOLWSD::isSSLTermination())
|
|
|
|
? new Poco::Net::HTTPSClientSession(uri.getHost(), uri.getPort(),
|
|
|
|
Poco::Net::SSLManager::instance().defaultClientContext())
|
|
|
|
: new Poco::Net::HTTPClientSession(uri.getHost(), uri.getPort());
|
2016-07-18 06:45:36 -05:00
|
|
|
}
|
|
|
|
|
2017-05-17 03:48:51 -05:00
|
|
|
void addStorageDebugCookie(Poco::Net::HTTPRequest& request)
|
|
|
|
{
|
2017-05-17 06:13:24 -05:00
|
|
|
(void) request;
|
2017-05-17 03:48:51 -05:00
|
|
|
#if ENABLE_DEBUG
|
|
|
|
if (std::getenv("LOOL_STORAGE_COOKIE"))
|
|
|
|
{
|
|
|
|
Poco::Net::NameValueCollection nvcCookies;
|
|
|
|
std::vector<std::string> cookieTokens = LOOLProtocol::tokenize(std::string(std::getenv("LOOL_STORAGE_COOKIE")), ':');
|
|
|
|
if (cookieTokens.size() == 2)
|
|
|
|
{
|
|
|
|
nvcCookies.add(cookieTokens[0], cookieTokens[1]);
|
|
|
|
request.setCookies(nvcCookies);
|
|
|
|
LOG_TRC("Added storage debug cookie [" << cookieTokens[0] << "=" << cookieTokens[1] << "].");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-02-17 16:32:41 -06:00
|
|
|
Poco::Timestamp iso8601ToTimestamp(const std::string& iso8601Time, const std::string& name)
|
2017-05-31 12:22:54 -05:00
|
|
|
{
|
|
|
|
Poco::Timestamp timestamp = Poco::Timestamp::fromEpochTime(0);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
int timeZoneDifferential;
|
|
|
|
Poco::DateTime dateTime;
|
|
|
|
Poco::DateTimeParser::parse(Poco::DateTimeFormat::ISO8601_FRAC_FORMAT, iso8601Time, dateTime, timeZoneDifferential);
|
|
|
|
timestamp = dateTime.timestamp();
|
|
|
|
}
|
|
|
|
catch (const Poco::SyntaxException& exc)
|
|
|
|
{
|
2018-02-17 16:32:41 -06:00
|
|
|
LOG_WRN(name << " [" << iso8601Time << "] is in invalid format: " << exc.displayText() <<
|
|
|
|
(exc.nested() ? " (" + exc.nested()->displayText() + ")" : "") << ". Returning " << timestamp);
|
2017-05-31 12:22:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return timestamp;
|
|
|
|
}
|
|
|
|
|
2016-07-18 06:45:36 -05:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2017-08-16 09:38:00 -05:00
|
|
|
std::unique_ptr<WopiStorage::WOPIFileInfo> WopiStorage::getWOPIFileInfo(const Authorization& auth)
|
2016-03-31 01:48:34 -05:00
|
|
|
{
|
2017-05-12 10:42:03 -05:00
|
|
|
// update the access_token to the one matching to the session
|
2017-05-19 03:32:24 -05:00
|
|
|
Poco::URI uriObject(_uri);
|
2017-08-16 09:38:00 -05:00
|
|
|
auth.authorizeURI(uriObject);
|
2017-05-12 10:42:03 -05:00
|
|
|
|
|
|
|
LOG_DBG("Getting info for wopi uri [" << uriObject.toString() << "].");
|
2016-10-14 05:09:43 -05:00
|
|
|
|
2016-12-23 01:53:50 -06:00
|
|
|
std::string resMsg;
|
2016-10-14 07:46:49 -05:00
|
|
|
const auto startTime = std::chrono::steady_clock::now();
|
2016-12-23 01:53:50 -06:00
|
|
|
std::chrono::duration<double> callDuration(0);
|
|
|
|
try
|
|
|
|
{
|
2017-05-12 10:42:03 -05:00
|
|
|
std::unique_ptr<Poco::Net::HTTPClientSession> psession(getHTTPClientSession(uriObject));
|
2016-12-23 01:53:50 -06:00
|
|
|
|
2017-05-12 10:42:03 -05:00
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, uriObject.getPathAndQuery(), Poco::Net::HTTPMessage::HTTP_1_1);
|
2017-05-14 14:17:19 -05:00
|
|
|
request.set("User-Agent", WOPI_AGENT_STRING);
|
2017-08-16 09:38:00 -05:00
|
|
|
auth.authorizeRequest(request);
|
2017-05-17 03:48:51 -05:00
|
|
|
addStorageDebugCookie(request);
|
2016-12-23 01:53:50 -06:00
|
|
|
psession->sendRequest(request);
|
2016-07-18 06:45:36 -05:00
|
|
|
|
2016-12-23 01:53:50 -06:00
|
|
|
Poco::Net::HTTPResponse response;
|
|
|
|
std::istream& rs = psession->receiveResponse(response);
|
|
|
|
callDuration = (std::chrono::steady_clock::now() - startTime);
|
2016-03-31 01:48:34 -05:00
|
|
|
|
2018-02-07 03:17:59 -06:00
|
|
|
Log::StreamLogger logger = Log::trace();
|
2017-03-11 13:42:34 -06:00
|
|
|
if (logger.enabled())
|
2016-12-23 01:53:50 -06:00
|
|
|
{
|
2017-05-12 10:42:03 -05:00
|
|
|
logger << "WOPI::CheckFileInfo header for URI [" << uriObject.toString() << "]:\n";
|
2016-12-22 15:41:05 -06:00
|
|
|
for (const auto& pair : response)
|
|
|
|
{
|
|
|
|
logger << '\t' << pair.first << ": " << pair.second << " / ";
|
|
|
|
}
|
|
|
|
|
2017-03-11 13:42:34 -06:00
|
|
|
LOG_END(logger);
|
2016-12-23 01:53:50 -06:00
|
|
|
}
|
2016-03-31 01:48:34 -05:00
|
|
|
|
2017-05-18 13:05:45 -05:00
|
|
|
if (response.getStatus() != Poco::Net::HTTPResponse::HTTP_OK)
|
|
|
|
{
|
2017-05-19 10:34:03 -05:00
|
|
|
LOG_ERR("WOPI::CheckFileInfo failed with " << response.getStatus() << ' ' << response.getReason());
|
2017-05-18 13:05:45 -05:00
|
|
|
throw StorageConnectionException("WOPI::CheckFileInfo failed");
|
|
|
|
}
|
|
|
|
|
2016-12-23 01:53:50 -06:00
|
|
|
Poco::StreamCopier::copyToString(rs, resMsg);
|
|
|
|
}
|
|
|
|
catch(const Poco::Exception& pexc)
|
2016-03-31 01:48:34 -05:00
|
|
|
{
|
2017-05-21 23:26:07 -05:00
|
|
|
LOG_ERR("Cannot get file info from WOPI storage uri [" << uriObject.toString() << "]. Error: " << pexc.displayText() <<
|
2016-12-23 01:53:50 -06:00
|
|
|
(pexc.nested() ? " (" + pexc.nested()->displayText() + ")" : ""));
|
|
|
|
throw;
|
2016-03-31 01:48:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the response.
|
|
|
|
std::string filename;
|
|
|
|
size_t size = 0;
|
2016-11-08 02:14:14 -06:00
|
|
|
std::string ownerId;
|
2016-08-29 08:41:37 -05:00
|
|
|
std::string userId;
|
|
|
|
std::string userName;
|
2017-05-28 11:20:49 -05:00
|
|
|
std::string userExtraInfo;
|
2017-09-04 08:40:04 -05:00
|
|
|
std::string watermarkText;
|
2016-10-19 09:52:53 -05:00
|
|
|
bool canWrite = false;
|
2016-11-08 07:37:28 -06:00
|
|
|
bool enableOwnerTermination = false;
|
2016-10-25 02:13:00 -05:00
|
|
|
std::string postMessageOrigin;
|
2016-11-10 06:21:39 -06:00
|
|
|
bool hidePrintOption = false;
|
|
|
|
bool hideSaveOption = false;
|
|
|
|
bool hideExportOption = false;
|
2016-12-13 05:30:43 -06:00
|
|
|
bool disablePrint = false;
|
|
|
|
bool disableExport = false;
|
|
|
|
bool disableCopy = false;
|
2017-08-29 11:59:14 -05:00
|
|
|
bool disableInactiveMessages = false;
|
2016-12-16 07:19:27 -06:00
|
|
|
std::string lastModifiedTime;
|
2017-10-03 10:06:02 -05:00
|
|
|
bool userCanNotWriteRelative = true;
|
2018-04-24 11:09:37 -05:00
|
|
|
WOPIFileInfo::TriState disableChangeTrackingRecord = WOPIFileInfo::TriState::Unset;
|
|
|
|
WOPIFileInfo::TriState disableChangeTrackingShow = WOPIFileInfo::TriState::Unset;
|
|
|
|
WOPIFileInfo::TriState hideChangeTrackingControls = WOPIFileInfo::TriState::Unset;
|
2016-10-14 07:46:49 -05:00
|
|
|
|
2016-12-22 15:41:05 -06:00
|
|
|
LOG_DBG("WOPI::CheckFileInfo returned: " << resMsg << ". Call duration: " << callDuration.count() << "s");
|
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-02-17 16:32:41 -06:00
|
|
|
if (JsonUtil::parseJSON(resMsg, object))
|
2016-03-31 01:48:34 -05:00
|
|
|
{
|
2018-02-17 16:32:41 -06:00
|
|
|
JsonUtil::findJSONValue(object, "BaseFileName", filename);
|
|
|
|
JsonUtil::findJSONValue(object, "Size", size);
|
|
|
|
JsonUtil::findJSONValue(object, "OwnerId", ownerId);
|
|
|
|
JsonUtil::findJSONValue(object, "UserId", userId);
|
|
|
|
JsonUtil::findJSONValue(object, "UserFriendlyName", userName);
|
|
|
|
JsonUtil::findJSONValue(object, "UserExtraInfo", userExtraInfo);
|
|
|
|
JsonUtil::findJSONValue(object, "WatermarkText", watermarkText);
|
|
|
|
JsonUtil::findJSONValue(object, "UserCanWrite", canWrite);
|
|
|
|
JsonUtil::findJSONValue(object, "PostMessageOrigin", postMessageOrigin);
|
|
|
|
JsonUtil::findJSONValue(object, "HidePrintOption", hidePrintOption);
|
|
|
|
JsonUtil::findJSONValue(object, "HideSaveOption", hideSaveOption);
|
|
|
|
JsonUtil::findJSONValue(object, "HideExportOption", hideExportOption);
|
|
|
|
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, "LastModifiedTime", lastModifiedTime);
|
|
|
|
JsonUtil::findJSONValue(object, "UserCanNotWriteRelative", userCanNotWriteRelative);
|
2018-04-24 11:09:37 -05:00
|
|
|
bool booleanFlag = false;
|
|
|
|
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);
|
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
|
|
|
{
|
2017-05-14 21:58:02 -05:00
|
|
|
LOG_ERR("WOPI::CheckFileInfo failed and no JSON payload returned. Access denied.");
|
2017-05-21 23:26:07 -05:00
|
|
|
throw UnauthorizedRequestException("Access denied. WOPI::CheckFileInfo failed on: " + uriObject.toString());
|
2016-12-22 15:41:05 -06:00
|
|
|
}
|
2016-03-31 01:48:34 -05:00
|
|
|
|
2018-02-17 16:32:41 -06:00
|
|
|
const Poco::Timestamp modifiedTime = iso8601ToTimestamp(lastModifiedTime, "LastModifiedTime");
|
2017-05-19 03:32:24 -05:00
|
|
|
_fileInfo = FileInfo({filename, ownerId, modifiedTime, size});
|
|
|
|
|
2018-04-24 11:09:37 -05:00
|
|
|
return std::unique_ptr<WopiStorage::WOPIFileInfo>(new WOPIFileInfo(
|
|
|
|
{userId, userName, userExtraInfo, watermarkText, canWrite,
|
|
|
|
postMessageOrigin, hidePrintOption, hideSaveOption, hideExportOption,
|
|
|
|
enableOwnerTermination, disablePrint, disableExport, disableCopy,
|
|
|
|
disableInactiveMessages, userCanNotWriteRelative,
|
|
|
|
disableChangeTrackingShow, disableChangeTrackingRecord,
|
|
|
|
hideChangeTrackingControls, callDuration}));
|
2016-03-31 01:48:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// uri format: http://server/<...>/wopi*/files/<id>/content
|
2017-08-16 09:38:00 -05:00
|
|
|
std::string WopiStorage::loadStorageFileToLocal(const Authorization& auth)
|
2016-03-31 01:48:34 -05:00
|
|
|
{
|
|
|
|
// WOPI URI to download files ends in '/contents'.
|
|
|
|
// Add it here to get the payload instead of file info.
|
|
|
|
Poco::URI uriObject(_uri);
|
2016-10-05 03:14:38 -05:00
|
|
|
uriObject.setPath(uriObject.getPath() + "/contents");
|
2017-08-16 09:38:00 -05:00
|
|
|
auth.authorizeURI(uriObject);
|
|
|
|
|
2016-12-22 15:41:05 -06:00
|
|
|
LOG_DBG("Wopi requesting: " << uriObject.toString());
|
2016-03-31 01:48:34 -05:00
|
|
|
|
2016-10-14 07:46:49 -05:00
|
|
|
const auto startTime = std::chrono::steady_clock::now();
|
2016-12-23 01:53:50 -06:00
|
|
|
try
|
|
|
|
{
|
|
|
|
std::unique_ptr<Poco::Net::HTTPClientSession> psession(getHTTPClientSession(uriObject));
|
2016-07-18 06:45:36 -05:00
|
|
|
|
2016-12-23 01:53:50 -06:00
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, uriObject.getPathAndQuery(), Poco::Net::HTTPMessage::HTTP_1_1);
|
2017-05-14 14:17:19 -05:00
|
|
|
request.set("User-Agent", WOPI_AGENT_STRING);
|
2017-08-16 09:38:00 -05:00
|
|
|
auth.authorizeRequest(request);
|
2017-05-17 03:48:51 -05:00
|
|
|
addStorageDebugCookie(request);
|
2016-12-23 01:53:50 -06:00
|
|
|
psession->sendRequest(request);
|
2016-03-31 01:48:34 -05:00
|
|
|
|
2016-12-23 01:53:50 -06:00
|
|
|
Poco::Net::HTTPResponse response;
|
|
|
|
std::istream& rs = psession->receiveResponse(response);
|
|
|
|
const std::chrono::duration<double> diff = (std::chrono::steady_clock::now() - startTime);
|
|
|
|
_wopiLoadDuration += diff;
|
2016-03-31 01:48:34 -05:00
|
|
|
|
2018-02-07 03:17:59 -06:00
|
|
|
Log::StreamLogger logger = Log::trace();
|
2017-03-11 13:42:34 -06:00
|
|
|
if (logger.enabled())
|
2016-12-23 01:53:50 -06:00
|
|
|
{
|
2016-12-22 15:41:05 -06:00
|
|
|
logger << "WOPI::GetFile header for URI [" << uriObject.toString() << "]:\n";
|
|
|
|
for (const auto& pair : response)
|
|
|
|
{
|
|
|
|
logger << '\t' << pair.first << ": " << pair.second << " / ";
|
|
|
|
}
|
|
|
|
|
2017-03-11 13:42:34 -06:00
|
|
|
LOG_END(logger);
|
2016-12-23 01:53:50 -06:00
|
|
|
}
|
2016-03-31 01:48:34 -05:00
|
|
|
|
2017-05-18 13:05:45 -05:00
|
|
|
if (response.getStatus() != Poco::Net::HTTPResponse::HTTP_OK)
|
|
|
|
{
|
2017-05-19 10:34:03 -05:00
|
|
|
LOG_ERR("WOPI::GetFile failed with " << response.getStatus() << ' ' << response.getReason());
|
2017-05-18 13:05:45 -05:00
|
|
|
throw StorageConnectionException("WOPI::GetFile failed");
|
|
|
|
}
|
|
|
|
else // Successful
|
|
|
|
{
|
|
|
|
_jailedFilePath = Poco::Path(getLocalRootPath(), _fileInfo._filename).toString();
|
|
|
|
std::ofstream ofs(_jailedFilePath);
|
|
|
|
std::copy(std::istreambuf_iterator<char>(rs),
|
|
|
|
std::istreambuf_iterator<char>(),
|
|
|
|
std::ostreambuf_iterator<char>(ofs));
|
2018-01-18 09:20:35 -06:00
|
|
|
ofs.close();
|
2017-05-18 13:05:45 -05:00
|
|
|
LOG_INF("WOPI::GetFile downloaded " << getFileSize(_jailedFilePath) << " bytes from [" << uriObject.toString() <<
|
|
|
|
"] -> " << _jailedFilePath << " in " << diff.count() << "s");
|
|
|
|
|
|
|
|
_isLoaded = true;
|
|
|
|
// Now return the jailed path.
|
|
|
|
return Poco::Path(_jailPath, _fileInfo._filename).toString();
|
|
|
|
}
|
2016-12-23 01:53:50 -06:00
|
|
|
}
|
|
|
|
catch(const Poco::Exception& pexc)
|
|
|
|
{
|
|
|
|
LOG_ERR("Cannot load document from WOPI storage uri [" + uriObject.toString() + "]. Error: " << pexc.displayText() <<
|
|
|
|
(pexc.nested() ? " (" + pexc.nested()->displayText() + ")" : ""));
|
|
|
|
throw;
|
|
|
|
}
|
2016-03-31 01:48:34 -05:00
|
|
|
|
2017-05-18 13:05:45 -05:00
|
|
|
return "";
|
2016-03-31 01:48:34 -05:00
|
|
|
}
|
|
|
|
|
2017-10-20 11:12:05 -05:00
|
|
|
StorageBase::SaveResult WopiStorage::saveLocalFileToStorage(const Authorization& auth, const std::string& saveAsPath, const std::string& saveAsFilename)
|
2016-03-31 01:48:34 -05:00
|
|
|
{
|
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
|
|
|
|
2017-10-24 04:31:39 -05:00
|
|
|
const bool isSaveAs = !saveAsPath.empty() && !saveAsFilename.empty();
|
2017-11-06 05:44:37 -06:00
|
|
|
const std::string filePath(isSaveAs? saveAsPath: _jailedFilePath);
|
|
|
|
|
2018-02-07 03:17:59 -06:00
|
|
|
const size_t size = getFileSize(filePath);
|
2017-10-20 11:12:05 -05:00
|
|
|
|
2017-05-12 10:42:03 -05:00
|
|
|
Poco::URI uriObject(_uri);
|
2017-10-20 11:12:05 -05:00
|
|
|
uriObject.setPath(isSaveAs? uriObject.getPath(): uriObject.getPath() + "/contents");
|
2017-08-16 09:38:00 -05:00
|
|
|
auth.authorizeURI(uriObject);
|
2017-05-12 10:42:03 -05:00
|
|
|
|
2017-11-06 05:44:37 -06:00
|
|
|
LOG_INF("Uploading URI via WOPI [" << uriObject.toString() << "] from [" << filePath + "].");
|
2016-03-31 01:48:34 -05:00
|
|
|
|
|
|
|
std::ostringstream oss;
|
2017-10-25 07:09:27 -05:00
|
|
|
StorageBase::SaveResult saveResult(StorageBase::SaveResult::FAILED);
|
2016-12-23 01:53:50 -06:00
|
|
|
try
|
2016-11-23 06:09:54 -06:00
|
|
|
{
|
2016-12-23 01:53:50 -06:00
|
|
|
std::unique_ptr<Poco::Net::HTTPClientSession> psession(getHTTPClientSession(uriObject));
|
|
|
|
|
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, uriObject.getPathAndQuery(), Poco::Net::HTTPMessage::HTTP_1_1);
|
2017-10-20 11:12:05 -05:00
|
|
|
request.set("User-Agent", WOPI_AGENT_STRING);
|
2017-08-16 09:38:00 -05:00
|
|
|
auth.authorizeRequest(request);
|
2017-10-20 11:12:05 -05:00
|
|
|
|
|
|
|
if (!isSaveAs)
|
|
|
|
{
|
|
|
|
// normal save
|
|
|
|
request.set("X-WOPI-Override", "PUT");
|
|
|
|
request.set("X-LOOL-WOPI-IsModifiedByUser", _isUserModified? "true": "false");
|
|
|
|
request.set("X-LOOL-WOPI-IsAutosave", _isAutosave? "true": "false");
|
|
|
|
|
|
|
|
if (!_forceSave)
|
|
|
|
{
|
|
|
|
// Request WOPI host to not overwrite if timestamps mismatch
|
|
|
|
request.set("X-LOOL-WOPI-Timestamp",
|
|
|
|
Poco::DateTimeFormatter::format(Poco::DateTime(_fileInfo._modifiedTime),
|
|
|
|
Poco::DateTimeFormat::ISO8601_FRAC_FORMAT));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2017-06-01 07:56:54 -05:00
|
|
|
{
|
2017-10-20 11:12:05 -05:00
|
|
|
// save as
|
|
|
|
request.set("X-WOPI-Override", "PUT_RELATIVE");
|
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
|
|
|
|
std::string suggestedTarget = "." + Poco::Path(saveAsFilename).getExtension();
|
|
|
|
|
|
|
|
iconv_t cd = iconv_open("UTF-7", "UTF-8");
|
|
|
|
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];
|
|
|
|
size_t in_left = input.size();
|
|
|
|
char* out = &buffer[0];
|
|
|
|
size_t out_left = buffer.size();
|
|
|
|
|
|
|
|
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 << "'.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
request.set("X-WOPI-SuggestedTarget", suggestedTarget);
|
|
|
|
|
2017-10-20 11:12:05 -05:00
|
|
|
request.set("X-WOPI-Size", std::to_string(size));
|
2017-06-01 07:56:54 -05:00
|
|
|
}
|
2017-06-01 09:16:03 -05:00
|
|
|
|
2016-12-23 01:53:50 -06:00
|
|
|
request.setContentType("application/octet-stream");
|
|
|
|
request.setContentLength(size);
|
2017-05-17 03:48:51 -05:00
|
|
|
addStorageDebugCookie(request);
|
2016-12-23 01:53:50 -06:00
|
|
|
std::ostream& os = psession->sendRequest(request);
|
2017-10-20 11:12:05 -05:00
|
|
|
|
|
|
|
std::ifstream ifs(filePath);
|
2016-12-23 01:53:50 -06:00
|
|
|
Poco::StreamCopier::copyStream(ifs, os);
|
|
|
|
|
|
|
|
Poco::Net::HTTPResponse response;
|
|
|
|
std::istream& rs = psession->receiveResponse(response);
|
|
|
|
Poco::StreamCopier::copyStream(rs, oss);
|
2017-10-20 11:12:05 -05:00
|
|
|
|
2018-02-17 16:32:41 -06:00
|
|
|
const std::string wopiLog(isSaveAs ? "WOPI::PutRelativeFile" : "WOPI::PutFile");
|
2017-10-20 11:12:05 -05:00
|
|
|
LOG_INF(wopiLog << " response: " << oss.str());
|
|
|
|
LOG_INF(wopiLog << " uploaded " << size << " bytes from [" << filePath <<
|
2016-12-23 01:53:50 -06:00
|
|
|
"] -> [" << uriObject.toString() << "]: " <<
|
|
|
|
response.getStatus() << " " << response.getReason());
|
|
|
|
|
|
|
|
if (response.getStatus() == Poco::Net::HTTPResponse::HTTP_OK)
|
|
|
|
{
|
2017-10-25 07:09:27 -05:00
|
|
|
saveResult.setResult(StorageBase::SaveResult::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;
|
2018-02-17 16:32:41 -06:00
|
|
|
if (JsonUtil::parseJSON(oss.str(), object))
|
2017-05-31 12:22:54 -05:00
|
|
|
{
|
2018-02-17 16:32:41 -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 << "].");
|
2018-02-17 16:32:41 -06:00
|
|
|
_fileInfo._modifiedTime = iso8601ToTimestamp(lastModifiedTime, "LastModifiedTime");
|
2017-06-01 09:16:03 -05:00
|
|
|
|
2017-10-25 07:09:27 -05:00
|
|
|
if (isSaveAs)
|
|
|
|
{
|
2018-02-17 16:32:41 -06:00
|
|
|
const std::string name = JsonUtil::getJSONValue<std::string>(object, "Name");
|
2017-10-25 07:09:27 -05:00
|
|
|
LOG_TRC(wopiLog << " returns Name [" << name << "].");
|
|
|
|
|
2018-02-17 16:32:41 -06:00
|
|
|
const std::string url = JsonUtil::getJSONValue<std::string>(object, "Url");
|
2017-10-25 07:09:27 -05:00
|
|
|
LOG_TRC(wopiLog << " returns Url [" << url << "].");
|
|
|
|
|
|
|
|
saveResult.setSaveAsResult(name, url);
|
|
|
|
}
|
|
|
|
|
2017-06-01 09:16:03 -05:00
|
|
|
// Reset the force save flag now, if any, since we are done saving
|
|
|
|
// Next saves shouldn't be saved forcefully unless commanded
|
|
|
|
_forceSave = false;
|
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
|
|
|
|
{
|
2017-10-25 07:09:27 -05:00
|
|
|
LOG_WRN("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
|
|
|
}
|
|
|
|
else if (response.getStatus() == Poco::Net::HTTPResponse::HTTP_REQUESTENTITYTOOLARGE)
|
|
|
|
{
|
2017-10-25 07:09:27 -05:00
|
|
|
saveResult.setResult(StorageBase::SaveResult::DISKFULL);
|
2016-12-23 01:53:50 -06:00
|
|
|
}
|
2017-05-31 01:21:12 -05:00
|
|
|
else if (response.getStatus() == Poco::Net::HTTPResponse::HTTP_UNAUTHORIZED)
|
|
|
|
{
|
2017-10-25 07:09:27 -05:00
|
|
|
saveResult.setResult(StorageBase::SaveResult::UNAUTHORIZED);
|
2017-05-31 01:21:12 -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 if (response.getStatus() == Poco::Net::HTTPResponse::HTTP_CONFLICT)
|
|
|
|
{
|
2017-10-25 07:09:27 -05:00
|
|
|
saveResult.setResult(StorageBase::SaveResult::CONFLICT);
|
Inform all clients when document changed behind our back
Introduce a new header X-LOOL-WOPI-Timestamp
This is a WOPI header extension to detect any external document change. For
example, when the file that is already opened by LOOL is changed
in storage.
The WOPI host sends LastModifiedTime field (in WOPI specs) as part
of the CheckFileInfo response. It also expects wsd to send the
same timestamp in X-LOOL-WOPI-Timestamp header during WOPI::PutFile. If
this header is present, then WOPI host checks, before saving the
document, if the timestamp in the header is equal to the timestamp of
the file in its storage. Only upon meeting this condition, it saves the
file back to storage, otherwise it informs us about some change
to the document.
We are supposed to inform the user accordingly. If user is okay
with over-writing the document, then we can omit sending
X-LOOL-WOPI-Timestamp header, in which case, no check as mentioned above
would be performed while saving the file and document will be
overwritten.
Also, use a separate list of LOOL status codes to denote such a change.
It would be wrong to use HTTP_CONFLICT status code for denoting doc
changed in storage scenario. WOPI specs reserves that for WOPI locks
which are not yet implemented. Better to use a separate LOOL specific
status codes synced across WOPI hosts and us to denote scenario that we
expect and are not covered in WOPI specs.
Change-Id: I61539dfae672bc104b8008f030f96e90f9ff48a5
2017-05-31 12:48:33 -05:00
|
|
|
Poco::JSON::Object::Ptr object;
|
2018-02-17 16:32:41 -06:00
|
|
|
if (JsonUtil::parseJSON(oss.str(), 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
|
|
|
{
|
2018-02-17 16:32:41 -06:00
|
|
|
const unsigned loolStatusCode = JsonUtil::getJSONValue<unsigned>(object, "LOOLStatusCode");
|
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
|
|
|
if (loolStatusCode == static_cast<unsigned>(LOOLStatusCode::DOC_CHANGED))
|
|
|
|
{
|
2017-10-25 07:09:27 -05:00
|
|
|
saveResult.setResult(StorageBase::SaveResult::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
|
|
|
|
{
|
2017-10-25 07:09:27 -05:00
|
|
|
LOG_WRN("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
|
|
|
}
|
|
|
|
}
|
2016-11-23 06:09:54 -06:00
|
|
|
}
|
2016-12-23 01:53:50 -06:00
|
|
|
catch(const Poco::Exception& pexc)
|
2016-11-23 06:09:54 -06:00
|
|
|
{
|
2016-12-23 01:53:50 -06:00
|
|
|
LOG_ERR("Cannot save file to WOPI storage uri [" + uriObject.toString() + "]. Error: " << pexc.displayText() <<
|
|
|
|
(pexc.nested() ? " (" + pexc.nested()->displayText() + ")" : ""));
|
2017-10-25 07:09:27 -05:00
|
|
|
saveResult.setResult(StorageBase::SaveResult::FAILED);
|
2016-11-23 06:09:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return saveResult;
|
2016-03-31 01:48:34 -05:00
|
|
|
}
|
|
|
|
|
2017-08-16 09:38:00 -05:00
|
|
|
std::string WebDAVStorage::loadStorageFileToLocal(const Authorization& /*auth*/)
|
2016-03-31 01:48:34 -05:00
|
|
|
{
|
|
|
|
// TODO: implement webdav GET.
|
2016-10-14 05:09:43 -05:00
|
|
|
_isLoaded = true;
|
2016-10-14 04:28:19 -05:00
|
|
|
return _uri.toString();
|
2016-03-31 01:48:34 -05:00
|
|
|
}
|
|
|
|
|
2017-10-20 11:12:05 -05:00
|
|
|
StorageBase::SaveResult WebDAVStorage::saveLocalFileToStorage(const Authorization& /*auth*/, const std::string& /*saveAsPath*/, const std::string& /*saveAsFilename*/)
|
2016-03-31 01:48:34 -05:00
|
|
|
{
|
|
|
|
// TODO: implement webdav PUT.
|
2017-10-25 07:09:27 -05:00
|
|
|
return StorageBase::SaveResult(StorageBase::SaveResult::OK);
|
2016-03-31 01:48:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|