| 1 | // Copyright (C) 2020 The Qt Company Ltd. |
| 2 | // Copyright (C) 2016 Intel Corporation. |
| 3 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 4 | // Qt-Security score:significant reason:default |
| 5 | |
| 6 | #ifndef QURL_H |
| 7 | #define QURL_H |
| 8 | |
| 9 | #include <QtCore/qbytearray.h> |
| 10 | #include <QtCore/qcompare.h> |
| 11 | #include <QtCore/qobjectdefs.h> |
| 12 | #include <QtCore/qstring.h> |
| 13 | #include <QtCore/qlist.h> |
| 14 | #include <QtCore/qglobal.h> |
| 15 | |
| 16 | #if defined(Q_OS_DARWIN) || defined(Q_QDOC) |
| 17 | Q_FORWARD_DECLARE_CF_TYPE(CFURL); |
| 18 | Q_FORWARD_DECLARE_OBJC_CLASS(NSURL); |
| 19 | #endif |
| 20 | |
| 21 | QT_BEGIN_NAMESPACE |
| 22 | |
| 23 | |
| 24 | class QUrlQuery; |
| 25 | class QUrlPrivate; |
| 26 | class QDataStream; |
| 27 | |
| 28 | template <typename E1, typename E2> |
| 29 | class QUrlTwoFlags |
| 30 | { |
| 31 | int i; |
| 32 | public: |
| 33 | constexpr inline QUrlTwoFlags() : i(0) {} |
| 34 | constexpr inline QUrlTwoFlags(E1 f) : i(f) {} |
| 35 | constexpr inline QUrlTwoFlags(E2 f) : i(f) {} |
| 36 | constexpr inline QUrlTwoFlags(QFlag f) : i(f) {} |
| 37 | constexpr inline QUrlTwoFlags(QFlags<E1> f) : i(f.operator typename QFlags<E1>::Int()) {} |
| 38 | constexpr inline QUrlTwoFlags(QFlags<E2> f) : i(f.operator typename QFlags<E2>::Int()) {} |
| 39 | |
| 40 | inline QUrlTwoFlags &operator&=(int mask) { i &= mask; return *this; } |
| 41 | inline QUrlTwoFlags &operator&=(uint mask) { i &= mask; return *this; } |
| 42 | inline QUrlTwoFlags &operator&=(QFlags<E1> mask) { i &= mask.toInt(); return *this; } |
| 43 | inline QUrlTwoFlags &operator&=(QFlags<E2> mask) { i &= mask.toInt(); return *this; } |
| 44 | inline QUrlTwoFlags &operator|=(QUrlTwoFlags f) { i |= f.i; return *this; } |
| 45 | inline QUrlTwoFlags &operator|=(E1 f) { i |= f; return *this; } |
| 46 | inline QUrlTwoFlags &operator|=(E2 f) { i |= f; return *this; } |
| 47 | inline QUrlTwoFlags &operator|=(QFlags<E1> mask) { i |= mask.toInt(); return *this; } |
| 48 | inline QUrlTwoFlags &operator|=(QFlags<E2> mask) { i |= mask.toInt(); return *this; } |
| 49 | inline QUrlTwoFlags &operator^=(QUrlTwoFlags f) { i ^= f.i; return *this; } |
| 50 | inline QUrlTwoFlags &operator^=(E1 f) { i ^= f; return *this; } |
| 51 | inline QUrlTwoFlags &operator^=(E2 f) { i ^= f; return *this; } |
| 52 | inline QUrlTwoFlags &operator^=(QFlags<E1> mask) { i ^= mask.toInt(); return *this; } |
| 53 | inline QUrlTwoFlags &operator^=(QFlags<E2> mask) { i ^= mask.toInt(); return *this; } |
| 54 | |
| 55 | constexpr inline operator QFlags<E1>() const { return QFlag(i); } |
| 56 | constexpr inline operator QFlags<E2>() const { return QFlag(i); } |
| 57 | constexpr inline operator int() const { return i; } |
| 58 | constexpr inline bool operator!() const { return !i; } |
| 59 | |
| 60 | constexpr inline QUrlTwoFlags operator|(QUrlTwoFlags f) const |
| 61 | { return QUrlTwoFlags(QFlag(i | f.i)); } |
| 62 | constexpr inline QUrlTwoFlags operator|(E1 f) const |
| 63 | { return QUrlTwoFlags(QFlag(i | f)); } |
| 64 | constexpr inline QUrlTwoFlags operator|(E2 f) const |
| 65 | { return QUrlTwoFlags(QFlag(i | f)); } |
| 66 | constexpr inline QUrlTwoFlags operator^(QUrlTwoFlags f) const |
| 67 | { return QUrlTwoFlags(QFlag(i ^ f.i)); } |
| 68 | constexpr inline QUrlTwoFlags operator^(E1 f) const |
| 69 | { return QUrlTwoFlags(QFlag(i ^ f)); } |
| 70 | constexpr inline QUrlTwoFlags operator^(E2 f) const |
| 71 | { return QUrlTwoFlags(QFlag(i ^ f)); } |
| 72 | constexpr inline QUrlTwoFlags operator&(int mask) const |
| 73 | { return QUrlTwoFlags(QFlag(i & mask)); } |
| 74 | constexpr inline QUrlTwoFlags operator&(uint mask) const |
| 75 | { return QUrlTwoFlags(QFlag(i & mask)); } |
| 76 | constexpr inline QUrlTwoFlags operator&(E1 f) const |
| 77 | { return QUrlTwoFlags(QFlag(i & f)); } |
| 78 | constexpr inline QUrlTwoFlags operator&(E2 f) const |
| 79 | { return QUrlTwoFlags(QFlag(i & f)); } |
| 80 | constexpr inline QUrlTwoFlags operator~() const |
| 81 | { return QUrlTwoFlags(QFlag(~i)); } |
| 82 | |
| 83 | constexpr inline bool testFlag(E1 f) const { return (i & f) == f && (f != 0 || i == int(f)); } |
| 84 | constexpr inline bool testFlag(E2 f) const { return (i & f) == f && (f != 0 || i == int(f)); } |
| 85 | }; |
| 86 | |
| 87 | template<typename E1, typename E2> |
| 88 | class QTypeInfo<QUrlTwoFlags<E1, E2> > : public QTypeInfoMerger<QUrlTwoFlags<E1, E2>, E1, E2> {}; |
| 89 | |
| 90 | class QUrl; |
| 91 | // qHash is a friend, but we can't use default arguments for friends (ยง8.3.6.4) |
| 92 | Q_CORE_EXPORT size_t qHash(const QUrl &url, size_t seed = 0) noexcept; |
| 93 | |
| 94 | class Q_CORE_EXPORT QUrl |
| 95 | { |
| 96 | public: |
| 97 | enum ParsingMode { |
| 98 | TolerantMode, |
| 99 | StrictMode, |
| 100 | DecodedMode |
| 101 | }; |
| 102 | |
| 103 | // encoding / toString values |
| 104 | enum UrlFormattingOption : unsigned int { |
| 105 | None = 0x0, |
| 106 | RemoveScheme = 0x1, |
| 107 | RemovePassword = 0x2, |
| 108 | RemoveUserInfo = RemovePassword | 0x4, |
| 109 | RemovePort = 0x8, |
| 110 | RemoveAuthority = RemoveUserInfo | RemovePort | 0x10, |
| 111 | RemovePath = 0x20, |
| 112 | RemoveQuery = 0x40, |
| 113 | RemoveFragment = 0x80, |
| 114 | // 0x100 was a private code in Qt 4, keep unused for a while |
| 115 | PreferLocalFile = 0x200, |
| 116 | StripTrailingSlash = 0x400, |
| 117 | RemoveFilename = 0x800, |
| 118 | NormalizePathSegments = 0x1000 |
| 119 | }; |
| 120 | |
| 121 | enum ComponentFormattingOption : unsigned int { |
| 122 | PrettyDecoded = 0x000000, |
| 123 | EncodeSpaces = 0x100000, |
| 124 | EncodeUnicode = 0x200000, |
| 125 | EncodeDelimiters = 0x400000 | 0x800000, |
| 126 | EncodeReserved = 0x1000000, |
| 127 | DecodeReserved = 0x2000000, |
| 128 | // 0x4000000 used to indicate full-decode mode |
| 129 | |
| 130 | FullyEncoded = EncodeSpaces | EncodeUnicode | EncodeDelimiters | EncodeReserved, |
| 131 | FullyDecoded = FullyEncoded | DecodeReserved | 0x4000000 |
| 132 | }; |
| 133 | Q_DECLARE_FLAGS(ComponentFormattingOptions, ComponentFormattingOption) |
| 134 | #ifdef Q_QDOC |
| 135 | private: |
| 136 | // We need to let qdoc think that FormattingOptions is a normal QFlags, but |
| 137 | // it needs to be a QUrlTwoFlags for compiling default arguments of some functions. |
| 138 | template<typename T> struct QFlags : QUrlTwoFlags<T, ComponentFormattingOption> |
| 139 | { using QUrlTwoFlags<T, ComponentFormattingOption>::QUrlTwoFlags; }; |
| 140 | public: |
| 141 | Q_DECLARE_FLAGS(FormattingOptions, UrlFormattingOption) |
| 142 | #else |
| 143 | typedef QUrlTwoFlags<UrlFormattingOption, ComponentFormattingOption> FormattingOptions; |
| 144 | #endif |
| 145 | |
| 146 | QUrl(); |
| 147 | QUrl(const QUrl ©) noexcept; |
| 148 | QUrl &operator =(const QUrl ©) noexcept; |
| 149 | #ifdef QT_NO_URL_CAST_FROM_STRING |
| 150 | explicit QUrl(const QString &url, ParsingMode mode = TolerantMode); |
| 151 | #else |
| 152 | QUrl(const QString &url, ParsingMode mode = TolerantMode); |
| 153 | QUrl &operator=(const QString &url); |
| 154 | #endif |
| 155 | QUrl(QUrl &&other) noexcept : d(other.d) |
| 156 | { other.d = nullptr; } |
| 157 | QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QUrl) |
| 158 | ~QUrl(); |
| 159 | |
| 160 | void swap(QUrl &other) noexcept { qt_ptr_swap(lhs&: d, rhs&: other.d); } |
| 161 | |
| 162 | void setUrl(const QString &url, ParsingMode mode = TolerantMode); |
| 163 | QString url(FormattingOptions options = FormattingOptions(PrettyDecoded)) const; |
| 164 | QString toString(FormattingOptions options = FormattingOptions(PrettyDecoded)) const; |
| 165 | QString toDisplayString(FormattingOptions options = FormattingOptions(PrettyDecoded)) const; |
| 166 | [[nodiscard]] QUrl adjusted(FormattingOptions options) const; |
| 167 | |
| 168 | QByteArray toEncoded(FormattingOptions options = FullyEncoded) const; |
| 169 | #if QT_CORE_REMOVED_SINCE(6, 7) |
| 170 | static QUrl fromEncoded(const QByteArray &url, ParsingMode mode = TolerantMode); |
| 171 | #endif |
| 172 | static QUrl fromEncoded(QByteArrayView input, ParsingMode mode = TolerantMode); |
| 173 | |
| 174 | enum UserInputResolutionOption { |
| 175 | DefaultResolution, |
| 176 | AssumeLocalFile |
| 177 | }; |
| 178 | Q_DECLARE_FLAGS(UserInputResolutionOptions, UserInputResolutionOption) |
| 179 | |
| 180 | static QUrl fromUserInput(const QString &userInput, const QString &workingDirectory = QString(), |
| 181 | UserInputResolutionOptions options = DefaultResolution); |
| 182 | |
| 183 | bool isValid() const; |
| 184 | QString errorString() const; |
| 185 | |
| 186 | bool isEmpty() const; |
| 187 | void clear(); |
| 188 | |
| 189 | void setScheme(const QString &scheme); |
| 190 | QString scheme() const; |
| 191 | |
| 192 | void setAuthority(const QString &authority, ParsingMode mode = TolerantMode); |
| 193 | QString authority(ComponentFormattingOptions options = PrettyDecoded) const; |
| 194 | |
| 195 | void setUserInfo(const QString &userInfo, ParsingMode mode = TolerantMode); |
| 196 | QString userInfo(ComponentFormattingOptions options = PrettyDecoded) const; |
| 197 | |
| 198 | void setUserName(const QString &userName, ParsingMode mode = DecodedMode); |
| 199 | QString userName(ComponentFormattingOptions options = FullyDecoded) const; |
| 200 | |
| 201 | void setPassword(const QString &password, ParsingMode mode = DecodedMode); |
| 202 | QString password(ComponentFormattingOptions = FullyDecoded) const; |
| 203 | |
| 204 | void setHost(const QString &host, ParsingMode mode = DecodedMode); |
| 205 | QString host(ComponentFormattingOptions = FullyDecoded) const; |
| 206 | |
| 207 | void setPort(int port); |
| 208 | int port(int defaultPort = -1) const; |
| 209 | |
| 210 | void setPath(const QString &path, ParsingMode mode = DecodedMode); |
| 211 | QString path(ComponentFormattingOptions options = FullyDecoded) const; |
| 212 | QString fileName(ComponentFormattingOptions options = FullyDecoded) const; |
| 213 | |
| 214 | bool hasQuery() const; |
| 215 | void setQuery(const QString &query, ParsingMode mode = TolerantMode); |
| 216 | void setQuery(const QUrlQuery &query); |
| 217 | QString query(ComponentFormattingOptions = PrettyDecoded) const; |
| 218 | |
| 219 | bool hasFragment() const; |
| 220 | QString fragment(ComponentFormattingOptions options = PrettyDecoded) const; |
| 221 | void setFragment(const QString &fragment, ParsingMode mode = TolerantMode); |
| 222 | |
| 223 | [[nodiscard]] QUrl resolved(const QUrl &relative) const; |
| 224 | |
| 225 | bool isRelative() const; |
| 226 | bool isParentOf(const QUrl &url) const; |
| 227 | |
| 228 | bool isLocalFile() const; |
| 229 | static QUrl fromLocalFile(const QString &localfile); |
| 230 | QString toLocalFile() const; |
| 231 | |
| 232 | void detach(); |
| 233 | bool isDetached() const; |
| 234 | |
| 235 | #if QT_CORE_REMOVED_SINCE(6, 8) |
| 236 | bool operator <(const QUrl &url) const; |
| 237 | bool operator ==(const QUrl &url) const; |
| 238 | bool operator !=(const QUrl &url) const; |
| 239 | #endif |
| 240 | |
| 241 | bool matches(const QUrl &url, FormattingOptions options) const; |
| 242 | |
| 243 | static QString fromPercentEncoding(const QByteArray &); |
| 244 | static QByteArray toPercentEncoding(const QString &, |
| 245 | const QByteArray &exclude = QByteArray(), |
| 246 | const QByteArray &include = QByteArray()); |
| 247 | #if defined(Q_OS_DARWIN) || defined(Q_QDOC) |
| 248 | static QUrl fromCFURL(CFURLRef url); |
| 249 | CFURLRef toCFURL() const Q_DECL_CF_RETURNS_RETAINED; |
| 250 | static QUrl fromNSURL(const NSURL *url); |
| 251 | NSURL *toNSURL() const Q_DECL_NS_RETURNS_AUTORELEASED; |
| 252 | #endif |
| 253 | |
| 254 | enum AceProcessingOption : unsigned int { |
| 255 | IgnoreIDNWhitelist = 0x1, |
| 256 | AceTransitionalProcessing = 0x2, |
| 257 | }; |
| 258 | Q_DECLARE_FLAGS(AceProcessingOptions, AceProcessingOption) |
| 259 | |
| 260 | #if QT_CORE_REMOVED_SINCE(6, 3) |
| 261 | static QString fromAce(const QByteArray &); |
| 262 | static QByteArray toAce(const QString &); |
| 263 | #endif |
| 264 | static QString fromAce(const QByteArray &domain, AceProcessingOptions options = {}); |
| 265 | static QByteArray toAce(const QString &domain, AceProcessingOptions options = {}); |
| 266 | |
| 267 | static QStringList idnWhitelist(); |
| 268 | static QStringList toStringList(const QList<QUrl> &uris, FormattingOptions options = FormattingOptions(PrettyDecoded)); |
| 269 | static QList<QUrl> fromStringList(const QStringList &uris, ParsingMode mode = TolerantMode); |
| 270 | |
| 271 | static void setIdnWhitelist(const QStringList &); |
| 272 | friend Q_CORE_EXPORT size_t qHash(const QUrl &url, size_t seed) noexcept; |
| 273 | |
| 274 | private: |
| 275 | friend Q_CORE_EXPORT bool comparesEqual(const QUrl &lhs, const QUrl &rhs); |
| 276 | friend Q_CORE_EXPORT Qt::weak_ordering |
| 277 | compareThreeWay(const QUrl &lhs, const QUrl &rhs); |
| 278 | Q_DECLARE_WEAKLY_ORDERED_NON_NOEXCEPT(QUrl) |
| 279 | |
| 280 | void detachToClear(); |
| 281 | |
| 282 | QUrlPrivate *d; |
| 283 | friend class QUrlQuery; |
| 284 | |
| 285 | public: |
| 286 | typedef QUrlPrivate * DataPtr; |
| 287 | inline DataPtr &data_ptr() { return d; } |
| 288 | }; |
| 289 | |
| 290 | Q_DECLARE_SHARED(QUrl) |
| 291 | Q_DECLARE_OPERATORS_FOR_FLAGS(QUrl::ComponentFormattingOptions) |
| 292 | //Q_DECLARE_OPERATORS_FOR_FLAGS(QUrl::FormattingOptions) |
| 293 | Q_DECLARE_OPERATORS_FOR_FLAGS(QUrl::AceProcessingOptions) |
| 294 | |
| 295 | #ifndef Q_QDOC |
| 296 | constexpr inline QUrl::FormattingOptions operator|(QUrl::UrlFormattingOption f1, QUrl::UrlFormattingOption f2) |
| 297 | { return QUrl::FormattingOptions(f1) | f2; } |
| 298 | constexpr inline QUrl::FormattingOptions operator|(QUrl::UrlFormattingOption f1, QUrl::FormattingOptions f2) |
| 299 | { return f2 | f1; } |
| 300 | constexpr inline QIncompatibleFlag operator|(QUrl::UrlFormattingOption f1, int f2) |
| 301 | { return QIncompatibleFlag(uint(f1) | f2); } |
| 302 | |
| 303 | // add operators for OR'ing the two types of flags |
| 304 | inline QUrl::FormattingOptions &operator|=(QUrl::FormattingOptions &i, QUrl::ComponentFormattingOptions f) |
| 305 | { i |= QUrl::UrlFormattingOption(f.toInt()); return i; } |
| 306 | constexpr inline QUrl::FormattingOptions operator|(QUrl::UrlFormattingOption i, QUrl::ComponentFormattingOption f) |
| 307 | { return i | QUrl::UrlFormattingOption(qToUnderlying(e: f)); } |
| 308 | constexpr inline QUrl::FormattingOptions operator|(QUrl::UrlFormattingOption i, QUrl::ComponentFormattingOptions f) |
| 309 | { return i | QUrl::UrlFormattingOption(f.toInt()); } |
| 310 | constexpr inline QUrl::FormattingOptions operator|(QUrl::ComponentFormattingOption f, QUrl::UrlFormattingOption i) |
| 311 | { return i | QUrl::UrlFormattingOption(qToUnderlying(e: f)); } |
| 312 | constexpr inline QUrl::FormattingOptions operator|(QUrl::ComponentFormattingOptions f, QUrl::UrlFormattingOption i) |
| 313 | { return i | QUrl::UrlFormattingOption(f.toInt()); } |
| 314 | constexpr inline QUrl::FormattingOptions operator|(QUrl::FormattingOptions i, QUrl::ComponentFormattingOptions f) |
| 315 | { return i | QUrl::UrlFormattingOption(f.toInt()); } |
| 316 | constexpr inline QUrl::FormattingOptions operator|(QUrl::ComponentFormattingOption f, QUrl::FormattingOptions i) |
| 317 | { return i | QUrl::UrlFormattingOption(qToUnderlying(e: f)); } |
| 318 | constexpr inline QUrl::FormattingOptions operator|(QUrl::ComponentFormattingOptions f, QUrl::FormattingOptions i) |
| 319 | { return i | QUrl::UrlFormattingOption(f.toInt()); } |
| 320 | |
| 321 | //inline QUrl::UrlFormattingOption &operator=(const QUrl::UrlFormattingOption &i, QUrl::ComponentFormattingOptions f) |
| 322 | //{ i = int(f); f; } |
| 323 | #endif // Q_QDOC |
| 324 | |
| 325 | #ifndef QT_NO_DATASTREAM |
| 326 | Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QUrl &); |
| 327 | Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QUrl &); |
| 328 | #endif |
| 329 | |
| 330 | #ifndef QT_NO_DEBUG_STREAM |
| 331 | Q_CORE_EXPORT QDebug operator<<(QDebug, const QUrl &); |
| 332 | #endif |
| 333 | |
| 334 | QT_END_NAMESPACE |
| 335 | |
| 336 | #endif // QURL_H |
| 337 | |