diff --git a/src/blackmisc/networkutils.cpp b/src/blackmisc/networkutils.cpp index 6ce1a2c04..d1d30abca 100644 --- a/src/blackmisc/networkutils.cpp +++ b/src/blackmisc/networkutils.cpp @@ -9,6 +9,7 @@ #include #include #include +#include namespace BlackMisc { @@ -48,7 +49,7 @@ namespace BlackMisc } /* - * my IP + * My IP */ QStringList CNetworkUtils::getKnownIpAddresses() { @@ -103,6 +104,44 @@ namespace BlackMisc return CNetworkUtils::canConnect(server.getAddress(), server.getPort(), message, timeoutMs); } + /* + * Can connect url? + */ + bool CNetworkUtils::canConnect(const QString &url, QString &message, int timeoutMs) + { + if (url.isEmpty()) + { + message = QObject::tr("Missing URL", "BlackMisc"); + return false; + } + return canConnect(QUrl(url), message, timeoutMs); + } + + /* + * Can connect url? + */ + bool CNetworkUtils::canConnect(const QUrl &url, QString &message, int timeoutMs) + { + if (!url.isValid()) + { + message = QObject::tr("Invalid URL: %1", "BlackMisc").arg(url.toString()); + return false; + } + + if (url.isRelative()) + { + message = QObject::tr("Relative URL cannot be tested: %1", "BlackMisc").arg(url.toString()); + return false; + } + + QString scheme(url.scheme().toLower()); + int p = scheme.contains("https") ? 443 : 80; + p = url.port(80); + + QString host(url.host()); + return canConnect(host, p, message, timeoutMs); + } + /* * Valid IPv4 address */ @@ -131,4 +170,23 @@ namespace BlackMisc if (!success) return false; return (p >= 1 && p <= 65535); } + + /* + * Build URL + */ + QString CNetworkUtils::buildUrl(const QString &protocol, const QString &server, const QString &baseUrl, const QString &serviceUrl) + { + Q_ASSERT_X(protocol.length() > 3, Q_FUNC_INFO, "worng protocol"); + Q_ASSERT_X(!server.isEmpty(), Q_FUNC_INFO, "missing server"); + Q_ASSERT_X(!serviceUrl.isEmpty(), Q_FUNC_INFO, "missing service URL"); + + QString url(server); + if (!baseUrl.isEmpty()) + { + url.append("/").append(baseUrl); + } + url.append("/").append(serviceUrl); + url.replace("//", "/"); + return protocol + "://" + url; + } } // namespace diff --git a/src/blackmisc/networkutils.h b/src/blackmisc/networkutils.h index 602fc2361..367a28cd1 100644 --- a/src/blackmisc/networkutils.h +++ b/src/blackmisc/networkutils.h @@ -14,42 +14,44 @@ #include "blackmiscexport.h" #include "network/server.h" +#include #include namespace BlackMisc { - //! Utilities, e.g. checking whether a network connection can be established class BLACKMISC_EXPORT CNetworkUtils { - public: - /*! - * Is a connected interface available? - * \param withDebugOutput enables some debugging output - * \return - */ + //! + //! Is a connected interface available? + //! \param withDebugOutput enables some debugging output + //! \return static bool hasConnectedInterface(bool withDebugOutput = false); - /*! - * Can connect? - * \param hostAddress 130.4.20.3, or myserver.com - * \param port 80, 1234 - * \param timeoutMs - * \param message human readable message - * \return - */ + //! + //! Can connect? + //! \param hostAddress 130.4.20.3, or myserver.com + //! \param port 80, 1234 + //! \param timeoutMs + //! \param message human readable message + //! \return static bool canConnect(const QString &hostAddress, int port, QString &message, int timeoutMs = 1500); - /*! - * Can connect to server? - * \param server - * \param message human readable message - * \param timeoutMs - * \return - */ + //! + //! Can connect to server? + //! \param server + //! \param message human readable message + //! \param timeoutMs + //! \return static bool canConnect(const BlackMisc::Network::CServer &server, QString &message, int timeoutMs = 1500); + //! Can connect to URL? + static bool canConnect(const QString &url, QString &message, int timeoutMs = 1500); + + //! Can connect to URL? + static bool canConnect(const QUrl &url, QString &message, int timeoutMs = 1500); + //! Find out my IPv4 address, empty if not possible static QStringList getKnownIpAddresses(); @@ -62,6 +64,9 @@ namespace BlackMisc //! Valid port static bool isValidPort(const QString &port); + //! Build / concatenate an URL + static QString buildUrl(const QString &protocol, const QString &server, const QString &baseUrl, const QString &serviceUrl); + private: //! Deleted constructor CNetworkUtils() {}