libreoffice-online/net/NetUtil.hpp
Michael Meeks 7183a3d3de spdx: improve machine and human readability of headers.
Change-Id: Ice934380029bf27054e830fffc07a5d037d1430f
Signed-off-by: Michael Meeks <michael.meeks@collabora.com>
2023-11-14 19:36:31 +00:00

81 lines
2.3 KiB
C++

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
* Copyright the Collabora Online contributors.
*
* SPDX-License-Identifier: MPL-2.0
*
* 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/.
*/
#pragma once
#include <string>
#include <memory>
// This file hosts network related common functionality
// and helper/utility functions and classes.
// HTTP-specific helpers are in HttpHelper.hpp.
class StreamSocket;
class ProtocolHandlerInterface;
namespace net
{
#if !MOBILEAPP
/// Resolves the IP of the given hostname. On failure, returns @targetHost.
std::string resolveHostAddress(const std::string& targetHost);
/// Returns true if @targetHost is on the same host.
bool isLocalhost(const std::string& targetHost);
#endif
/// Connect to an end-point at the given host and port and return StreamSocket.
std::shared_ptr<StreamSocket>
connect(const std::string& host, const std::string& port, const bool isSSL,
const std::shared_ptr<ProtocolHandlerInterface>& protocolHandler);
/// Connect to an end-point at the given @uri and return StreamSocket.
std::shared_ptr<StreamSocket>
connect(std::string uri, const std::shared_ptr<ProtocolHandlerInterface>& protocolHandler);
/// Decomposes a URI into its components.
/// Returns true if parsing was successful.
bool parseUri(std::string uri, std::string& scheme, std::string& host, std::string& port,
std::string& url);
/// Decomposes a URI into its components.
/// Returns true if parsing was successful.
inline bool parseUri(std::string uri, std::string& scheme, std::string& host, std::string& port)
{
std::string url;
return parseUri(std::move(uri), scheme, host, port, url);
}
/// Return the locator given a URI.
inline std::string parseUrl(const std::string& uri)
{
auto itScheme = uri.find("://");
if (itScheme != uri.npos)
{
itScheme += 3; // Skip it.
}
else
{
itScheme = 0;
}
const auto itUrl = uri.find('/', itScheme);
if (itUrl != uri.npos)
{
return uri.substr(itUrl); // Including the first foreslash.
}
return std::string();
}
} // namespace net