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/qobjectdefs.h>
10#include <QtCore/qstring.h>
11#include <QtCore/qlist.h>
12#include <QtCore/qpair.h>
13#include <QtCore/qglobal.h>
14
15#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
16Q_FORWARD_DECLARE_CF_TYPE(CFURL);
17Q_FORWARD_DECLARE_OBJC_CLASS(NSURL);
18#endif
19
20QT_BEGIN_NAMESPACE
21
22
23class QUrlQuery;
24class QUrlPrivate;
25class QDataStream;
26
27template <typename E1, typename E2>
28class QUrlTwoFlags
29{
30 int i;
31public:
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
86template<typename E1, typename E2>
87class QTypeInfo<QUrlTwoFlags<E1, E2> > : public QTypeInfoMerger<QUrlTwoFlags<E1, E2>, E1, E2> {};
88
89class QUrl;
90// qHash is a friend, but we can't use default arguments for friends (ยง8.3.6.4)
91Q_CORE_EXPORT size_t qHash(const QUrl &url, size_t seed = 0) noexcept;
92
93class Q_CORE_EXPORT QUrl
94{
95public:
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
134private:
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; };
139public:
140 Q_DECLARE_FLAGS(FormattingOptions, UrlFormattingOption)
141#else
142 typedef QUrlTwoFlags<UrlFormattingOption, ComponentFormattingOption> FormattingOptions;
143#endif
144
145 QUrl();
146 QUrl(const QUrl &copy) noexcept;
147 QUrl &operator =(const QUrl &copy) 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 static QUrl fromEncoded(const QByteArray &url, ParsingMode mode = TolerantMode);
169
170 enum UserInputResolutionOption {
171 DefaultResolution,
172 AssumeLocalFile
173 };
174 Q_DECLARE_FLAGS(UserInputResolutionOptions, UserInputResolutionOption)
175
176 static QUrl fromUserInput(const QString &userInput, const QString &workingDirectory = QString(),
177 UserInputResolutionOptions options = DefaultResolution);
178
179 bool isValid() const;
180 QString errorString() const;
181
182 bool isEmpty() const;
183 void clear();
184
185 void setScheme(const QString &scheme);
186 QString scheme() const;
187
188 void setAuthority(const QString &authority, ParsingMode mode = TolerantMode);
189 QString authority(ComponentFormattingOptions options = PrettyDecoded) const;
190
191 void setUserInfo(const QString &userInfo, ParsingMode mode = TolerantMode);
192 QString userInfo(ComponentFormattingOptions options = PrettyDecoded) const;
193
194 void setUserName(const QString &userName, ParsingMode mode = DecodedMode);
195 QString userName(ComponentFormattingOptions options = FullyDecoded) const;
196
197 void setPassword(const QString &password, ParsingMode mode = DecodedMode);
198 QString password(ComponentFormattingOptions = FullyDecoded) const;
199
200 void setHost(const QString &host, ParsingMode mode = DecodedMode);
201 QString host(ComponentFormattingOptions = FullyDecoded) const;
202
203 void setPort(int port);
204 int port(int defaultPort = -1) const;
205
206 void setPath(const QString &path, ParsingMode mode = DecodedMode);
207 QString path(ComponentFormattingOptions options = FullyDecoded) const;
208 QString fileName(ComponentFormattingOptions options = FullyDecoded) const;
209
210 bool hasQuery() const;
211 void setQuery(const QString &query, ParsingMode mode = TolerantMode);
212 void setQuery(const QUrlQuery &query);
213 QString query(ComponentFormattingOptions = PrettyDecoded) const;
214
215 bool hasFragment() const;
216 QString fragment(ComponentFormattingOptions options = PrettyDecoded) const;
217 void setFragment(const QString &fragment, ParsingMode mode = TolerantMode);
218
219 [[nodiscard]] QUrl resolved(const QUrl &relative) const;
220
221 bool isRelative() const;
222 bool isParentOf(const QUrl &url) const;
223
224 bool isLocalFile() const;
225 static QUrl fromLocalFile(const QString &localfile);
226 QString toLocalFile() const;
227
228 void detach();
229 bool isDetached() const;
230
231 bool operator <(const QUrl &url) const;
232 bool operator ==(const QUrl &url) const;
233 bool operator !=(const QUrl &url) const;
234
235 bool matches(const QUrl &url, FormattingOptions options) const;
236
237 static QString fromPercentEncoding(const QByteArray &);
238 static QByteArray toPercentEncoding(const QString &,
239 const QByteArray &exclude = QByteArray(),
240 const QByteArray &include = QByteArray());
241#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
242 static QUrl fromCFURL(CFURLRef url);
243 CFURLRef toCFURL() const Q_DECL_CF_RETURNS_RETAINED;
244 static QUrl fromNSURL(const NSURL *url);
245 NSURL *toNSURL() const Q_DECL_NS_RETURNS_AUTORELEASED;
246#endif
247
248 enum AceProcessingOption : unsigned int {
249 IgnoreIDNWhitelist = 0x1,
250 AceTransitionalProcessing = 0x2,
251 };
252 Q_DECLARE_FLAGS(AceProcessingOptions, AceProcessingOption)
253
254#if QT_CORE_REMOVED_SINCE(6, 3)
255 static QString fromAce(const QByteArray &);
256 static QByteArray toAce(const QString &);
257#endif
258 static QString fromAce(const QByteArray &domain, AceProcessingOptions options = {});
259 static QByteArray toAce(const QString &domain, AceProcessingOptions options = {});
260
261 static QStringList idnWhitelist();
262 static QStringList toStringList(const QList<QUrl> &uris, FormattingOptions options = FormattingOptions(PrettyDecoded));
263 static QList<QUrl> fromStringList(const QStringList &uris, ParsingMode mode = TolerantMode);
264
265 static void setIdnWhitelist(const QStringList &);
266 friend Q_CORE_EXPORT size_t qHash(const QUrl &url, size_t seed) noexcept;
267
268private:
269 QUrlPrivate *d;
270 friend class QUrlQuery;
271
272public:
273 typedef QUrlPrivate * DataPtr;
274 inline DataPtr &data_ptr() { return d; }
275};
276
277Q_DECLARE_SHARED(QUrl)
278Q_DECLARE_OPERATORS_FOR_FLAGS(QUrl::ComponentFormattingOptions)
279//Q_DECLARE_OPERATORS_FOR_FLAGS(QUrl::FormattingOptions)
280Q_DECLARE_OPERATORS_FOR_FLAGS(QUrl::AceProcessingOptions)
281
282#ifndef Q_QDOC
283constexpr inline QUrl::FormattingOptions operator|(QUrl::UrlFormattingOption f1, QUrl::UrlFormattingOption f2)
284{ return QUrl::FormattingOptions(f1) | f2; }
285constexpr inline QUrl::FormattingOptions operator|(QUrl::UrlFormattingOption f1, QUrl::FormattingOptions f2)
286{ return f2 | f1; }
287constexpr inline QIncompatibleFlag operator|(QUrl::UrlFormattingOption f1, int f2)
288{ return QIncompatibleFlag(uint(f1) | f2); }
289
290// add operators for OR'ing the two types of flags
291inline QUrl::FormattingOptions &operator|=(QUrl::FormattingOptions &i, QUrl::ComponentFormattingOptions f)
292{ i |= QUrl::UrlFormattingOption(f.toInt()); return i; }
293constexpr inline QUrl::FormattingOptions operator|(QUrl::UrlFormattingOption i, QUrl::ComponentFormattingOption f)
294{ return i | QUrl::UrlFormattingOption(qToUnderlying(e: f)); }
295constexpr inline QUrl::FormattingOptions operator|(QUrl::UrlFormattingOption i, QUrl::ComponentFormattingOptions f)
296{ return i | QUrl::UrlFormattingOption(f.toInt()); }
297constexpr inline QUrl::FormattingOptions operator|(QUrl::ComponentFormattingOption f, QUrl::UrlFormattingOption i)
298{ return i | QUrl::UrlFormattingOption(qToUnderlying(e: f)); }
299constexpr inline QUrl::FormattingOptions operator|(QUrl::ComponentFormattingOptions f, QUrl::UrlFormattingOption i)
300{ return i | QUrl::UrlFormattingOption(f.toInt()); }
301constexpr inline QUrl::FormattingOptions operator|(QUrl::FormattingOptions i, QUrl::ComponentFormattingOptions f)
302{ return i | QUrl::UrlFormattingOption(f.toInt()); }
303constexpr inline QUrl::FormattingOptions operator|(QUrl::ComponentFormattingOption f, QUrl::FormattingOptions i)
304{ return i | QUrl::UrlFormattingOption(qToUnderlying(e: f)); }
305constexpr inline QUrl::FormattingOptions operator|(QUrl::ComponentFormattingOptions f, QUrl::FormattingOptions i)
306{ return i | QUrl::UrlFormattingOption(f.toInt()); }
307
308//inline QUrl::UrlFormattingOption &operator=(const QUrl::UrlFormattingOption &i, QUrl::ComponentFormattingOptions f)
309//{ i = int(f); f; }
310#endif // Q_QDOC
311
312#ifndef QT_NO_DATASTREAM
313Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QUrl &);
314Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QUrl &);
315#endif
316
317#ifndef QT_NO_DEBUG_STREAM
318Q_CORE_EXPORT QDebug operator<<(QDebug, const QUrl &);
319#endif
320
321QT_END_NAMESPACE
322
323#endif // QURL_H
324

source code of qtbase/src/corelib/io/qurl.h