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
130 FullyEncoded = EncodeSpaces | EncodeUnicode | EncodeDelimiters | EncodeReserved,
131 FullyDecoded = FullyEncoded | DecodeReserved | 0x4000000
132 };
133 Q_DECLARE_FLAGS(ComponentFormattingOptions, ComponentFormattingOption)
134#ifdef Q_QDOC
135private:
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; };
140public:
141 Q_DECLARE_FLAGS(FormattingOptions, UrlFormattingOption)
142#else
143 typedef QUrlTwoFlags<UrlFormattingOption, ComponentFormattingOption> FormattingOptions;
144#endif
145
146 QUrl();
147 QUrl(const QUrl &copy) noexcept;
148 QUrl &operator =(const QUrl &copy) 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(d, 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
274private:
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
285public:
286 typedef QUrlPrivate * DataPtr;
287 inline DataPtr &data_ptr() { return d; }
288};
289
291Q_DECLARE_OPERATORS_FOR_FLAGS(QUrl::ComponentFormattingOptions)
292//Q_DECLARE_OPERATORS_FOR_FLAGS(QUrl::FormattingOptions)
293Q_DECLARE_OPERATORS_FOR_FLAGS(QUrl::AceProcessingOptions)
294
295#ifndef Q_QDOC
296constexpr inline QUrl::FormattingOptions operator|(QUrl::UrlFormattingOption f1, QUrl::UrlFormattingOption f2)
297{ return QUrl::FormattingOptions(f1) | f2; }
298constexpr inline QUrl::FormattingOptions operator|(QUrl::UrlFormattingOption f1, QUrl::FormattingOptions f2)
299{ return f2 | f1; }
300constexpr 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
304inline QUrl::FormattingOptions &operator|=(QUrl::FormattingOptions &i, QUrl::ComponentFormattingOptions f)
305{ i |= QUrl::UrlFormattingOption(f.toInt()); return i; }
306constexpr inline QUrl::FormattingOptions operator|(QUrl::UrlFormattingOption i, QUrl::ComponentFormattingOption f)
307{ return i | QUrl::UrlFormattingOption(qToUnderlying(f)); }
308constexpr inline QUrl::FormattingOptions operator|(QUrl::UrlFormattingOption i, QUrl::ComponentFormattingOptions f)
309{ return i | QUrl::UrlFormattingOption(f.toInt()); }
310constexpr inline QUrl::FormattingOptions operator|(QUrl::ComponentFormattingOption f, QUrl::UrlFormattingOption i)
311{ return i | QUrl::UrlFormattingOption(qToUnderlying(f)); }
312constexpr inline QUrl::FormattingOptions operator|(QUrl::ComponentFormattingOptions f, QUrl::UrlFormattingOption i)
313{ return i | QUrl::UrlFormattingOption(f.toInt()); }
314constexpr inline QUrl::FormattingOptions operator|(QUrl::FormattingOptions i, QUrl::ComponentFormattingOptions f)
315{ return i | QUrl::UrlFormattingOption(f.toInt()); }
316constexpr inline QUrl::FormattingOptions operator|(QUrl::ComponentFormattingOption f, QUrl::FormattingOptions i)
317{ return i | QUrl::UrlFormattingOption(qToUnderlying(f)); }
318constexpr 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
326Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QUrl &);
327Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QUrl &);
328#endif
329
330#ifndef QT_NO_DEBUG_STREAM
331Q_CORE_EXPORT QDebug operator<<(QDebug, const QUrl &);
332#endif
333
334QT_END_NAMESPACE
335
336#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
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:2568
constexpr QUrl::FormattingOptions operator|(QUrl::UrlFormattingOption f1, QUrl::FormattingOptions f2)
Definition qurl.h:298
QUrl::FormattingOptions & operator|=(QUrl::FormattingOptions &i, QUrl::ComponentFormattingOptions f)
Definition qurl.h:304