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