This repository has been archived by the owner on Jan 2, 2023. It is now read-only.
/
qurl.cpp
6713 lines (5893 loc) · 224 KB
/
qurl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
/*!
\class QUrl
\brief The QUrl class provides a convenient interface for working
with URLs.
\reentrant
\ingroup io
\ingroup network
\ingroup shared
It can parse and construct URLs in both encoded and unencoded
form. QUrl also has support for internationalized domain names
(IDNs).
The most common way to use QUrl is to initialize it via the
constructor by passing a QString. Otherwise, setUrl() and
setEncodedUrl() can also be used.
URLs can be represented in two forms: encoded or unencoded. The
unencoded representation is suitable for showing to users, but
the encoded representation is typically what you would send to
a web server. For example, the unencoded URL
"http://b\uuml\c{}hler.example.com/List of applicants.xml" would be sent to the server as
"http://xn--bhler-kva.example.com/List%20of%20applicants.xml",
and this can be verified by calling the toEncoded() function.
A URL can also be constructed piece by piece by calling
setScheme(), setUserName(), setPassword(), setHost(), setPort(),
setPath(), setEncodedQuery() and setFragment(). Some convenience
functions are also available: setAuthority() sets the user name,
password, host and port. setUserInfo() sets the user name and
password at once.
Call isValid() to check if the URL is valid. This can be done at
any point during the constructing of a URL.
Constructing a query is particularly convenient through the use
of setQueryItems(), addQueryItem() and removeQueryItem(). Use
setQueryDelimiters() to customize the delimiters used for
generating the query string.
For the convenience of generating encoded URL strings or query
strings, there are two static functions called
fromPercentEncoding() and toPercentEncoding() which deal with
percent encoding and decoding of QStrings.
Calling isRelative() will tell whether or not the URL is
relative. A relative URL can be resolved by passing it as argument
to resolved(), which returns an absolute URL. isParentOf() is used
for determining whether one URL is a parent of another.
fromLocalFile() constructs a QUrl by parsing a local
file path. toLocalFile() converts a URL to a local file path.
The human readable representation of the URL is fetched with
toString(). This representation is appropriate for displaying a
URL to a user in unencoded form. The encoded form however, as
returned by toEncoded(), is for internal use, passing to web
servers, mail clients and so on.
QUrl conforms to the URI specification from
\l{RFC 3986} (Uniform Resource Identifier: Generic Syntax), and includes
scheme extensions from \l{RFC 1738} (Uniform Resource Locators). Case
folding rules in QUrl conform to \l{RFC 3491} (Nameprep: A Stringprep
Profile for Internationalized Domain Names (IDN)).
\section2 Character Conversions
Follow these rules to avoid erroneous character conversion when
dealing with URLs and strings:
\list
\o When creating an QString to contain a URL from a QByteArray or a
char*, always use QString::fromUtf8().
\o Favor the use of QUrl::fromEncoded() and QUrl::toEncoded() instead of
QUrl(string) and QUrl::toString() when converting a QUrl to or from
a string.
\endlist
\sa QUrlInfo
*/
/*!
\enum QUrl::ParsingMode
The parsing mode controls the way QUrl parses strings.
\value TolerantMode QUrl will try to correct some common errors in URLs.
This mode is useful when processing URLs entered by
users.
\value StrictMode Only valid URLs are accepted. This mode is useful for
general URL validation.
In TolerantMode, the parser corrects the following invalid input:
\list
\o Spaces and "%20": If an encoded URL contains a space, this will be
replaced with "%20". If a decoded URL contains "%20", this will be
replaced with a single space before the URL is parsed.
\o Single "%" characters: Any occurrences of a percent character "%" not
followed by exactly two hexadecimal characters (e.g., "13% coverage.html")
will be replaced by "%25".
\o Reserved and unreserved characters: An encoded URL should only
contain a few characters as literals; all other characters should
be percent-encoded. In TolerantMode, these characters will be
automatically percent-encoded where they are not allowed:
space / double-quote / "<" / ">" / "[" / "\" /
"]" / "^" / "`" / "{" / "|" / "}"
\endlist
*/
/*!
\enum QUrl::FormattingOption
The formatting options define how the URL is formatted when written out
as text.
\value None The format of the URL is unchanged.
\value RemoveScheme The scheme is removed from the URL.
\value RemovePassword Any password in the URL is removed.
\value RemoveUserInfo Any user information in the URL is removed.
\value RemovePort Any specified port is removed from the URL.
\value RemoveAuthority
\value RemovePath The URL's path is removed, leaving only the scheme,
host address, and port (if present).
\value RemoveQuery The query part of the URL (following a '?' character)
is removed.
\value RemoveFragment
\value StripTrailingSlash The trailing slash is removed if one is present.
Note that the case folding rules in \l{RFC 3491}{Nameprep}, which QUrl
conforms to, require host names to always be converted to lower case,
regardless of the Qt::FormattingOptions used.
*/
/*!
\fn uint qHash(const QUrl &url)
\since 4.7
\relates QUrl
Computes a hash key from the normalized version of \a url.
*/
#include "qplatformdefs.h"
#include "qurl.h"
#include "qatomic.h"
#include "qbytearray.h"
#include "qdir.h"
#include "qfile.h"
#include "qlist.h"
#ifndef QT_NO_REGEXP
#include "qregexp.h"
#endif
#include "qstring.h"
#include "qstringlist.h"
#include "qstack.h"
#include "qvarlengtharray.h"
#include "qdebug.h"
#if defined QT3_SUPPORT
#include "qfileinfo.h"
#endif
#ifndef QT_BOOTSTRAPPED
#include "qtldurl_p.h"
#endif
#if defined(Q_OS_WINCE_WM)
#pragma optimize("g", off)
#endif
#include "qmutex.h"
#include "private/qorderedmutexlocker_p.h"
QT_BEGIN_NAMESPACE
extern void q_normalizePercentEncoding(QByteArray *ba, const char *exclude);
extern void q_toPercentEncoding(QByteArray *ba, const char *exclude, const char *include = 0);
extern void q_fromPercentEncoding(QByteArray *ba);
static QByteArray toPercentEncodingHelper(const QString &s, const char *exclude, const char *include = 0)
{
if (s.isNull())
return QByteArray(); // null
QByteArray ba = s.toUtf8();
q_toPercentEncoding(&ba, exclude, include);
return ba;
}
static QString fromPercentEncodingHelper(const QByteArray &ba)
{
if (ba.isNull())
return QString(); // null
QByteArray copy = ba;
q_fromPercentEncoding(©);
return QString::fromUtf8(copy.constData(), copy.length());
}
static QString fromPercentEncodingMutable(QByteArray *ba)
{
if (ba->isNull())
return QString(); // null
q_fromPercentEncoding(ba);
return QString::fromUtf8(ba->constData(), ba->length());
}
// ### Qt 5: Consider accepting empty strings as valid. See task 144227.
//#define QURL_DEBUG
// implemented in qvsnprintf.cpp
Q_CORE_EXPORT int qsnprintf(char *str, size_t n, const char *fmt, ...);
// needed by the punycode encoder/decoder
#define Q_MAXINT ((uint)((uint)(-1)>>1))
static const uint base = 36;
static const uint tmin = 1;
static const uint tmax = 26;
static const uint skew = 38;
static const uint damp = 700;
static const uint initial_bias = 72;
static const uint initial_n = 128;
#define QURL_SETFLAG(a, b) { (a) |= (b); }
#define QURL_UNSETFLAG(a, b) { (a) &= ~(b); }
#define QURL_HASFLAG(a, b) (((a) & (b)) == (b))
struct QUrlErrorInfo {
inline QUrlErrorInfo() : _source(0), _message(0), _expected(0), _found(0)
{ }
const char *_source;
const char *_message;
char _expected;
char _found;
inline void setParams(const char *source, const char *message, char expected, char found)
{
_source = source;
_message = message;
_expected = expected;
_found = found;
}
};
struct QUrlParseData
{
const char *scheme;
int schemeLength;
const char *userInfo;
int userInfoDelimIndex;
int userInfoLength;
const char *host;
int hostLength;
int port;
const char *path;
int pathLength;
const char *query;
int queryLength;
const char *fragment;
int fragmentLength;
};
class QUrlPrivate
{
public:
QUrlPrivate();
QUrlPrivate(const QUrlPrivate &other);
bool setUrl(const QString &url);
QString canonicalHost() const;
void ensureEncodedParts() const;
QString authority(QUrl::FormattingOptions options = QUrl::None) const;
void setAuthority(const QString &auth);
void setUserInfo(const QString &userInfo);
QString userInfo(QUrl::FormattingOptions options = QUrl::None) const;
void setEncodedAuthority(const QByteArray &authority);
void setEncodedUserInfo(const QUrlParseData *parseData);
void setEncodedUrl(const QByteArray &encodedUrl, QUrl::ParsingMode parsingMode);
QString fragmentImpl() const;
QByteArray mergePaths(const QByteArray &relativePath) const;
void queryItem(int pos, int *value, int *end);
enum ParseOptions {
ParseAndSet,
ParseOnly
};
void validate() const;
void parse(ParseOptions parseOptions = ParseAndSet) const;
void clear();
QByteArray toEncoded(QUrl::FormattingOptions options = QUrl::None) const;
QAtomicInt ref;
QString scheme;
QString userName;
QString password;
QString host;
QString path;
QByteArray query;
QString fragment;
QByteArray encodedOriginal;
QByteArray encodedUserName;
QByteArray encodedPassword;
QByteArray encodedPath;
QByteArray encodedFragment;
int port;
QUrl::ParsingMode parsingMode;
bool hasQuery;
bool hasFragment;
bool isValid;
bool isHostValid;
char valueDelimiter;
char pairDelimiter;
enum State {
Parsed = 0x1,
Validated = 0x2,
Normalized = 0x4,
HostCanonicalized = 0x8
};
int stateFlags;
// This mutex protects stateFlags, errorInfo, host,
// hasQuery, hasFragment, isValid, isHostValid,
// encodedNormalized and all other encoded* variables above.
// This also protects all other variables when set by parse,
// but not when set by setters.
mutable QMutex mutex;
mutable QByteArray encodedNormalized;
const QByteArray & normalized() const;
mutable QUrlErrorInfo errorInfo;
QString createErrorString();
};
static bool QT_FASTCALL _HEXDIG(const char **ptr)
{
char ch = **ptr;
if ((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F')) {
++(*ptr);
return true;
}
return false;
}
// pct-encoded = "%" HEXDIG HEXDIG
static bool QT_FASTCALL _pctEncoded(const char **ptr)
{
const char *ptrBackup = *ptr;
if (**ptr != '%')
return false;
++(*ptr);
if (!_HEXDIG(ptr)) {
*ptr = ptrBackup;
return false;
}
if (!_HEXDIG(ptr)) {
*ptr = ptrBackup;
return false;
}
return true;
}
#if 0
// gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"
static bool QT_FASTCALL _genDelims(const char **ptr, char *c)
{
char ch = **ptr;
switch (ch) {
case ':': case '/': case '?': case '#':
case '[': case ']': case '@':
*c = ch;
++(*ptr);
return true;
default:
return false;
}
}
#endif
// sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
// / "*" / "+" / "," / ";" / "="
static bool QT_FASTCALL _subDelims(const char **ptr)
{
char ch = **ptr;
switch (ch) {
case '!': case '$': case '&': case '\'':
case '(': case ')': case '*': case '+':
case ',': case ';': case '=':
++(*ptr);
return true;
default:
return false;
}
}
// unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
static bool QT_FASTCALL _unreserved(const char **ptr)
{
char ch = **ptr;
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')
|| (ch >= '0' && ch <= '9')
|| ch == '-' || ch == '.' || ch == '_' || ch == '~') {
++(*ptr);
return true;
}
return false;
}
// scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
static bool QT_FASTCALL _scheme(const char **ptr, QUrlParseData *parseData)
{
bool first = true;
bool isSchemeValid = true;
parseData->scheme = *ptr;
for (;;) {
char ch = **ptr;
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
;
} else if ((ch >= '0' && ch <= '9') || ch == '+' || ch == '-' || ch == '.') {
if (first)
isSchemeValid = false;
} else {
break;
}
++(*ptr);
first = false;
}
if (**ptr != ':') {
isSchemeValid = true;
*ptr = parseData->scheme;
} else {
parseData->schemeLength = *ptr - parseData->scheme;
++(*ptr); // skip ':'
}
return isSchemeValid;
}
// IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" )
static bool QT_FASTCALL _IPvFuture(const char **ptr)
{
if (**ptr != 'v')
return false;
const char *ptrBackup = *ptr;
++(*ptr);
if (!_HEXDIG(ptr)) {
*ptr = ptrBackup;
return false;
}
while (_HEXDIG(ptr))
;
if (**ptr != '.') {
*ptr = ptrBackup;
return false;
}
++(*ptr);
if (!_unreserved(ptr) && !_subDelims(ptr) && *((*ptr)++) != ':') {
*ptr = ptrBackup;
return false;
}
while (_unreserved(ptr) || _subDelims(ptr) || *((*ptr)++) == ':')
;
return true;
}
// h16 = 1*4HEXDIG
// ; 16 bits of address represented in hexadecimal
static bool QT_FASTCALL _h16(const char **ptr)
{
int i = 0;
for (; i < 4; ++i) {
if (!_HEXDIG(ptr))
break;
}
return (i != 0);
}
// dec-octet = DIGIT ; 0-9
// / %x31-39 DIGIT ; 10-99
// / "1" 2DIGIT ; 100-199
// / "2" %x30-34 DIGIT ; 200-249
// / "25" %x30-35 ; 250-255
static bool QT_FASTCALL _decOctet(const char **ptr)
{
const char *ptrBackup = *ptr;
char c1 = **ptr;
if (c1 < '0' || c1 > '9')
return false;
++(*ptr);
if (c1 == '0')
return true;
char c2 = **ptr;
if (c2 < '0' || c2 > '9')
return true;
++(*ptr);
char c3 = **ptr;
if (c3 < '0' || c3 > '9')
return true;
// If there is a three digit number larger than 255, reject the
// whole token.
if (c1 >= '2' && c2 >= '5' && c3 > '5') {
*ptr = ptrBackup;
return false;
}
++(*ptr);
return true;
}
// IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
static bool QT_FASTCALL _IPv4Address(const char **ptr)
{
const char *ptrBackup = *ptr;
if (!_decOctet(ptr)) {
*ptr = ptrBackup;
return false;
}
for (int i = 0; i < 3; ++i) {
char ch = *((*ptr)++);
if (ch != '.') {
*ptr = ptrBackup;
return false;
}
if (!_decOctet(ptr)) {
*ptr = ptrBackup;
return false;
}
}
return true;
}
// ls32 = ( h16 ":" h16 ) / IPv4address
// ; least-significant 32 bits of address
static bool QT_FASTCALL _ls32(const char **ptr)
{
const char *ptrBackup = *ptr;
if (_h16(ptr) && *((*ptr)++) == ':' && _h16(ptr))
return true;
*ptr = ptrBackup;
return _IPv4Address(ptr);
}
// IPv6address = 6( h16 ":" ) ls32 // case 1
// / "::" 5( h16 ":" ) ls32 // case 2
// / [ h16 ] "::" 4( h16 ":" ) ls32 // case 3
// / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32 // case 4
// / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32 // case 5
// / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32 // case 6
// / [ *4( h16 ":" ) h16 ] "::" ls32 // case 7
// / [ *5( h16 ":" ) h16 ] "::" h16 // case 8
// / [ *6( h16 ":" ) h16 ] "::" // case 9
static bool QT_FASTCALL _IPv6Address(const char **ptr)
{
const char *ptrBackup = *ptr;
// count of (h16 ":") to the left of and including ::
int leftHexColons = 0;
// count of (h16 ":") to the right of ::
int rightHexColons = 0;
// first count the number of (h16 ":") on the left of ::
while (_h16(ptr)) {
// an h16 not followed by a colon is considered an
// error.
if (**ptr != ':') {
*ptr = ptrBackup;
return false;
}
++(*ptr);
++leftHexColons;
// check for case 1, the only time when there can be no ::
if (leftHexColons == 6 && _ls32(ptr)) {
return true;
}
}
// check for case 2 where the address starts with a :
if (leftHexColons == 0 && *((*ptr)++) != ':') {
*ptr = ptrBackup;
return false;
}
// check for the second colon in ::
if (*((*ptr)++) != ':') {
*ptr = ptrBackup;
return false;
}
int canBeCase = -1;
bool ls32WasRead = false;
const char *tmpBackup = *ptr;
// count the number of (h16 ":") on the right of ::
for (;;) {
tmpBackup = *ptr;
if (!_h16(ptr)) {
if (!_ls32(ptr)) {
if (rightHexColons != 0) {
*ptr = ptrBackup;
return false;
}
// the address ended with :: (case 9)
// only valid if 1 <= leftHexColons <= 7
canBeCase = 9;
} else {
ls32WasRead = true;
}
break;
}
++rightHexColons;
if (**ptr != ':') {
// no colon could mean that what was read as an h16
// was in fact the first part of an ls32. we backtrack
// and retry.
const char *pb = *ptr;
*ptr = tmpBackup;
if (_ls32(ptr)) {
ls32WasRead = true;
--rightHexColons;
} else {
*ptr = pb;
// address ends with only 1 h16 after :: (case 8)
if (rightHexColons == 1)
canBeCase = 8;
}
break;
}
++(*ptr);
}
// determine which case it is based on the number of rightHexColons
if (canBeCase == -1) {
// check if a ls32 was read. If it wasn't and rightHexColons >= 2 then the
// last 2 HexColons are in fact a ls32
if (!ls32WasRead && rightHexColons >= 2)
rightHexColons -= 2;
canBeCase = 7 - rightHexColons;
}
// based on the case we need to check that the number of leftHexColons is valid
if (leftHexColons > (canBeCase - 2)) {
*ptr = ptrBackup;
return false;
}
return true;
}
// IP-literal = "[" ( IPv6address / IPvFuture ) "]"
static bool QT_FASTCALL _IPLiteral(const char **ptr)
{
const char *ptrBackup = *ptr;
if (**ptr != '[')
return false;
++(*ptr);
if (!_IPv6Address(ptr) && !_IPvFuture(ptr)) {
*ptr = ptrBackup;
return false;
}
if (**ptr != ']') {
*ptr = ptrBackup;
return false;
}
++(*ptr);
return true;
}
// reg-name = *( unreserved / pct-encoded / sub-delims )
static void QT_FASTCALL _regName(const char **ptr)
{
for (;;) {
if (!_unreserved(ptr) && !_subDelims(ptr)) {
if (!_pctEncoded(ptr))
break;
}
}
}
// host = IP-literal / IPv4address / reg-name
static void QT_FASTCALL _host(const char **ptr, QUrlParseData *parseData)
{
parseData->host = *ptr;
if (!_IPLiteral(ptr)) {
if (_IPv4Address(ptr)) {
char ch = **ptr;
if (ch && ch != ':' && ch != '/') {
// reset
*ptr = parseData->host;
_regName(ptr);
}
} else {
_regName(ptr);
}
}
parseData->hostLength = *ptr - parseData->host;
}
// userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
static void QT_FASTCALL _userInfo(const char **ptr, QUrlParseData *parseData)
{
parseData->userInfo = *ptr;
for (;;) {
if (_unreserved(ptr) || _subDelims(ptr)) {
;
} else {
if (_pctEncoded(ptr)) {
;
} else if (**ptr == ':') {
parseData->userInfoDelimIndex = *ptr - parseData->userInfo;
++(*ptr);
} else {
break;
}
}
}
if (**ptr != '@') {
*ptr = parseData->userInfo;
parseData->userInfoDelimIndex = -1;
return;
}
parseData->userInfoLength = *ptr - parseData->userInfo;
++(*ptr);
}
// port = *DIGIT
static void QT_FASTCALL _port(const char **ptr, int *port)
{
bool first = true;
for (;;) {
const char *ptrBackup = *ptr;
char ch = *((*ptr)++);
if (ch < '0' || ch > '9') {
*ptr = ptrBackup;
break;
}
if (first) {
first = false;
*port = 0;
}
*port *= 10;
*port += ch - '0';
}
}
// authority = [ userinfo "@" ] host [ ":" port ]
static void QT_FASTCALL _authority(const char **ptr, QUrlParseData *parseData)
{
_userInfo(ptr, parseData);
_host(ptr, parseData);
if (**ptr != ':')
return;
++(*ptr);
_port(ptr, &parseData->port);
}
// pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
static bool QT_FASTCALL _pchar(const char **ptr)
{
char c = *(*ptr);
switch (c) {
case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
case '+': case ',': case ';': case '=': case ':': case '@':
case '-': case '.': case '_': case '~':
++(*ptr);
return true;
default:
break;
};
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) {
++(*ptr);
return true;
}
if (_pctEncoded(ptr))
return true;
return false;
}
// segment = *pchar
static bool QT_FASTCALL _segmentNZ(const char **ptr)
{
if (!_pchar(ptr))
return false;
while(_pchar(ptr))
;
return true;
}
// path-abempty = *( "/" segment )
static void QT_FASTCALL _pathAbEmpty(const char **ptr)
{
for (;;) {
if (**ptr != '/')
break;
++(*ptr);
while (_pchar(ptr))
;
}
}
// path-abs = "/" [ segment-nz *( "/" segment ) ]
static bool QT_FASTCALL _pathAbs(const char **ptr)
{
// **ptr == '/' already checked in caller
++(*ptr);
// we might be able to unnest this to gain some performance.
if (!_segmentNZ(ptr))
return true;
_pathAbEmpty(ptr);
return true;
}
// path-rootless = segment-nz *( "/" segment )
static bool QT_FASTCALL _pathRootless(const char **ptr)
{
// we might be able to unnest this to gain some performance.
if (!_segmentNZ(ptr))
return false;
_pathAbEmpty(ptr);
return true;
}
// hier-part = "//" authority path-abempty
// / path-abs
// / path-rootless
// / path-empty
static void QT_FASTCALL _hierPart(const char **ptr, QUrlParseData *parseData)
{
const char *ptrBackup = *ptr;
const char *pathStart = 0;
if (*((*ptr)++) == '/' && *((*ptr)++) == '/') {
_authority(ptr, parseData);
pathStart = *ptr;
_pathAbEmpty(ptr);
} else {
*ptr = ptrBackup;
pathStart = *ptr;
if (**ptr == '/')
_pathAbs(ptr);
else
_pathRootless(ptr);
}
parseData->path = pathStart;
parseData->pathLength = *ptr - pathStart;
}
// query = *( pchar / "/" / "?" )
static void QT_FASTCALL _query(const char **ptr, QUrlParseData *parseData)
{
parseData->query = *ptr;
for (;;) {
if (_pchar(ptr)) {
;
} else if (**ptr == '/' || **ptr == '?') {
++(*ptr);
} else {
break;
}
}
parseData->queryLength = *ptr - parseData->query;
}
// fragment = *( pchar / "/" / "?" )
static void QT_FASTCALL _fragment(const char **ptr, QUrlParseData *parseData)
{
parseData->fragment = *ptr;
for (;;) {
if (_pchar(ptr)) {
;
} else if (**ptr == '/' || **ptr == '?' || **ptr == '#') {
++(*ptr);
} else {
break;
}
}
parseData->fragmentLength = *ptr - parseData->fragment;
}
struct NameprepCaseFoldingEntry {
uint uc;
ushort mapping[4];
};
inline bool operator<(uint one, const NameprepCaseFoldingEntry &other)
{ return one < other.uc; }