// SPDX-FileCopyrightText: 2020 Jonah BrĂ¼chert // SPDX-FileCopyrightText: 2020 Rinigus // // SPDX-License-Identifier: GPL-2.0-or-later #include "urlutils.h" #include #include #include #include #include #include UrlUtils::UrlUtils(QObject *parent) : QObject(parent) { } QString UrlUtils::urlFromUserInput(const QString &input) { return QUrl::fromUserInput(input).toString(); } QString UrlUtils::urlScheme(const QString &url) { return QUrl::fromUserInput(url).scheme(); } QString UrlUtils::urlHostPort(const QString &url) { const QUrl u(url); static std::array common = { QStringView(u"www."), QStringView(u"m."), QStringView(u"mobile."), }; QString r = u.host(); for (const auto &i : common) { if (r.startsWith(i) && r.length() > i.length()) { r.remove(0, i.length()); break; // strip prefix only once } } const int p = u.port(-1); if (p > 0) r = QStringLiteral(u"%1:%2").arg(r).arg(p); return r; } QString UrlUtils::urlHost(const QString &url) { return QUrl::fromUserInput(url).host(); } QString UrlUtils::htmlFormattedUrl(const QString &url) { const QUrl parsedUrl = QUrl::fromUserInput(url); const QString path = parsedUrl.path(); return QStringView(uR"(%1%2)").arg(parsedUrl.host(), path == QStringView(u"/") ? QString() : path); } #include "moc_urlutils.cpp"