2020-05-06 11:02:51 -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/.
|
2020-05-06 11:02:51 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <string>
|
2020-05-12 10:19:41 -05:00
|
|
|
#include "RequestDetails.hpp"
|
2021-11-18 06:08:14 -06:00
|
|
|
#include "COOLWSD.hpp"
|
2020-05-06 11:02:51 -05:00
|
|
|
|
|
|
|
/** This class helps us to build a URL that will reliably point back
|
2020-05-12 10:19:41 -05:00
|
|
|
* at our service. It does very simple splitting of proxy URL
|
2020-05-06 11:02:51 -05:00
|
|
|
* and handles the proxy prefix feature.
|
|
|
|
*/
|
|
|
|
class ServerURL
|
|
|
|
{
|
2020-05-07 11:30:04 -05:00
|
|
|
bool _ssl;
|
|
|
|
bool _websocket;
|
2020-05-06 11:02:51 -05:00
|
|
|
std::string _schemeAuthority;
|
|
|
|
std::string _pathPlus;
|
|
|
|
public:
|
2020-05-12 10:19:41 -05:00
|
|
|
ServerURL(const RequestDetails &requestDetails)
|
2020-05-06 11:02:51 -05:00
|
|
|
{
|
2020-05-12 10:19:41 -05:00
|
|
|
init(requestDetails.getHostUntrusted(),
|
|
|
|
requestDetails.getProxyPrefix());
|
2020-05-06 11:02:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
explicit ServerURL()
|
|
|
|
{
|
|
|
|
init("nohostname", "");
|
|
|
|
}
|
|
|
|
|
|
|
|
void init(const std::string &host, const std::string &proxyPrefix)
|
|
|
|
{
|
|
|
|
// The user can override the ServerRoot with a new prefix.
|
2021-11-18 06:08:14 -06:00
|
|
|
_pathPlus = COOLWSD::ServiceRoot;
|
2020-05-06 11:02:51 -05:00
|
|
|
|
2021-11-18 06:08:14 -06:00
|
|
|
_ssl = (COOLWSD::isSSLEnabled() || COOLWSD::isSSLTermination());
|
2020-05-07 11:30:04 -05:00
|
|
|
_websocket = true;
|
2023-06-26 07:20:00 -05:00
|
|
|
_schemeAuthority = COOLWSD::ServerName.empty() ? host : COOLWSD::ServerName;
|
2020-05-06 11:02:51 -05:00
|
|
|
|
|
|
|
// A well formed ProxyPrefix will override it.
|
2020-06-01 22:21:22 -05:00
|
|
|
const std::string& url = proxyPrefix;
|
2020-05-06 11:02:51 -05:00
|
|
|
if (url.size() <= 0)
|
|
|
|
return;
|
|
|
|
|
2020-11-15 11:03:45 -06:00
|
|
|
std::size_t pos = url.find("://");
|
2020-05-06 11:02:51 -05:00
|
|
|
if (pos != std::string::npos) {
|
|
|
|
pos += 3;
|
2020-05-24 08:10:18 -05:00
|
|
|
auto hostEndPos = url.find('/', pos);
|
2020-05-06 11:02:51 -05:00
|
|
|
if (hostEndPos != std::string::npos)
|
|
|
|
{
|
2020-05-07 11:30:04 -05:00
|
|
|
_websocket = false;
|
|
|
|
std::string schemeProtocol = url.substr(0, pos);
|
|
|
|
_ssl = (schemeProtocol != "http://");
|
2020-05-06 11:02:51 -05:00
|
|
|
_schemeAuthority = url.substr(pos, hostEndPos - pos);
|
|
|
|
_pathPlus = url.substr(hostEndPos);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
2020-05-24 08:10:18 -05:00
|
|
|
LOG_ERR("Unusual proxy prefix '" << url << '\'');
|
2020-05-06 11:02:51 -05:00
|
|
|
} else
|
2020-05-24 08:10:18 -05:00
|
|
|
LOG_ERR("No http[s]:// in unusual proxy prefix '" << url << '\'');
|
2020-05-06 11:02:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string getResponseRoot() const
|
|
|
|
{
|
|
|
|
return _pathPlus;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string getWebSocketUrl() const
|
|
|
|
{
|
2020-05-07 11:30:04 -05:00
|
|
|
std::string schemeProtocol = (_websocket ? "ws" : "http");
|
|
|
|
if (_ssl)
|
2020-05-24 08:10:18 -05:00
|
|
|
schemeProtocol += 's';
|
2020-05-07 11:30:04 -05:00
|
|
|
return schemeProtocol + "://" + _schemeAuthority;
|
2020-05-06 11:02:51 -05:00
|
|
|
}
|
|
|
|
|
2023-03-06 07:41:28 -06:00
|
|
|
std::string getWebServerUrl() const
|
|
|
|
{
|
|
|
|
std::string schemeProtocol = "http";
|
|
|
|
if (_ssl)
|
|
|
|
schemeProtocol += 's';
|
|
|
|
return schemeProtocol + "://" + _schemeAuthority;
|
|
|
|
}
|
|
|
|
|
2020-05-06 11:02:51 -05:00
|
|
|
std::string getSubURLForEndpoint(const std::string &path) const
|
|
|
|
{
|
2020-05-07 11:30:04 -05:00
|
|
|
return std::string("http") + (_ssl ? "s" : "") + "://" + _schemeAuthority + _pathPlus + path;
|
2020-05-06 11:02:51 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|