kio Library API Documentation

kprotocolmanager.cpp

00001 /* This file is part of the KDE libraries
00002    Copyright (C) 1999 Torben Weis <weis@kde.org>
00003    Copyright (C) 2000- Waldo Bastain <bastain@kde.org>
00004    Copyright (C) 2000- Dawit Alemayehu <adawit@kde.org>
00005 
00006    This library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Library General Public
00008    License version 2 as published by the Free Software Foundation.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018    Boston, MA 02111-1307, USA.
00019 */
00020 
00021 #include <string.h>
00022 #include <sys/utsname.h>
00023 
00024 #include <dcopref.h>
00025 #include <kdebug.h>
00026 #include <kglobal.h>
00027 #include <klocale.h>
00028 #include <kconfig.h>
00029 #include <kstandarddirs.h>
00030 #include <klibloader.h>
00031 #include <kstringhandler.h>
00032 #include <kstaticdeleter.h>
00033 #include <kio/slaveconfig.h>
00034 #include <kio/ioslave_defaults.h>
00035 #include <kio/http_slave_defaults.h>
00036 
00037 #include "kprotocolmanager.h"
00038 
00039 class
00040 KProtocolManagerPrivate
00041 {
00042 public:
00043    KProtocolManagerPrivate();
00044 
00045    ~KProtocolManagerPrivate();
00046 
00047    KConfig *config;
00048    KConfig *http_config;
00049    bool init_busy;
00050    KURL url;
00051    QString protocol;
00052    QString proxy;
00053    QString modifiers;
00054    QString useragent;
00055 };
00056 
00057 static KProtocolManagerPrivate* d = 0;
00058 static KStaticDeleter<KProtocolManagerPrivate> kpmpksd;
00059 
00060 KProtocolManagerPrivate::KProtocolManagerPrivate()
00061                         :config(0), http_config(0), init_busy(false)
00062 {
00063    kpmpksd.setObject(d, this);
00064 }
00065 
00066 KProtocolManagerPrivate::~KProtocolManagerPrivate()
00067 {
00068    delete config;
00069    delete http_config;
00070 }
00071 
00072 
00073 // DEFAULT USERAGENT STRING
00074 #define CFG_DEFAULT_UAGENT(X) \
00075 QString("Mozilla/5.0 (compatible; Konqueror/%1.%2%3) KHTML/%4.%5.%6 (like Gecko)") \
00076         .arg(KDE_VERSION_MAJOR).arg(KDE_VERSION_MINOR).arg(X).arg(KDE_VERSION_MAJOR).arg(KDE_VERSION_MINOR).arg(KDE_VERSION_RELEASE)
00077 
00078 void KProtocolManager::reparseConfiguration()
00079 {
00080   kpmpksd.destructObject();
00081 
00082   // Force the slave config to re-read its config...
00083   KIO::SlaveConfig::self()->reset ();
00084 }
00085 
00086 KConfig *KProtocolManager::config()
00087 {
00088   if (!d)
00089      d = new KProtocolManagerPrivate;
00090 
00091   if (!d->config)
00092   {
00093      d->config = new KConfig("kioslaverc", true, false);
00094   }
00095   return d->config;
00096 }
00097 
00098 KConfig *KProtocolManager::http_config()
00099 {
00100   if (!d)
00101      d = new KProtocolManagerPrivate;
00102 
00103   if (!d->http_config)
00104   {
00105      d->http_config = new KConfig("kio_httprc", false, false);
00106   }
00107   return d->http_config;
00108 }
00109 
00110 /*=============================== TIMEOUT SETTINGS ==========================*/
00111 
00112 int KProtocolManager::readTimeout()
00113 {
00114   KConfig *cfg = config();
00115   cfg->setGroup( QString::null );
00116   int val = cfg->readNumEntry( "ReadTimeout", DEFAULT_READ_TIMEOUT );
00117   return QMAX(MIN_TIMEOUT_VALUE, val);
00118 }
00119 
00120 int KProtocolManager::connectTimeout()
00121 {
00122   KConfig *cfg = config();
00123   cfg->setGroup( QString::null );
00124   int val = cfg->readNumEntry( "ConnectTimeout", DEFAULT_CONNECT_TIMEOUT );
00125   return QMAX(MIN_TIMEOUT_VALUE, val);
00126 }
00127 
00128 int KProtocolManager::proxyConnectTimeout()
00129 {
00130   KConfig *cfg = config();
00131   cfg->setGroup( QString::null );
00132   int val = cfg->readNumEntry( "ProxyConnectTimeout", DEFAULT_PROXY_CONNECT_TIMEOUT );
00133   return QMAX(MIN_TIMEOUT_VALUE, val);
00134 }
00135 
00136 int KProtocolManager::responseTimeout()
00137 {
00138   KConfig *cfg = config();
00139   cfg->setGroup( QString::null );
00140   int val = cfg->readNumEntry( "ResponseTimeout", DEFAULT_RESPONSE_TIMEOUT );
00141   return QMAX(MIN_TIMEOUT_VALUE, val);
00142 }
00143 
00144 /*========================== PROXY SETTINGS =================================*/
00145 
00146 bool KProtocolManager::useProxy()
00147 {
00148   return proxyType() != NoProxy;
00149 }
00150 
00151 bool KProtocolManager::useReverseProxy()
00152 {
00153   KConfig *cfg = config();
00154   cfg->setGroup( "Proxy Settings" );
00155   return cfg->readBoolEntry("ReversedException", false);
00156 }
00157 
00158 KProtocolManager::ProxyType KProtocolManager::proxyType()
00159 {
00160   KConfig *cfg = config();
00161   cfg->setGroup( "Proxy Settings" );
00162   return static_cast<ProxyType>(cfg->readNumEntry( "ProxyType" ));
00163 }
00164 
00165 KProtocolManager::ProxyAuthMode KProtocolManager::proxyAuthMode()
00166 {
00167   KConfig *cfg = config();
00168   cfg->setGroup( "Proxy Settings" );
00169   return static_cast<ProxyAuthMode>(cfg->readNumEntry( "AuthMode" ));
00170 }
00171 
00172 /*========================== CACHING =====================================*/
00173 
00174 bool KProtocolManager::useCache()
00175 {
00176   KConfig *cfg = http_config();
00177   return cfg->readBoolEntry( "UseCache", true );
00178 }
00179 
00180 KIO::CacheControl KProtocolManager::cacheControl()
00181 {
00182   KConfig *cfg = http_config();
00183   QString tmp = cfg->readEntry("cache");
00184   if (tmp.isEmpty())
00185     return DEFAULT_CACHE_CONTROL;
00186   return KIO::parseCacheControl(tmp);
00187 }
00188 
00189 QString KProtocolManager::cacheDir()
00190 {
00191   KConfig *cfg = http_config();
00192   return cfg->readPathEntry("CacheDir", KGlobal::dirs()->saveLocation("cache","http"));
00193 }
00194 
00195 int KProtocolManager::maxCacheAge()
00196 {
00197   KConfig *cfg = http_config();
00198   return cfg->readNumEntry( "MaxCacheAge", DEFAULT_MAX_CACHE_AGE ); // 14 days
00199 }
00200 
00201 int KProtocolManager::maxCacheSize()
00202 {
00203   KConfig *cfg = http_config();
00204   return cfg->readNumEntry( "MaxCacheSize", DEFAULT_MAX_CACHE_SIZE ); // 5 MB
00205 }
00206 
00207 QString KProtocolManager::noProxyFor()
00208 {
00209   KProtocolManager::ProxyType type = proxyType();
00210 
00211   if ( (type != ManualProxy) && (type != EnvVarProxy) )
00212     return QString::null;
00213 
00214         
00215   KConfig *cfg = config();
00216   cfg->setGroup( "Proxy Settings" );
00217 
00218   QString noProxy = cfg->readEntry( "NoProxyFor" );
00219   if (type == EnvVarProxy)
00220     noProxy = QString::fromLocal8Bit(getenv(noProxy.local8Bit()));
00221 
00222   return noProxy;
00223 }
00224 
00225 QString KProtocolManager::proxyFor( const QString& protocol )
00226 {
00227   QString scheme = protocol.lower();
00228 
00229   if (scheme == "webdav")
00230     scheme = "http";
00231   else if (scheme == "webdavs")
00232     scheme = "https";
00233 
00234   KConfig *cfg = config();
00235   cfg->setGroup( "Proxy Settings" );
00236   return cfg->readEntry( scheme + "Proxy" );
00237 }
00238 
00239 QString KProtocolManager::proxyForURL( const KURL &url )
00240 {
00241   QString proxy;
00242   ProxyType pt = proxyType();
00243 
00244   switch (pt)
00245   {
00246       case PACProxy:
00247       case WPADProxy:
00248           if (!url.host().isEmpty())
00249           {
00250             QString p = url.protocol();
00251             if ( p.startsWith( "http" ) || p == "ftp" || p == "gopher" )
00252               DCOPRef( "kded", "proxyscout" ).call( "proxyForURL", url ).get( proxy );
00253           }
00254           break;
00255       case EnvVarProxy:
00256           proxy = QString::fromLocal8Bit(getenv(proxyFor(url.protocol()).local8Bit())).stripWhiteSpace();
00257           break;
00258       case ManualProxy:
00259           proxy = proxyFor( url.protocol() );
00260           break;
00261       case NoProxy:
00262       default:
00263           break;
00264   }
00265 
00266   return (proxy.isEmpty() ? QString::fromLatin1("DIRECT") : proxy);
00267 }
00268 
00269 void KProtocolManager::badProxy( const QString &proxy )
00270 {
00271     DCOPRef( "kded", "proxyscout" ).send( "blackListProxy", proxy );
00272 }
00273 
00274 /*
00275     Domain suffix match. E.g. return true if host is "cuzco.inka.de" and
00276     nplist is "inka.de,hadiko.de" or if host is "localhost" and nplist is
00277     "localhost".
00278 */
00279 static bool revmatch(const char *host, const char *nplist)
00280 {
00281   if (host == 0)
00282     return false;
00283 
00284   const char *hptr = host + strlen( host ) - 1;
00285   const char *nptr = nplist + strlen( nplist ) - 1;
00286   const char *shptr = hptr;
00287 
00288   while ( nptr >= nplist )
00289   {
00290     if ( *hptr != *nptr )
00291     {
00292       hptr = shptr;
00293 
00294       // Try to find another domain or host in the list
00295       while(--nptr>=nplist && *nptr!=',' && *nptr!=' ') ;
00296 
00297       // Strip out multiple spaces and commas
00298       while(--nptr>=nplist && (*nptr==',' || *nptr==' ')) ;
00299     }
00300     else
00301     {
00302       if ( nptr==nplist || nptr[-1]==',' || nptr[-1]==' ')
00303         return true;
00304       if ( hptr == host ) // e.g. revmatch("bugs.kde.org","mybugs.kde.org")
00305         return false;
00306 
00307       hptr--;
00308       nptr--;
00309     }
00310   }
00311 
00312   return false;
00313 }
00314 
00315 QString KProtocolManager::slaveProtocol(const KURL &url, QString &proxy)
00316 {
00317   if (url.hasSubURL()) // We don't want the suburl's protocol
00318   {
00319      KURL::List list = KURL::split(url);
00320      KURL::List::Iterator it = list.fromLast();
00321      return slaveProtocol(*it, proxy);
00322   }
00323 
00324   if (!d)
00325     d = new KProtocolManagerPrivate;
00326 
00327   if (d->url == url)
00328   {
00329      proxy = d->proxy;
00330      return d->protocol;
00331   }
00332 
00333   if (useProxy())
00334   {
00335      proxy = proxyForURL(url);
00336      if ((proxy != "DIRECT") && (!proxy.isEmpty()))
00337      {
00338         bool isRevMatch = false;
00339         bool useRevProxy = ((proxyType() == ManualProxy) && useReverseProxy());
00340 
00341         QString noProxy = noProxyFor();
00342 
00343         if (!noProxy.isEmpty())
00344         {
00345            QString qhost = url.host().lower();
00346            const char *host = qhost.latin1();
00347            QString qno_proxy = noProxy.stripWhiteSpace().lower();
00348            const char *no_proxy = qno_proxy.latin1();
00349            isRevMatch = revmatch(host, no_proxy);
00350 
00351            // If no match is found and the request url has a port
00352            // number, try the combination of "host:port". This allows
00353            // users to enter host:port in the No-proxy-For list.
00354            if (!isRevMatch && url.port() > 0)
00355            {
00356               qhost += ':' + QString::number (url.port());
00357               host = qhost.latin1();
00358               isRevMatch = revmatch (host, no_proxy);
00359            }
00360 
00361            // If the hostname does not contain a dot, check if
00362            // <local> is part of noProxy.
00363            if (!isRevMatch && host && (strchr(host, '.') == NULL))
00364               isRevMatch = revmatch("<local>", no_proxy);
00365         }
00366 
00367         if ( (!useRevProxy && !isRevMatch) || (useRevProxy && isRevMatch) )
00368         {
00369            d->url = proxy;
00370            if ( d->url.isValid() )
00371            {
00372               // The idea behind slave protocols is not applicable to http
00373               // and webdav protocols.
00374               QString protocol = url.protocol().lower();
00375               if (protocol.startsWith("http") || protocol.startsWith("webdav"))
00376                 d->protocol = protocol;
00377               else
00378               {
00379                 d->protocol = d->url.protocol();
00380                 kdDebug () << "slaveProtocol: " << d->protocol << endl;
00381               }
00382 
00383               d->url = url;
00384               d->proxy = proxy;
00385               return d->protocol;
00386            }
00387         }
00388      }
00389   }
00390 
00391   d->url = url;
00392   d->proxy = proxy = QString::null;
00393   d->protocol = url.protocol();
00394   return d->protocol;
00395 }
00396 
00397 /*================================= USER-AGENT SETTINGS =====================*/
00398 
00399 QString KProtocolManager::userAgentForHost( const QString& hostname )
00400 {
00401   QString sendUserAgent = KIO::SlaveConfig::self()->configData("http", hostname.lower(), "SendUserAgent").lower();
00402   if (sendUserAgent == "false")
00403      return QString::null;
00404 
00405   QString useragent = KIO::SlaveConfig::self()->configData("http", hostname.lower(), "UserAgent");
00406 
00407   // Return the default user-agent if none is specified
00408   // for the requested host.
00409   if (useragent.isEmpty())
00410     return defaultUserAgent();
00411 
00412   return useragent;
00413 }
00414 
00415 QString KProtocolManager::defaultUserAgent( )
00416 {
00417   QString modifiers = KIO::SlaveConfig::self()->configData("http", QString::null, "UserAgentKeys");
00418   return defaultUserAgent(modifiers);
00419 }
00420 
00421 QString KProtocolManager::defaultUserAgent( const QString &_modifiers )
00422 {
00423   if (!d)
00424      d = new KProtocolManagerPrivate;
00425 
00426   QString modifiers = _modifiers.lower();
00427   if (modifiers.isEmpty())
00428      modifiers = DEFAULT_USER_AGENT_KEYS;
00429 
00430   if (d->modifiers == modifiers)
00431      return d->useragent;
00432 
00433   QString supp;
00434   struct utsname nam;
00435   if( uname(&nam) >= 0 )
00436   {
00437     if( modifiers.contains('o') )
00438     {
00439       supp += QString("; %1").arg(nam.sysname);
00440       if ( modifiers.contains('v') )
00441         supp += QString(" %1").arg(nam.release);
00442     }
00443     if( modifiers.contains('p') )
00444     {
00445       supp += QString::fromLatin1("; X11");  // TODO: determine this valye instead of hardcoding...
00446     }
00447     if( modifiers.contains('m') )
00448     {
00449       supp += QString("; %1").arg(nam.machine);
00450     }
00451     if( modifiers.contains('l') )
00452     {
00453       QStringList languageList = KGlobal::locale()->languageList();
00454       QStringList::Iterator it = languageList.find( QString::fromLatin1("C") );
00455       if( it != languageList.end() )
00456       {
00457         if( languageList.contains( QString::fromLatin1("en") ) > 0 )
00458           languageList.remove( it );
00459         else
00460           (*it) = QString::fromLatin1("en");
00461       }
00462       if( languageList.count() )
00463         supp += QString("; %1").arg(languageList.join(", "));
00464     }
00465   }
00466   d->modifiers = modifiers;
00467   d->useragent = CFG_DEFAULT_UAGENT(supp);
00468   return d->useragent;
00469 }
00470 
00471 /*==================================== OTHERS ===============================*/
00472 
00473 bool KProtocolManager::markPartial()
00474 {
00475   KConfig *cfg = config();
00476   cfg->setGroup( QString::null );
00477   return cfg->readBoolEntry( "MarkPartial", true );
00478 }
00479 
00480 int KProtocolManager::minimumKeepSize()
00481 {
00482   KConfig *cfg = config();
00483   cfg->setGroup( QString::null );
00484   return cfg->readNumEntry( "MinimumKeepSize",
00485                             DEFAULT_MINIMUM_KEEP_SIZE ); // 5000 byte
00486 }
00487 
00488 bool KProtocolManager::autoResume()
00489 {
00490   KConfig *cfg = config();
00491   cfg->setGroup( QString::null );
00492   return cfg->readBoolEntry( "AutoResume", false );
00493 }
00494 
00495 bool KProtocolManager::persistentConnections()
00496 {
00497   KConfig *cfg = config();
00498   cfg->setGroup( QString::null );
00499   return cfg->readBoolEntry( "PersistentConnections", true );
00500 }
00501 
00502 bool KProtocolManager::persistentProxyConnection()
00503 {
00504   KConfig *cfg = config();
00505   cfg->setGroup( QString::null );
00506   return cfg->readBoolEntry( "PersistentProxyConnection", false );
00507 }
00508 
00509 QString KProtocolManager::proxyConfigScript()
00510 {
00511   KConfig *cfg = config();
00512   cfg->setGroup( "Proxy Settings" );
00513   return cfg->readEntry( "Proxy Config Script" );
00514 }
KDE Logo
This file is part of the documentation for kio Library Version 3.3.90.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Mar 30 10:15:30 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003