Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qurl.h
Go to the documentation of this file.
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)
17Q_FORWARD_DECLARE_CF_TYPE(CFURL);
18Q_FORWARD_DECLARE_OBJC_CLASS(NSURL);
19#endif
20
21QT_BEGIN_NAMESPACE
22
23
24class QUrlQuery;
25class QUrlPrivate;
26class QDataStream;
27
28template <typename E1, typename E2>
30{
31 int i;
32public:
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
87template<typename E1, typename E2>
89
90class QUrl;
91// qHash is a friend, but we can't use default arguments for friends (ยง8.3.6.4)
92Q_CORE_EXPORT size_t qHash(const QUrl &url, size_t seed = 0) noexcept;
93
94class Q_CORE_EXPORT QUrl
95{
96public:
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 // 0x8000000 is QUrlDecodeForLocalFile (see qurl_p.h)
130
131 FullyEncoded = EncodeSpaces | EncodeUnicode | EncodeDelimiters | EncodeReserved,
132 FullyDecoded = FullyEncoded | DecodeReserved | 0x4000000
133 };
134 Q_DECLARE_FLAGS(ComponentFormattingOptions, ComponentFormattingOption)
135#ifdef Q_QDOC
136private:
137 // We need to let qdoc think that FormattingOptions is a normal QFlags, but
138 // it needs to be a QUrlTwoFlags for compiling default arguments of some functions.
139 template<typename T> struct QFlags : QUrlTwoFlags<T, ComponentFormattingOption>
140 { using QUrlTwoFlags<T, ComponentFormattingOption>::QUrlTwoFlags; };
141public:
142 Q_DECLARE_FLAGS(FormattingOptions, UrlFormattingOption)
143#else
144 typedef QUrlTwoFlags<UrlFormattingOption, ComponentFormattingOption> FormattingOptions;
145#endif
146
147 QUrl();
148 QUrl(const QUrl &copy) noexcept;
149 QUrl &operator =(const QUrl &copy) noexcept;
150#ifdef QT_NO_URL_CAST_FROM_STRING
151 explicit QUrl(const QString &url, ParsingMode mode = TolerantMode);
152#else
153 QUrl(const QString &url, ParsingMode mode = TolerantMode);
154 QUrl &operator=(const QString &url);
155#endif
156 QUrl(QUrl &&other) noexcept : d(other.d)
157 { other.d = nullptr; }
158 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QUrl)
159 ~QUrl();
160
161 void swap(QUrl &other) noexcept { qt_ptr_swap(d, other.d); }
162
163 void setUrl(const QString &url, ParsingMode mode = TolerantMode);
164 QString url(FormattingOptions options = FormattingOptions(PrettyDecoded)) const;
165 QString toString(FormattingOptions options = FormattingOptions(PrettyDecoded)) const;
166 QString toDisplayString(FormattingOptions options = FormattingOptions(PrettyDecoded)) const;
167 [[nodiscard]] QUrl adjusted(FormattingOptions options) const;
168
169 QByteArray toEncoded(FormattingOptions options = FullyEncoded) const;
170#if QT_CORE_REMOVED_SINCE(6, 7)
171 static QUrl fromEncoded(const QByteArray &url, ParsingMode mode = TolerantMode);
172#endif
173 static QUrl fromEncoded(QByteArrayView input, ParsingMode mode = TolerantMode);
174
175 enum UserInputResolutionOption {
176 DefaultResolution,
177 AssumeLocalFile
178 };
179 Q_DECLARE_FLAGS(UserInputResolutionOptions, UserInputResolutionOption)
180
181 static QUrl fromUserInput(const QString &userInput, const QString &workingDirectory = QString(),
182 UserInputResolutionOptions options = DefaultResolution);
183
184 bool isValid() const;
185 QString errorString() const;
186
187 bool isEmpty() const;
188 void clear();
189
190 void setScheme(const QString &scheme);
191 QString scheme() const;
192
193 void setAuthority(const QString &authority, ParsingMode mode = TolerantMode);
194 QString authority(ComponentFormattingOptions options = PrettyDecoded) const;
195
196 void setUserInfo(const QString &userInfo, ParsingMode mode = TolerantMode);
197 QString userInfo(ComponentFormattingOptions options = PrettyDecoded) const;
198
199 void setUserName(const QString &userName, ParsingMode mode = DecodedMode);
200 QString userName(ComponentFormattingOptions options = FullyDecoded) const;
201
202 void setPassword(const QString &password, ParsingMode mode = DecodedMode);
203 QString password(ComponentFormattingOptions = FullyDecoded) const;
204
205 void setHost(const QString &host, ParsingMode mode = DecodedMode);
206 QString host(ComponentFormattingOptions = FullyDecoded) const;
207
208 void setPort(int port);
209 int port(int defaultPort = -1) const;
210
211 void setPath(const QString &path, ParsingMode mode = DecodedMode);
212 QString path(ComponentFormattingOptions options = FullyDecoded) const;
213 QString fileName(ComponentFormattingOptions options = FullyDecoded) const;
214
215 bool hasQuery() const;
216 void setQuery(const QString &query, ParsingMode mode = TolerantMode);
217 void setQuery(const QUrlQuery &query);
218 QString query(ComponentFormattingOptions = PrettyDecoded) const;
219
220 bool hasFragment() const;
221 QString fragment(ComponentFormattingOptions options = PrettyDecoded) const;
222 void setFragment(const QString &fragment, ParsingMode mode = TolerantMode);
223
224 [[nodiscard]] QUrl resolved(const QUrl &relative) const;
225
226 bool isRelative() const;
227 bool isParentOf(const QUrl &url) const;
228
229 bool isLocalFile() const;
230 static QUrl fromLocalFile(const QString &localfile);
231 QString toLocalFile() const;
232
233 void detach();
234 bool isDetached() const;
235
236#if QT_CORE_REMOVED_SINCE(6, 8)
237 bool operator <(const QUrl &url) const;
238 bool operator ==(const QUrl &url) const;
239 bool operator !=(const QUrl &url) const;
240#endif
241
242 bool matches(const QUrl &url, FormattingOptions options) const;
243
244 static QString fromPercentEncoding(const QByteArray &);
245 static QByteArray toPercentEncoding(const QString &,
246 const QByteArray &exclude = QByteArray(),
247 const QByteArray &include = QByteArray());
248#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
249 static QUrl fromCFURL(CFURLRef url);
250 CFURLRef toCFURL() const Q_DECL_CF_RETURNS_RETAINED;
251 static QUrl fromNSURL(const NSURL *url);
252 NSURL *toNSURL() const Q_DECL_NS_RETURNS_AUTORELEASED;
253#endif
254
255 enum AceProcessingOption : unsigned int {
256 IgnoreIDNWhitelist = 0x1,
257 AceTransitionalProcessing = 0x2,
258 };
259 Q_DECLARE_FLAGS(AceProcessingOptions, AceProcessingOption)
260
261#if QT_CORE_REMOVED_SINCE(6, 3)
262 static QString fromAce(const QByteArray &);
263 static QByteArray toAce(const QString &);
264#endif
265 static QString fromAce(const QByteArray &domain, AceProcessingOptions options = {});
266 static QByteArray toAce(const QString &domain, AceProcessingOptions options = {});
267
268 static QStringList idnWhitelist();
269 static QStringList toStringList(const QList<QUrl> &uris, FormattingOptions options = FormattingOptions(PrettyDecoded));
270 static QList<QUrl> fromStringList(const QStringList &uris, ParsingMode mode = TolerantMode);
271
272 static void setIdnWhitelist(const QStringList &);
273 friend Q_CORE_EXPORT size_t qHash(const QUrl &url, size_t seed) noexcept;
274
275private:
276 friend Q_CORE_EXPORT bool comparesEqual(const QUrl &lhs, const QUrl &rhs);
277 friend Q_CORE_EXPORT Qt::weak_ordering
278 compareThreeWay(const QUrl &lhs, const QUrl &rhs);
279 Q_DECLARE_WEAKLY_ORDERED_NON_NOEXCEPT(QUrl)
280
281 void detachToClear();
282
283 QUrlPrivate *d;
284 friend class QUrlQuery;
285
286public:
287 typedef QUrlPrivate * DataPtr;
288 inline DataPtr &data_ptr() { return d; }
289};
290
292Q_DECLARE_OPERATORS_FOR_FLAGS(QUrl::ComponentFormattingOptions)
293//Q_DECLARE_OPERATORS_FOR_FLAGS(QUrl::FormattingOptions)
294Q_DECLARE_OPERATORS_FOR_FLAGS(QUrl::AceProcessingOptions)
295
296#ifndef Q_QDOC
297constexpr inline QUrl::FormattingOptions operator|(QUrl::UrlFormattingOption f1, QUrl::UrlFormattingOption f2)
298{ return QUrl::FormattingOptions(f1) | f2; }
299constexpr inline QUrl::FormattingOptions operator|(QUrl::UrlFormattingOption f1, QUrl::FormattingOptions f2)
300{ return f2 | f1; }
301constexpr inline QIncompatibleFlag operator|(QUrl::UrlFormattingOption f1, int f2)
302{ return QIncompatibleFlag(uint(f1) | f2); }
303
304// add operators for OR'ing the two types of flags
305inline QUrl::FormattingOptions &operator|=(QUrl::FormattingOptions &i, QUrl::ComponentFormattingOptions f)
306{ i |= QUrl::UrlFormattingOption(f.toInt()); return i; }
307constexpr inline QUrl::FormattingOptions operator|(QUrl::UrlFormattingOption i, QUrl::ComponentFormattingOption f)
308{ return i | QUrl::UrlFormattingOption(qToUnderlying(f)); }
309constexpr inline QUrl::FormattingOptions operator|(QUrl::UrlFormattingOption i, QUrl::ComponentFormattingOptions f)
310{ return i | QUrl::UrlFormattingOption(f.toInt()); }
311constexpr inline QUrl::FormattingOptions operator|(QUrl::ComponentFormattingOption f, QUrl::UrlFormattingOption i)
312{ return i | QUrl::UrlFormattingOption(qToUnderlying(f)); }
313constexpr inline QUrl::FormattingOptions operator|(QUrl::ComponentFormattingOptions f, QUrl::UrlFormattingOption i)
314{ return i | QUrl::UrlFormattingOption(f.toInt()); }
315constexpr inline QUrl::FormattingOptions operator|(QUrl::FormattingOptions i, QUrl::ComponentFormattingOptions f)
316{ return i | QUrl::UrlFormattingOption(f.toInt()); }
317constexpr inline QUrl::FormattingOptions operator|(QUrl::ComponentFormattingOption f, QUrl::FormattingOptions i)
318{ return i | QUrl::UrlFormattingOption(qToUnderlying(f)); }
319constexpr inline QUrl::FormattingOptions operator|(QUrl::ComponentFormattingOptions f, QUrl::FormattingOptions i)
320{ return i | QUrl::UrlFormattingOption(f.toInt()); }
321
322//inline QUrl::UrlFormattingOption &operator=(const QUrl::UrlFormattingOption &i, QUrl::ComponentFormattingOptions f)
323//{ i = int(f); f; }
324#endif // Q_QDOC
325
326#ifndef QT_NO_DATASTREAM
327Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QUrl &);
328Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QUrl &);
329#endif
330
331#ifndef QT_NO_DEBUG_STREAM
332Q_CORE_EXPORT QDebug operator<<(QDebug, const QUrl &);
333#endif
334
335QT_END_NAMESPACE
336
337#endif // QURL_H
\inmodule QtCore\reentrant
Definition qdatastream.h:50
constexpr QUrlTwoFlags(E2 f)
Definition qurl.h:35
constexpr QUrlTwoFlags(E1 f)
Definition qurl.h:34
constexpr QUrlTwoFlags(QFlag f)
Definition qurl.h:36
constexpr QUrlTwoFlags()
Definition qurl.h:33
\inmodule QtCore
Definition qurl.h:95
Combined button and popup list for selecting options.
Q_CORE_EXPORT bool qDecodeDataUrl(const QUrl &uri, QString &mimeType, QByteArray &payload)
Definition qdataurl.cpp:19
Q_CORE_EXPORT QDebug operator<<(QDebug debug, QDir::Filters filters)
Definition qdir.cpp:2620
constexpr QUrl::FormattingOptions operator|(QUrl::UrlFormattingOption f1, QUrl::FormattingOptions f2)
Definition qurl.h:299
QUrl::FormattingOptions & operator|=(QUrl::FormattingOptions &i, QUrl::ComponentFormattingOptions f)
Definition qurl.h:305