2022-06-22 08:44:29 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* 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>
|
|
|
|
|
|
|
|
#include <Poco/URI.h>
|
|
|
|
|
|
|
|
#include <COOLWSD.hpp>
|
|
|
|
#include "ProxyRequestHandler.hpp"
|
|
|
|
#include <net/HttpRequest.hpp>
|
|
|
|
#include <net/HttpHelper.hpp>
|
|
|
|
|
2022-06-22 09:02:58 -05:00
|
|
|
std::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,
|
|
|
|
const std::shared_ptr<StreamSocket>& socket)
|
|
|
|
{
|
|
|
|
Poco::URI uriProxy(ProxyServer);
|
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();
|
|
|
|
if (httpResponse->statusLine().statusCode() == 200)
|
|
|
|
{
|
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
|
|
|
|
|
|
|
httpResponse->add("Server", HTTP_SERVER_STRING);
|
|
|
|
httpResponse->add("Date", Util::getHttpTimeNow());
|
2022-06-22 08:59:11 -05:00
|
|
|
socket->sendAndShutdown(*httpResponse);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
HttpHelper::sendErrorAndShutdown(400, socket);
|
|
|
|
}
|
|
|
|
}
|
2022-06-23 10:20:06 -05:00
|
|
|
catch(std::exception& exc)
|
|
|
|
{
|
|
|
|
LOG_ERR("ProxyCallback: " << exc.what());
|
|
|
|
HttpHelper::sendErrorAndShutdown(400, socket);
|
|
|
|
}
|
2022-06-22 08:59:11 -05:00
|
|
|
catch(...)
|
|
|
|
{
|
2022-06-23 10:20:06 -05:00
|
|
|
LOG_ERR("ProxyCallback: Unknown exception");
|
2022-06-22 08:59:11 -05:00
|
|
|
HttpHelper::sendErrorAndShutdown(400, socket);
|
|
|
|
}
|
|
|
|
};
|
2022-06-22 11:04:11 -05:00
|
|
|
|
2022-06-22 08:59:11 -05:00
|
|
|
sessionProxy->setFinishedHandler(proxyCallback);
|
2022-06-22 11:04:11 -05:00
|
|
|
if (!sessionProxy->asyncRequest(requestProxy, *COOLWSD::getWebServerPoll()))
|
|
|
|
{
|
|
|
|
HttpHelper::sendErrorAndShutdown(400, socket);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-22 08:44:29 -05:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|