kdecore Library API Documentation

kurl.cpp

00001 /*
00002     Copyright (C) 1999 Torben Weis <weis@kde.org>
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License as published by the Free Software Foundation; either
00007     version 2 of the License, or (at your option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Library General Public License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to
00016     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017     Boston, MA 02111-1307, USA.
00018 */
00019 
00020 #include "kurl.h"
00021 
00022 #ifndef KDE_QT_ONLY
00023 #include <kdebug.h>
00024 #include <kglobal.h>
00025 #endif
00026 
00027 #include <stdio.h>
00028 #include <assert.h>
00029 #include <ctype.h>
00030 #include <stdlib.h>
00031 
00032 #include <qurl.h>
00033 #include <qdir.h>
00034 #include <qstringlist.h>
00035 #include <qregexp.h>
00036 #include <qstylesheet.h>
00037 #include <qmap.h>
00038 #include <qtextcodec.h>
00039 
00040 static QTextCodec * codecForHint( int encoding_hint /* not 0 ! */ )
00041 {
00042     return QTextCodec::codecForMib( encoding_hint );
00043 }
00044 
00045 static QString encode( const QString& segment, bool encode_slash, int encoding_hint )
00046 {
00047   const char *encode_string;
00048   if (encode_slash)
00049     encode_string = "<>#@\"&%?={}|^~[]\'`\\:+/";
00050   else
00051     encode_string = "<>#@\"&%?={}|^~[]\'`\\:+";
00052 
00053   QCString local;
00054   if (encoding_hint==0)
00055     local = segment.local8Bit();
00056   else
00057   {
00058       QTextCodec * textCodec = codecForHint( encoding_hint );
00059       if (!textCodec)
00060           local = segment.local8Bit();
00061       else
00062           local = textCodec->fromUnicode( segment );
00063   }
00064 
00065   int old_length = local.length();
00066 
00067   if ( !old_length )
00068     return segment.isNull() ? QString::null : QString(""); // differenciate null and empty
00069 
00070   // a worst case approximation
00071   QChar *new_segment = new QChar[ old_length * 3 + 1 ];
00072   int new_length = 0;
00073 
00074   for ( int i = 0; i < old_length; i++ )
00075   {
00076     // 'unsave' and 'reserved' characters
00077     // according to RFC 1738,
00078     // 2.2. URL Character Encoding Issues (pp. 3-4)
00079     // WABA: Added non-ascii
00080     unsigned char character = local[i];
00081     if ( (character <= 32) || (character >= 127) ||
00082          strchr(encode_string, character) )
00083     {
00084       new_segment[ new_length++ ] = '%';
00085 
00086       unsigned int c = character / 16;
00087       c += (c > 9) ? ('A' - 10) : '0';
00088       new_segment[ new_length++ ] = c;
00089 
00090       c = character % 16;
00091       c += (c > 9) ? ('A' - 10) : '0';
00092       new_segment[ new_length++ ] = c;
00093 
00094     }
00095     else
00096       new_segment[ new_length++ ] = local[i];
00097   }
00098 
00099   QString result = QString(new_segment, new_length);
00100   delete [] new_segment;
00101   return result;
00102 }
00103 
00104 static int hex2int( unsigned int _char )
00105 {
00106   if ( _char >= 'A' && _char <='F')
00107     return _char - 'A' + 10;
00108   if ( _char >= 'a' && _char <='f')
00109     return _char - 'a' + 10;
00110   if ( _char >= '0' && _char <='9')
00111     return _char - '0';
00112   return -1;
00113 }
00114 
00115 // WABA: The result of lazy_encode isn't usable for a URL which
00116 // needs to satisfies RFC requirements. However, the following
00117 // operation will make it usable again:
00118 //      encode(decode(...))
00119 //
00120 // As a result one can see that url.prettyURL() does not result in
00121 // a RFC compliant URL but that the following sequence does:
00122 //      KURL(url.prettyURL()).url()
00123 
00124 
00125 static QString lazy_encode( const QString& segment )
00126 {
00127   int old_length = segment.length();
00128 
00129   if ( !old_length )
00130     return QString::null;
00131 
00132   // a worst case approximation
00133   QChar *new_segment = new QChar[ old_length * 3 + 1 ];
00134   int new_length = 0;
00135 
00136   for ( int i = 0; i < old_length; i++ )
00137   {
00138     unsigned int character = segment[i].unicode(); // Don't use latin1()
00139                                                    // It returns 0 for non-latin1 values
00140     // Small set of really ambiguous chars
00141     if ((character < 32) ||  // Low ASCII
00142         ((character == '%') && // The escape character itself
00143            (i+2 < old_length) && // But only if part of a valid escape sequence!
00144           (hex2int(segment[i+1].unicode())!= -1) &&
00145           (hex2int(segment[i+2].unicode())!= -1)) ||
00146         (character == '?') || // Start of query delimiter
00147         (character == '#') || // Start of reference delimiter
00148         ((character == 32) && (i+1 == old_length))) // A trailing space
00149     {
00150       new_segment[ new_length++ ] = '%';
00151 
00152       unsigned int c = character / 16;
00153       c += (c > 9) ? ('A' - 10) : '0';
00154       new_segment[ new_length++ ] = c;
00155 
00156       c = character % 16;
00157       c += (c > 9) ? ('A' - 10) : '0';
00158       new_segment[ new_length++ ] = c;
00159     }
00160     else
00161     new_segment[ new_length++ ] = segment[i];
00162   }
00163 
00164   QString result = QString(new_segment, new_length);
00165   delete [] new_segment;
00166   return result;
00167 }
00168 
00169 static void decode( const QString& segment, QString &decoded, QString &encoded, int encoding_hint=0, bool updateDecoded = true )
00170 {
00171   decoded = QString::null;
00172   encoded = segment;
00173 
00174   int old_length = segment.length();
00175   if ( !old_length )
00176     return;
00177 
00178   QTextCodec *textCodec = 0;
00179   if (encoding_hint)
00180       textCodec = codecForHint( encoding_hint );
00181 
00182   if (!textCodec)
00183       textCodec = QTextCodec::codecForLocale();
00184 
00185   if (!textCodec->canEncode(segment))
00186       textCodec = codecForHint( 106 ); // Fall back to utf-8 if it doesn't fit.
00187 
00188   QCString csegment = textCodec->fromUnicode(segment);
00189   old_length = csegment.length();
00190 
00191   int new_length = 0;
00192   int new_length2 = 0;
00193 
00194   // make a copy of the old one
00195   char *new_segment = new char[ old_length + 1 ];
00196   QChar *new_usegment = new QChar[ old_length * 3 + 1 ];
00197 
00198   int i = 0;
00199   while( i < old_length )
00200   {
00201     bool bReencode = false;
00202     unsigned char character = csegment[ i++ ];
00203     if ((character <= ' ') || (character > 127))
00204        bReencode = true;
00205 
00206     new_usegment [ new_length2++ ] = character;
00207     if (character == '%' )
00208     {
00209       int a = i+1 < old_length ? hex2int( csegment[i] ) : -1;
00210       int b = i+1 < old_length ? hex2int( csegment[i+1] ) : -1;
00211       if ((a == -1) || (b == -1)) // Only replace if sequence is valid
00212       {
00213          // Contains stray %, make sure to re-encode!
00214          bReencode = true;
00215       }
00216       else
00217       {
00218          // Valid %xx sequence
00219          character = a * 16 + b; // Replace with value of %dd
00220          if (!character && updateDecoded)
00221             break; // Stop at %00
00222 
00223          new_usegment [ new_length2++ ] = (unsigned char) csegment[i++];
00224          new_usegment [ new_length2++ ] = (unsigned char) csegment[i++];
00225       }
00226     }
00227     if (bReencode)
00228     {
00229       new_length2--;
00230       new_usegment [ new_length2++ ] = '%';
00231 
00232       unsigned int c = character / 16;
00233       c += (c > 9) ? ('A' - 10) : '0';
00234       new_usegment[ new_length2++ ] = c;
00235 
00236       c = character % 16;
00237       c += (c > 9) ? ('A' - 10) : '0';
00238       new_usegment[ new_length2++ ] = c;
00239     }
00240 
00241     new_segment [ new_length++ ] = character;
00242   }
00243   new_segment [ new_length ] = 0;
00244 
00245   encoded = QString( new_usegment, new_length2);
00246 
00247   // Encoding specified
00248   if (updateDecoded)
00249   {
00250      QByteArray array;
00251      array.setRawData(new_segment, new_length);
00252      decoded = textCodec->toUnicode( array, new_length );
00253      array.resetRawData(new_segment, new_length);
00254      QCString validate = textCodec->fromUnicode(decoded);
00255 
00256      if (strcmp(validate.data(), new_segment) != 0)
00257      {
00258         decoded = QString::fromLocal8Bit(new_segment, new_length);
00259      }
00260   }
00261 
00262   delete [] new_segment;
00263   delete [] new_usegment;
00264 }
00265 
00266 static QString decode(const QString &segment, int encoding_hint = 0)
00267 {
00268   QString result;
00269   QString tmp;
00270   decode(segment, result, tmp, encoding_hint);
00271   return result;
00272 }
00273 
00274 static QString cleanpath(const QString &_path, bool cleanDirSeparator, bool decodeDots)
00275 {
00276   if (_path.isEmpty()) return QString::null;
00277   
00278   if (_path[0] != '/')
00279      return _path; // Don't mangle mailto-style URLs
00280   
00281   QString path = _path;
00282 
00283   int len = path.length();
00284 
00285   if (decodeDots)
00286   {
00287 #ifndef KDE_QT_ONLY
00288      static const QString &encodedDot = KGlobal::staticQString("%2e");
00289 #else
00290      QString encodedDot("%2e");
00291 #endif
00292      if (path.find(encodedDot, 0, false) != -1)
00293      {
00294 #ifndef KDE_QT_ONLY
00295         static const QString &encodedDOT = KGlobal::staticQString("%2E"); // Uppercase!
00296 #else
00297         QString encodedDOT("%2E");
00298 #endif
00299         path.replace(encodedDot, ".");
00300         path.replace(encodedDOT, ".");
00301         len = path.length();
00302      }
00303   }
00304 
00305   bool slash = (len && path[len-1] == '/') ||
00306                (len > 1 && path[len-2] == '/' && path[len-1] == '.');
00307 
00308   // The following code cleans up directory path much like
00309   // QDir::cleanDirPath() except it can be made to ignore multiple
00310   // directory separators by setting the flag to false.  That fixes
00311   // bug# 15044, mail.altavista.com and other similar brain-dead server
00312   // implementations that do not follow what has been specified in
00313   // RFC 2396!! (dA)
00314   QString result;
00315   int cdUp, orig_pos, pos;
00316 
00317   cdUp = 0;
00318   pos = orig_pos = len;
00319   while ( pos && (pos = path.findRev('/',--pos)) != -1 )
00320   {
00321     len = orig_pos - pos - 1;
00322     if ( len == 2 && path[pos+1] == '.' && path[pos+2] == '.' )
00323       cdUp++;
00324     else
00325     {
00326       // Ignore any occurances of '.'
00327       // This includes entries that simply do not make sense like /..../
00328       if ( (len || !cleanDirSeparator) &&
00329            (len != 1 || path[pos+1] != '.' ) )
00330       {
00331           if ( !cdUp )
00332               result.prepend(path.mid(pos, len+1));
00333           else
00334               cdUp--;
00335       }
00336     }
00337     orig_pos = pos;
00338   }
00339 
00340   if ( result.isEmpty() )
00341     result = "/";
00342   else if ( slash && result[result.length()-1] != '/' )
00343        result.append('/');
00344 
00345   return result;
00346 }
00347 
00348 bool KURL::isRelativeURL(const QString &_url)
00349 {
00350   int len = _url.length();
00351   if (!len) return true; // Very short relative URL.
00352   const QChar *str = _url.unicode();
00353 
00354   // Absolute URL must start with alpha-character
00355   if (!isalpha(str[0].latin1()))
00356      return true; // Relative URL
00357 
00358   for(int i = 1; i < len; i++)
00359   {
00360      char c = str[i].latin1(); // Note: non-latin1 chars return 0!
00361      if (c == ':')
00362         return false; // Absolute URL
00363 
00364      // Protocol part may only contain alpha, digit, + or -
00365      if (!isalpha(c) && !isdigit(c) && (c != '+') && (c != '-'))
00366         return true; // Relative URL
00367   }
00368   // URL did not contain ':'
00369   return true; // Relative URL
00370 }
00371 
00372 KURL::List::List(const KURL &url)
00373 {
00374     append( url );
00375 }
00376 
00377 KURL::List::List(const QStringList &list)
00378 {
00379   for (QStringList::ConstIterator it = list.begin();
00380        it != list.end();
00381        it++)
00382     {
00383       append( KURL(*it) );
00384     }
00385 }
00386 
00387 QStringList KURL::List::toStringList() const
00388 {
00389   QStringList lst;
00390    for( KURL::List::ConstIterator it = begin();
00391         it != end();
00392         it++)
00393    {
00394       lst.append( (*it).url() );
00395    }
00396    return lst;
00397 }
00398 
00399 
00400 KURL::KURL()
00401 {
00402   reset();
00403 }
00404 
00405 KURL::~KURL()
00406 {
00407 }
00408 
00409 
00410 KURL::KURL( const QString &url, int encoding_hint )
00411 {
00412   reset();
00413   parse( url, encoding_hint );
00414 }
00415 
00416 KURL::KURL( const char * url, int encoding_hint )
00417 {
00418   reset();
00419   parse( QString::fromLatin1(url), encoding_hint );
00420 }
00421 
00422 KURL::KURL( const QCString& url, int encoding_hint )
00423 {
00424   reset();
00425   parse( QString::fromLatin1(url), encoding_hint );
00426 }
00427 
00428 KURL::KURL( const KURL& _u )
00429 {
00430   *this = _u;
00431 }
00432 
00433 QDataStream & operator<< (QDataStream & s, const KURL & a)
00434 {
00435   QString QueryForWire=a.m_strQuery_encoded;
00436   if (!a.m_strQuery_encoded.isNull())
00437     QueryForWire.prepend("?");
00438 
00439     s << a.m_strProtocol << a.m_strUser << a.m_strPass << a.m_strHost
00440       << a.m_strPath << a.m_strPath_encoded << QueryForWire << a.m_strRef_encoded
00441       << Q_INT8(a.m_bIsMalformed ? 1 : 0) << a.m_iPort;
00442     return s;
00443 }
00444 
00445 QDataStream & operator>> (QDataStream & s, KURL & a)
00446 {
00447     Q_INT8 malf;
00448     QString QueryFromWire;
00449     s >> a.m_strProtocol >> a.m_strUser >> a.m_strPass >> a.m_strHost
00450       >> a.m_strPath >> a.m_strPath_encoded >> QueryFromWire >> a.m_strRef_encoded
00451       >> malf >> a.m_iPort;
00452     a.m_bIsMalformed = (malf != 0);
00453 
00454     if ( QueryFromWire.isEmpty() )
00455       a.m_strQuery_encoded = QString::null;
00456     else
00457       a.m_strQuery_encoded = QueryFromWire.mid(1);
00458 
00459     return s;
00460 }
00461 
00462 #ifndef QT_NO_NETWORKPROTOCOL
00463 KURL::KURL( const QUrl &u )
00464 {
00465   *this = u;
00466 }
00467 #endif
00468 
00469 KURL::KURL( const KURL& _u, const QString& _rel_url, int encoding_hint )
00470 {
00471   // WORKAROUND THE RFC 1606 LOOPHOLE THAT ALLOWS
00472   // http:/index.html AS A VALID SYNTAX FOR RELATIVE
00473   // URLS. ( RFC 2396 section 5.2 item # 3 )
00474   QString rUrl = _rel_url;
00475   int len = _u.m_strProtocol.length();
00476   if ( !_u.m_strHost.isEmpty() && !rUrl.isEmpty() &&
00477        rUrl.find( _u.m_strProtocol, 0, false ) == 0 &&
00478        rUrl[len] == ':' && (rUrl[len+1] != '/' ||
00479        (rUrl[len+1] == '/' && rUrl[len+2] != '/')) )
00480   {
00481     rUrl.remove( 0, rUrl.find( ':' ) + 1 );
00482   }
00483 
00484   if ( rUrl.isEmpty() )
00485   {
00486     *this = _u;
00487   }
00488   else if ( rUrl[0] == '#' )
00489   {
00490     *this = _u;
00491     QString ref = decode(rUrl.mid(1), encoding_hint);
00492     if ( ref.isNull() )
00493         ref = ""; // we know there was an (empty) html ref, we saw the '#'
00494     setHTMLRef( ref );
00495   }
00496   else if ( isRelativeURL( rUrl) )
00497   {
00498     *this = _u;
00499     m_strQuery_encoded = QString::null;
00500     m_strRef_encoded = QString::null;
00501     if ( rUrl[0] == '/')
00502     {
00503         if ((rUrl.length() > 1) && (rUrl[1] == '/'))
00504         {
00505            m_strHost = QString::null;
00506         }
00507         m_strPath = QString::null;
00508         m_strPath_encoded = QString::null;
00509     }
00510     else if ( rUrl[0] != '?' )
00511     {
00512        int pos = m_strPath.findRev( '/' );
00513        if (pos >= 0)
00514           m_strPath.truncate(pos);
00515        m_strPath += '/';
00516        if (!m_strPath_encoded.isEmpty())
00517        {
00518           pos = m_strPath_encoded.findRev( '/' );
00519           if (pos >= 0)
00520              m_strPath_encoded.truncate(pos);
00521           m_strPath_encoded += '/';
00522        }
00523     }
00524     else
00525     {
00526        if ( m_strPath.isEmpty() )
00527           m_strPath = '/';
00528     }
00529     KURL tmp( url() + rUrl, encoding_hint);
00530     *this = tmp;
00531     cleanPath(false);
00532   }
00533   else
00534   {
00535     KURL tmp( rUrl, encoding_hint);
00536     *this = tmp;
00537     if (!_u.m_strUser.isEmpty() && m_strUser.isEmpty() && (_u.m_strHost == m_strHost) && (_u.m_strProtocol == m_strProtocol))
00538     {
00539       m_strUser = _u.m_strUser;
00540       m_strPass = _u.m_strPass;
00541     }
00542     cleanPath(false);
00543   }
00544 }
00545 
00546 void KURL::reset()
00547 {
00548   m_strProtocol = QString::null;
00549   m_strUser = QString::null;
00550   m_strPass = QString::null;
00551   m_strHost = QString::null;
00552   m_strPath = QString::null;
00553   m_strPath_encoded = QString::null;
00554   m_strQuery_encoded = QString::null;
00555   m_strRef_encoded = QString::null;
00556   m_bIsMalformed = true;
00557   m_iPort = 0;
00558 }
00559 
00560 bool KURL::isEmpty() const
00561 {
00562   return (m_strPath.isEmpty() && m_strProtocol.isEmpty());
00563 }
00564 
00565 void KURL::parse( const QString& _url, int encoding_hint )
00566 {
00567   //kdDebug(126) << "parse " << _url << endl;
00568   // Return immediately whenever the given url
00569   // is empty or null.
00570   if ( _url.isEmpty() )
00571   {
00572     m_strProtocol = _url;
00573     return;
00574   }
00575 
00576   QString port;
00577   bool badHostName = false;
00578   int start = 0;
00579   uint len = _url.length();
00580   QChar* buf = new QChar[ len + 1 ];
00581   QChar* orig = buf;
00582   memcpy( buf, _url.unicode(), len * sizeof( QChar ) );
00583 
00584   QChar delim;
00585   QString tmp;
00586 
00587   uint pos = 0;
00588 
00589   // Node 1: Accept alpha or slash
00590   QChar x = buf[pos++];
00591   if ( x == '/' )
00592     goto Node9;
00593   if ( !isalpha( (int)x ) )
00594     goto NodeErr;
00595 
00596   // Node 2: Accept any amount of (alpha|digit|'+'|'-')
00597   // '.' is not currently accepted, because current KURL may be confused.
00598   // Proceed with :// :/ or :
00599   while( (isalpha((int)buf[pos]) || isdigit((int)buf[pos]) ||
00600           buf[pos] == '+' || buf[pos] == '-') &&
00601          pos < len ) pos++;
00602   if ( pos == len - 1 ) // Need to always compare length()-1 otherwise KURL passes "http:" as legal!!! (DA)
00603     goto NodeErr;
00604   if (buf[pos] == ':' && buf[pos+1] == '/' && buf[pos+2] == '/' )
00605     {
00606       m_strProtocol = QString( orig, pos ).lower();
00607       pos += 3;
00608     }
00609   else if (buf[pos] == ':' && buf[pos+1] == '/' )
00610     {
00611       m_strProtocol = QString( orig, pos ).lower();
00612       //kdDebug(126)<<"setting protocol to "<<m_strProtocol<<endl;
00613       pos++;
00614       start = pos;
00615       goto Node9;
00616     }
00617   else if ( buf[pos] == ':' )
00618     {
00619       m_strProtocol = QString( orig, pos ).lower();
00620       //kdDebug(126)<<"setting protocol to "<<m_strProtocol<<endl;
00621       pos++;
00622       start = pos;
00623       goto Node9;
00624     }
00625   else
00626     goto NodeErr;
00627 
00628   //Node 3: We need at least one character here
00629   if ( pos == len )
00630       goto NodeErr;
00631   start = pos;
00632 
00633   // Node 4: Accept any amount of characters.
00634   if (buf[pos] == '[')     // An IPv6 host follows.
00635       goto Node8;
00636   // Terminate on / or @ or ? or # or " or ; or <
00637   x = buf[pos];
00638   while( (x != ':') && (x != '@') && (x != '/') && (x != '?') && (x != '#') &&  (pos < len) )
00639   {
00640      if ((x == '\"') || (x == ';') || (x == '<'))
00641         badHostName = true;
00642      x = buf[++pos];
00643   }
00644   if ( pos == len )
00645     {
00646       if (badHostName)
00647          goto NodeErr;
00648 
00649       m_strHost = decode(QString( buf + start, pos - start ), encoding_hint);
00650       goto NodeOk;
00651     }
00652   if ( x == '@' )
00653     {
00654       m_strUser = decode(QString( buf + start, pos - start ), encoding_hint);
00655       pos++;
00656       goto Node7;
00657     }
00658   /* else if ( x == ':' )
00659      {
00660      m_strHost = decode(QString( buf + start, pos - start ), encoding_hint);
00661      pos++;
00662      goto Node8a;
00663      } */
00664   else if ( (x == '/') || (x == '?') || (x == '#'))
00665     {
00666       if (badHostName)
00667          goto NodeErr;
00668 
00669       m_strHost = decode(QString( buf + start, pos - start ), encoding_hint);
00670       start = pos;
00671       goto Node9;
00672     }
00673   else if ( x != ':' )
00674     goto NodeErr;
00675   m_strUser = decode(QString( buf + start, pos - start ), encoding_hint);
00676   pos++;
00677 
00678   // Node 5: We need at least one character
00679   if ( pos == len )
00680     goto NodeErr;
00681   start = pos++;
00682 
00683   // Node 6: Read everything until @, /, ? or #
00684   while( (pos < len) &&
00685         (buf[pos] != '@') &&
00686         (buf[pos] != '/') &&
00687         (buf[pos] != '?') &&
00688         (buf[pos] != '#')) pos++;
00689   // If we now have a '@' the ':' seperates user and password.
00690   // Otherwise it seperates host and port.
00691   if ( (pos == len) || (buf[pos] != '@') )
00692     {
00693       // Ok the : was used to separate host and port
00694       if (badHostName)
00695          goto NodeErr;
00696       m_strHost = m_strUser;
00697       m_strUser = QString::null;
00698       QString tmp( buf + start, pos - start );
00699       char *endptr;
00700       m_iPort = (unsigned short int)strtol(tmp.ascii(), &endptr, 10);
00701       if ((pos == len) && (strlen(endptr) == 0))
00702         goto NodeOk;
00703       // there is more after the digits
00704       pos -= strlen(endptr);
00705       start = pos++;
00706       goto Node9;
00707     }
00708   m_strPass = decode(QString( buf + start, pos - start), encoding_hint);
00709   pos++;
00710 
00711   // Node 7: We need at least one character
00712  Node7:
00713   if ( pos == len )
00714     goto NodeErr;
00715 
00716  Node8:
00717   if (buf[pos] == '[')
00718   {
00719     // IPv6 address
00720     start = ++pos; // Skip '['
00721 
00722     // Node 8b: Read everything until ] or terminate
00723     badHostName = false;
00724     x = buf[pos];
00725     while( (x != ']') &&  (pos < len) )
00726     {
00727        if ((x == '\"') || (x == ';') || (x == '<'))
00728           badHostName = true;
00729        x = buf[++pos];
00730     }
00731     if (badHostName)
00732        goto NodeErr;
00733     m_strHost = decode(QString( buf + start, pos - start ), encoding_hint);
00734     if (pos < len) pos++; // Skip ']'
00735     if (pos == len)
00736        goto NodeOk;
00737   }
00738   else
00739   {
00740     // Non IPv6 address
00741     start = pos++;
00742 
00743     // Node 8b: Read everything until / : or terminate
00744     badHostName = false;
00745     x = buf[pos];
00746     while( (x != ':') && (x != '@') && (x != '/') && (x != '?') && (x != '#') &&  (pos < len) )
00747     {
00748        if ((x == '\"') || (x == ';') || (x == '<'))
00749           badHostName = true;
00750        x = buf[++pos];
00751     }
00752     if (badHostName)
00753        goto NodeErr;
00754     if ( pos == len )
00755     {
00756        m_strHost = decode(QString( buf + start, pos - start ), encoding_hint);
00757        goto NodeOk;
00758     }
00759     m_strHost = decode(QString( buf + start, pos - start ), encoding_hint);
00760   }
00761   x = buf[pos];
00762   if ( x == '/' )
00763     {
00764       start = pos++;
00765       goto Node9;
00766     }
00767   else if ( x != ':' )
00768     goto NodeErr;
00769   pos++;
00770 
00771   // Node 8a: Accept at least one digit
00772   if ( pos == len )
00773     goto NodeErr;
00774   start = pos;
00775   if ( !isdigit( buf[pos++] ) )
00776     goto NodeErr;
00777 
00778   // Node 8b: Accept any amount of digits
00779   while( isdigit( buf[pos] ) && pos < len ) pos++;
00780   port = QString( buf + start, pos - start );
00781   m_iPort = port.toUShort();
00782   if ( pos == len )
00783     goto NodeOk;
00784   start = pos++;
00785 
00786  Node9: // parse path until query or reference reached
00787 
00788   while( buf[pos] != '#' && buf[pos]!='?' && pos < len ) pos++;
00789 
00790   tmp = QString( buf + start, pos - start );
00791   //kdDebug(126)<<" setting encoded path&query to:"<<tmp<<endl;
00792   setEncodedPath( tmp, encoding_hint );
00793 
00794   if ( pos == len )
00795       goto NodeOk;
00796 
00797  //Node10: // parse query or reference depending on what comes first
00798   delim = (buf[pos++]=='#'?'?':'#');
00799 
00800   start = pos;
00801 
00802   while(buf[pos]!=delim && pos < len) pos++;
00803 
00804   tmp = QString(buf + start, pos - start);
00805   if (delim=='#')
00806       setQuery(tmp, encoding_hint);
00807   else
00808       m_strRef_encoded = tmp;
00809 
00810   if (pos == len)
00811       goto NodeOk;
00812 
00813  //Node11: // feed the rest into the remaining variable
00814   tmp = QString( buf + pos + 1, len - pos - 1);
00815   if (delim == '#')
00816       m_strRef_encoded = tmp;
00817   else
00818       setQuery(tmp, encoding_hint);
00819 
00820  NodeOk:
00821   //kdDebug(126)<<"parsing finished. m_strProtocol="<<m_strProtocol<<" m_strHost="<<m_strHost<<" m_strPath="<<m_strPath<<endl;
00822   delete []orig;
00823   m_bIsMalformed = false; // Valid URL
00824   if (m_strProtocol.isEmpty())
00825     m_strProtocol = "file";
00826 
00827   //kdDebug()<<"Prot="<<m_strProtocol<<"\nUser="<<m_strUser<<"\nPass="<<m_strPass<<"\nHost="<<m_strHost<<"\nPath="<<m_strPath<<"\nQuery="<<m_strQuery_encoded<<"\nRef="<<m_strRef_encoded<<"\nPort="<<m_iPort<<endl;
00828   if (m_strProtocol == "file")
00829   {
00830     if (!m_strHost.isEmpty())
00831     {
00832       // File-protocol has a host name..... hmm?
00833       if (m_strHost.lower() == "localhost")
00834       {
00835         m_strHost = QString::null; // We can ignore localhost
00836       }
00837       else {
00838         // Pass the hostname as part of the path. Perhaps system calls
00839         // just handle it.
00840         m_strPath = "//"+m_strHost+m_strPath;
00841         m_strPath_encoded = QString::null;
00842         m_strHost = QString::null;
00843       }
00844     }
00845   }
00846   return;
00847 
00848  NodeErr:
00849 //  kdDebug(126) << "KURL couldn't parse URL \"" << _url << "\"" << endl;
00850   delete []orig;
00851   reset();
00852   m_strProtocol = _url;
00853 }
00854 
00855 KURL& KURL::operator=( const QString& _url )
00856 {
00857   reset();
00858   parse( _url );
00859 
00860   return *this;
00861 }
00862 
00863 KURL& KURL::operator=( const char * _url )
00864 {
00865   reset();
00866   parse( QString::fromLatin1(_url) );
00867 
00868   return *this;
00869 }
00870 
00871 #ifndef QT_NO_NETWORKPROTOCOL
00872 KURL& KURL::operator=( const QUrl & u )
00873 {
00874   m_strProtocol = u.protocol();
00875   m_strUser = u.user();
00876   m_strPass = u.password();
00877   m_strHost = u.host();
00878   m_strPath = u.path( FALSE );
00879   m_strPath_encoded = QString::null;
00880   m_strQuery_encoded = u.query();
00881   m_strRef_encoded = u.ref();
00882   m_bIsMalformed = !u.isValid();
00883   m_iPort = u.port();
00884 
00885   return *this;
00886 }
00887 #endif
00888 
00889 KURL& KURL::operator=( const KURL& _u )
00890 {
00891   m_strProtocol = _u.m_strProtocol;
00892   m_strUser = _u.m_strUser;
00893   m_strPass = _u.m_strPass;
00894   m_strHost = _u.m_strHost;
00895   m_strPath = _u.m_strPath;
00896   m_strPath_encoded = _u.m_strPath_encoded;
00897   m_strQuery_encoded = _u.m_strQuery_encoded;
00898   m_strRef_encoded = _u.m_strRef_encoded;
00899   m_bIsMalformed = _u.m_bIsMalformed;
00900   m_iPort = _u.m_iPort;
00901 
00902   return *this;
00903 }
00904 
00905 bool KURL::operator==( const KURL& _u ) const
00906 {
00907   if ( isMalformed() || _u.isMalformed() )
00908     return false;
00909 
00910   if ( m_strProtocol == _u.m_strProtocol &&
00911        m_strUser == _u.m_strUser &&
00912        m_strPass == _u.m_strPass &&
00913        m_strHost.lower() == _u.m_strHost.lower() &&
00914        m_strPath == _u.m_strPath &&
00915        // The encoded path may be null, but the URLs are still equal (David)
00916        ( m_strPath_encoded.isNull() || _u.m_strPath_encoded.isNull() ||
00917          m_strPath_encoded == _u.m_strPath_encoded ) &&
00918        m_strQuery_encoded == _u.m_strQuery_encoded &&
00919        m_strRef_encoded == _u.m_strRef_encoded &&
00920        m_iPort == _u.m_iPort )
00921   {
00922     return true;
00923   }
00924 
00925   return false;
00926 }
00927 
00928 bool KURL::operator==( const QString& _u ) const
00929 {
00930   KURL u( _u );
00931   return ( *this == u );
00932 }
00933 
00934 bool KURL::cmp( const KURL &u, bool ignore_trailing ) const
00935 {
00936   return equals( u, ignore_trailing );
00937 }
00938 
00939 bool KURL::equals( const KURL &_u, bool ignore_trailing ) const
00940 {
00941   if ( isMalformed() || _u.isMalformed() )
00942     return false;
00943 
00944   if ( ignore_trailing )
00945   {
00946     QString path1 = path(1);
00947     QString path2 = _u.path(1);
00948     if ( path1 != path2 )
00949       return false;
00950 
00951     if ( m_strProtocol == _u.m_strProtocol &&
00952          m_strUser == _u.m_strUser &&
00953          m_strPass == _u.m_strPass &&
00954          m_strHost == _u.m_strHost &&
00955          m_strQuery_encoded == _u.m_strQuery_encoded &&
00956          m_strRef_encoded == _u.m_strRef_encoded &&
00957          m_iPort == _u.m_iPort )
00958       return true;
00959 
00960     return false;
00961   }
00962 
00963   return ( *this == _u );
00964 }
00965 
00966 bool KURL::isParentOf( const KURL& _u ) const
00967 {
00968   if ( isMalformed() || _u.isMalformed() )
00969     return false;
00970 
00971   if ( m_strProtocol == _u.m_strProtocol &&
00972        m_strUser == _u.m_strUser &&
00973        m_strPass == _u.m_strPass &&
00974        m_strHost == _u.m_strHost &&
00975        m_strQuery_encoded == _u.m_strQuery_encoded &&
00976        m_strRef_encoded == _u.m_strRef_encoded &&
00977        m_iPort == _u.m_iPort )
00978   {
00979     if ( path().isEmpty() || _u.path().isEmpty() )
00980         return false; // can't work with implicit paths
00981 
00982     QString p1( cleanpath( path(), true, false ) );
00983     if ( p1[p1.length()-1] != '/' )
00984         p1 += '/';
00985     QString p2( cleanpath( _u.path(), true, false ) );
00986     if ( p2[p2.length()-1] != '/' )
00987         p2 += '/';
00988 
00989     //kdDebug(126) << "p1=" << p1 << endl;
00990     //kdDebug(126) << "p2=" << p2 << endl;
00991     //kdDebug(126) << "p1.length()=" << p1.length() << endl;
00992     //kdDebug(126) << "p2.left(!$)=" << p2.left( p1.length() ) << endl;
00993     return p2.startsWith( p1 );
00994   }
00995   return false;
00996 }
00997 
00998 void KURL::setFileName( const QString& _txt )
00999 {
01000   m_strRef_encoded = QString::null;
01001   int i = 0;
01002   while( _txt[i] == '/' ) ++i;
01003   QString tmp;
01004   if ( i )
01005     tmp = _txt.mid( i );
01006   else
01007     tmp = _txt;
01008 
01009   QString path = m_strPath_encoded.isEmpty() ? m_strPath : m_strPath_encoded;
01010   if ( path.isEmpty() )
01011     path = "/";
01012   else
01013   {
01014     int lastSlash = path.findRev( '/' );
01015     if ( lastSlash == -1)
01016     {
01017       // The first character is not a '/' ???
01018       // This looks strange ...
01019       path = "/";
01020     }
01021     else if ( path.right(1) != "/" )
01022       path.truncate( lastSlash+1 ); // keep the "/"
01023   }
01024   if (m_strPath_encoded.isEmpty())
01025   {
01026      path += tmp;
01027      setPath( path );
01028   }
01029   else
01030   {
01031      path += encode_string(tmp);
01032      setEncodedPath( path );
01033   }
01034   cleanPath();
01035 }
01036 
01037 void KURL::cleanPath( bool cleanDirSeparator ) // taken from the old KURL
01038 {
01039   m_strPath = cleanpath(m_strPath, cleanDirSeparator, false);
01040   // WABA: Is this safe when "/../" is encoded with %?
01041   m_strPath_encoded = cleanpath(m_strPath_encoded, cleanDirSeparator, true);
01042 }
01043 
01044 static QString trailingSlash( int _trailing, const QString &path )
01045 {
01046   QString result = path;
01047 
01048   if ( _trailing == 0 )
01049     return result;
01050   else if ( _trailing == 1 )
01051   {
01052     int len = result.length();
01053     if ( len == 0 )
01054       result = QString::null;
01055     else if ( result[ len - 1 ] != '/' )
01056       result += "/";
01057     return result;
01058   }
01059   else if ( _trailing == -1 )
01060   {
01061     if ( result == "/" )
01062       return result;
01063     int len = result.length();
01064     if ( len != 0 && result[ len - 1 ] == '/' )
01065       result.truncate( len - 1 );
01066     return result;
01067   }
01068   else {
01069     assert( 0 );
01070     return QString::null;
01071   }
01072 }
01073 
01074 void KURL::adjustPath( int _trailing )
01075 {
01076   if (!m_strPath_encoded.isEmpty())
01077   {
01078      m_strPath_encoded = trailingSlash( _trailing, m_strPath_encoded );
01079   }
01080   m_strPath = trailingSlash( _trailing, m_strPath );
01081 }
01082 
01083 
01084 QString KURL::encodedPathAndQuery( int _trailing, bool _no_empty_path, int encoding_hint ) const
01085 {
01086   QString tmp;
01087   if (!m_strPath_encoded.isEmpty() && encoding_hint == 0)
01088   {
01089      tmp = trailingSlash( _trailing, m_strPath_encoded );
01090   }
01091   else
01092   {
01093      tmp = path( _trailing );
01094      if ( _no_empty_path && tmp.isEmpty() )
01095         tmp = "/";
01096      tmp = encode( tmp, false, encoding_hint );
01097   }
01098 
01099   // TODO apply encoding_hint to the query
01100   if (!m_strQuery_encoded.isNull())
01101       tmp += '?' + m_strQuery_encoded;
01102   return tmp;
01103 }
01104 
01105 void KURL::setEncodedPath( const QString& _txt, int encoding_hint )
01106 {
01107 #ifdef KDE_QT_ONLY
01108   QString fileProt = "file";
01109 #else
01110   static const QString & fileProt = KGlobal::staticQString( "file" );
01111 #endif
01112   m_strPath_encoded = _txt;
01113 
01114   decode( m_strPath_encoded, m_strPath, m_strPath_encoded, encoding_hint );
01115   // Throw away encoding for local files, makes file-operations faster.
01116   if (m_strProtocol == fileProt)
01117      m_strPath_encoded = QString::null;
01118 }
01119 
01120 
01121 void KURL::setEncodedPathAndQuery( const QString& _txt, int encoding_hint )
01122 {
01123   int pos = _txt.find( '?' );
01124   if ( pos == -1 )
01125   {
01126     setEncodedPath(_txt, encoding_hint);
01127     m_strQuery_encoded = QString::null;
01128   }
01129   else
01130   {
01131     setEncodedPath(_txt.left( pos ), encoding_hint);
01132     setQuery(_txt.right(_txt.length() - pos - 1), encoding_hint);
01133   }
01134 }
01135 
01136 QString KURL::path( int _trailing ) const
01137 {
01138   return trailingSlash( _trailing, path() );
01139 }
01140 
01141 bool KURL::isLocalFile() const
01142 {
01143 #ifdef KDE_QT_ONLY
01144   QString fileProt = "file";
01145 #else
01146   static const QString & fileProt = KGlobal::staticQString( "file" );
01147 #endif
01148   return ( ( m_strProtocol == fileProt ) && ( m_strHost.isEmpty()) && !hasSubURL() );
01149 }
01150 
01151 void KURL::setFileEncoding(const QString &encoding)
01152 {
01153   if (!isLocalFile())
01154      return;
01155 
01156   QString q = query();
01157 
01158   if (!q.isEmpty() && (q[0] == '?'))
01159      q = q.mid(1);
01160 
01161   QStringList args = QStringList::split('&', q);
01162   for(QStringList::Iterator it = args.begin();
01163       it != args.end();)
01164   {
01165       QString s = decode_string(*it);
01166       if (s.startsWith("charset="))
01167          it = args.erase(it);
01168       else
01169          ++it;
01170   }
01171   if (!encoding.isEmpty())
01172      args.append("charset="+encode_string(encoding));
01173 
01174   if (args.isEmpty())
01175      setQuery(QString::null);
01176   else
01177      setQuery(args.join("&"));
01178 }
01179 
01180 QString KURL::fileEncoding() const
01181 {
01182   if (!isLocalFile())
01183      return QString::null;
01184 
01185   QString q = query();
01186 
01187   if (q.isEmpty())
01188      return QString::null;
01189 
01190   if (q[0] == '?')
01191      q = q.mid(1);
01192 
01193   QStringList args = QStringList::split('&', q);
01194   for(QStringList::ConstIterator it = args.begin();
01195       it != args.end();
01196       ++it)
01197   {
01198       QString s = decode_string(*it);
01199       if (s.startsWith("charset="))
01200          return s.mid(8);
01201   }
01202   return QString::null;
01203 }
01204 
01205 bool KURL::hasSubURL() const
01206 {
01207   if ( m_strProtocol.isEmpty() || m_bIsMalformed )
01208     return false;
01209   if (m_strRef_encoded.isEmpty())
01210      return false;
01211   if (m_strRef_encoded.startsWith("gzip:"))
01212      return true;
01213   if (m_strRef_encoded.startsWith("bzip:"))
01214      return true;
01215   if (m_strRef_encoded.startsWith("bzip2:"))
01216      return true;
01217   if (m_strRef_encoded.startsWith("tar:"))
01218      return true;
01219   if ( m_strProtocol == "error" ) // anything that starts with error: has suburls
01220      return true;
01221   return false;
01222 }
01223 
01224 QString KURL::url( int _trailing, int encoding_hint ) const
01225 {
01226   if( m_bIsMalformed )
01227   {
01228     // Return the whole url even when the url is
01229     // malformed.  Under such conditions the url
01230     // is stored in m_strProtocol.
01231     return m_strProtocol;
01232   }
01233 
01234   QString u = m_strProtocol;
01235   if (!u.isEmpty())
01236      u += ":";
01237 
01238   if ( hasHost() )
01239   {
01240     u += "//";
01241     if ( hasUser() )
01242     {
01243       u += encode(m_strUser, true, encoding_hint);
01244       if ( hasPass() )
01245       {
01246         u += ":";
01247         u += encode(m_strPass, true, encoding_hint);
01248       }
01249       u += "@";
01250     }
01251     bool IPv6 = (m_strHost.find(':') != -1);
01252     if (IPv6)
01253        u += '[' + m_strHost + ']';
01254     else
01255        u += encode(m_strHost, true, encoding_hint);
01256     if ( m_iPort != 0 ) {
01257       QString buffer;
01258       buffer.sprintf( ":%u", m_iPort );
01259       u += buffer;
01260     }
01261   }
01262 
01263   u += encodedPathAndQuery( _trailing, false, encoding_hint );
01264 
01265   if ( hasRef() )
01266   {
01267     u += "#";
01268     u += m_strRef_encoded;
01269   }
01270 
01271   return u;
01272 }
01273 
01274 QString KURL::prettyURL( int _trailing ) const
01275 {
01276   if( m_bIsMalformed )
01277   {
01278     // Return the whole url even when the url is
01279     // malformed.  Under such conditions the url
01280     // is stored in m_strProtocol.
01281     return m_strProtocol;
01282   }
01283 
01284   QString u = m_strProtocol;
01285   if (!u.isEmpty())
01286      u += ":";
01287 
01288   if ( hasHost() )
01289   {
01290     u += "//";
01291     if ( hasUser() )
01292     {
01293       u += lazy_encode(m_strUser);
01294       // Don't show password!
01295       u += "@";
01296     }
01297     bool IPv6 = (m_strHost.find(':') != -1);
01298     if (IPv6)
01299        u += '[' + m_strHost + ']';
01300     else
01301        u += lazy_encode(m_strHost);
01302     if ( m_iPort != 0 ) {
01303       QString buffer;
01304       buffer.sprintf( ":%u", m_iPort );
01305       u += buffer;
01306     }
01307   }
01308 
01309   u += trailingSlash( _trailing, lazy_encode( m_strPath ) );
01310   if (!m_strQuery_encoded.isNull())
01311       u += '?' + m_strQuery_encoded;
01312 
01313   if ( hasRef() )
01314   {
01315     u += "#";
01316     u += m_strRef_encoded;
01317   }
01318 
01319   return u;
01320 }
01321 
01322 QString KURL::prettyURL( int _trailing, AdjustementFlags _flags) const
01323 {
01324     QString u = prettyURL(_trailing);
01325     if (_flags & StripFileProtocol && u.startsWith("file:"))
01326         u.remove(0, 5);
01327     return u;
01328 }
01329 
01330 QString KURL::htmlURL() const
01331 {
01332   return QStyleSheet::escape(prettyURL());
01333 }
01334 
01335 KURL::List KURL::split( const KURL& _url )
01336 {
01337   QString ref;
01338   KURL::List lst;
01339   KURL url = _url;
01340 
01341   while(true)
01342   {
01343      KURL u = url;
01344      u.m_strRef_encoded = QString::null;
01345      lst.append(u);
01346      if (url.hasSubURL())
01347      {
01348         url = KURL(url.m_strRef_encoded);
01349      }
01350      else
01351      {
01352         ref = url.m_strRef_encoded;
01353         break;
01354      }
01355   }
01356 
01357   // Set HTML ref in all URLs.
01358   KURL::List::Iterator it;
01359   for( it = lst.begin() ; it != lst.end(); ++it )
01360   {
01361      (*it).m_strRef_encoded = ref;
01362   }
01363 
01364   return lst;
01365 }
01366 
01367 KURL::List KURL::split( const QString& _url )
01368 {
01369   return split(KURL(_url));
01370 }
01371 
01372 KURL KURL::join( const KURL::List & lst )
01373 {
01374   if (lst.isEmpty()) return KURL();
01375   KURL tmp;
01376 
01377   KURL::List::ConstIterator first = lst.fromLast();
01378   for( KURL::List::ConstIterator it = first; it != lst.end(); --it )
01379   {
01380      KURL u(*it);
01381      if (it != first)
01382      {
01383         u.m_strRef_encoded = tmp.url();
01384      }
01385      tmp = u;
01386   }
01387 
01388   return tmp;
01389 }
01390 
01391 QString KURL::fileName( bool _strip_trailing_slash ) const
01392 {
01393   QString fname;
01394   const QString &path = m_strPath;
01395 
01396   int len = path.length();
01397   if ( len == 0 )
01398     return fname;
01399 
01400   if ( _strip_trailing_slash )
01401   {
01402     while ( len >= 1 && path[ len - 1 ] == '/' )
01403       len--;
01404   }
01405   else if ( path[ len - 1 ] == '/' )
01406     return fname;
01407 
01408   // Does the path only consist of '/' characters ?
01409   if ( len == 1 && path[ 0 ] == '/' )
01410     return fname;
01411 
01412   // Skip last n slashes
01413   int n = 1;
01414   if (!m_strPath_encoded.isEmpty())
01415   {
01416      // This is hairy, we need the last unencoded slash.
01417      // Count in the encoded string how many encoded slashes follow the last
01418      // unencoded one.
01419      int i = m_strPath_encoded.findRev( '/', len - 1 );
01420      QString fileName_encoded = m_strPath_encoded.mid(i+1);
01421      n += fileName_encoded.contains("%2f", false);
01422   }
01423   int i = len;
01424   do {
01425     i = path.findRev( '/', i - 1 );
01426   }
01427   while (--n && (i > 0));
01428 
01429   // If ( i == -1 ) => the first character is not a '/'
01430   // So it's some URL like file:blah.tgz, return the whole path
01431   if ( i == -1 ) {
01432     if ( len == (int)path.length() )
01433       fname = path;
01434     else
01435       // Might get here if _strip_trailing_slash is true
01436       fname = path.left( len );
01437   }
01438   else
01439   {
01440      fname = path.mid( i + 1, len - i - 1 ); // TO CHECK
01441   }
01442      return fname;
01443 }
01444 
01445 void KURL::addPath( const QString& _txt )
01446 {
01447   m_strPath_encoded = QString::null;
01448 
01449   if ( _txt.isEmpty() )
01450     return;
01451 
01452   int i = 0;
01453   int len = m_strPath.length();
01454   // NB: avoid three '/' when building a new path from nothing
01455   if ( len == 0 ) {
01456     while( _txt[i] == '/' ) ++i;
01457   }
01458   // Add the trailing '/' if it is missing
01459   else if ( _txt[0] != '/' && ( len == 0 || m_strPath[ len - 1 ] != '/' ) )
01460     m_strPath += "/";
01461 
01462   // No double '/' characters
01463   i = 0;
01464   if ( len != 0 && m_strPath[ len - 1 ] == '/' )
01465   {
01466     while( _txt[i] == '/' )
01467       ++i;
01468   }
01469 
01470   m_strPath += _txt.mid( i );
01471 }
01472 
01473 QString KURL::directory( bool _strip_trailing_slash_from_result,
01474                          bool _ignore_trailing_slash_in_path ) const
01475 {
01476   QString result = m_strPath_encoded.isEmpty() ? m_strPath : m_strPath_encoded;
01477   if ( _ignore_trailing_slash_in_path )
01478     result = trailingSlash( -1, result );
01479 
01480   if ( result.isEmpty() || result == "/" )
01481     return result;
01482 
01483   int i = result.findRev( "/" );
01484   // If ( i == -1 ) => the first character is not a '/'
01485   // So it's some URL like file:blah.tgz, with no path
01486   if ( i == -1 )
01487     return QString::null;
01488 
01489   if ( i == 0 )
01490   {
01491     result = "/";
01492     return result;
01493   }
01494 
01495   if ( _strip_trailing_slash_from_result )
01496     result = result.left( i );
01497   else
01498     result = result.left( i + 1 );
01499 
01500   if (!m_strPath_encoded.isEmpty())
01501     result = decode(result);
01502 
01503   return result;
01504 }
01505 
01506 
01507 bool KURL::cd( const QString& _dir )
01508 {
01509   if ( _dir.isEmpty() || m_bIsMalformed )
01510     return false;
01511 
01512   if (hasSubURL())
01513   {
01514      KURL::List lst = split( *this );
01515      KURL &u = lst.last();
01516      u.cd(_dir);
01517      *this = join( lst );
01518      return true;
01519   }
01520 
01521   // absolute path ?
01522   if ( _dir[0] == '/' )
01523   {
01524     m_strPath_encoded = QString::null;
01525     m_strPath = _dir;
01526     setHTMLRef( QString::null );
01527     m_strQuery_encoded = QString::null;
01528     return true;
01529   }
01530 
01531   // Users home directory on the local disk ?
01532   if ( ( _dir[0] == '~' ) && ( m_strProtocol == "file" ))
01533   {
01534     m_strPath_encoded = QString::null;
01535     m_strPath = QDir::homeDirPath();
01536     m_strPath += "/";
01537     m_strPath += _dir.right(m_strPath.length() - 1);
01538     setHTMLRef( QString::null );
01539     m_strQuery_encoded = QString::null;
01540     return true;
01541   }
01542 
01543   // relative path
01544   // we always work on the past of the first url.
01545   // Sub URLs are not touched.
01546 
01547   // append '/' if necessary
01548   QString p = path(1);
01549   p += _dir;
01550   p = cleanpath( p, true, false );
01551   setPath( p );
01552 
01553   setHTMLRef( QString::null );
01554   m_strQuery_encoded = QString::null;
01555 
01556   return true;
01557 }
01558 
01559 KURL KURL::upURL( ) const
01560 {
01561   if (!query().isEmpty())
01562   {
01563      KURL u(*this);
01564      u.setQuery(QString::null);
01565      return u;
01566   };
01567 
01568   if (!hasSubURL())
01569   {
01570      KURL u(*this);
01571      u.cd("../");
01572      return u;
01573   }
01574 
01575   // We have a subURL.
01576   KURL::List lst = split( *this );
01577   if (lst.isEmpty())
01578       return KURL(); // Huh?
01579   while (true)
01580   {
01581      KURL &u = lst.last();
01582      QString old = u.path();
01583      u.cd("../");
01584      if (u.path() != old)
01585          break; // Finshed.
01586      if (lst.count() == 1)
01587          break; // Finished.
01588      lst.remove(lst.fromLast());
01589   }
01590   return join( lst );
01591 }
01592 
01593 QString KURL::htmlRef() const
01594 {
01595   if ( !hasSubURL() )
01596   {
01597     return decode( ref() );
01598   }
01599 
01600   List lst = split( *this );
01601   return decode( (*lst.begin()).ref() );
01602 }
01603 
01604 QString KURL::encodedHtmlRef() const
01605 {
01606   if ( !hasSubURL() )
01607   {
01608     return ref();
01609   }
01610 
01611   List lst = split( *this );
01612   return (*lst.begin()).ref();
01613 }
01614 
01615 void KURL::setHTMLRef( const QString& _ref )
01616 {
01617   if ( !hasSubURL() )
01618   {
01619     m_strRef_encoded = encode( _ref, true, 0 /*?*/);
01620     return;
01621   }
01622 
01623   List lst = split( *this );
01624 
01625   (*lst.begin()).setRef( encode( _ref, true, 0 /*?*/) );
01626 
01627   *this = join( lst );
01628 }
01629 
01630 bool KURL::hasHTMLRef() const
01631 {
01632   if ( !hasSubURL() )
01633   {
01634     return hasRef();
01635   }
01636 
01637   List lst = split( *this );
01638   return (*lst.begin()).hasRef();
01639 }
01640 
01641 void
01642 KURL::setProtocol( const QString& _txt )
01643 {
01644    m_strProtocol = _txt;
01645    m_bIsMalformed = false;
01646 }
01647 
01648 void
01649 KURL::setUser( const QString& _txt )
01650 {
01651    m_strUser = _txt;
01652 }
01653 
01654 void
01655 KURL::setPass( const QString& _txt )
01656 {
01657    m_strPass = _txt;
01658 }
01659 
01660 void
01661 KURL::setHost( const QString& _txt )
01662 {
01663    m_strHost = _txt;
01664 }
01665 
01666 void
01667 KURL::setPort( unsigned short int _p )
01668 {
01669    m_iPort = _p;
01670 }
01671 
01672 void KURL::setPath( const QString & path )
01673 {
01674   if (isEmpty())
01675     m_bIsMalformed = false;
01676   if (m_strProtocol.isEmpty())
01677     m_strProtocol = "file";
01678   m_strPath = path;
01679   m_strPath_encoded = QString::null;
01680 }
01681 
01682 void KURL::setQuery( const QString &_txt, int encoding_hint)
01683 {
01684    if (!_txt.length())
01685    {
01686       m_strQuery_encoded = _txt;
01687       return;
01688    }
01689    if (_txt[0] =='?')
01690       m_strQuery_encoded = _txt.mid(1);
01691    else
01692       m_strQuery_encoded = _txt;
01693 
01694    int l = m_strQuery_encoded.length();
01695    int i = 0;
01696    QString result;
01697    while (i < l)
01698    {
01699       int s = i;
01700       // Re-encode. Break encoded string up according to the reserved
01701       // characters '&:;=/?' and re-encode part by part.
01702       while(i < l)
01703       {
01704          char c = m_strQuery_encoded[i].latin1();
01705          if ((c == '&') || (c == ':') || (c == ';') ||
01706              (c == '=') || (c == '/') || (c == '?'))
01707             break;
01708          i++;
01709       }
01710       if (i > s)
01711       {
01712          QString tmp = m_strQuery_encoded.mid(s, i-s);
01713          QString newTmp;
01714          decode( tmp, newTmp, tmp, encoding_hint, false );
01715          result += tmp;
01716       }
01717       if (i < l)
01718       {
01719          result += m_strQuery_encoded[i];
01720          i++;
01721       }
01722    }
01723    m_strQuery_encoded = result;
01724 }
01725 
01726 QString KURL::query() const
01727 {
01728     if (m_strQuery_encoded.isNull())
01729         return QString::null;
01730     return '?'+m_strQuery_encoded;
01731 }
01732 
01733 QString KURL::decode_string(const QString &str, int encoding_hint)
01734 {
01735    return decode(str, encoding_hint);
01736 }
01737 
01738 QString KURL::encode_string(const QString &str, int encoding_hint)
01739 {
01740    return encode(str, false, encoding_hint);
01741 }
01742 
01743 QString KURL::encode_string_no_slash(const QString &str, int encoding_hint)
01744 {
01745    return encode(str, true, encoding_hint);
01746 }
01747 
01748 bool urlcmp( const QString& _url1, const QString& _url2 )
01749 {
01750   // Both empty ?
01751   if ( _url1.isEmpty() && _url2.isEmpty() )
01752     return true;
01753   // Only one empty ?
01754   if ( _url1.isEmpty() || _url2.isEmpty() )
01755     return false;
01756 
01757   KURL::List list1 = KURL::split( _url1 );
01758   KURL::List list2 = KURL::split( _url2 );
01759 
01760   // Malformed ?
01761   if ( list1.isEmpty() || list2.isEmpty() )
01762     return false;
01763 
01764   return ( list1 == list2 );
01765 }
01766 
01767 bool urlcmp( const QString& _url1, const QString& _url2, bool _ignore_trailing, bool _ignore_ref )
01768 {
01769   // Both empty ?
01770   if ( _url1.isEmpty() && _url2.isEmpty() )
01771     return true;
01772   // Only one empty ?
01773   if ( _url1.isEmpty() || _url2.isEmpty() )
01774     return false;
01775 
01776   KURL::List list1 = KURL::split( _url1 );
01777   KURL::List list2 = KURL::split( _url2 );
01778 
01779   // Malformed ?
01780   if ( list1.isEmpty() || list2.isEmpty() )
01781     return false;
01782 
01783   unsigned int size = list1.count();
01784   if ( list2.count() != size )
01785     return false;
01786 
01787   if ( _ignore_ref )
01788   {
01789     (*list1.begin()).setRef(QString::null);
01790     (*list2.begin()).setRef(QString::null);
01791   }
01792 
01793   KURL::List::Iterator it1 = list1.begin();
01794   KURL::List::Iterator it2 = list2.begin();
01795   for( ; it1 != list1.end() ; ++it1, ++it2 )
01796     if ( !(*it1).equals( *it2, _ignore_trailing ) )
01797       return false;
01798 
01799   return true;
01800 }
01801 
01802 QMap< QString, QString > KURL::queryItems( int options ) const {
01803   if ( m_strQuery_encoded.isEmpty() )
01804     return QMap<QString,QString>();
01805 
01806   QMap< QString, QString > result;
01807   QStringList items = QStringList::split( '&', m_strQuery_encoded );
01808   for ( QStringList::const_iterator it = items.begin() ; it != items.end() ; ++it ) {
01809     int equal_pos = (*it).find( '=' );
01810     if ( equal_pos > 0 ) { // = is not the first char...
01811       QString name = (*it).left( equal_pos );
01812       if ( options & CaseInsensitiveKeys )
01813     name = name.lower();
01814       QString value = (*it).mid( equal_pos + 1 );
01815       if ( value.isEmpty() )
01816     result.insert( name, QString::fromLatin1("") );
01817       else {
01818     // ### why is decoding name not neccessary?
01819     value.replace( '+', ' ' ); // + in queries means space
01820     result.insert( name, decode_string( value ) );
01821       }
01822     } else if ( equal_pos < 0 ) { // no =
01823       QString name = (*it);
01824       if ( options & CaseInsensitiveKeys )
01825     name = name.lower();
01826       result.insert( name, QString::null );
01827     }
01828   }
01829 
01830   return result;
01831 }
01832 
01833 QString KURL::queryItem( const QString& _item ) const
01834 {
01835   QString item = _item + '=';
01836   if ( m_strQuery_encoded.length() <= 1 )
01837     return QString::null;
01838 
01839   QStringList items = QStringList::split( '&', m_strQuery_encoded );
01840   unsigned int _len = item.length();
01841   for ( QStringList::ConstIterator it = items.begin(); it != items.end(); ++it )
01842   {
01843     if ( (*it).startsWith( item ) )
01844     {
01845       if ( (*it).length() > _len )
01846       {
01847         QString str = (*it).mid( _len );
01848         str.replace( '+', ' ' ); // + in queries means space.
01849         return decode_string( str );
01850       }
01851       else // empty value
01852         return QString::fromLatin1("");
01853     }
01854   }
01855 
01856   return QString::null;
01857 }
01858 
01859 void KURL::removeQueryItem( const QString& _item )
01860 {
01861   QString item = _item + '=';
01862   if ( m_strQuery_encoded.length() <= 1 )
01863     return;
01864 
01865   QStringList items = QStringList::split( '&', m_strQuery_encoded );
01866   for ( QStringList::Iterator it = items.begin(); it != items.end(); )
01867   {
01868     if ( (*it).startsWith( item ) || (*it == _item) )
01869     {
01870       QStringList::Iterator deleteIt = it;
01871       ++it;
01872       items.remove(deleteIt);
01873     }
01874     else
01875     {
01876        ++it;
01877     }
01878   }
01879   m_strQuery_encoded = items.join( "&" );
01880 }
01881 
01882 void KURL::addQueryItem( const QString& _item, const QString& _value, int encoding_hint )
01883 {
01884   QString item = _item + '=';
01885   QString value = encode( _value, true, encoding_hint );
01886 
01887   if (!m_strQuery_encoded.isEmpty())
01888      m_strQuery_encoded += '&';
01889   m_strQuery_encoded += item + value;
01890 }
01891 
01892 // static
01893 KURL KURL::fromPathOrURL( const QString& text )
01894 {
01895     if ( text.isEmpty() )
01896         return KURL();
01897     
01898     KURL url;
01899     if ( text[0] == '/' )
01900         url.setPath( text );
01901     else
01902         url = text;
01903 
01904     return url;
01905 }
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.0.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Fri Feb 24 21:53:34 2006 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2001