Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/io/qurlquery.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||
---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||
2 | ** | - | ||||||
3 | ** Copyright (C) 2016 Intel Corporation. | - | ||||||
4 | ** Contact: https://www.qt.io/licensing/ | - | ||||||
5 | ** | - | ||||||
6 | ** This file is part of the QtCore module of the Qt Toolkit. | - | ||||||
7 | ** | - | ||||||
8 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||
9 | ** Commercial License Usage | - | ||||||
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||
11 | ** accordance with the commercial license agreement provided with the | - | ||||||
12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||
13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||
14 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||
15 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||
16 | ** | - | ||||||
17 | ** GNU Lesser General Public License Usage | - | ||||||
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||
19 | ** General Public License version 3 as published by the Free Software | - | ||||||
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||
21 | ** packaging of this file. Please review the following information to | - | ||||||
22 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||
24 | ** | - | ||||||
25 | ** GNU General Public License Usage | - | ||||||
26 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||
27 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||
28 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||
29 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||
31 | ** included in the packaging of this file. Please review the following | - | ||||||
32 | ** information to ensure the GNU General Public License requirements will | - | ||||||
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||
35 | ** | - | ||||||
36 | ** $QT_END_LICENSE$ | - | ||||||
37 | ** | - | ||||||
38 | ****************************************************************************/ | - | ||||||
39 | - | |||||||
40 | #include "qurlquery.h" | - | ||||||
41 | #include "qurl_p.h" | - | ||||||
42 | - | |||||||
43 | #include <QtCore/qhashfunctions.h> | - | ||||||
44 | #include <QtCore/qstringlist.h> | - | ||||||
45 | - | |||||||
46 | #include <algorithm> | - | ||||||
47 | - | |||||||
48 | QT_BEGIN_NAMESPACE | - | ||||||
49 | - | |||||||
50 | /*! | - | ||||||
51 | \class QUrlQuery | - | ||||||
52 | \inmodule QtCore | - | ||||||
53 | \since 5.0 | - | ||||||
54 | - | |||||||
55 | \brief The QUrlQuery class provides a way to manipulate a key-value pairs in | - | ||||||
56 | a URL's query. | - | ||||||
57 | - | |||||||
58 | \reentrant | - | ||||||
59 | \ingroup io | - | ||||||
60 | \ingroup network | - | ||||||
61 | \ingroup shared | - | ||||||
62 | - | |||||||
63 | It is used to parse the query strings found in URLs like the following: | - | ||||||
64 | - | |||||||
65 | \image qurl-querystring.png | - | ||||||
66 | - | |||||||
67 | Query strings like the above are used to transmit options in the URL and are | - | ||||||
68 | usually decoded into multiple key-value pairs. The one above would contain | - | ||||||
69 | two entries in its list, with keys "type" and "color". QUrlQuery can also be | - | ||||||
70 | used to create a query string suitable for use in QUrl::setQuery() from the | - | ||||||
71 | individual components of the query. | - | ||||||
72 | - | |||||||
73 | The most common way of parsing a query string is to initialize it in the | - | ||||||
74 | constructor by passing it the query string. Otherwise, the setQuery() method | - | ||||||
75 | can be used to set the query to be parsed. That method can also be used to | - | ||||||
76 | parse a query with non-standard delimiters, after having set them using the | - | ||||||
77 | setQueryDelimiters() function. | - | ||||||
78 | - | |||||||
79 | The encoded query string can be obtained again using query(). This will take | - | ||||||
80 | all the internally-stored items and encode the string using the delimiters. | - | ||||||
81 | - | |||||||
82 | \section1 Encoding | - | ||||||
83 | - | |||||||
84 | All of the getter methods in QUrlQuery support an optional parameter of type | - | ||||||
85 | QUrl::ComponentFormattingOptions, including query(), which dictate how to | - | ||||||
86 | encode the data in question. Except for QUrl::FullyDecoded, the returned value must | - | ||||||
87 | still be considered a percent-encoded string, as there are certain values | - | ||||||
88 | which cannot be expressed in decoded form (like control characters, byte | - | ||||||
89 | sequences not decodable to UTF-8). For that reason, the percent character is | - | ||||||
90 | always represented by the string "%25". | - | ||||||
91 | - | |||||||
92 | \section2 Handling of spaces and plus ("+") | - | ||||||
93 | - | |||||||
94 | Web browsers usually encode spaces found in HTML FORM elements to a plus sign | - | ||||||
95 | ("+") and plus signs to its percent-encoded form (%2B). However, the Internet | - | ||||||
96 | specifications governing URLs do not consider spaces and the plus character | - | ||||||
97 | equivalent. | - | ||||||
98 | - | |||||||
99 | For that reason, QUrlQuery never encodes the space character to "+" and will | - | ||||||
100 | never decode "+" to a space character. Instead, space characters will be | - | ||||||
101 | rendered "%20" in encoded form. | - | ||||||
102 | - | |||||||
103 | To support encoding like that of HTML forms, QUrlQuery also never decodes the | - | ||||||
104 | "%2B" sequence to a plus sign nor encode a plus sign. In fact, any "%2B" or | - | ||||||
105 | "+" sequences found in the keys, values, or query string are left exactly | - | ||||||
106 | like written (except for the uppercasing of "%2b" to "%2B"). | - | ||||||
107 | - | |||||||
108 | \section2 Full decoding | - | ||||||
109 | - | |||||||
110 | With QUrl::FullyDecoded formatting, all percent-encoded sequences will be | - | ||||||
111 | decoded fully and the '%' character is used to represent itself. | - | ||||||
112 | QUrl::FullyDecoded should be used with care, since it may cause data loss. | - | ||||||
113 | See the documentation of QUrl::FullyDecoded for information on what data may | - | ||||||
114 | be lost. | - | ||||||
115 | - | |||||||
116 | This formatting mode should be used only when dealing with text presented to | - | ||||||
117 | the user in contexts where percent-encoding is not desired. Note that | - | ||||||
118 | QUrlQuery setters and query methods do not support the counterpart | - | ||||||
119 | QUrl::DecodedMode parsing, so using QUrl::FullyDecoded to obtain a listing of | - | ||||||
120 | keys may result in keys not found in the object. | - | ||||||
121 | - | |||||||
122 | \section1 Non-standard delimiters | - | ||||||
123 | - | |||||||
124 | By default, QUrlQuery uses an equal sign ("=") to separate a key from its | - | ||||||
125 | value, and an ampersand ("&") to separate key-value pairs from each other. It | - | ||||||
126 | is possible to change the delimiters that QUrlQuery uses for parsing and for | - | ||||||
127 | reconstructing the query by calling setQueryDelimiters(). | - | ||||||
128 | - | |||||||
129 | Non-standard delimiters should be chosen from among what RFC 3986 calls | - | ||||||
130 | "sub-delimiters". They are: | - | ||||||
131 | - | |||||||
132 | \code | - | ||||||
133 | sub-delims = "!" / "$" / "&" / "'" / "(" / ")" | - | ||||||
134 | / "*" / "+" / "," / ";" / "=" | - | ||||||
135 | \endcode | - | ||||||
136 | - | |||||||
137 | Use of other characters is not supported and may result in unexpected | - | ||||||
138 | behaviour. QUrlQuery does not verify that you passed a valid delimiter. | - | ||||||
139 | - | |||||||
140 | \sa QUrl | - | ||||||
141 | */ | - | ||||||
142 | - | |||||||
143 | /*! | - | ||||||
144 | \fn QUrlQuery &QUrlQuery::operator=(QUrlQuery &&other) | - | ||||||
145 | - | |||||||
146 | Move-assigns \a other to this QUrlQuery instance. | - | ||||||
147 | - | |||||||
148 | \since 5.2 | - | ||||||
149 | */ | - | ||||||
150 | - | |||||||
151 | typedef QList<QPair<QString, QString> > Map; | - | ||||||
152 | - | |||||||
153 | class QUrlQueryPrivate : public QSharedData | - | ||||||
154 | { | - | ||||||
155 | public: | - | ||||||
156 | QUrlQueryPrivate(const QString &query = QString()) | - | ||||||
157 | : valueDelimiter(QUrlQuery::defaultQueryValueDelimiter()), | - | ||||||
158 | pairDelimiter(QUrlQuery::defaultQueryPairDelimiter()) | - | ||||||
159 | { if (!query.isEmpty()) setQuery(query); } | - | ||||||
160 | - | |||||||
161 | QString recodeFromUser(const QString &input) const; | - | ||||||
162 | QString recodeToUser(const QString &input, QUrl::ComponentFormattingOptions encoding) const; | - | ||||||
163 | - | |||||||
164 | void setQuery(const QString &query); | - | ||||||
165 | - | |||||||
166 | void addQueryItem(const QString &key, const QString &value) | - | ||||||
167 | { itemList.append(qMakePair(recodeFromUser(key), recodeFromUser(value))); } | - | ||||||
168 | int findRecodedKey(const QString &key, int from = 0) const | - | ||||||
169 | { | - | ||||||
170 | for (int i = from; i < itemList.size(); ++i) | - | ||||||
171 | if (itemList.at(i).first == key) | - | ||||||
172 | return i; | - | ||||||
173 | return itemList.size(); | - | ||||||
174 | } | - | ||||||
175 | Map::const_iterator findKey(const QString &key) const | - | ||||||
176 | { return itemList.constBegin() + findRecodedKey(recodeFromUser(key)); } | - | ||||||
177 | Map::iterator findKey(const QString &key) | - | ||||||
178 | { return itemList.begin() + findRecodedKey(recodeFromUser(key)); } | - | ||||||
179 | - | |||||||
180 | Map itemList; | - | ||||||
181 | QChar valueDelimiter; | - | ||||||
182 | QChar pairDelimiter; | - | ||||||
183 | }; | - | ||||||
184 | - | |||||||
185 | template<> void QSharedDataPointer<QUrlQueryPrivate>::detach() | - | ||||||
186 | { | - | ||||||
187 | if (d && d->ref.load() == 1) | - | ||||||
188 | return; | - | ||||||
189 | QUrlQueryPrivate *x = (d ? new QUrlQueryPrivate(*d) | - | ||||||
190 | : new QUrlQueryPrivate); | - | ||||||
191 | x->ref.ref(); | - | ||||||
192 | if (d && !d->ref.deref()) | - | ||||||
193 | delete d; | - | ||||||
194 | d = x; | - | ||||||
195 | } | - | ||||||
196 | - | |||||||
197 | // Here's how we do the encoding in QUrlQuery | - | ||||||
198 | // The RFC says these are the delimiters: | - | ||||||
199 | // gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@" | - | ||||||
200 | // sub-delims = "!" / "$" / "&" / "'" / "(" / ")" | - | ||||||
201 | // / "*" / "+" / "," / ";" / "=" | - | ||||||
202 | // And the definition of query is: | - | ||||||
203 | // query = *( pchar / "/" / "?" ) | - | ||||||
204 | // pchar = unreserved / pct-encoded / sub-delims / ":" / "@" | - | ||||||
205 | // | - | ||||||
206 | // The strict definition of query says that it can have unencoded any | - | ||||||
207 | // unreserved, sub-delim, ":", "@", "/" and "?". Or, by exclusion, excluded | - | ||||||
208 | // delimiters are "#", "[" and "]" -- if those are present, they must be | - | ||||||
209 | // percent-encoded. The fact that "[" and "]" should be encoded is probably a | - | ||||||
210 | // mistake in the spec, so we ignore it and leave the decoded. | - | ||||||
211 | // | - | ||||||
212 | // The internal storage in the Map is equivalent to PrettyDecoded. That means | - | ||||||
213 | // the getter methods, when called with the default encoding value, will not | - | ||||||
214 | // have to recode anything (except for toString()). | - | ||||||
215 | // | - | ||||||
216 | // QUrlQuery handling of delimiters is quite simple: we never touch any of | - | ||||||
217 | // them, except for the "#" character and the pair and value delimiters. Those | - | ||||||
218 | // are always kept in their decoded forms. | - | ||||||
219 | // | - | ||||||
220 | // But when recreating the query string, in toString(), we must take care of | - | ||||||
221 | // the special delimiters: the pair and value delimiters, as well as the "#" | - | ||||||
222 | // character if unambiguous decoding is requested. | - | ||||||
223 | - | |||||||
224 | #define decode(x) ushort(x) | - | ||||||
225 | #define leave(x) ushort(0x100 | (x)) | - | ||||||
226 | #define encode(x) ushort(0x200 | (x)) | - | ||||||
227 | - | |||||||
228 | inline QString QUrlQueryPrivate::recodeFromUser(const QString &input) const | - | ||||||
229 | { | - | ||||||
230 | // note: duplicated in setQuery() | - | ||||||
231 | QString output; | - | ||||||
232 | ushort prettyDecodedActions[] = { | - | ||||||
233 | decode(pairDelimiter.unicode()), | - | ||||||
234 | decode(valueDelimiter.unicode()), | - | ||||||
235 | decode('#'), | - | ||||||
236 | 0 | - | ||||||
237 | }; | - | ||||||
238 | if (qt_urlRecode(output, input.constData(), input.constData() + input.length(), | - | ||||||
239 | QUrl::DecodeReserved, | - | ||||||
240 | prettyDecodedActions)) | - | ||||||
241 | return output; | - | ||||||
242 | return input; | - | ||||||
243 | } | - | ||||||
244 | - | |||||||
245 | inline bool idempotentRecodeToUser(QUrl::ComponentFormattingOptions encoding) | - | ||||||
246 | { | - | ||||||
247 | return encoding == QUrl::PrettyDecoded; | - | ||||||
248 | } | - | ||||||
249 | - | |||||||
250 | inline QString QUrlQueryPrivate::recodeToUser(const QString &input, QUrl::ComponentFormattingOptions encoding) const | - | ||||||
251 | { | - | ||||||
252 | // our internal formats are stored in "PrettyDecoded" form | - | ||||||
253 | // and there are no ambiguous characters | - | ||||||
254 | if (idempotentRecodeToUser(encoding)) | - | ||||||
255 | return input; | - | ||||||
256 | - | |||||||
257 | if (!(encoding & QUrl::EncodeDelimiters)) { | - | ||||||
258 | QString output; | - | ||||||
259 | if (qt_urlRecode(output, input.constData(), input.constData() + input.length(), | - | ||||||
260 | encoding, 0)) | - | ||||||
261 | return output; | - | ||||||
262 | return input; | - | ||||||
263 | } | - | ||||||
264 | - | |||||||
265 | // re-encode the "#" character and the query delimiter pair | - | ||||||
266 | ushort actions[] = { encode(pairDelimiter.unicode()), encode(valueDelimiter.unicode()), | - | ||||||
267 | encode('#'), 0 }; | - | ||||||
268 | QString output; | - | ||||||
269 | if (qt_urlRecode(output, input.constData(), input.constData() + input.length(), encoding, actions)) | - | ||||||
270 | return output; | - | ||||||
271 | return input; | - | ||||||
272 | } | - | ||||||
273 | - | |||||||
274 | void QUrlQueryPrivate::setQuery(const QString &query) | - | ||||||
275 | { | - | ||||||
276 | ushort prettyDecodedActions[] = { | - | ||||||
277 | decode(pairDelimiter.unicode()), | - | ||||||
278 | decode(valueDelimiter.unicode()), | - | ||||||
279 | decode('#'), | - | ||||||
280 | 0 | - | ||||||
281 | }; | - | ||||||
282 | - | |||||||
283 | itemList.clear(); | - | ||||||
284 | const QChar *pos = query.constData(); | - | ||||||
285 | const QChar *const end = pos + query.size(); | - | ||||||
286 | while (pos != end) { | - | ||||||
287 | const QChar *begin = pos; | - | ||||||
288 | const QChar *delimiter = 0; | - | ||||||
289 | while (pos != end) { | - | ||||||
290 | // scan for the component parts of this pair | - | ||||||
291 | if (!delimiter && pos->unicode() == valueDelimiter) | - | ||||||
292 | delimiter = pos; | - | ||||||
293 | if (pos->unicode() == pairDelimiter) | - | ||||||
294 | break; | - | ||||||
295 | ++pos; | - | ||||||
296 | } | - | ||||||
297 | if (!delimiter) | - | ||||||
298 | delimiter = pos; | - | ||||||
299 | - | |||||||
300 | // pos is the end of this pair (the end of the string or the pair delimiter) | - | ||||||
301 | // delimiter points to the value delimiter or to the end of this pair | - | ||||||
302 | - | |||||||
303 | QString key; | - | ||||||
304 | if (!qt_urlRecode(key, begin, delimiter, | - | ||||||
305 | QUrl::DecodeReserved, | - | ||||||
306 | prettyDecodedActions)) | - | ||||||
307 | key = QString(begin, delimiter - begin); | - | ||||||
308 | - | |||||||
309 | if (delimiter == pos) { | - | ||||||
310 | // the value delimiter wasn't found, store a null value | - | ||||||
311 | itemList.append(qMakePair(key, QString())); | - | ||||||
312 | } else if (delimiter + 1 == pos) { | - | ||||||
313 | // if the delimiter was found but the value is empty, store empty-but-not-null | - | ||||||
314 | itemList.append(qMakePair(key, QString(0, Qt::Uninitialized))); | - | ||||||
315 | } else { | - | ||||||
316 | QString value; | - | ||||||
317 | if (!qt_urlRecode(value, delimiter + 1, pos, | - | ||||||
318 | QUrl::DecodeReserved, | - | ||||||
319 | prettyDecodedActions)) | - | ||||||
320 | value = QString(delimiter + 1, pos - delimiter - 1); | - | ||||||
321 | itemList.append(qMakePair(key, value)); | - | ||||||
322 | } | - | ||||||
323 | - | |||||||
324 | if (pos != end) | - | ||||||
325 | ++pos; | - | ||||||
326 | } | - | ||||||
327 | } | - | ||||||
328 | - | |||||||
329 | // allow QUrlQueryPrivate to detach from null | - | ||||||
330 | template <> inline QUrlQueryPrivate * | - | ||||||
331 | QSharedDataPointer<QUrlQueryPrivate>::clone() | - | ||||||
332 | { | - | ||||||
333 | return d ? new QUrlQueryPrivate(*d) : new QUrlQueryPrivate; | - | ||||||
334 | } | - | ||||||
335 | - | |||||||
336 | /*! | - | ||||||
337 | Constructs an empty QUrlQuery object. A query can be set afterwards by | - | ||||||
338 | calling setQuery() or items can be added by using addQueryItem(). | - | ||||||
339 | - | |||||||
340 | \sa setQuery(), addQueryItem() | - | ||||||
341 | */ | - | ||||||
342 | QUrlQuery::QUrlQuery() | - | ||||||
343 | : d(0) | - | ||||||
344 | { | - | ||||||
345 | } | - | ||||||
346 | - | |||||||
347 | /*! | - | ||||||
348 | Constructs a QUrlQuery object and parses the \a queryString query string, | - | ||||||
349 | using the default query delimiters. To parse a query string using other | - | ||||||
350 | delimiters, you should first set them using setQueryDelimiters() and then | - | ||||||
351 | set the query with setQuery(). | - | ||||||
352 | */ | - | ||||||
353 | QUrlQuery::QUrlQuery(const QString &queryString) | - | ||||||
354 | : d(queryString.isEmpty() ? 0 : new QUrlQueryPrivate(queryString)) | - | ||||||
355 | { | - | ||||||
356 | } | - | ||||||
357 | - | |||||||
358 | /*! | - | ||||||
359 | Constructs a QUrlQuery object and parses the query string found in the \a | - | ||||||
360 | url URL, using the default query delimiters. To parse a query string using | - | ||||||
361 | other delimiters, you should first set them using setQueryDelimiters() and | - | ||||||
362 | then set the query with setQuery(). | - | ||||||
363 | - | |||||||
364 | \sa QUrl::query() | - | ||||||
365 | */ | - | ||||||
366 | QUrlQuery::QUrlQuery(const QUrl &url) | - | ||||||
367 | : d(0) | - | ||||||
368 | { | - | ||||||
369 | // use internals to avoid unnecessary recoding | - | ||||||
370 | // ### FIXME: actually do it | - | ||||||
371 | if (url.hasQuery()) | - | ||||||
372 | d = new QUrlQueryPrivate(url.query()); | - | ||||||
373 | } | - | ||||||
374 | - | |||||||
375 | /*! | - | ||||||
376 | Copies the contents of the \a other QUrlQuery object, including the query | - | ||||||
377 | delimiters. | - | ||||||
378 | */ | - | ||||||
379 | QUrlQuery::QUrlQuery(const QUrlQuery &other) | - | ||||||
380 | : d(other.d) | - | ||||||
381 | { | - | ||||||
382 | } | - | ||||||
383 | - | |||||||
384 | /*! | - | ||||||
385 | Copies the contents of the \a other QUrlQuery object, including the query | - | ||||||
386 | delimiters. | - | ||||||
387 | */ | - | ||||||
388 | QUrlQuery &QUrlQuery::operator =(const QUrlQuery &other) | - | ||||||
389 | { | - | ||||||
390 | d = other.d; | - | ||||||
391 | return *this; | - | ||||||
392 | } | - | ||||||
393 | - | |||||||
394 | /*! | - | ||||||
395 | \fn void QUrlQuery::swap(QUrlQuery &other) | - | ||||||
396 | - | |||||||
397 | Swaps this URL query instance with \a other. This function is very | - | ||||||
398 | fast and never fails. | - | ||||||
399 | */ | - | ||||||
400 | - | |||||||
401 | /*! | - | ||||||
402 | Destroys this QUrlQuery object. | - | ||||||
403 | */ | - | ||||||
404 | QUrlQuery::~QUrlQuery() | - | ||||||
405 | { | - | ||||||
406 | // d auto-deletes | - | ||||||
407 | } | - | ||||||
408 | - | |||||||
409 | /*! | - | ||||||
410 | Returns \c true if this object and the \a other object contain the same | - | ||||||
411 | contents, in the same order, and use the same query delimiters. | - | ||||||
412 | */ | - | ||||||
413 | bool QUrlQuery::operator ==(const QUrlQuery &other) const | - | ||||||
414 | { | - | ||||||
415 | if (d == other.d) | - | ||||||
416 | return true; | - | ||||||
417 | if (d && other.d) | - | ||||||
418 | // keep in sync with qHash(QUrlQuery): | - | ||||||
419 | return d->valueDelimiter == other.d->valueDelimiter && | - | ||||||
420 | d->pairDelimiter == other.d->pairDelimiter && | - | ||||||
421 | d->itemList == other.d->itemList; | - | ||||||
422 | return false; | - | ||||||
423 | } | - | ||||||
424 | - | |||||||
425 | /*! | - | ||||||
426 | \since 5.6 | - | ||||||
427 | \relates QUrlQuery | - | ||||||
428 | - | |||||||
429 | Returns the hash value for \a key, | - | ||||||
430 | using \a seed to seed the calculation. | - | ||||||
431 | */ | - | ||||||
432 | uint qHash(const QUrlQuery &key, uint seed) Q_DECL_NOTHROW | - | ||||||
433 | { | - | ||||||
434 | if (const QUrlQueryPrivate *d = key.d) {
| 2-4 | ||||||
435 | QtPrivate::QHashCombine hash; | - | ||||||
436 | // keep in sync with operator==: | - | ||||||
437 | seed = hash(seed, d->valueDelimiter); | - | ||||||
438 | seed = hash(seed, d->pairDelimiter); | - | ||||||
439 | seed = hash(seed, d->itemList); | - | ||||||
440 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||
441 | return seed; executed 6 times by 1 test: return seed; Executed by:
| 6 | ||||||
442 | } | - | ||||||
443 | - | |||||||
444 | /*! | - | ||||||
445 | Returns \c true if this QUrlQuery object contains no key-value pairs, such as | - | ||||||
446 | after being default-constructed or after parsing an empty query string. | - | ||||||
447 | - | |||||||
448 | \sa setQuery(), clear() | - | ||||||
449 | */ | - | ||||||
450 | bool QUrlQuery::isEmpty() const | - | ||||||
451 | { | - | ||||||
452 | return d ? d->itemList.isEmpty() : true; | - | ||||||
453 | } | - | ||||||
454 | - | |||||||
455 | /*! | - | ||||||
456 | \internal | - | ||||||
457 | */ | - | ||||||
458 | bool QUrlQuery::isDetached() const | - | ||||||
459 | { | - | ||||||
460 | return d && d->ref.load() == 1; | - | ||||||
461 | } | - | ||||||
462 | - | |||||||
463 | /*! | - | ||||||
464 | Clears this QUrlQuery object by removing all of the key-value pairs | - | ||||||
465 | currently stored. If the query delimiters have been changed, this function | - | ||||||
466 | will leave them with their changed values. | - | ||||||
467 | - | |||||||
468 | \sa isEmpty(), setQueryDelimiters() | - | ||||||
469 | */ | - | ||||||
470 | void QUrlQuery::clear() | - | ||||||
471 | { | - | ||||||
472 | if (d.constData()) | - | ||||||
473 | d->itemList.clear(); | - | ||||||
474 | } | - | ||||||
475 | - | |||||||
476 | /*! | - | ||||||
477 | Parses the query string in \a queryString and sets the internal items to | - | ||||||
478 | the values found there. If any delimiters have been specified with | - | ||||||
479 | setQueryDelimiters(), this function will use them instead of the default | - | ||||||
480 | delimiters to parse the string. | - | ||||||
481 | */ | - | ||||||
482 | void QUrlQuery::setQuery(const QString &queryString) | - | ||||||
483 | { | - | ||||||
484 | d->setQuery(queryString); | - | ||||||
485 | } | - | ||||||
486 | - | |||||||
487 | static void recodeAndAppend(QString &to, const QString &input, | - | ||||||
488 | QUrl::ComponentFormattingOptions encoding, const ushort *tableModifications) | - | ||||||
489 | { | - | ||||||
490 | if (!qt_urlRecode(to, input.constData(), input.constData() + input.length(), encoding, tableModifications)) | - | ||||||
491 | to += input; | - | ||||||
492 | } | - | ||||||
493 | - | |||||||
494 | /*! | - | ||||||
495 | Returns the reconstructed query string, formed from the key-value pairs | - | ||||||
496 | currently stored in this QUrlQuery object and separated by the query | - | ||||||
497 | delimiters chosen for this object. The keys and values are encoded using | - | ||||||
498 | the options given by the \a encoding parameter. | - | ||||||
499 | - | |||||||
500 | For this function, the only ambiguous delimiter is the hash ("#"), as in | - | ||||||
501 | URLs it is used to separate the query string from the fragment that may | - | ||||||
502 | follow. | - | ||||||
503 | - | |||||||
504 | The order of the key-value pairs in the returned string is exactly the same | - | ||||||
505 | as in the original query. | - | ||||||
506 | - | |||||||
507 | \sa setQuery(), QUrl::setQuery(), QUrl::fragment(), {encoding}{Encoding} | - | ||||||
508 | */ | - | ||||||
509 | QString QUrlQuery::query(QUrl::ComponentFormattingOptions encoding) const | - | ||||||
510 | { | - | ||||||
511 | if (!d) | - | ||||||
512 | return QString(); | - | ||||||
513 | - | |||||||
514 | // unlike the component encoding, for the whole query we need to modify a little: | - | ||||||
515 | // - the "#" character is unambiguous, so we encode it in EncodeDelimiters mode | - | ||||||
516 | // - the query delimiter pair must always be encoded | - | ||||||
517 | - | |||||||
518 | // start with what's always encoded | - | ||||||
519 | ushort tableActions[] = { | - | ||||||
520 | encode(d->pairDelimiter.unicode()), // 0 | - | ||||||
521 | encode(d->valueDelimiter.unicode()), // 1 | - | ||||||
522 | 0, // 2 | - | ||||||
523 | 0 | - | ||||||
524 | }; | - | ||||||
525 | if (encoding & QUrl::EncodeDelimiters) { | - | ||||||
526 | tableActions[2] = encode('#'); | - | ||||||
527 | } | - | ||||||
528 | - | |||||||
529 | QString result; | - | ||||||
530 | Map::const_iterator it = d->itemList.constBegin(); | - | ||||||
531 | Map::const_iterator end = d->itemList.constEnd(); | - | ||||||
532 | - | |||||||
533 | { | - | ||||||
534 | int size = 0; | - | ||||||
535 | for ( ; it != end; ++it) | - | ||||||
536 | size += it->first.length() + 1 + it->second.length() + 1; | - | ||||||
537 | result.reserve(size + size / 4); | - | ||||||
538 | } | - | ||||||
539 | - | |||||||
540 | for (it = d->itemList.constBegin(); it != end; ++it) { | - | ||||||
541 | if (!result.isEmpty()) | - | ||||||
542 | result += QChar(d->pairDelimiter); | - | ||||||
543 | recodeAndAppend(result, it->first, encoding, tableActions); | - | ||||||
544 | if (!it->second.isNull()) { | - | ||||||
545 | result += QChar(d->valueDelimiter); | - | ||||||
546 | recodeAndAppend(result, it->second, encoding, tableActions); | - | ||||||
547 | } | - | ||||||
548 | } | - | ||||||
549 | return result; | - | ||||||
550 | } | - | ||||||
551 | - | |||||||
552 | /*! | - | ||||||
553 | Sets the characters used for delimiting between keys and values, | - | ||||||
554 | and between key-value pairs in the URL's query string. The default | - | ||||||
555 | value delimiter is '=' and the default pair delimiter is '&'. | - | ||||||
556 | - | |||||||
557 | \image qurl-querystring.png | - | ||||||
558 | - | |||||||
559 | \a valueDelimiter will be used for separating keys from values, | - | ||||||
560 | and \a pairDelimiter will be used to separate key-value pairs. | - | ||||||
561 | Any occurrences of these delimiting characters in the encoded | - | ||||||
562 | representation of the keys and values of the query string are | - | ||||||
563 | percent encoded when returned in query(). | - | ||||||
564 | - | |||||||
565 | If \a valueDelimiter is set to '(' and \a pairDelimiter is ')', | - | ||||||
566 | the above query string would instead be represented like this: | - | ||||||
567 | - | |||||||
568 | \snippet code/src_corelib_io_qurl.cpp 4 | - | ||||||
569 | - | |||||||
570 | \note Non-standard delimiters should be chosen from among what RFC 3986 calls | - | ||||||
571 | "sub-delimiters". They are: | - | ||||||
572 | - | |||||||
573 | \code | - | ||||||
574 | sub-delims = "!" / "$" / "&" / "'" / "(" / ")" | - | ||||||
575 | / "*" / "+" / "," / ";" / "=" | - | ||||||
576 | \endcode | - | ||||||
577 | - | |||||||
578 | Use of other characters is not supported and may result in unexpected | - | ||||||
579 | behaviour. This method does not verify that you passed a valid delimiter. | - | ||||||
580 | - | |||||||
581 | \sa queryValueDelimiter(), queryPairDelimiter() | - | ||||||
582 | */ | - | ||||||
583 | void QUrlQuery::setQueryDelimiters(QChar valueDelimiter, QChar pairDelimiter) | - | ||||||
584 | { | - | ||||||
585 | d->valueDelimiter = valueDelimiter.unicode(); | - | ||||||
586 | d->pairDelimiter = pairDelimiter.unicode(); | - | ||||||
587 | } | - | ||||||
588 | - | |||||||
589 | /*! | - | ||||||
590 | Returns the character used to delimit between keys and values when | - | ||||||
591 | reconstructing the query string in query() or when parsing in setQuery(). | - | ||||||
592 | - | |||||||
593 | \sa setQueryDelimiters(), queryPairDelimiter() | - | ||||||
594 | */ | - | ||||||
595 | QChar QUrlQuery::queryValueDelimiter() const | - | ||||||
596 | { | - | ||||||
597 | return d ? d->valueDelimiter : defaultQueryValueDelimiter(); | - | ||||||
598 | } | - | ||||||
599 | - | |||||||
600 | /*! | - | ||||||
601 | Returns the character used to delimit between keys-value pairs when | - | ||||||
602 | reconstructing the query string in query() or when parsing in setQuery(). | - | ||||||
603 | - | |||||||
604 | \sa setQueryDelimiters(), queryValueDelimiter() | - | ||||||
605 | */ | - | ||||||
606 | QChar QUrlQuery::queryPairDelimiter() const | - | ||||||
607 | { | - | ||||||
608 | return d ? d->pairDelimiter : defaultQueryPairDelimiter(); | - | ||||||
609 | } | - | ||||||
610 | - | |||||||
611 | /*! | - | ||||||
612 | Sets the items in this QUrlQuery object to \a query. The order of the | - | ||||||
613 | elements in \a query is preserved. | - | ||||||
614 | - | |||||||
615 | \note This method does not treat spaces (ASCII 0x20) and plus ("+") signs | - | ||||||
616 | as the same, like HTML forms do. If you need spaces to be represented as | - | ||||||
617 | plus signs, use actual plus signs. | - | ||||||
618 | - | |||||||
619 | \sa queryItems(), isEmpty() | - | ||||||
620 | */ | - | ||||||
621 | void QUrlQuery::setQueryItems(const QList<QPair<QString, QString> > &query) | - | ||||||
622 | { | - | ||||||
623 | clear(); | - | ||||||
624 | if (query.isEmpty()) | - | ||||||
625 | return; | - | ||||||
626 | - | |||||||
627 | QUrlQueryPrivate *dd = d; | - | ||||||
628 | QList<QPair<QString, QString> >::const_iterator it = query.constBegin(), | - | ||||||
629 | end = query.constEnd(); | - | ||||||
630 | for ( ; it != end; ++it) | - | ||||||
631 | dd->addQueryItem(it->first, it->second); | - | ||||||
632 | } | - | ||||||
633 | - | |||||||
634 | /*! | - | ||||||
635 | Returns the query string of the URL, as a map of keys and values, using the | - | ||||||
636 | options specified in \a encoding to encode the items. The order of the | - | ||||||
637 | elements is the same as the one found in the query string or set with | - | ||||||
638 | setQueryItems(). | - | ||||||
639 | - | |||||||
640 | \sa setQueryItems(), {encoding}{Encoding} | - | ||||||
641 | */ | - | ||||||
642 | QList<QPair<QString, QString> > QUrlQuery::queryItems(QUrl::ComponentFormattingOptions encoding) const | - | ||||||
643 | { | - | ||||||
644 | if (!d) | - | ||||||
645 | return QList<QPair<QString, QString> >(); | - | ||||||
646 | if (idempotentRecodeToUser(encoding)) | - | ||||||
647 | return d->itemList; | - | ||||||
648 | - | |||||||
649 | QList<QPair<QString, QString> > result; | - | ||||||
650 | Map::const_iterator it = d->itemList.constBegin(); | - | ||||||
651 | Map::const_iterator end = d->itemList.constEnd(); | - | ||||||
652 | result.reserve(d->itemList.count()); | - | ||||||
653 | for ( ; it != end; ++it) | - | ||||||
654 | result << qMakePair(d->recodeToUser(it->first, encoding), | - | ||||||
655 | d->recodeToUser(it->second, encoding)); | - | ||||||
656 | return result; | - | ||||||
657 | } | - | ||||||
658 | - | |||||||
659 | /*! | - | ||||||
660 | Returns \c true if there is a query string pair whose key is equal | - | ||||||
661 | to \a key from the URL. | - | ||||||
662 | - | |||||||
663 | \sa addQueryItem(), queryItemValue() | - | ||||||
664 | */ | - | ||||||
665 | bool QUrlQuery::hasQueryItem(const QString &key) const | - | ||||||
666 | { | - | ||||||
667 | if (!d) | - | ||||||
668 | return false; | - | ||||||
669 | return d->findKey(key) != d->itemList.constEnd(); | - | ||||||
670 | } | - | ||||||
671 | - | |||||||
672 | /*! | - | ||||||
673 | Appends the pair \a key = \a value to the end of the query string of the | - | ||||||
674 | URL. This method does not overwrite existing items that might exist with | - | ||||||
675 | the same key. | - | ||||||
676 | - | |||||||
677 | \note This method does not treat spaces (ASCII 0x20) and plus ("+") signs | - | ||||||
678 | as the same, like HTML forms do. If you need spaces to be represented as | - | ||||||
679 | plus signs, use actual plus signs. | - | ||||||
680 | - | |||||||
681 | \sa hasQueryItem(), queryItemValue() | - | ||||||
682 | */ | - | ||||||
683 | void QUrlQuery::addQueryItem(const QString &key, const QString &value) | - | ||||||
684 | { | - | ||||||
685 | d->addQueryItem(key, value); | - | ||||||
686 | } | - | ||||||
687 | - | |||||||
688 | /*! | - | ||||||
689 | Returns the query value associated with key \a key from the URL, using the | - | ||||||
690 | options specified in \a encoding to encode the return value. If the key \a | - | ||||||
691 | key is not found, this function returns an empty string. If you need to | - | ||||||
692 | distinguish between an empty value and a non-existent key, you should check | - | ||||||
693 | for the key's presence first using hasQueryItem(). | - | ||||||
694 | - | |||||||
695 | If the key \a key is multiply defined, this function will return the first | - | ||||||
696 | one found, in the order they were present in the query string or added | - | ||||||
697 | using addQueryItem(). | - | ||||||
698 | - | |||||||
699 | \sa addQueryItem(), allQueryItemValues(), {encoding}{Encoding} | - | ||||||
700 | */ | - | ||||||
701 | QString QUrlQuery::queryItemValue(const QString &key, QUrl::ComponentFormattingOptions encoding) const | - | ||||||
702 | { | - | ||||||
703 | QString result; | - | ||||||
704 | if (d) { | - | ||||||
705 | Map::const_iterator it = d->findKey(key); | - | ||||||
706 | if (it != d->itemList.constEnd()) | - | ||||||
707 | result = d->recodeToUser(it->second, encoding); | - | ||||||
708 | } | - | ||||||
709 | return result; | - | ||||||
710 | } | - | ||||||
711 | - | |||||||
712 | /*! | - | ||||||
713 | Returns the a list of query string values whose key is equal to \a key from | - | ||||||
714 | the URL, using the options specified in \a encoding to encode the return | - | ||||||
715 | value. If the key \a key is not found, this function returns an empty list. | - | ||||||
716 | - | |||||||
717 | \sa queryItemValue(), addQueryItem() | - | ||||||
718 | */ | - | ||||||
719 | QStringList QUrlQuery::allQueryItemValues(const QString &key, QUrl::ComponentFormattingOptions encoding) const | - | ||||||
720 | { | - | ||||||
721 | QStringList result; | - | ||||||
722 | if (d) { | - | ||||||
723 | QString encodedKey = d->recodeFromUser(key); | - | ||||||
724 | int idx = d->findRecodedKey(encodedKey); | - | ||||||
725 | while (idx < d->itemList.size()) { | - | ||||||
726 | result << d->recodeToUser(d->itemList.at(idx).second, encoding); | - | ||||||
727 | idx = d->findRecodedKey(encodedKey, idx + 1); | - | ||||||
728 | } | - | ||||||
729 | } | - | ||||||
730 | return result; | - | ||||||
731 | } | - | ||||||
732 | - | |||||||
733 | /*! | - | ||||||
734 | Removes the query string pair whose key is equal to \a key from the URL. If | - | ||||||
735 | there are multiple items with a key equal to \a key, it removes the first | - | ||||||
736 | item in the order they were present in the query string or added with | - | ||||||
737 | addQueryItem(). | - | ||||||
738 | - | |||||||
739 | \sa removeAllQueryItems() | - | ||||||
740 | */ | - | ||||||
741 | void QUrlQuery::removeQueryItem(const QString &key) | - | ||||||
742 | { | - | ||||||
743 | if (d).constData()) {
| 0-6 | ||||||
744 | Map::iterator it = d->findKey(key); | - | ||||||
745 | if (it != d->itemList.end())
| 1-5 | ||||||
746 | d->itemList.erase(it); executed 5 times by 1 test: d->itemList.erase(it); Executed by:
| 5 | ||||||
747 | } executed 6 times by 1 test: end of block Executed by:
| 6 | ||||||
748 | } executed 6 times by 1 test: end of block Executed by:
| 6 | ||||||
749 | - | |||||||
750 | /*! | - | ||||||
751 | Removes all the query string pairs whose key is equal to \a key | - | ||||||
752 | from the URL. | - | ||||||
753 | - | |||||||
754 | \sa removeQueryItem() | - | ||||||
755 | */ | - | ||||||
756 | void QUrlQuery::removeAllQueryItems(const QString &key) | - | ||||||
757 | { | - | ||||||
758 | if (d.constData()) {
| 0-3 | ||||||
759 | const QString encodedKey = d->recodeFromUser(key); | - | ||||||
760 | Map::iterator itauto firstEqualsEncodedKey = [&encodedKey](const QPair<QString, QString> &item) { | - | ||||||
761 | return item.first == encodedKey; executed 14 times by 1 test: return item.first == encodedKey; Executed by:
| 14 | ||||||
762 | }; | - | ||||||
763 | const auto end = d->itemList.beginend(); | - | ||||||
764 | while (it !=d->itemList.end()) { | - | ||||||
iferase(it->first == encodedKey) | ||||||||
it =std::remove_if(d->itemList.erase(itbegin(), end, firstEqualsEncodedKey), end);else | ||||||||
++it; | ||||||||
} | ||||||||
765 | } executed 3 times by 1 test: end of block Executed by:
| 3 | ||||||
766 | } executed 3 times by 1 test: end of block Executed by:
| 3 | ||||||
767 | - | |||||||
768 | /*! | - | ||||||
769 | \fn QChar QUrlQuery::defaultQueryValueDelimiter() | - | ||||||
770 | Returns the default character for separating keys from values in the query, | - | ||||||
771 | an equal sign ("="). | - | ||||||
772 | - | |||||||
773 | \sa setQueryDelimiters(), queryValueDelimiter(), defaultQueryPairDelimiter() | - | ||||||
774 | */ | - | ||||||
775 | - | |||||||
776 | /*! | - | ||||||
777 | \fn QChar QUrlQuery::defaultQueryPairDelimiter() | - | ||||||
778 | Returns the default character for separating keys-value pairs from each | - | ||||||
779 | other, an ampersand ("&"). | - | ||||||
780 | - | |||||||
781 | \sa setQueryDelimiters(), queryPairDelimiter(), defaultQueryValueDelimiter() | - | ||||||
782 | */ | - | ||||||
783 | - | |||||||
784 | /*! | - | ||||||
785 | \typedef QUrlQuery::DataPtr | - | ||||||
786 | \internal | - | ||||||
787 | */ | - | ||||||
788 | - | |||||||
789 | /*! | - | ||||||
790 | \fn DataPtr &QUrlQuery::data_ptr() | - | ||||||
791 | \internal | - | ||||||
792 | */ | - | ||||||
793 | - | |||||||
794 | /*! | - | ||||||
795 | \fn QString QUrlQuery::toString(QUrl::ComponentFormattingOptions encoding = QUrl::PrettyDecoded) const | - | ||||||
796 | - | |||||||
797 | Returns this QUrlQuery as a QString. \a encoding can be used to specify the URL string encoding of the return value. | - | ||||||
798 | */ | - | ||||||
799 | - | |||||||
800 | /*! | - | ||||||
801 | \fn bool QUrlQuery::operator!=(const QUrlQuery &other) const | - | ||||||
802 | - | |||||||
803 | Returns \c true if \a other is not equal to this QUrlQuery. Otherwise, returns \c false. | - | ||||||
804 | - | |||||||
805 | \sa operator==() | - | ||||||
806 | */ | - | ||||||
807 | QT_END_NAMESPACE | - | ||||||
Source code | Switch to Preprocessed file |