2022-06-22 08:44:29 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
2023-10-30 16:58:54 -05:00
|
|
|
* Copyright the Collabora Online contributors.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
2023-11-09 12:23:00 -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/.
|
2022-06-22 08:44:29 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <Poco/URI.h>
|
|
|
|
|
|
|
|
#include <COOLWSD.hpp>
|
|
|
|
#include "ProxyRequestHandler.hpp"
|
|
|
|
#include <net/HttpRequest.hpp>
|
|
|
|
#include <net/HttpHelper.hpp>
|
|
|
|
|
2022-06-23 10:35:36 -05:00
|
|
|
std::unordered_map<std::string, std::shared_ptr<http::Response>> ProxyRequestHandler::CacheFileHash;
|
2022-06-22 09:09:12 -05:00
|
|
|
std::chrono::system_clock::time_point ProxyRequestHandler::MaxAge;
|
2022-06-22 09:02:58 -05:00
|
|
|
|
2022-06-22 11:04:11 -05:00
|
|
|
void ProxyRequestHandler::handleRequest(const std::string& relPath,
|
2022-07-05 07:03:14 -05:00
|
|
|
const std::shared_ptr<StreamSocket>& socket,
|
|
|
|
const std::string& serverUri)
|
2022-06-22 11:04:11 -05:00
|
|
|
{
|
2022-07-05 07:03:14 -05:00
|
|
|
|
|
|
|
Poco::URI uriProxy(serverUri);
|
2022-06-23 09:30:36 -05:00
|
|
|
constexpr const auto zero = std::chrono::system_clock::time_point();
|
2022-06-22 09:09:12 -05:00
|
|
|
const auto timeNow = std::chrono::system_clock::now();
|
|
|
|
|
2022-06-23 09:30:36 -05:00
|
|
|
if (MaxAge > zero && timeNow > MaxAge)
|
2022-06-22 09:09:12 -05:00
|
|
|
{
|
|
|
|
CacheFileHash.clear();
|
2022-06-23 09:30:36 -05:00
|
|
|
MaxAge = zero;
|
2022-06-22 09:09:12 -05:00
|
|
|
}
|
2022-06-22 11:04:11 -05:00
|
|
|
|
2022-06-22 09:02:58 -05:00
|
|
|
const auto cacheEntry = CacheFileHash.find(relPath);
|
|
|
|
if (cacheEntry != CacheFileHash.end())
|
|
|
|
{
|
|
|
|
socket->sendAndShutdown(*cacheEntry->second);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-22 11:04:11 -05:00
|
|
|
uriProxy.setPath(relPath);
|
|
|
|
auto sessionProxy = http::Session::create(uriProxy.getHost(),
|
|
|
|
http::Session::Protocol::HttpSsl,
|
|
|
|
uriProxy.getPort());
|
|
|
|
sessionProxy->setTimeout(std::chrono::seconds(10));
|
|
|
|
http::Request requestProxy(uriProxy.getPathAndQuery());
|
2022-06-22 08:59:11 -05:00
|
|
|
http::Session::FinishedCallback proxyCallback =
|
2022-06-23 09:30:36 -05:00
|
|
|
[socket, zero](const std::shared_ptr<http::Session>& httpSession)
|
2022-06-22 08:59:11 -05:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2022-06-22 09:09:12 -05:00
|
|
|
const auto callbackNow = std::chrono::system_clock::now();
|
2022-06-22 08:59:11 -05:00
|
|
|
std::shared_ptr<http::Response> httpResponse = httpSession->response();
|
2023-05-02 13:21:33 -05:00
|
|
|
if (httpResponse->statusLine().statusCode() == http::StatusCode::OK)
|
2022-06-22 08:59:11 -05:00
|
|
|
{
|
2022-06-23 09:30:36 -05:00
|
|
|
if (MaxAge == zero)
|
2022-06-22 09:09:12 -05:00
|
|
|
{
|
|
|
|
MaxAge = callbackNow + std::chrono::hours(10);
|
|
|
|
}
|
|
|
|
|
2022-06-22 09:02:58 -05:00
|
|
|
CacheFileHash[httpSession->getUrl()] = httpResponse;
|
2022-06-22 09:10:47 -05:00
|
|
|
|
2024-06-28 09:41:49 -05:00
|
|
|
// We're proxying, we take responsibility.
|
|
|
|
httpResponse->set("Server", http::getServerString());
|
|
|
|
|
2022-06-22 08:59:11 -05:00
|
|
|
socket->sendAndShutdown(*httpResponse);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-10-22 04:57:32 -05:00
|
|
|
HttpHelper::sendErrorAndShutdown(http::StatusCode::BadRequest, socket);
|
2022-06-22 08:59:11 -05:00
|
|
|
}
|
|
|
|
}
|
2022-06-23 10:20:06 -05:00
|
|
|
catch(std::exception& exc)
|
|
|
|
{
|
|
|
|
LOG_ERR("ProxyCallback: " << exc.what());
|
2023-10-22 04:57:32 -05:00
|
|
|
HttpHelper::sendErrorAndShutdown(http::StatusCode::BadRequest, socket);
|
2022-06-23 10:20:06 -05:00
|
|
|
}
|
2022-06-22 08:59:11 -05:00
|
|
|
catch(...)
|
|
|
|
{
|
2022-06-23 10:20:06 -05:00
|
|
|
LOG_ERR("ProxyCallback: Unknown exception");
|
2023-10-22 04:57:32 -05:00
|
|
|
HttpHelper::sendErrorAndShutdown(http::StatusCode::BadRequest, socket);
|
2022-06-22 08:59:11 -05:00
|
|
|
}
|
|
|
|
};
|
2022-06-22 11:04:11 -05:00
|
|
|
|
2023-12-06 05:59:39 -06:00
|
|
|
sessionProxy->setFinishedHandler(std::move(proxyCallback));
|
2022-06-22 11:04:11 -05:00
|
|
|
if (!sessionProxy->asyncRequest(requestProxy, *COOLWSD::getWebServerPoll()))
|
|
|
|
{
|
2023-10-22 04:57:32 -05:00
|
|
|
HttpHelper::sendErrorAndShutdown(http::StatusCode::BadRequest, socket);
|
2022-06-22 11:04:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-22 08:44:29 -05:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|