libreoffice-online/loolwsd/DocumentBroker.cpp
Pranav Kant cfac07e3a1 loolwsd: Split DocumentBroker to its header and impl file
Change-Id: I8219300e271892f9f8fdecd8b38d9ea445cb7199
2016-03-23 22:16:35 +05:30

107 lines
3 KiB
C++

/* -*- 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/.
*/
#include <Poco/Path.h>
#include "DocumentBroker.hpp"
Poco::URI DocumentBroker::sanitizeURI(std::string uri)
{
// The URI of the document should be url-encoded.
std::string decodedUri;
Poco::URI::decode(uri, decodedUri);
auto uriPublic = Poco::URI(decodedUri);
if (uriPublic.isRelative() || uriPublic.getScheme() == "file")
{
// TODO: Validate and limit access to local paths!
uriPublic.normalize();
}
if (uriPublic.getPath().empty())
{
throw std::runtime_error("Invalid URI.");
}
return uriPublic;
}
std::string DocumentBroker::getDocKey(const Poco::URI& uri)
{
// Keep the host as part of the key to close a potential security hole.
std::string docKey;
Poco::URI::encode(uri.getHost() + uri.getPath(), "", docKey);
return docKey;
}
DocumentBroker::DocumentBroker(const Poco::URI& uriPublic,
const std::string& docKey,
const std::string& childRoot) :
_uriPublic(uriPublic),
_docKey(docKey),
_childRoot(childRoot),
_sessionsCount(0)
{
assert(!_docKey.empty());
assert(!_childRoot.empty());
Log::info("DocumentBroker [" + _uriPublic.toString() + "] created. DocKey: [" + _docKey + "]");
}
void DocumentBroker::validate(const Poco::URI& uri)
{
Log::info("Validating: " + uri.toString());
auto storage = createStorage("", "", uri);
storage->getFileInfo(uri);
}
bool DocumentBroker::load(const std::string& jailId)
{
Log::debug("Loading from URI: " + _uriPublic.toString());
std::unique_lock<std::mutex> lock(_mutex);
if (_storage)
{
// Already loaded. Nothing to do.
return true;
}
_jailId = jailId;
// The URL is the publicly visible one, not visible in the chroot jail.
// We need to map it to a jailed path and copy the file there.
// user/doc/jailId
const auto jailPath = Poco::Path(JailedDocumentRoot, jailId);
const std::string jailRoot = getJailRoot();
Log::info("jailPath: " + jailPath.toString() + ", jailRoot: " + jailRoot);
_storage = createStorage(jailRoot, jailPath.toString(), _uriPublic);
const auto localPath = _storage->loadStorageFileToLocal();
_uriJailed = Poco::URI(Poco::URI("file://"), localPath);
return true;
}
bool DocumentBroker::save()
{
Log::debug("Saving to URI: " + _uriPublic.toString());
assert(_storage);
return _storage->saveLocalFileToStorage();
}
std::string DocumentBroker::getJailRoot() const
{
assert(!_jailId.empty());
return Poco::Path(_childRoot, _jailId).toString();
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */