2023-11-06 05:04:27 -06:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
2024-03-06 05:18:45 -06:00
|
|
|
* Copyright the Collabora Online contributors.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*
|
2023-11-06 05:04:27 -06:00
|
|
|
* 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 <config.h>
|
|
|
|
|
2024-03-05 17:40:47 -06:00
|
|
|
#include "WopiProxy.hpp"
|
2023-11-06 05:04:27 -06:00
|
|
|
|
|
|
|
#include "FileUtil.hpp"
|
|
|
|
#include "HttpHelper.hpp"
|
|
|
|
#include "HttpRequest.hpp"
|
|
|
|
#include <COOLWSD.hpp>
|
|
|
|
#include <Exceptions.hpp>
|
|
|
|
#include <Log.hpp>
|
|
|
|
#include <Util.hpp>
|
2024-03-06 07:50:24 -06:00
|
|
|
#include <common/JsonUtil.hpp>
|
|
|
|
#include <wopi/StorageConnectionManager.hpp>
|
|
|
|
#include <wopi/WopiStorage.hpp>
|
2023-11-06 05:04:27 -06:00
|
|
|
|
2024-03-05 19:22:31 -06:00
|
|
|
void WopiProxy::handleRequest([[maybe_unused]] const std::shared_ptr<TerminatingPoll>& poll,
|
|
|
|
SocketDisposition& disposition)
|
2023-11-06 05:04:27 -06:00
|
|
|
{
|
|
|
|
std::string url = _requestDetails.getDocumentURI();
|
2024-02-21 22:12:57 -06:00
|
|
|
if (url.starts_with("/wasm/"))
|
2023-11-06 05:04:27 -06:00
|
|
|
{
|
|
|
|
url = url.substr(6);
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_INF("URL [" << url << "] for WS Request.");
|
|
|
|
const auto uriPublic = RequestDetails::sanitizeURI(url);
|
|
|
|
const auto docKey = RequestDetails::getDocKey(uriPublic);
|
|
|
|
const std::string fileId = Util::getFilenameFromURL(docKey);
|
|
|
|
Util::mapAnonymized(fileId, fileId); // Identity mapping, since fileId is already obfuscated
|
|
|
|
|
|
|
|
LOG_INF("Starting GET request handler for session [" << _id << "] on url ["
|
|
|
|
<< COOLWSD::anonymizeUrl(url) << "].");
|
|
|
|
|
|
|
|
LOG_INF("Sanitized URI [" << COOLWSD::anonymizeUrl(url) << "] to ["
|
|
|
|
<< COOLWSD::anonymizeUrl(uriPublic.toString())
|
|
|
|
<< "] and mapped to docKey [" << docKey << "] for session [" << _id
|
|
|
|
<< "].");
|
|
|
|
|
|
|
|
// Before we create DocBroker with a SocketPoll thread, a ClientSession, and a Kit process,
|
|
|
|
// we need to vet this request by invoking CheckFileInfo.
|
|
|
|
// For that, we need the storage settings to create a connection.
|
|
|
|
const StorageBase::StorageType storageType =
|
|
|
|
StorageBase::validate(uriPublic, /*takeOwnership=*/false);
|
|
|
|
switch (storageType)
|
|
|
|
{
|
|
|
|
case StorageBase::StorageType::Unsupported:
|
|
|
|
LOG_ERR("Unsupported URI [" << COOLWSD::anonymizeUrl(uriPublic.toString())
|
|
|
|
<< "] or no storage configured");
|
|
|
|
throw BadRequestException("No Storage configured or invalid URI " +
|
|
|
|
COOLWSD::anonymizeUrl(uriPublic.toString()) + ']');
|
|
|
|
|
|
|
|
break;
|
|
|
|
case StorageBase::StorageType::Unauthorized:
|
|
|
|
LOG_ERR("No authorized hosts found matching the target host [" << uriPublic.getHost()
|
|
|
|
<< "] in config");
|
|
|
|
HttpHelper::sendErrorAndShutdown(http::StatusCode::Unauthorized, _socket);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case StorageBase::StorageType::FileSystem:
|
|
|
|
LOG_INF("URI [" << COOLWSD::anonymizeUrl(uriPublic.toString()) << "] on docKey ["
|
|
|
|
<< docKey << "] is for a FileSystem document");
|
|
|
|
|
|
|
|
// Remove from the current poll and transfer.
|
|
|
|
disposition.setMove(
|
|
|
|
[this, docKey, url, uriPublic](const std::shared_ptr<Socket>& moveSocket)
|
|
|
|
{
|
|
|
|
LOG_TRC_S('#' << moveSocket->getFD()
|
|
|
|
<< ": Dissociating client socket from "
|
|
|
|
"ClientRequestDispatcher and creating DocBroker for ["
|
|
|
|
<< docKey << ']');
|
|
|
|
|
|
|
|
// Send the file contents.
|
|
|
|
std::unique_ptr<std::vector<char>> data =
|
|
|
|
FileUtil::readFile(uriPublic.getPath());
|
|
|
|
if (data)
|
|
|
|
{
|
|
|
|
http::Response response(http::StatusCode::OK);
|
|
|
|
response.setBody(std::string(data->data(), data->size()),
|
|
|
|
"application/octet-stream");
|
|
|
|
_socket->sendAndShutdown(response);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-03-05 19:22:31 -06:00
|
|
|
HttpHelper::sendErrorAndShutdown(http::StatusCode::NotFound, _socket);
|
2023-11-06 05:04:27 -06:00
|
|
|
}
|
|
|
|
});
|
|
|
|
break;
|
2024-01-20 12:02:33 -06:00
|
|
|
#if !MOBILEAPP
|
2023-11-06 05:04:27 -06:00
|
|
|
case StorageBase::StorageType::Wopi:
|
|
|
|
LOG_INF("URI [" << COOLWSD::anonymizeUrl(uriPublic.toString()) << "] on docKey ["
|
|
|
|
<< docKey << "] is for a WOPI document");
|
|
|
|
// Remove from the current poll and transfer.
|
|
|
|
disposition.setMove(
|
|
|
|
[this, &poll, docKey, url, uriPublic](const std::shared_ptr<Socket>& moveSocket)
|
|
|
|
{
|
|
|
|
LOG_TRC_S('#' << moveSocket->getFD()
|
|
|
|
<< ": Dissociating client socket from "
|
|
|
|
"ClientRequestDispatcher and invoking CheckFileInfo for ["
|
|
|
|
<< docKey << ']');
|
|
|
|
|
2024-03-05 19:22:31 -06:00
|
|
|
poll->insertNewSocket(moveSocket);
|
2023-11-06 05:04:27 -06:00
|
|
|
|
|
|
|
// CheckFileInfo and only when it's good create DocBroker.
|
2024-03-05 19:22:31 -06:00
|
|
|
checkFileInfo(poll, uriPublic, RedirectionLimit);
|
2023-11-06 05:04:27 -06:00
|
|
|
});
|
|
|
|
break;
|
2024-01-20 12:02:33 -06:00
|
|
|
#endif //!MOBILEAPP
|
2023-11-06 05:04:27 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-20 12:02:33 -06:00
|
|
|
#if !MOBILEAPP
|
2024-03-05 19:22:31 -06:00
|
|
|
void WopiProxy::checkFileInfo(const std::shared_ptr<TerminatingPoll>& poll, const Poco::URI& uri,
|
|
|
|
int redirectLimit)
|
2023-11-06 05:04:27 -06:00
|
|
|
{
|
2024-03-05 19:22:31 -06:00
|
|
|
auto cfiContinuation = [this, poll, uri]([[maybe_unused]] CheckFileInfo& checkFileInfo)
|
2023-11-06 05:04:27 -06:00
|
|
|
{
|
2024-03-05 19:22:31 -06:00
|
|
|
const std::string uriAnonym = COOLWSD::anonymizeUrl(uri.toString());
|
2023-11-06 05:04:27 -06:00
|
|
|
|
2024-03-05 19:22:31 -06:00
|
|
|
assert(&checkFileInfo == _checkFileInfo.get() && "Unknown CheckFileInfo instance");
|
|
|
|
if (_checkFileInfo && _checkFileInfo->state() == CheckFileInfo::State::Pass &&
|
|
|
|
_checkFileInfo->wopiInfo())
|
2023-11-06 05:04:27 -06:00
|
|
|
{
|
2024-03-05 19:22:31 -06:00
|
|
|
Poco::JSON::Object::Ptr object = _checkFileInfo->wopiInfo();
|
2023-11-06 05:04:27 -06:00
|
|
|
|
2024-03-05 19:22:31 -06:00
|
|
|
std::size_t size = 0;
|
|
|
|
std::string filename, ownerId, lastModifiedTime;
|
|
|
|
JsonUtil::findJSONValue(object, "Size", size);
|
|
|
|
JsonUtil::findJSONValue(object, "OwnerId", ownerId);
|
|
|
|
JsonUtil::findJSONValue(object, "BaseFileName", filename);
|
|
|
|
JsonUtil::findJSONValue(object, "LastModifiedTime", lastModifiedTime);
|
2023-11-06 05:04:27 -06:00
|
|
|
|
2024-03-05 19:22:31 -06:00
|
|
|
LocalStorage::FileInfo fileInfo =
|
2024-03-24 08:34:58 -05:00
|
|
|
LocalStorage::FileInfo({ size, filename, ownerId, lastModifiedTime });
|
2023-11-06 05:04:27 -06:00
|
|
|
|
2024-03-05 19:22:31 -06:00
|
|
|
// if (COOLWSD::AnonymizeUserData)
|
|
|
|
// Util::mapAnonymized(Util::getFilenameFromURL(filename),
|
|
|
|
// Util::getFilenameFromURL(getUri().toString()));
|
2023-11-06 05:04:27 -06:00
|
|
|
|
2024-03-05 19:22:31 -06:00
|
|
|
auto wopiInfo = std::make_unique<WopiStorage::WOPIFileInfo>(fileInfo, object, uri);
|
|
|
|
// if (wopiInfo->getSupportsLocks())
|
|
|
|
// lockCtx.initSupportsLocks();
|
2023-11-06 05:04:27 -06:00
|
|
|
|
2024-03-05 19:22:31 -06:00
|
|
|
std::string url = checkFileInfo.url().toString();
|
2023-11-06 05:04:27 -06:00
|
|
|
|
2024-03-05 19:22:31 -06:00
|
|
|
// If FileUrl is set, we use it for GetFile.
|
|
|
|
const std::string fileUrl = wopiInfo->getFileUrl();
|
|
|
|
|
|
|
|
// First try the FileUrl, if provided.
|
|
|
|
if (!fileUrl.empty())
|
2023-11-06 05:04:27 -06:00
|
|
|
{
|
2024-03-05 19:22:31 -06:00
|
|
|
const std::string fileUrlAnonym = COOLWSD::anonymizeUrl(fileUrl);
|
|
|
|
const auto uriPublic = RequestDetails::sanitizeURI(url);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
LOG_INF("WOPI::GetFile using FileUrl: " << fileUrlAnonym);
|
|
|
|
return download(poll, url, Poco::URI(fileUrl), RedirectionLimit);
|
|
|
|
}
|
|
|
|
catch (const std::exception& ex)
|
|
|
|
{
|
|
|
|
LOG_ERR("Could not download document from WOPI FileUrl [" + fileUrlAnonym +
|
|
|
|
"]. Will use default URL. Error: "
|
|
|
|
<< ex.what());
|
|
|
|
// Fall-through.
|
|
|
|
}
|
2023-11-06 05:04:27 -06:00
|
|
|
}
|
|
|
|
|
2024-03-05 19:22:31 -06:00
|
|
|
// Try the default URL, we either don't have FileUrl, or it failed.
|
|
|
|
// WOPI URI to download files ends in '/contents'.
|
|
|
|
// Add it here to get the payload instead of file info.
|
|
|
|
Poco::URI uriObject(uri);
|
|
|
|
uriObject.setPath(uriObject.getPath() + "/contents");
|
|
|
|
url = uriObject.toString();
|
2023-11-06 05:04:27 -06:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2024-03-05 19:22:31 -06:00
|
|
|
LOG_INF("WOPI::GetFile using default URI: " << uriAnonym);
|
|
|
|
return download(poll, url, uriObject, RedirectionLimit);
|
2023-11-06 05:04:27 -06:00
|
|
|
}
|
|
|
|
catch (const std::exception& ex)
|
|
|
|
{
|
2024-03-05 19:22:31 -06:00
|
|
|
LOG_ERR(
|
|
|
|
"Cannot download document from WOPI storage uri [" + uriAnonym + "]. Error: "
|
|
|
|
<< ex.what());
|
|
|
|
// Fall-through.
|
2023-11-06 05:04:27 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-05 19:22:31 -06:00
|
|
|
LOG_ERR("Invalid URI or access denied to [" << uriAnonym << ']');
|
|
|
|
HttpHelper::sendErrorAndShutdown(http::StatusCode::Unauthorized, _socket);
|
2023-11-06 05:04:27 -06:00
|
|
|
};
|
|
|
|
|
2024-03-05 19:22:31 -06:00
|
|
|
// CheckFileInfo asynchronously.
|
2024-04-02 06:21:18 -05:00
|
|
|
_checkFileInfo = std::make_unique<CheckFileInfo>(poll, uri, std::move(cfiContinuation));
|
|
|
|
_checkFileInfo->checkFileInfo(redirectLimit);
|
2023-11-06 05:04:27 -06:00
|
|
|
}
|
|
|
|
|
2024-03-05 19:22:31 -06:00
|
|
|
void WopiProxy::download(const std::shared_ptr<TerminatingPoll>& poll, const std::string& url,
|
|
|
|
const Poco::URI& uriPublic, int redirectLimit)
|
2023-11-06 05:04:27 -06:00
|
|
|
{
|
|
|
|
const std::string uriAnonym = COOLWSD::anonymizeUrl(uriPublic.toString());
|
|
|
|
|
|
|
|
LOG_DBG("Getting info for wopi uri [" << uriAnonym << ']');
|
|
|
|
_httpSession = StorageConnectionManager::getHttpSession(uriPublic);
|
|
|
|
Authorization auth = Authorization::create(uriPublic);
|
|
|
|
http::Request httpRequest = StorageConnectionManager::createHttpRequest(uriPublic, auth);
|
|
|
|
|
|
|
|
const auto startTime = std::chrono::steady_clock::now();
|
|
|
|
|
|
|
|
LOG_TRC("WOPI::GetFile request header for URI [" << uriAnonym << "]:\n"
|
|
|
|
<< httpRequest.header());
|
|
|
|
|
|
|
|
http::Session::FinishedCallback finishedCallback =
|
2024-03-05 19:22:31 -06:00
|
|
|
[this, &poll, startTime, url, uriPublic, uriAnonym,
|
2023-11-06 05:04:27 -06:00
|
|
|
redirectLimit](const std::shared_ptr<http::Session>& session)
|
|
|
|
{
|
|
|
|
if (SigUtil::getShutdownRequestFlag())
|
|
|
|
{
|
|
|
|
LOG_DBG("Shutdown flagged, giving up on in-flight requests");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::shared_ptr<const http::Response> httpResponse = session->response();
|
|
|
|
LOG_TRC("WOPI::GetFile returned " << httpResponse->statusLine().statusCode());
|
|
|
|
|
|
|
|
const http::StatusCode statusCode = httpResponse->statusLine().statusCode();
|
|
|
|
if (statusCode == http::StatusCode::MovedPermanently ||
|
|
|
|
statusCode == http::StatusCode::Found ||
|
|
|
|
statusCode == http::StatusCode::TemporaryRedirect ||
|
|
|
|
statusCode == http::StatusCode::PermanentRedirect)
|
|
|
|
{
|
|
|
|
if (redirectLimit)
|
|
|
|
{
|
|
|
|
const std::string& location = httpResponse->get("Location");
|
|
|
|
LOG_TRC("WOPI::GetFile redirect to URI [" << COOLWSD::anonymizeUrl(location)
|
|
|
|
<< "]");
|
|
|
|
|
2024-03-05 19:22:31 -06:00
|
|
|
download(poll, location, Poco::URI(location), redirectLimit - 1);
|
2023-11-06 05:04:27 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOG_WRN("WOPI::GetFile redirected too many times. Giving up on URI [" << uriAnonym
|
|
|
|
<< ']');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::chrono::milliseconds callDurationMs =
|
|
|
|
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() -
|
|
|
|
startTime);
|
2023-11-27 09:51:16 -06:00
|
|
|
(void)callDurationMs;
|
2023-11-06 05:04:27 -06:00
|
|
|
|
|
|
|
// Note: we don't log the response if obfuscation is enabled, except for failures.
|
|
|
|
std::string wopiResponse = httpResponse->getBody();
|
|
|
|
const bool failed = (httpResponse->statusLine().statusCode() != http::StatusCode::OK);
|
|
|
|
|
2024-04-23 09:26:34 -05:00
|
|
|
Log::Level level = failed ? Log::Level::ERR : Log::Level::TRC;
|
|
|
|
if (Log::isEnabled(level))
|
2023-11-06 05:04:27 -06:00
|
|
|
{
|
2024-04-23 09:26:34 -05:00
|
|
|
std::ostringstream oss;
|
|
|
|
oss << "WOPI::GetFile " << (failed ? "failed" : "returned") << " for URI ["
|
2023-11-06 05:04:27 -06:00
|
|
|
<< uriAnonym << "]: " << httpResponse->statusLine().statusCode() << ' '
|
|
|
|
<< httpResponse->statusLine().reasonPhrase()
|
|
|
|
<< ". Headers: " << httpResponse->header()
|
|
|
|
<< (failed ? "\tBody: [" + wopiResponse + ']' : std::string());
|
|
|
|
|
2024-04-23 09:26:34 -05:00
|
|
|
LOG_END_FLUSH(oss);
|
|
|
|
Log::log(level, oss.str());
|
2023-11-06 05:04:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (failed)
|
|
|
|
{
|
|
|
|
if (httpResponse->statusLine().statusCode() == http::StatusCode::Forbidden)
|
|
|
|
{
|
|
|
|
LOG_ERR("Access denied to [" << uriAnonym << ']');
|
|
|
|
HttpHelper::sendErrorAndShutdown(http::StatusCode::Forbidden, _socket);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_ERR("Invalid URI or access denied to [" << uriAnonym << ']');
|
|
|
|
HttpHelper::sendErrorAndShutdown(http::StatusCode::Unauthorized, _socket);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
http::Response response(http::StatusCode::OK);
|
|
|
|
response.setBody(httpResponse->getBody(), "application/octet-stream");
|
|
|
|
_socket->sendAndShutdown(response);
|
|
|
|
};
|
|
|
|
|
2023-12-06 05:59:39 -06:00
|
|
|
_httpSession->setFinishedHandler(std::move(finishedCallback));
|
2023-11-06 05:04:27 -06:00
|
|
|
|
2024-03-05 19:22:31 -06:00
|
|
|
// Run the GET request on the WebServer Poll.
|
|
|
|
_httpSession->asyncRequest(httpRequest, *poll);
|
2023-11-06 05:04:27 -06:00
|
|
|
}
|
2024-01-20 12:02:33 -06:00
|
|
|
#endif //!MOBILEAPP
|
2023-11-27 09:51:16 -06:00
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|