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