2016-02-19 15:19:15 -06: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/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Storage abstraction.
|
|
|
|
#ifndef INCLUDED_STORAGE_HPP
|
|
|
|
#define INCLUDED_STORAGE_HPP
|
|
|
|
|
|
|
|
#include <string>
|
2016-03-05 09:21:25 -06:00
|
|
|
#include <fstream>
|
2016-02-19 15:19:15 -06:00
|
|
|
|
2016-03-03 20:05:30 -06:00
|
|
|
#include <Poco/Net/HTTPResponse.h>
|
2016-03-11 12:45:13 -06:00
|
|
|
#include <Poco/StreamCopier.h>
|
2016-03-03 20:05:30 -06:00
|
|
|
|
2016-03-05 09:27:30 -06:00
|
|
|
#include "Common.hpp"
|
2016-02-19 15:19:15 -06:00
|
|
|
#include "Auth.hpp"
|
|
|
|
#include "Util.hpp"
|
|
|
|
|
|
|
|
/// Base class of all Storage abstractions.
|
|
|
|
class StorageBase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2016-03-05 09:27:30 -06:00
|
|
|
/// localStorePath the absolute root path of the chroot.
|
|
|
|
/// jailPath the path within the jail that the child uses.
|
|
|
|
StorageBase(const std::string& localStorePath,
|
2016-03-09 17:36:25 -06:00
|
|
|
const std::string& jailPath,
|
|
|
|
const std::string& uri) :
|
2016-03-05 09:27:30 -06:00
|
|
|
_localStorePath(localStorePath),
|
2016-03-09 17:36:25 -06:00
|
|
|
_jailPath(jailPath),
|
|
|
|
_uri(uri)
|
2016-03-03 20:05:30 -06:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-03-09 17:36:25 -06:00
|
|
|
std::string getLocalRootPath() const
|
2016-03-08 18:21:16 -06:00
|
|
|
{
|
|
|
|
auto localPath = _jailPath;
|
|
|
|
if (localPath[0] == '/')
|
|
|
|
{
|
|
|
|
// Remove the leading /
|
|
|
|
localPath.erase(0, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// /chroot/jailId/user/doc/childId
|
|
|
|
const auto rootPath = Poco::Path(_localStorePath, localPath);
|
|
|
|
Poco::File(rootPath).createDirectories();
|
|
|
|
|
|
|
|
return rootPath.toString();
|
|
|
|
}
|
|
|
|
|
2016-03-09 17:36:25 -06:00
|
|
|
const std::string& getUri() const { return _uri; }
|
|
|
|
|
2016-03-03 20:05:30 -06:00
|
|
|
/// Returns a local file path given a URI or ID.
|
2016-02-19 15:19:15 -06:00
|
|
|
/// If necessary copies the file locally first.
|
2016-03-10 21:01:34 -06:00
|
|
|
virtual std::string loadStorageFileToLocal() = 0;
|
2016-02-19 15:19:15 -06:00
|
|
|
|
2016-03-03 20:05:30 -06:00
|
|
|
/// Writes the contents of the file back to the source.
|
2016-03-10 21:01:34 -06:00
|
|
|
virtual bool saveLocalFileToStorage() = 0;
|
2016-02-19 15:19:15 -06:00
|
|
|
|
2016-03-11 12:45:13 -06:00
|
|
|
static
|
|
|
|
size_t getFileSize(const std::string& filename)
|
|
|
|
{
|
|
|
|
return std::ifstream(filename, std::ifstream::ate | std::ifstream::binary).tellg();
|
|
|
|
}
|
|
|
|
|
2016-03-03 20:05:30 -06:00
|
|
|
protected:
|
|
|
|
const std::string _localStorePath;
|
2016-03-05 09:27:30 -06:00
|
|
|
const std::string _jailPath;
|
2016-03-09 17:36:25 -06:00
|
|
|
const std::string _uri;
|
|
|
|
std::string _jailedFilePath;
|
2016-03-11 12:45:13 -06:00
|
|
|
std::string _filename;
|
2016-02-19 15:19:15 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Trivial implementation of local storage that does not need do anything.
|
|
|
|
class LocalStorage : public StorageBase
|
|
|
|
{
|
|
|
|
public:
|
2016-03-05 09:27:30 -06:00
|
|
|
LocalStorage(const std::string& localStorePath,
|
2016-03-09 17:36:25 -06:00
|
|
|
const std::string& jailPath,
|
|
|
|
const std::string& uri) :
|
|
|
|
StorageBase(localStorePath, jailPath, uri),
|
|
|
|
_isCopy(false)
|
2016-03-05 09:27:30 -06:00
|
|
|
{
|
|
|
|
}
|
2016-02-19 15:19:15 -06:00
|
|
|
|
2016-03-10 21:01:34 -06:00
|
|
|
std::string loadStorageFileToLocal() override
|
2016-02-19 15:19:15 -06:00
|
|
|
{
|
2016-03-09 17:36:25 -06:00
|
|
|
const auto rootPath = getLocalRootPath();
|
2016-03-05 09:27:30 -06:00
|
|
|
|
|
|
|
// /chroot/jailId/user/doc/childId/file.ext
|
2016-03-09 17:36:25 -06:00
|
|
|
const auto filename = Poco::Path(_uri).getFileName();
|
|
|
|
_jailedFilePath = Poco::Path(rootPath, filename).toString();
|
2016-03-05 09:27:30 -06:00
|
|
|
|
2016-03-09 17:36:25 -06:00
|
|
|
Log::info("Public URI [" + _uri +
|
|
|
|
"] jailed to [" + _jailedFilePath + "].");
|
2016-03-05 09:27:30 -06:00
|
|
|
|
2016-03-09 17:36:25 -06:00
|
|
|
const auto publicFilePath = _uri;
|
|
|
|
Log::info("Linking " + publicFilePath + " to " + _jailedFilePath);
|
|
|
|
if (!Poco::File(_jailedFilePath).exists() && link(publicFilePath.c_str(), _jailedFilePath.c_str()) == -1)
|
2016-03-05 09:27:30 -06:00
|
|
|
{
|
|
|
|
// Failed
|
2016-03-09 17:36:25 -06:00
|
|
|
Log::error("link(\"" + publicFilePath + "\", \"" + _jailedFilePath + "\") failed.");
|
2016-03-05 09:27:30 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Fallback to copying.
|
2016-03-09 17:36:25 -06:00
|
|
|
if (!Poco::File(_jailedFilePath).exists())
|
2016-03-05 09:27:30 -06:00
|
|
|
{
|
2016-03-09 17:36:25 -06:00
|
|
|
Log::info("Copying " + publicFilePath + " to " + _jailedFilePath);
|
|
|
|
Poco::File(publicFilePath).copyTo(_jailedFilePath);
|
|
|
|
_isCopy = true;
|
2016-03-05 09:27:30 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const Poco::Exception& exc)
|
|
|
|
{
|
2016-03-09 17:36:25 -06:00
|
|
|
Log::error("copyTo(\"" + publicFilePath + "\", \"" + _jailedFilePath + "\") failed: " + exc.displayText());
|
2016-03-05 09:27:30 -06:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now return the jailed path.
|
|
|
|
return Poco::Path(_jailPath, filename).toString();
|
2016-02-19 15:19:15 -06:00
|
|
|
}
|
|
|
|
|
2016-03-10 21:01:34 -06:00
|
|
|
bool saveLocalFileToStorage() override
|
2016-02-19 15:19:15 -06:00
|
|
|
{
|
2016-03-09 17:36:25 -06:00
|
|
|
try
|
|
|
|
{
|
|
|
|
// Copy the file back.
|
|
|
|
if (_isCopy && Poco::File(_jailedFilePath).exists())
|
|
|
|
{
|
|
|
|
Log::info("Copying " + _jailedFilePath + " to " + _uri);
|
|
|
|
Poco::File(_jailedFilePath).copyTo(_uri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const Poco::Exception& exc)
|
|
|
|
{
|
|
|
|
Log::error("copyTo(\"" + _jailedFilePath + "\", \"" + _uri + "\") failed: " + exc.displayText());
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2016-02-19 15:19:15 -06:00
|
|
|
}
|
2016-03-09 17:36:25 -06:00
|
|
|
|
|
|
|
private:
|
|
|
|
/// True if the jailed file is not linked but copied.
|
|
|
|
bool _isCopy;
|
2016-02-19 15:19:15 -06:00
|
|
|
};
|
|
|
|
|
2016-03-03 20:05:30 -06:00
|
|
|
class WopiStorage : public StorageBase
|
|
|
|
{
|
|
|
|
public:
|
2016-03-05 09:27:30 -06:00
|
|
|
WopiStorage(const std::string& localStorePath,
|
2016-03-09 17:36:25 -06:00
|
|
|
const std::string& jailPath,
|
|
|
|
const std::string& uri) :
|
|
|
|
StorageBase(localStorePath, jailPath, uri)
|
2016-03-03 20:05:30 -06:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/// uri format: http://server/<...>/wopi*/files/<id>/content
|
2016-03-10 21:01:34 -06:00
|
|
|
std::string loadStorageFileToLocal() override
|
2016-03-03 20:05:30 -06:00
|
|
|
{
|
2016-03-09 17:36:25 -06:00
|
|
|
Log::info("Downloading URI [" + _uri + "].");
|
2016-03-08 18:50:35 -06:00
|
|
|
|
2016-03-09 17:36:25 -06:00
|
|
|
Poco::URI uriObject(_uri);
|
2016-03-03 20:05:30 -06:00
|
|
|
Poco::Net::HTTPClientSession session(uriObject.getHost(), uriObject.getPort());
|
2016-03-08 18:50:35 -06:00
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, uriObject.getPathAndQuery(), Poco::Net::HTTPMessage::HTTP_1_1);
|
2016-03-08 19:35:16 -06:00
|
|
|
request.set("User-Agent", "LOOLWSD WOPI Agent");
|
2016-03-03 20:05:30 -06:00
|
|
|
session.sendRequest(request);
|
2016-03-08 19:35:16 -06:00
|
|
|
|
2016-03-08 18:50:35 -06:00
|
|
|
Poco::Net::HTTPResponse response;
|
2016-03-03 20:05:30 -06:00
|
|
|
std::istream& rs = session.receiveResponse(response);
|
2016-03-08 19:35:16 -06:00
|
|
|
|
2016-03-11 12:45:13 -06:00
|
|
|
auto logger = Log::trace();
|
2016-03-09 17:36:25 -06:00
|
|
|
logger << "WOPI::GetFile header for URI [" << _uri << "]:\n";
|
2016-03-08 19:35:16 -06:00
|
|
|
for (auto& pair : response)
|
|
|
|
{
|
|
|
|
logger << '\t' + pair.first + ": " + pair.second << '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
logger << Log::end;
|
2016-03-03 20:05:30 -06:00
|
|
|
|
|
|
|
//TODO: Get proper filename.
|
2016-03-11 12:45:13 -06:00
|
|
|
_filename = "filename";
|
|
|
|
_jailedFilePath = Poco::Path(getLocalRootPath(), _filename).toString();
|
2016-03-09 17:36:25 -06:00
|
|
|
std::ofstream ofs(_jailedFilePath);
|
2016-03-03 20:05:30 -06:00
|
|
|
std::copy(std::istreambuf_iterator<char>(rs),
|
|
|
|
std::istreambuf_iterator<char>(),
|
|
|
|
std::ostreambuf_iterator<char>(ofs));
|
2016-03-11 12:45:13 -06:00
|
|
|
const auto size = getFileSize(_jailedFilePath);
|
|
|
|
|
|
|
|
Log::info() << "WOPI::GetFile downloaded " << size << " bytes from [" << _uri
|
|
|
|
<< "] -> " << _jailedFilePath << ": "
|
|
|
|
<< response.getStatus() << " " << response.getReason() << Log::end;
|
2016-03-08 18:50:35 -06:00
|
|
|
|
|
|
|
// Now return the jailed path.
|
2016-03-11 12:45:13 -06:00
|
|
|
return Poco::Path(_jailPath, _filename).toString();
|
2016-03-03 20:05:30 -06:00
|
|
|
}
|
|
|
|
|
2016-03-10 21:01:34 -06:00
|
|
|
bool saveLocalFileToStorage() override
|
2016-03-03 20:05:30 -06:00
|
|
|
{
|
2016-03-11 12:45:13 -06:00
|
|
|
Log::info("Uploading URI [" + _uri + "] from [" + _jailedFilePath + "].");
|
|
|
|
const auto size = getFileSize(_jailedFilePath);
|
|
|
|
|
2016-03-09 17:36:25 -06:00
|
|
|
Poco::URI uriObject(_uri);
|
2016-03-03 20:05:30 -06:00
|
|
|
Poco::Net::HTTPClientSession session(uriObject.getHost(), uriObject.getPort());
|
2016-03-11 12:45:13 -06:00
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, uriObject.getPathAndQuery(), Poco::Net::HTTPMessage::HTTP_1_1);
|
|
|
|
request.set("X-WOPIOverride", "PUT");
|
|
|
|
request.setContentType("application/octet-stream");
|
|
|
|
request.setContentLength(size);
|
2016-03-03 20:05:30 -06:00
|
|
|
|
2016-03-11 12:45:13 -06:00
|
|
|
std::ostream& os = session.sendRequest(request);
|
2016-03-09 17:36:25 -06:00
|
|
|
std::ifstream ifs(_jailedFilePath);
|
2016-03-11 12:45:13 -06:00
|
|
|
Poco::StreamCopier::copyStream(ifs, os);
|
2016-03-03 20:05:30 -06:00
|
|
|
|
|
|
|
Poco::Net::HTTPResponse response;
|
2016-03-11 12:45:13 -06:00
|
|
|
std::istream& rs = session.receiveResponse(response);
|
|
|
|
std::ostringstream oss;
|
|
|
|
Poco::StreamCopier::copyStream(rs, oss);
|
|
|
|
|
|
|
|
Log::info("WOPI::PutFile response: " + oss.str());
|
|
|
|
const auto success = (response.getStatus() == Poco::Net::HTTPResponse::HTTP_OK);
|
|
|
|
Log::info() << "WOPI::PutFile uploaded " << size << " bytes from [" << _jailedFilePath << "]:"
|
|
|
|
<< "] -> [" << _uri << "]: "
|
|
|
|
<< response.getStatus() << " " << response.getReason() << Log::end;
|
2016-03-03 20:05:30 -06:00
|
|
|
|
2016-03-11 12:45:13 -06:00
|
|
|
return success;
|
2016-03-03 20:05:30 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-02-19 15:19:15 -06:00
|
|
|
class WebDAVStorage : public StorageBase
|
|
|
|
{
|
|
|
|
public:
|
2016-03-03 20:05:30 -06:00
|
|
|
WebDAVStorage(const std::string& localStorePath,
|
2016-03-05 09:27:30 -06:00
|
|
|
const std::string& jailPath,
|
2016-03-09 17:36:25 -06:00
|
|
|
const std::string& uri,
|
2016-03-03 20:05:30 -06:00
|
|
|
std::unique_ptr<AuthBase> authAgent) :
|
2016-03-09 17:36:25 -06:00
|
|
|
StorageBase(localStorePath, jailPath, uri),
|
2016-02-19 15:19:15 -06:00
|
|
|
_authAgent(std::move(authAgent))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-03-10 21:01:34 -06:00
|
|
|
std::string loadStorageFileToLocal() override
|
2016-02-19 15:19:15 -06:00
|
|
|
{
|
|
|
|
// TODO: implement webdav GET.
|
2016-03-09 17:36:25 -06:00
|
|
|
return _uri;
|
2016-02-19 15:19:15 -06:00
|
|
|
}
|
|
|
|
|
2016-03-10 21:01:34 -06:00
|
|
|
bool saveLocalFileToStorage() override
|
2016-02-19 15:19:15 -06:00
|
|
|
{
|
|
|
|
// TODO: implement webdav PUT.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::unique_ptr<AuthBase> _authAgent;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|