author | Michael Krelin <hacker@klever.net> | 2007-07-04 11:23:42 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2007-07-04 11:23:42 (UTC) |
commit | a08aff328d4393031d5ba7d622c2b05705a89d73 (patch) (unidiff) | |
tree | 8ee90d686081c52e7c69b5ce946e9b1a7d690001 /microkde/kurl.cpp | |
parent | 11edc920afe4f274c0964436633aa632c8288a40 (diff) | |
download | kdepimpi-p1.zip kdepimpi-p1.tar.gz kdepimpi-p1.tar.bz2 |
initial public commit of qt4 portp1
-rw-r--r-- | microkde/kurl.cpp | 30 |
1 files changed, 16 insertions, 14 deletions
diff --git a/microkde/kurl.cpp b/microkde/kurl.cpp index 2574e25..122ad71 100644 --- a/microkde/kurl.cpp +++ b/microkde/kurl.cpp | |||
@@ -13,115 +13,117 @@ | |||
13 | 13 | ||
14 | You should have received a copy of the GNU Library General Public License | 14 | You should have received a copy of the GNU Library General Public License |
15 | along with this library; see the file COPYING.LIB. If not, write to | 15 | along with this library; see the file COPYING.LIB. If not, write to |
16 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | 16 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
17 | Boston, MA 02111-1307, USA. | 17 | Boston, MA 02111-1307, USA. |
18 | */ | 18 | */ |
19 | 19 | ||
20 | #include "kurl.h" | 20 | #include "kurl.h" |
21 | 21 | ||
22 | #ifndef KDE_QT_ONLY | 22 | #ifndef KDE_QT_ONLY |
23 | #include <kdebug.h> | 23 | #include <kdebug.h> |
24 | #include <kglobal.h> | 24 | #include <kglobal.h> |
25 | //US#include <kidna.h> | 25 | //US#include <kidna.h> |
26 | #endif | 26 | #endif |
27 | 27 | ||
28 | #include <stdio.h> | 28 | #include <stdio.h> |
29 | #include <assert.h> | 29 | #include <assert.h> |
30 | #include <ctype.h> | 30 | #include <ctype.h> |
31 | #include <stdlib.h> | 31 | #include <stdlib.h> |
32 | #ifdef _WIN32_ | 32 | #ifdef _WIN32_ |
33 | 33 | ||
34 | #else | 34 | #else |
35 | #include <unistd.h> | 35 | #include <unistd.h> |
36 | #endif | 36 | #endif |
37 | #include <qurl.h> | 37 | #include <q3url.h> |
38 | #include <qdir.h> | 38 | #include <qdir.h> |
39 | #include <qstringlist.h> | 39 | #include <qstringlist.h> |
40 | #include <qregexp.h> | 40 | #include <qregexp.h> |
41 | //US#include <qstylesheet.h> | 41 | //US#include <qstylesheet.h> |
42 | #include <qmap.h> | 42 | #include <qmap.h> |
43 | #include <qtextcodec.h> | 43 | #include <qtextcodec.h> |
44 | //Added by qt3to4: | ||
45 | #include <Q3CString> | ||
44 | 46 | ||
45 | static const QString fileProt = "file"; | 47 | static const QString fileProt = "file"; |
46 | 48 | ||
47 | static QTextCodec * codecForHint( int encoding_hint /* not 0 ! */ ) | 49 | static QTextCodec * codecForHint( int encoding_hint /* not 0 ! */ ) |
48 | { | 50 | { |
49 | return QTextCodec::codecForMib( encoding_hint ); | 51 | return QTextCodec::codecForMib( encoding_hint ); |
50 | } | 52 | } |
51 | 53 | ||
52 | static QString encode( const QString& segment, bool encode_slash, int encoding_hint ) | 54 | static QString encode( const QString& segment, bool encode_slash, int encoding_hint ) |
53 | { | 55 | { |
54 | const char *encode_string; | 56 | const char *encode_string; |
55 | if (encode_slash) | 57 | if (encode_slash) |
56 | encode_string = "<>#@\"&%?={}|^~[]\'`\\:+/"; | 58 | encode_string = "<>#@\"&%?={}|^~[]\'`\\:+/"; |
57 | else | 59 | else |
58 | encode_string = "<>#@\"&%?={}|^~[]\'`\\:+"; | 60 | encode_string = "<>#@\"&%?={}|^~[]\'`\\:+"; |
59 | 61 | ||
60 | QCString local; | 62 | Q3CString local; |
61 | if (encoding_hint==0) | 63 | if (encoding_hint==0) |
62 | local = segment.local8Bit(); | 64 | local = segment.local8Bit(); |
63 | else | 65 | else |
64 | { | 66 | { |
65 | QTextCodec * textCodec = codecForHint( encoding_hint ); | 67 | QTextCodec * textCodec = codecForHint( encoding_hint ); |
66 | if (!textCodec) | 68 | if (!textCodec) |
67 | local = segment.local8Bit(); | 69 | local = segment.local8Bit(); |
68 | else | 70 | else |
69 | local = textCodec->fromUnicode( segment ); | 71 | local = textCodec->fromUnicode( segment ); |
70 | } | 72 | } |
71 | 73 | ||
72 | int old_length = local.length(); | 74 | int old_length = local.length(); |
73 | 75 | ||
74 | if ( !old_length ) | 76 | if ( !old_length ) |
75 | return segment.isNull() ? QString::null : QString(""); // differenciate null and empty | 77 | return segment.isNull() ? QString::null : QString(""); // differenciate null and empty |
76 | 78 | ||
77 | // a worst case approximation | 79 | // a worst case approximation |
78 | QChar *new_segment = new QChar[ old_length * 3 + 1 ]; | 80 | QChar *new_segment = new QChar[ old_length * 3 + 1 ]; |
79 | int new_length = 0; | 81 | int new_length = 0; |
80 | 82 | ||
81 | for ( int i = 0; i < old_length; i++ ) | 83 | for ( int i = 0; i < old_length; i++ ) |
82 | { | 84 | { |
83 | // 'unsave' and 'reserved' characters | 85 | // 'unsave' and 'reserved' characters |
84 | // according to RFC 1738, | 86 | // according to RFC 1738, |
85 | // 2.2. URL Character Encoding Issues (pp. 3-4) | 87 | // 2.2. URL Character Encoding Issues (pp. 3-4) |
86 | // WABA: Added non-ascii | 88 | // WABA: Added non-ascii |
87 | unsigned char character = local[i]; | 89 | unsigned char character = local[i]; |
88 | if ( (character <= 32) || (character >= 127) || | 90 | if ( (character <= 32) || (character >= 127) || |
89 | strchr(encode_string, character) ) | 91 | strchr(encode_string, character) ) |
90 | { | 92 | { |
91 | new_segment[ new_length++ ] = '%'; | 93 | new_segment[ new_length++ ] = '%'; |
92 | 94 | ||
93 | unsigned int c = character / 16; | 95 | unsigned int c = character / 16; |
94 | c += (c > 9) ? ('A' - 10) : '0'; | 96 | c += (c > 9) ? ('A' - 10) : '0'; |
95 | new_segment[ new_length++ ] = c; | 97 | new_segment[ new_length++ ] = c; |
96 | 98 | ||
97 | c = character % 16; | 99 | c = character % 16; |
98 | c += (c > 9) ? ('A' - 10) : '0'; | 100 | c += (c > 9) ? ('A' - 10) : '0'; |
99 | new_segment[ new_length++ ] = c; | 101 | new_segment[ new_length++ ] = c; |
100 | 102 | ||
101 | } | 103 | } |
102 | else | 104 | else |
103 | new_segment[ new_length++ ] = local[i]; | 105 | new_segment[ new_length++ ] = character; |
104 | } | 106 | } |
105 | 107 | ||
106 | QString result = QString(new_segment, new_length); | 108 | QString result = QString(new_segment, new_length); |
107 | delete [] new_segment; | 109 | delete [] new_segment; |
108 | return result; | 110 | return result; |
109 | } | 111 | } |
110 | 112 | ||
111 | static QString encodeHost( const QString& segment, bool encode_slash, int encoding_hint ) | 113 | static QString encodeHost( const QString& segment, bool encode_slash, int encoding_hint ) |
112 | { | 114 | { |
113 | // Hostnames are encoded differently | 115 | // Hostnames are encoded differently |
114 | // we use the IDNA transformation instead | 116 | // we use the IDNA transformation instead |
115 | 117 | ||
116 | // Note: when merging qt-addon, use QResolver::domainToAscii here | 118 | // Note: when merging qt-addon, use QResolver::domainToAscii here |
117 | #ifndef KDE_QT_ONLY | 119 | #ifndef KDE_QT_ONLY |
118 | Q_UNUSED( encode_slash ); | 120 | Q_UNUSED( encode_slash ); |
119 | Q_UNUSED( encoding_hint ); | 121 | Q_UNUSED( encoding_hint ); |
120 | return KIDNA::toAscii(segment); | 122 | return KIDNA::toAscii(segment); |
121 | #else | 123 | #else |
122 | return encode(segment, encode_slash, encoding_hint); | 124 | return encode(segment, encode_slash, encoding_hint); |
123 | #endif | 125 | #endif |
124 | } | 126 | } |
125 | 127 | ||
126 | static int hex2int( unsigned int _char ) | 128 | static int hex2int( unsigned int _char ) |
127 | { | 129 | { |
@@ -184,49 +186,49 @@ static QString lazy_encode( const QString& segment ) | |||
184 | new_segment[ new_length++ ] = segment[i]; | 186 | new_segment[ new_length++ ] = segment[i]; |
185 | } | 187 | } |
186 | 188 | ||
187 | QString result = QString(new_segment, new_length); | 189 | QString result = QString(new_segment, new_length); |
188 | delete [] new_segment; | 190 | delete [] new_segment; |
189 | return result; | 191 | return result; |
190 | } | 192 | } |
191 | 193 | ||
192 | static void decode( const QString& segment, QString &decoded, QString &encoded, int encoding_hint=0, bool updateDecoded = true ) | 194 | static void decode( const QString& segment, QString &decoded, QString &encoded, int encoding_hint=0, bool updateDecoded = true ) |
193 | { | 195 | { |
194 | decoded = QString::null; | 196 | decoded = QString::null; |
195 | encoded = segment; | 197 | encoded = segment; |
196 | 198 | ||
197 | int old_length = segment.length(); | 199 | int old_length = segment.length(); |
198 | if ( !old_length ) | 200 | if ( !old_length ) |
199 | return; | 201 | return; |
200 | 202 | ||
201 | QTextCodec *textCodec = 0; | 203 | QTextCodec *textCodec = 0; |
202 | if (encoding_hint) | 204 | if (encoding_hint) |
203 | textCodec = codecForHint( encoding_hint ); | 205 | textCodec = codecForHint( encoding_hint ); |
204 | 206 | ||
205 | if (!textCodec) | 207 | if (!textCodec) |
206 | textCodec = QTextCodec::codecForLocale(); | 208 | textCodec = QTextCodec::codecForLocale(); |
207 | 209 | ||
208 | QCString csegment = textCodec->fromUnicode(segment); | 210 | Q3CString csegment = textCodec->fromUnicode(segment); |
209 | // Check if everything went ok | 211 | // Check if everything went ok |
210 | if (textCodec->toUnicode(csegment) != segment) | 212 | if (textCodec->toUnicode(csegment) != segment) |
211 | { | 213 | { |
212 | // Uh oh | 214 | // Uh oh |
213 | textCodec = codecForHint( 106 ); // Fall back to utf-8 | 215 | textCodec = codecForHint( 106 ); // Fall back to utf-8 |
214 | csegment = textCodec->fromUnicode(segment); | 216 | csegment = textCodec->fromUnicode(segment); |
215 | } | 217 | } |
216 | old_length = csegment.length(); | 218 | old_length = csegment.length(); |
217 | 219 | ||
218 | int new_length = 0; | 220 | int new_length = 0; |
219 | int new_length2 = 0; | 221 | int new_length2 = 0; |
220 | 222 | ||
221 | // make a copy of the old one | 223 | // make a copy of the old one |
222 | char *new_segment = new char[ old_length + 1 ]; | 224 | char *new_segment = new char[ old_length + 1 ]; |
223 | QChar *new_usegment = new QChar[ old_length * 3 + 1 ]; | 225 | QChar *new_usegment = new QChar[ old_length * 3 + 1 ]; |
224 | 226 | ||
225 | int i = 0; | 227 | int i = 0; |
226 | while( i < old_length ) | 228 | while( i < old_length ) |
227 | { | 229 | { |
228 | bool bReencode = false; | 230 | bool bReencode = false; |
229 | unsigned char character = csegment[ i++ ]; | 231 | unsigned char character = csegment[ i++ ]; |
230 | if ((character <= ' ') || (character > 127)) | 232 | if ((character <= ' ') || (character > 127)) |
231 | bReencode = true; | 233 | bReencode = true; |
232 | 234 | ||
@@ -257,49 +259,49 @@ static void decode( const QString& segment, QString &decoded, QString &encoded, | |||
257 | new_usegment [ new_length2++ ] = '%'; | 259 | new_usegment [ new_length2++ ] = '%'; |
258 | 260 | ||
259 | unsigned int c = character / 16; | 261 | unsigned int c = character / 16; |
260 | c += (c > 9) ? ('A' - 10) : '0'; | 262 | c += (c > 9) ? ('A' - 10) : '0'; |
261 | new_usegment[ new_length2++ ] = c; | 263 | new_usegment[ new_length2++ ] = c; |
262 | 264 | ||
263 | c = character % 16; | 265 | c = character % 16; |
264 | c += (c > 9) ? ('A' - 10) : '0'; | 266 | c += (c > 9) ? ('A' - 10) : '0'; |
265 | new_usegment[ new_length2++ ] = c; | 267 | new_usegment[ new_length2++ ] = c; |
266 | } | 268 | } |
267 | 269 | ||
268 | new_segment [ new_length++ ] = character; | 270 | new_segment [ new_length++ ] = character; |
269 | } | 271 | } |
270 | new_segment [ new_length ] = 0; | 272 | new_segment [ new_length ] = 0; |
271 | 273 | ||
272 | encoded = QString( new_usegment, new_length2); | 274 | encoded = QString( new_usegment, new_length2); |
273 | 275 | ||
274 | // Encoding specified | 276 | // Encoding specified |
275 | if (updateDecoded) | 277 | if (updateDecoded) |
276 | { | 278 | { |
277 | QByteArray array; | 279 | QByteArray array; |
278 | array.setRawData(new_segment, new_length); | 280 | array.setRawData(new_segment, new_length); |
279 | decoded = textCodec->toUnicode( array, new_length ); | 281 | decoded = textCodec->toUnicode( array, new_length ); |
280 | array.resetRawData(new_segment, new_length); | 282 | array.resetRawData(new_segment, new_length); |
281 | QCString validate = textCodec->fromUnicode(decoded); | 283 | Q3CString validate = textCodec->fromUnicode(decoded); |
282 | 284 | ||
283 | if (strcmp(validate.data(), new_segment) != 0) | 285 | if (strcmp(validate.data(), new_segment) != 0) |
284 | { | 286 | { |
285 | decoded = QString::fromLocal8Bit(new_segment, new_length); | 287 | decoded = QString::fromLocal8Bit(new_segment, new_length); |
286 | } | 288 | } |
287 | } | 289 | } |
288 | 290 | ||
289 | delete [] new_segment; | 291 | delete [] new_segment; |
290 | delete [] new_usegment; | 292 | delete [] new_usegment; |
291 | } | 293 | } |
292 | 294 | ||
293 | static QString decode(const QString &segment, int encoding_hint = 0) | 295 | static QString decode(const QString &segment, int encoding_hint = 0) |
294 | { | 296 | { |
295 | QString result; | 297 | QString result; |
296 | QString tmp; | 298 | QString tmp; |
297 | decode(segment, result, tmp, encoding_hint); | 299 | decode(segment, result, tmp, encoding_hint); |
298 | return result; | 300 | return result; |
299 | } | 301 | } |
300 | 302 | ||
301 | static QString cleanpath(const QString &path, bool cleanDirSeparator=true) | 303 | static QString cleanpath(const QString &path, bool cleanDirSeparator=true) |
302 | { | 304 | { |
303 | if (path.isEmpty()) return QString::null; | 305 | if (path.isEmpty()) return QString::null; |
304 | int len = path.length(); | 306 | int len = path.length(); |
305 | bool slash = (len && path[len-1] == '/') || | 307 | bool slash = (len && path[len-1] == '/') || |
@@ -398,90 +400,90 @@ QStringList KURL::List::toStringList() const | |||
398 | 400 | ||
399 | 401 | ||
400 | KURL::KURL() | 402 | KURL::KURL() |
401 | { | 403 | { |
402 | reset(); | 404 | reset(); |
403 | } | 405 | } |
404 | 406 | ||
405 | KURL::~KURL() | 407 | KURL::~KURL() |
406 | { | 408 | { |
407 | } | 409 | } |
408 | 410 | ||
409 | 411 | ||
410 | KURL::KURL( const QString &url, int encoding_hint ) | 412 | KURL::KURL( const QString &url, int encoding_hint ) |
411 | { | 413 | { |
412 | reset(); | 414 | reset(); |
413 | parse( url, encoding_hint ); | 415 | parse( url, encoding_hint ); |
414 | } | 416 | } |
415 | 417 | ||
416 | KURL::KURL( const char * url, int encoding_hint ) | 418 | KURL::KURL( const char * url, int encoding_hint ) |
417 | { | 419 | { |
418 | reset(); | 420 | reset(); |
419 | parse( QString::fromLatin1(url), encoding_hint ); | 421 | parse( QString::fromLatin1(url), encoding_hint ); |
420 | } | 422 | } |
421 | 423 | ||
422 | KURL::KURL( const QCString& url, int encoding_hint ) | 424 | KURL::KURL( const Q3CString& url, int encoding_hint ) |
423 | { | 425 | { |
424 | reset(); | 426 | reset(); |
425 | parse( QString::fromLatin1(url), encoding_hint ); | 427 | parse( QString::fromLatin1(url), encoding_hint ); |
426 | } | 428 | } |
427 | 429 | ||
428 | KURL::KURL( const KURL& _u ) | 430 | KURL::KURL( const KURL& _u ) |
429 | { | 431 | { |
430 | *this = _u; | 432 | *this = _u; |
431 | } | 433 | } |
432 | 434 | ||
433 | QDataStream & operator<< (QDataStream & s, const KURL & a) | 435 | QDataStream & operator<< (QDataStream & s, const KURL & a) |
434 | { | 436 | { |
435 | QString QueryForWire=a.m_strQuery_encoded; | 437 | QString QueryForWire=a.m_strQuery_encoded; |
436 | if (!a.m_strQuery_encoded.isNull()) | 438 | if (!a.m_strQuery_encoded.isNull()) |
437 | QueryForWire.prepend("?"); | 439 | QueryForWire.prepend("?"); |
438 | 440 | ||
439 | s << a.m_strProtocol << a.m_strUser << a.m_strPass << a.m_strHost | 441 | s << a.m_strProtocol << a.m_strUser << a.m_strPass << a.m_strHost |
440 | << a.m_strPath << a.m_strPath_encoded << QueryForWire << a.m_strRef_encoded | 442 | << a.m_strPath << a.m_strPath_encoded << QueryForWire << a.m_strRef_encoded |
441 | << Q_INT8(a.m_bIsMalformed ? 1 : 0) << a.m_iPort; | 443 | << Q_INT8(a.m_bIsMalformed ? 1 : 0) << a.m_iPort; |
442 | return s; | 444 | return s; |
443 | } | 445 | } |
444 | 446 | ||
445 | QDataStream & operator>> (QDataStream & s, KURL & a) | 447 | QDataStream & operator>> (QDataStream & s, KURL & a) |
446 | { | 448 | { |
447 | Q_INT8 malf; | 449 | Q_INT8 malf; |
448 | QString QueryFromWire; | 450 | QString QueryFromWire; |
449 | s >> a.m_strProtocol >> a.m_strUser >> a.m_strPass >> a.m_strHost | 451 | s >> a.m_strProtocol >> a.m_strUser >> a.m_strPass >> a.m_strHost |
450 | >> a.m_strPath >> a.m_strPath_encoded >> QueryFromWire >> a.m_strRef_encoded | 452 | >> a.m_strPath >> a.m_strPath_encoded >> QueryFromWire >> a.m_strRef_encoded |
451 | >> malf >> a.m_iPort; | 453 | >> malf >> a.m_iPort; |
452 | a.m_bIsMalformed = (malf != 0); | 454 | a.m_bIsMalformed = (malf != 0); |
453 | 455 | ||
454 | if ( QueryFromWire.isEmpty() ) | 456 | if ( QueryFromWire.isEmpty() ) |
455 | a.m_strQuery_encoded = QString::null; | 457 | a.m_strQuery_encoded = QString::null; |
456 | else | 458 | else |
457 | a.m_strQuery_encoded = QueryFromWire.mid(1); | 459 | a.m_strQuery_encoded = QueryFromWire.mid(1); |
458 | 460 | ||
459 | return s; | 461 | return s; |
460 | } | 462 | } |
461 | 463 | ||
462 | #ifndef QT_NO_NETWORKPROTOCOL | 464 | #ifndef QT_NO_NETWORKPROTOCOL |
463 | KURL::KURL( const QUrl &u ) | 465 | KURL::KURL( const Q3Url &u ) |
464 | { | 466 | { |
465 | *this = u; | 467 | *this = u; |
466 | } | 468 | } |
467 | #endif | 469 | #endif |
468 | 470 | ||
469 | KURL::KURL( const KURL& _u, const QString& _rel_url, int encoding_hint ) | 471 | KURL::KURL( const KURL& _u, const QString& _rel_url, int encoding_hint ) |
470 | { | 472 | { |
471 | // WORKAROUND THE RFC 1606 LOOPHOLE THAT ALLOWS | 473 | // WORKAROUND THE RFC 1606 LOOPHOLE THAT ALLOWS |
472 | // http:/index.html AS A VALID SYNTAX FOR RELATIVE | 474 | // http:/index.html AS A VALID SYNTAX FOR RELATIVE |
473 | // URLS. ( RFC 2396 section 5.2 item # 3 ) | 475 | // URLS. ( RFC 2396 section 5.2 item # 3 ) |
474 | QString rUrl = _rel_url; | 476 | QString rUrl = _rel_url; |
475 | int len = _u.m_strProtocol.length(); | 477 | int len = _u.m_strProtocol.length(); |
476 | if ( !_u.m_strHost.isEmpty() && !rUrl.isEmpty() && | 478 | if ( !_u.m_strHost.isEmpty() && !rUrl.isEmpty() && |
477 | rUrl.find( _u.m_strProtocol, 0, false ) == 0 && | 479 | rUrl.find( _u.m_strProtocol, 0, false ) == 0 && |
478 | rUrl[len] == ':' && (rUrl[len+1] != '/' || | 480 | rUrl[len] == ':' && (rUrl[len+1] != '/' || |
479 | (rUrl[len+1] == '/' && rUrl[len+2] != '/')) ) | 481 | (rUrl[len+1] == '/' && rUrl[len+2] != '/')) ) |
480 | { | 482 | { |
481 | rUrl.remove( 0, rUrl.find( ':' ) + 1 ); | 483 | rUrl.remove( 0, rUrl.find( ':' ) + 1 ); |
482 | } | 484 | } |
483 | 485 | ||
484 | if ( rUrl.isEmpty() ) | 486 | if ( rUrl.isEmpty() ) |
485 | { | 487 | { |
486 | *this = _u; | 488 | *this = _u; |
487 | } | 489 | } |
@@ -568,55 +570,55 @@ void KURL::parse( const QString& _url, int encoding_hint ) | |||
568 | // Return immediately whenever the given url | 570 | // Return immediately whenever the given url |
569 | // is empty or null. | 571 | // is empty or null. |
570 | if ( _url.isEmpty() ) | 572 | if ( _url.isEmpty() ) |
571 | { | 573 | { |
572 | m_strProtocol = _url; | 574 | m_strProtocol = _url; |
573 | return; | 575 | return; |
574 | } | 576 | } |
575 | 577 | ||
576 | QString port; | 578 | QString port; |
577 | bool badHostName = false; | 579 | bool badHostName = false; |
578 | int start = 0; | 580 | int start = 0; |
579 | uint len = _url.length(); | 581 | uint len = _url.length(); |
580 | const QChar* buf = _url.unicode(); | 582 | const QChar* buf = _url.unicode(); |
581 | const QChar* orig = buf; | 583 | const QChar* orig = buf; |
582 | 584 | ||
583 | QChar delim; | 585 | QChar delim; |
584 | QString tmp; | 586 | QString tmp; |
585 | 587 | ||
586 | uint pos = 0; | 588 | uint pos = 0; |
587 | 589 | ||
588 | // Node 1: Accept alpha or slash | 590 | // Node 1: Accept alpha or slash |
589 | QChar x = buf[pos++]; | 591 | QChar x = buf[pos++]; |
590 | if ( x == '/' ) | 592 | if ( x == '/' ) |
591 | goto Node9; | 593 | goto Node9; |
592 | if ( !isalpha( (int)x ) ) | 594 | if ( !x.isLetter() ) |
593 | goto NodeErr; | 595 | goto NodeErr; |
594 | 596 | ||
595 | // Node 2: Accept any amount of (alpha|digit|'+'|'-') | 597 | // Node 2: Accept any amount of (alpha|digit|'+'|'-') |
596 | // '.' is not currently accepted, because current KURL may be confused. | 598 | // '.' is not currently accepted, because current KURL may be confused. |
597 | // Proceed with :// :/ or : | 599 | // Proceed with :// :/ or : |
598 | while( pos < len && (isalpha((int)buf[pos]) || isdigit((int)buf[pos]) || | 600 | while( pos < len && ( buf[pos].isLetter() || buf[pos].isDigit() || |
599 | buf[pos] == '+' || buf[pos] == '-')) pos++; | 601 | buf[pos] == '+' || buf[pos] == '-')) pos++; |
600 | 602 | ||
601 | if ( pos+2 < len && buf[pos] == ':' && buf[pos+1] == '/' && buf[pos+2] == '/' ) | 603 | if ( pos+2 < len && buf[pos] == ':' && buf[pos+1] == '/' && buf[pos+2] == '/' ) |
602 | { | 604 | { |
603 | m_strProtocol = QString( orig, pos ).lower(); | 605 | m_strProtocol = QString( orig, pos ).lower(); |
604 | pos += 3; | 606 | pos += 3; |
605 | } | 607 | } |
606 | else if (pos+1 < len && buf[pos] == ':' ) // Need to always compare length()-1 otherwise KURL passes "http:" as legal!! | 608 | else if (pos+1 < len && buf[pos] == ':' ) // Need to always compare length()-1 otherwise KURL passes "http:" as legal!! |
607 | { | 609 | { |
608 | m_strProtocol = QString( orig, pos ).lower(); | 610 | m_strProtocol = QString( orig, pos ).lower(); |
609 | //kdDebug(126)<<"setting protocol to "<<m_strProtocol<<endl; | 611 | //kdDebug(126)<<"setting protocol to "<<m_strProtocol<<endl; |
610 | pos++; | 612 | pos++; |
611 | start = pos; | 613 | start = pos; |
612 | goto Node9; | 614 | goto Node9; |
613 | } | 615 | } |
614 | else | 616 | else |
615 | goto NodeErr; | 617 | goto NodeErr; |
616 | 618 | ||
617 | //Node 3: We need at least one character here | 619 | //Node 3: We need at least one character here |
618 | if ( pos == len ) | 620 | if ( pos == len ) |
619 | goto NodeErr; | 621 | goto NodeErr; |
620 | start = pos; | 622 | start = pos; |
621 | 623 | ||
622 | // Node 4: Accept any amount of characters. | 624 | // Node 4: Accept any amount of characters. |
@@ -748,53 +750,53 @@ void KURL::parse( const QString& _url, int encoding_hint ) | |||
748 | } | 750 | } |
749 | if (badHostName) | 751 | if (badHostName) |
750 | goto NodeErr; | 752 | goto NodeErr; |
751 | if ( pos == len ) | 753 | if ( pos == len ) |
752 | { | 754 | { |
753 | setHost(decode(QString( buf + start, pos - start ), encoding_hint)); | 755 | setHost(decode(QString( buf + start, pos - start ), encoding_hint)); |
754 | goto NodeOk; | 756 | goto NodeOk; |
755 | } | 757 | } |
756 | setHost(decode(QString( buf + start, pos - start ), encoding_hint)); | 758 | setHost(decode(QString( buf + start, pos - start ), encoding_hint)); |
757 | } | 759 | } |
758 | x = buf[pos]; | 760 | x = buf[pos]; |
759 | if ( x == '/' ) | 761 | if ( x == '/' ) |
760 | { | 762 | { |
761 | start = pos++; | 763 | start = pos++; |
762 | goto Node9; | 764 | goto Node9; |
763 | } | 765 | } |
764 | else if ( x != ':' ) | 766 | else if ( x != ':' ) |
765 | goto NodeErr; | 767 | goto NodeErr; |
766 | pos++; | 768 | pos++; |
767 | 769 | ||
768 | // Node 8a: Accept at least one digit | 770 | // Node 8a: Accept at least one digit |
769 | if ( pos == len ) | 771 | if ( pos == len ) |
770 | goto NodeErr; | 772 | goto NodeErr; |
771 | start = pos; | 773 | start = pos; |
772 | if ( !isdigit( buf[pos++] ) ) | 774 | if ( !buf[pos++].isDigit() ) |
773 | goto NodeErr; | 775 | goto NodeErr; |
774 | 776 | ||
775 | // Node 8b: Accept any amount of digits | 777 | // Node 8b: Accept any amount of digits |
776 | while( pos < len && isdigit( buf[pos] ) ) pos++; | 778 | while( pos < len && buf[pos].isDigit() ) pos++; |
777 | port = QString( buf + start, pos - start ); | 779 | port = QString( buf + start, pos - start ); |
778 | m_iPort = port.toUShort(); | 780 | m_iPort = port.toUShort(); |
779 | if ( pos == len ) | 781 | if ( pos == len ) |
780 | goto NodeOk; | 782 | goto NodeOk; |
781 | start = pos++; | 783 | start = pos++; |
782 | 784 | ||
783 | Node9: // parse path until query or reference reached | 785 | Node9: // parse path until query or reference reached |
784 | 786 | ||
785 | while( pos < len && buf[pos] != '#' && buf[pos]!='?' ) pos++; | 787 | while( pos < len && buf[pos] != '#' && buf[pos]!='?' ) pos++; |
786 | 788 | ||
787 | tmp = QString( buf + start, pos - start ); | 789 | tmp = QString( buf + start, pos - start ); |
788 | //kdDebug(126)<<" setting encoded path&query to:"<<tmp<<endl; | 790 | //kdDebug(126)<<" setting encoded path&query to:"<<tmp<<endl; |
789 | setEncodedPath( tmp, encoding_hint ); | 791 | setEncodedPath( tmp, encoding_hint ); |
790 | 792 | ||
791 | if ( pos == len ) | 793 | if ( pos == len ) |
792 | goto NodeOk; | 794 | goto NodeOk; |
793 | 795 | ||
794 | //Node10: // parse query or reference depending on what comes first | 796 | //Node10: // parse query or reference depending on what comes first |
795 | delim = (buf[pos++]=='#'?'?':'#'); | 797 | delim = (buf[pos++]=='#'?'?':'#'); |
796 | 798 | ||
797 | start = pos; | 799 | start = pos; |
798 | 800 | ||
799 | while(pos < len && buf[pos]!=delim ) pos++; | 801 | while(pos < len && buf[pos]!=delim ) pos++; |
800 | 802 | ||
@@ -827,49 +829,49 @@ void KURL::parse( const QString& _url, int encoding_hint ) | |||
827 | 829 | ||
828 | NodeErr: | 830 | NodeErr: |
829 | // kdDebug(126) << "KURL couldn't parse URL \"" << _url << "\"" << endl; | 831 | // kdDebug(126) << "KURL couldn't parse URL \"" << _url << "\"" << endl; |
830 | reset(); | 832 | reset(); |
831 | m_strProtocol = _url; | 833 | m_strProtocol = _url; |
832 | } | 834 | } |
833 | 835 | ||
834 | KURL& KURL::operator=( const QString& _url ) | 836 | KURL& KURL::operator=( const QString& _url ) |
835 | { | 837 | { |
836 | reset(); | 838 | reset(); |
837 | parse( _url ); | 839 | parse( _url ); |
838 | 840 | ||
839 | return *this; | 841 | return *this; |
840 | } | 842 | } |
841 | 843 | ||
842 | KURL& KURL::operator=( const char * _url ) | 844 | KURL& KURL::operator=( const char * _url ) |
843 | { | 845 | { |
844 | reset(); | 846 | reset(); |
845 | parse( QString::fromLatin1(_url) ); | 847 | parse( QString::fromLatin1(_url) ); |
846 | 848 | ||
847 | return *this; | 849 | return *this; |
848 | } | 850 | } |
849 | 851 | ||
850 | #ifndef QT_NO_NETWORKPROTOCOL | 852 | #ifndef QT_NO_NETWORKPROTOCOL |
851 | KURL& KURL::operator=( const QUrl & u ) | 853 | KURL& KURL::operator=( const Q3Url & u ) |
852 | { | 854 | { |
853 | m_strProtocol = u.protocol(); | 855 | m_strProtocol = u.protocol(); |
854 | m_strUser = u.user(); | 856 | m_strUser = u.user(); |
855 | m_strPass = u.password(); | 857 | m_strPass = u.password(); |
856 | m_strHost = u.host(); | 858 | m_strHost = u.host(); |
857 | m_strPath = u.path( FALSE ); | 859 | m_strPath = u.path( FALSE ); |
858 | m_strPath_encoded = QString::null; | 860 | m_strPath_encoded = QString::null; |
859 | m_strQuery_encoded = u.query(); | 861 | m_strQuery_encoded = u.query(); |
860 | m_strRef_encoded = u.ref(); | 862 | m_strRef_encoded = u.ref(); |
861 | m_bIsMalformed = !u.isValid(); | 863 | m_bIsMalformed = !u.isValid(); |
862 | m_iPort = u.port(); | 864 | m_iPort = u.port(); |
863 | 865 | ||
864 | return *this; | 866 | return *this; |
865 | } | 867 | } |
866 | #endif | 868 | #endif |
867 | 869 | ||
868 | KURL& KURL::operator=( const KURL& _u ) | 870 | KURL& KURL::operator=( const KURL& _u ) |
869 | { | 871 | { |
870 | m_strProtocol = _u.m_strProtocol; | 872 | m_strProtocol = _u.m_strProtocol; |
871 | m_strUser = _u.m_strUser; | 873 | m_strUser = _u.m_strUser; |
872 | m_strPass = _u.m_strPass; | 874 | m_strPass = _u.m_strPass; |
873 | m_strHost = _u.m_strHost; | 875 | m_strHost = _u.m_strHost; |
874 | m_strPath = _u.m_strPath; | 876 | m_strPath = _u.m_strPath; |
875 | m_strPath_encoded = _u.m_strPath_encoded; | 877 | m_strPath_encoded = _u.m_strPath_encoded; |
@@ -1353,93 +1355,93 @@ KURL::List KURL::split( const KURL& _url ) | |||
1353 | for( it = lst.begin() ; it != lst.end(); ++it ) | 1355 | for( it = lst.begin() ; it != lst.end(); ++it ) |
1354 | { | 1356 | { |
1355 | (*it).m_strRef_encoded = ref; | 1357 | (*it).m_strRef_encoded = ref; |
1356 | } | 1358 | } |
1357 | 1359 | ||
1358 | return lst; | 1360 | return lst; |
1359 | } | 1361 | } |
1360 | 1362 | ||
1361 | KURL::List KURL::split( const QString& _url ) | 1363 | KURL::List KURL::split( const QString& _url ) |
1362 | { | 1364 | { |
1363 | return split(KURL(_url)); | 1365 | return split(KURL(_url)); |
1364 | } | 1366 | } |
1365 | 1367 | ||
1366 | KURL KURL::join( const KURL::List & lst ) | 1368 | KURL KURL::join( const KURL::List & lst ) |
1367 | { | 1369 | { |
1368 | if (lst.isEmpty()) return KURL(); | 1370 | if (lst.isEmpty()) return KURL(); |
1369 | KURL tmp; | 1371 | KURL tmp; |
1370 | 1372 | ||
1371 | KURL::List::ConstIterator first = lst.fromLast(); | 1373 | KURL::List::ConstIterator first = lst.fromLast(); |
1372 | for( KURL::List::ConstIterator it = first; it != lst.end(); --it ) | 1374 | for( KURL::List::ConstIterator it = first; it != lst.end(); --it ) |
1373 | { | 1375 | { |
1374 | KURL u(*it); | 1376 | KURL u(*it); |
1375 | if (it != first) | 1377 | if (it != first) |
1376 | { | 1378 | { |
1377 | if (!u.m_strRef_encoded) u.m_strRef_encoded = tmp.url(); | 1379 | if (u.m_strRef_encoded.isEmpty()) u.m_strRef_encoded = tmp.url(); |
1378 | else u.m_strRef_encoded += "#" + tmp.url(); // Support more than one suburl thingy | 1380 | else u.m_strRef_encoded += "#" + tmp.url(); // Support more than one suburl thingy |
1379 | } | 1381 | } |
1380 | tmp = u; | 1382 | tmp = u; |
1381 | } | 1383 | } |
1382 | 1384 | ||
1383 | return tmp; | 1385 | return tmp; |
1384 | } | 1386 | } |
1385 | 1387 | ||
1386 | QString KURL::fileName( bool _strip_trailing_slash ) const | 1388 | QString KURL::fileName( bool _strip_trailing_slash ) const |
1387 | { | 1389 | { |
1388 | QString fname; | 1390 | QString fname; |
1389 | if (hasSubURL()) { // If we have a suburl, then return the filename from there | 1391 | if (hasSubURL()) { // If we have a suburl, then return the filename from there |
1390 | KURL::List list = KURL::split(*this); | 1392 | KURL::List list = KURL::split(*this); |
1391 | KURL::List::Iterator it = list.fromLast(); | 1393 | KURL::List::Iterator it = list.fromLast(); |
1392 | return (*it).fileName(_strip_trailing_slash); | 1394 | return (*it).fileName(_strip_trailing_slash); |
1393 | } | 1395 | } |
1394 | const QString &path = m_strPath; | 1396 | const QString &path = m_strPath; |
1395 | 1397 | ||
1396 | int len = path.length(); | 1398 | int len = path.length(); |
1397 | if ( len == 0 ) | 1399 | if ( len == 0 ) |
1398 | return fname; | 1400 | return fname; |
1399 | 1401 | ||
1400 | if ( _strip_trailing_slash ) | 1402 | if ( _strip_trailing_slash ) |
1401 | { | 1403 | { |
1402 | while ( len >= 1 && path[ len - 1 ] == '/' ) | 1404 | while ( len >= 1 && path[ len - 1 ] == '/' ) |
1403 | len--; | 1405 | len--; |
1404 | } | 1406 | } |
1405 | else if ( path[ len - 1 ] == '/' ) | 1407 | else if ( path[ len - 1 ] == '/' ) |
1406 | return fname; | 1408 | return fname; |
1407 | 1409 | ||
1408 | // Does the path only consist of '/' characters ? | 1410 | // Does the path only consist of '/' characters ? |
1409 | if ( len == 1 && path[ 0 ] == '/' ) | 1411 | if ( len == 1 && path[ 0 ] == '/' ) |
1410 | return fname; | 1412 | return fname; |
1411 | 1413 | ||
1412 | // Skip last n slashes | 1414 | // Skip last n slashes |
1413 | int n = 1; | 1415 | int n = 1; |
1414 | if (!m_strPath_encoded.isEmpty()) | 1416 | if (!m_strPath_encoded.isEmpty()) |
1415 | { | 1417 | { |
1416 | // This is hairy, we need the last unencoded slash. | 1418 | // This is hairy, we need the last unencoded slash. |
1417 | // Count in the encoded string how many encoded slashes follow the last | 1419 | // Count in the encoded string how many encoded slashes follow the last |
1418 | // unencoded one. | 1420 | // unencoded one. |
1419 | int i = m_strPath_encoded.findRev( '/', len - 1 ); | 1421 | int i = m_strPath_encoded.findRev( '/', len - 1 ); |
1420 | QString fileName_encoded = m_strPath_encoded.mid(i+1); | 1422 | QString fileName_encoded = m_strPath_encoded.mid(i+1); |
1421 | n += fileName_encoded.contains("%2f", false); | 1423 | n += fileName_encoded.count("%2f", Qt::CaseInsensitive); |
1422 | } | 1424 | } |
1423 | int i = len; | 1425 | int i = len; |
1424 | do { | 1426 | do { |
1425 | i = path.findRev( '/', i - 1 ); | 1427 | i = path.findRev( '/', i - 1 ); |
1426 | } | 1428 | } |
1427 | while (--n && (i > 0)); | 1429 | while (--n && (i > 0)); |
1428 | 1430 | ||
1429 | // If ( i == -1 ) => the first character is not a '/' | 1431 | // If ( i == -1 ) => the first character is not a '/' |
1430 | // So it's some URL like file:blah.tgz, return the whole path | 1432 | // So it's some URL like file:blah.tgz, return the whole path |
1431 | if ( i == -1 ) { | 1433 | if ( i == -1 ) { |
1432 | if ( len == (int)path.length() ) | 1434 | if ( len == (int)path.length() ) |
1433 | fname = path; | 1435 | fname = path; |
1434 | else | 1436 | else |
1435 | // Might get here if _strip_trailing_slash is true | 1437 | // Might get here if _strip_trailing_slash is true |
1436 | fname = path.left( len ); | 1438 | fname = path.left( len ); |
1437 | } | 1439 | } |
1438 | else | 1440 | else |
1439 | { | 1441 | { |
1440 | fname = path.mid( i + 1, len - i - 1 ); // TO CHECK | 1442 | fname = path.mid( i + 1, len - i - 1 ); // TO CHECK |
1441 | } | 1443 | } |
1442 | return fname; | 1444 | return fname; |
1443 | } | 1445 | } |
1444 | 1446 | ||
1445 | void KURL::addPath( const QString& _txt ) | 1447 | void KURL::addPath( const QString& _txt ) |