|
| 1 | +#include "uri.h" |
| 2 | + |
| 3 | +#include "logging.h" |
| 4 | + |
| 5 | +#include <QtCore/QRegularExpression> |
| 6 | + |
| 7 | +using namespace Quotient; |
| 8 | + |
| 9 | +struct ReplacePair { QByteArray uriString; char sigil; }; |
| 10 | +/// Defines bi-directional mapping of path prefixes and sigils |
| 11 | +static const auto replacePairs = { |
| 12 | + ReplacePair { "user/", '@' }, |
| 13 | + { "roomid/", '!' }, |
| 14 | + { "room/", '#' }, |
| 15 | + // The notation for bare event ids is not proposed in MSC2312 but there's |
| 16 | + // https://github.com/matrix-org/matrix-doc/pull/2644 |
| 17 | + { "event/", '$' } |
| 18 | +}; |
| 19 | + |
| 20 | +Uri::Uri(QByteArray primaryId, QByteArray secondaryId, QString query) |
| 21 | +{ |
| 22 | + if (primaryId.isEmpty()) |
| 23 | + primaryType_ = Empty; |
| 24 | + else { |
| 25 | + setScheme("matrix"); |
| 26 | + QString pathToBe; |
| 27 | + primaryType_ = Invalid; |
| 28 | + if (primaryId.size() < 2) // There should be something after sigil |
| 29 | + return; |
| 30 | + for (const auto& p: replacePairs) |
| 31 | + if (primaryId[0] == p.sigil) { |
| 32 | + primaryType_ = Type(p.sigil); |
| 33 | + pathToBe = p.uriString + primaryId.mid(1); |
| 34 | + break; |
| 35 | + } |
| 36 | + if (!secondaryId.isEmpty()) { |
| 37 | + if (secondaryId.size() < 2) { |
| 38 | + primaryType_ = Invalid; |
| 39 | + return; |
| 40 | + } |
| 41 | + pathToBe += "/event/" + secondaryId.mid(1); |
| 42 | + } |
| 43 | + setPath(pathToBe); |
| 44 | + } |
| 45 | + setQuery(std::move(query)); |
| 46 | +} |
| 47 | + |
| 48 | +Uri::Uri(QUrl url) : QUrl(std::move(url)) |
| 49 | +{ |
| 50 | + // NB: don't try to use `url` from here on, it's moved-from and empty |
| 51 | + if (isEmpty()) |
| 52 | + return; // primaryType_ == Empty |
| 53 | + |
| 54 | + if (!QUrl::isValid()) { // MatrixUri::isValid() checks primaryType_ |
| 55 | + primaryType_ = Invalid; |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + if (scheme() == "matrix") { |
| 60 | + // Check sanity as per https://github.com/matrix-org/matrix-doc/pull/2312 |
| 61 | + const auto& urlPath = path(); |
| 62 | + const auto& splitPath = urlPath.splitRef('/'); |
| 63 | + switch (splitPath.size()) { |
| 64 | + case 2: |
| 65 | + break; |
| 66 | + case 4: |
| 67 | + if (splitPath[2] == "event") |
| 68 | + break; |
| 69 | + [[fallthrough]]; |
| 70 | + default: |
| 71 | + return; // Invalid |
| 72 | + } |
| 73 | + |
| 74 | + for (const auto& p: replacePairs) |
| 75 | + if (urlPath.startsWith(p.uriString)) { |
| 76 | + primaryType_ = Type(p.sigil); |
| 77 | + return; // The only valid return path for matrix: URIs |
| 78 | + } |
| 79 | + qCDebug(MAIN) << "The matrix: URI is not recognised:" |
| 80 | + << toDisplayString(); |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + primaryType_ = NonMatrix; // Default, unless overridden by the code below |
| 85 | + if (scheme() == "https" && authority() == "matrix.to") { |
| 86 | + // See https://matrix.org/docs/spec/appendices#matrix-to-navigation |
| 87 | + static const QRegularExpression MatrixToUrlRE { |
| 88 | + R"(^/(?<main>[^/?]+)(/(?<sec>[^?]+))?(\?(?<query>.+))?$)" |
| 89 | + }; |
| 90 | + // matrix.to accepts both literal sigils (as well as & and ? used in |
| 91 | + // its "query" substitute) and their %-encoded forms; |
| 92 | + // so force QUrl to decode everything. |
| 93 | + auto f = fragment(QUrl::FullyDecoded); |
| 94 | + if (auto&& m = MatrixToUrlRE.match(f); m.hasMatch()) |
| 95 | + *this = Uri { m.captured("main").toUtf8(), |
| 96 | + m.captured("sec").toUtf8(), m.captured("query") }; |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +Uri::Uri(const QString& uriOrId) : Uri(fromUserInput(uriOrId)) {} |
| 101 | + |
| 102 | +Uri Uri::fromUserInput(const QString& uriOrId) |
| 103 | +{ |
| 104 | + if (uriOrId.isEmpty()) |
| 105 | + return {}; // type() == None |
| 106 | + |
| 107 | + // A quick check if uriOrId is a plain Matrix id |
| 108 | + // Bare event ids cannot be resolved without a room scope as per the current |
| 109 | + // spec but there's a movement towards making them navigable (see, e.g., |
| 110 | + // https://github.com/matrix-org/matrix-doc/pull/2644) - so treat them |
| 111 | + // as valid |
| 112 | + if (QStringLiteral("!@#+$").contains(uriOrId[0])) |
| 113 | + return Uri { uriOrId.toUtf8() }; |
| 114 | + |
| 115 | + return Uri { QUrl::fromUserInput(uriOrId) }; |
| 116 | +} |
| 117 | + |
| 118 | +Uri::Type Uri::type() const { return primaryType_; } |
| 119 | + |
| 120 | +Uri::SecondaryType Uri::secondaryType() const |
| 121 | +{ |
| 122 | + return path().section('/', 2, 2) == "event" ? EventId : NoSecondaryId; |
| 123 | +} |
| 124 | + |
| 125 | +QUrl Uri::toUrl(UriForm form) const |
| 126 | +{ |
| 127 | + if (!isValid()) |
| 128 | + return {}; |
| 129 | + |
| 130 | + if (form == CanonicalUri || type() == NonMatrix) |
| 131 | + return *this; |
| 132 | + |
| 133 | + QUrl url; |
| 134 | + url.setScheme("https"); |
| 135 | + url.setHost("matrix.to"); |
| 136 | + url.setPath("/"); |
| 137 | + auto fragment = primaryId(); |
| 138 | + if (const auto& secId = secondaryId(); !secId.isEmpty()) |
| 139 | + fragment += '/' + secId; |
| 140 | + if (const auto& q = query(); !q.isEmpty()) |
| 141 | + fragment += '?' + q; |
| 142 | + url.setFragment(fragment); |
| 143 | + return url; |
| 144 | +} |
| 145 | + |
| 146 | +QString Uri::primaryId() const |
| 147 | +{ |
| 148 | + if (primaryType_ == Empty || primaryType_ == Invalid) |
| 149 | + return {}; |
| 150 | + |
| 151 | + const auto& idStem = path().section('/', 1, 1); |
| 152 | + return idStem.isEmpty() ? idStem : primaryType_ + idStem; |
| 153 | +} |
| 154 | + |
| 155 | +QString Uri::secondaryId() const |
| 156 | +{ |
| 157 | + const auto& idStem = path().section('/', 3); |
| 158 | + return idStem.isEmpty() ? idStem : secondaryType() + idStem; |
| 159 | +} |
| 160 | + |
| 161 | +static const auto ActionKey = QStringLiteral("action"); |
| 162 | + |
| 163 | +QString Uri::action() const |
| 164 | +{ |
| 165 | + return type() == NonMatrix || !isValid() |
| 166 | + ? QString() |
| 167 | + : QUrlQuery { query() }.queryItemValue(ActionKey); |
| 168 | +} |
| 169 | + |
| 170 | +void Uri::setAction(const QString& newAction) |
| 171 | +{ |
| 172 | + if (!isValid()) { |
| 173 | + qCWarning(MAIN) << "Cannot set an action on an invalid Quotient::Uri"; |
| 174 | + return; |
| 175 | + } |
| 176 | + QUrlQuery q { query() }; |
| 177 | + q.removeQueryItem(ActionKey); |
| 178 | + q.addQueryItem(ActionKey, newAction); |
| 179 | + setQuery(q); |
| 180 | +} |
| 181 | + |
| 182 | +QStringList Uri::viaServers() const |
| 183 | +{ |
| 184 | + return QUrlQuery { query() }.allQueryItemValues(QStringLiteral("via"), |
| 185 | + QUrl::EncodeReserved); |
| 186 | +} |
| 187 | + |
| 188 | +bool Uri::isValid() const |
| 189 | +{ |
| 190 | + return primaryType_ != Empty && primaryType_ != Invalid; |
| 191 | +} |
0 commit comments