TDE core libraries
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

kprotocolmanager.cpp 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /* This file is part of the KDE libraries
  2. Copyright (C) 1999 Torben Weis <weis@kde.org>
  3. Copyright (C) 2000- Waldo Bastain <bastain@kde.org>
  4. Copyright (C) 2000- Dawit Alemayehu <adawit@kde.org>
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public
  7. License version 2 as published by the Free Software Foundation.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public License
  13. along with this library; see the file COPYING.LIB. If not, write to
  14. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. */
  17. #include <string.h>
  18. #include <sys/utsname.h>
  19. #include <dcopref.h>
  20. #include <kdebug.h>
  21. #include <kglobal.h>
  22. #include <klocale.h>
  23. #include <tdeconfig.h>
  24. #include <kstandarddirs.h>
  25. #include <klibloader.h>
  26. #include <kstringhandler.h>
  27. #include <kstaticdeleter.h>
  28. #include <tdeio/slaveconfig.h>
  29. #include <tdeio/ioslave_defaults.h>
  30. #include <tdeio/http_slave_defaults.h>
  31. #include "kprotocolmanager.h"
  32. class
  33. KProtocolManagerPrivate
  34. {
  35. public:
  36. KProtocolManagerPrivate();
  37. ~KProtocolManagerPrivate();
  38. TDEConfig *config;
  39. TDEConfig *http_config;
  40. bool init_busy;
  41. KURL url;
  42. TQString protocol;
  43. TQString proxy;
  44. TQString modifiers;
  45. TQString useragent;
  46. };
  47. static KProtocolManagerPrivate* d = 0;
  48. static KStaticDeleter<KProtocolManagerPrivate> kpmpksd;
  49. KProtocolManagerPrivate::KProtocolManagerPrivate()
  50. :config(0), http_config(0), init_busy(false)
  51. {
  52. kpmpksd.setObject(d, this);
  53. }
  54. KProtocolManagerPrivate::~KProtocolManagerPrivate()
  55. {
  56. delete config;
  57. delete http_config;
  58. }
  59. // DEFAULT USERAGENT STRING
  60. #define CFG_DEFAULT_UAGENT(X) \
  61. TQString("Mozilla/5.0 (compatible; Konqueror/%1.%2%3) TDEHTML/%4.%5.%6 (like Gecko)") \
  62. .arg(TDE_VERSION_MAJOR).arg(TDE_VERSION_MINOR).arg(X).arg(TDE_VERSION_MAJOR).arg(TDE_VERSION_MINOR).arg(TDE_VERSION_RELEASE)
  63. void KProtocolManager::reparseConfiguration()
  64. {
  65. kpmpksd.destructObject();
  66. // Force the slave config to re-read its config...
  67. TDEIO::SlaveConfig::self()->reset ();
  68. }
  69. TDEConfig *KProtocolManager::config()
  70. {
  71. if (!d)
  72. d = new KProtocolManagerPrivate;
  73. if (!d->config)
  74. {
  75. d->config = new TDEConfig("tdeioslaverc", true, false);
  76. }
  77. return d->config;
  78. }
  79. TDEConfig *KProtocolManager::http_config()
  80. {
  81. if (!d)
  82. d = new KProtocolManagerPrivate;
  83. if (!d->http_config)
  84. {
  85. d->http_config = new TDEConfig("tdeio_httprc", false, false);
  86. }
  87. return d->http_config;
  88. }
  89. /*=============================== TIMEOUT SETTINGS ==========================*/
  90. int KProtocolManager::readTimeout()
  91. {
  92. TDEConfig *cfg = config();
  93. cfg->setGroup( TQString::null );
  94. int val = cfg->readNumEntry( "ReadTimeout", DEFAULT_READ_TIMEOUT );
  95. return QMAX(MIN_TIMEOUT_VALUE, val);
  96. }
  97. int KProtocolManager::connectTimeout()
  98. {
  99. TDEConfig *cfg = config();
  100. cfg->setGroup( TQString::null );
  101. int val = cfg->readNumEntry( "ConnectTimeout", DEFAULT_CONNECT_TIMEOUT );
  102. return QMAX(MIN_TIMEOUT_VALUE, val);
  103. }
  104. int KProtocolManager::proxyConnectTimeout()
  105. {
  106. TDEConfig *cfg = config();
  107. cfg->setGroup( TQString::null );
  108. int val = cfg->readNumEntry( "ProxyConnectTimeout", DEFAULT_PROXY_CONNECT_TIMEOUT );
  109. return QMAX(MIN_TIMEOUT_VALUE, val);
  110. }
  111. int KProtocolManager::responseTimeout()
  112. {
  113. TDEConfig *cfg = config();
  114. cfg->setGroup( TQString::null );
  115. int val = cfg->readNumEntry( "ResponseTimeout", DEFAULT_RESPONSE_TIMEOUT );
  116. return QMAX(MIN_TIMEOUT_VALUE, val);
  117. }
  118. /*========================== PROXY SETTINGS =================================*/
  119. bool KProtocolManager::useProxy()
  120. {
  121. return proxyType() != NoProxy;
  122. }
  123. bool KProtocolManager::useReverseProxy()
  124. {
  125. TDEConfig *cfg = config();
  126. cfg->setGroup( "Proxy Settings" );
  127. return cfg->readBoolEntry("ReversedException", false);
  128. }
  129. KProtocolManager::ProxyType KProtocolManager::proxyType()
  130. {
  131. TDEConfig *cfg = config();
  132. cfg->setGroup( "Proxy Settings" );
  133. return static_cast<ProxyType>(cfg->readNumEntry( "ProxyType" ));
  134. }
  135. KProtocolManager::ProxyAuthMode KProtocolManager::proxyAuthMode()
  136. {
  137. TDEConfig *cfg = config();
  138. cfg->setGroup( "Proxy Settings" );
  139. return static_cast<ProxyAuthMode>(cfg->readNumEntry( "AuthMode" ));
  140. }
  141. /*========================== CACHING =====================================*/
  142. bool KProtocolManager::useCache()
  143. {
  144. TDEConfig *cfg = http_config();
  145. return cfg->readBoolEntry( "UseCache", true );
  146. }
  147. TDEIO::CacheControl KProtocolManager::cacheControl()
  148. {
  149. TDEConfig *cfg = http_config();
  150. TQString tmp = cfg->readEntry("cache");
  151. if (tmp.isEmpty())
  152. return DEFAULT_CACHE_CONTROL;
  153. return TDEIO::parseCacheControl(tmp);
  154. }
  155. TQString KProtocolManager::cacheDir()
  156. {
  157. TDEConfig *cfg = http_config();
  158. return cfg->readPathEntry("CacheDir", TDEGlobal::dirs()->saveLocation("cache","http"));
  159. }
  160. int KProtocolManager::maxCacheAge()
  161. {
  162. TDEConfig *cfg = http_config();
  163. return cfg->readNumEntry( "MaxCacheAge", DEFAULT_MAX_CACHE_AGE ); // 14 days
  164. }
  165. int KProtocolManager::maxCacheSize()
  166. {
  167. TDEConfig *cfg = http_config();
  168. return cfg->readNumEntry( "MaxCacheSize", DEFAULT_MAX_CACHE_SIZE ); // 5 MB
  169. }
  170. TQString KProtocolManager::noProxyForRaw()
  171. {
  172. TDEConfig *cfg = config();
  173. cfg->setGroup( "Proxy Settings" );
  174. return cfg->readEntry( "NoProxyFor" );
  175. }
  176. TQString KProtocolManager::noProxyFor()
  177. {
  178. TQString noProxy = noProxyForRaw();
  179. if (proxyType() == EnvVarProxy)
  180. noProxy = TQString::fromLocal8Bit(getenv(noProxy.local8Bit()));
  181. return noProxy;
  182. }
  183. TQString KProtocolManager::proxyFor( const TQString& protocol )
  184. {
  185. TQString scheme = protocol.lower();
  186. if (scheme == "webdav")
  187. scheme = "http";
  188. else if (scheme == "webdavs")
  189. scheme = "https";
  190. TDEConfig *cfg = config();
  191. cfg->setGroup( "Proxy Settings" );
  192. return cfg->readEntry( scheme + "Proxy" );
  193. }
  194. TQString KProtocolManager::proxyForURL( const KURL &url )
  195. {
  196. TQString proxy;
  197. ProxyType pt = proxyType();
  198. switch (pt)
  199. {
  200. case PACProxy:
  201. case WPADProxy:
  202. if (!url.host().isEmpty())
  203. {
  204. KURL u (url);
  205. TQString p = u.protocol().lower();
  206. // webdav is a KDE specific protocol. Look up proxy
  207. // information using HTTP instead...
  208. if ( p == "webdav" )
  209. {
  210. p = "http";
  211. u.setProtocol( p );
  212. }
  213. else if ( p == "webdavs" )
  214. {
  215. p = "https";
  216. u.setProtocol( p );
  217. }
  218. if ( p.startsWith("http") || p == "ftp" || p == "gopher" )
  219. DCOPRef( "kded", "proxyscout" ).call( "proxyForURL", u ).get( proxy );
  220. }
  221. break;
  222. case EnvVarProxy:
  223. proxy = TQString(TQString::fromLocal8Bit(getenv(proxyFor(url.protocol()).local8Bit()))).stripWhiteSpace();
  224. break;
  225. case ManualProxy:
  226. proxy = proxyFor( url.protocol() );
  227. break;
  228. case NoProxy:
  229. default:
  230. break;
  231. }
  232. return (proxy.isEmpty() ? TQString::fromLatin1("DIRECT") : proxy);
  233. }
  234. void KProtocolManager::badProxy( const TQString &proxy )
  235. {
  236. DCOPRef( "kded", "proxyscout" ).send( "blackListProxy", proxy );
  237. }
  238. /*
  239. Domain suffix match. E.g. return true if host is "cuzco.inka.de" and
  240. nplist is "inka.de,hadiko.de" or if host is "localhost" and nplist is
  241. "localhost".
  242. */
  243. static bool revmatch(const char *host, const char *nplist)
  244. {
  245. if (host == 0)
  246. return false;
  247. const char *hptr = host + strlen( host ) - 1;
  248. const char *nptr = nplist + strlen( nplist ) - 1;
  249. const char *shptr = hptr;
  250. while ( nptr >= nplist )
  251. {
  252. if ( *hptr != *nptr )
  253. {
  254. hptr = shptr;
  255. // Try to find another domain or host in the list
  256. while(--nptr>=nplist && *nptr!=',' && *nptr!=' ') ;
  257. // Strip out multiple spaces and commas
  258. while(--nptr>=nplist && (*nptr==',' || *nptr==' ')) ;
  259. }
  260. else
  261. {
  262. if ( nptr==nplist || nptr[-1]==',' || nptr[-1]==' ')
  263. return true;
  264. if ( hptr == host ) // e.g. revmatch("bugs.kde.org","mybugs.kde.org")
  265. return false;
  266. hptr--;
  267. nptr--;
  268. }
  269. }
  270. return false;
  271. }
  272. TQString KProtocolManager::slaveProtocol(const KURL &url, TQString &proxy)
  273. {
  274. if (url.hasSubURL()) // We don't want the suburl's protocol
  275. {
  276. KURL::List list = KURL::split(url);
  277. KURL::List::Iterator it = list.fromLast();
  278. return slaveProtocol(*it, proxy);
  279. }
  280. if (!d)
  281. d = new KProtocolManagerPrivate;
  282. if (d->url == url)
  283. {
  284. proxy = d->proxy;
  285. return d->protocol;
  286. }
  287. if (useProxy())
  288. {
  289. proxy = proxyForURL(url);
  290. if ((proxy != "DIRECT") && (!proxy.isEmpty()))
  291. {
  292. bool isRevMatch = false;
  293. KProtocolManager::ProxyType type = proxyType();
  294. bool useRevProxy = ((type == ManualProxy) && useReverseProxy());
  295. TQString noProxy;
  296. // Check no proxy information iff the proxy type is either
  297. // ManualProxy or EnvVarProxy
  298. if ( (type == ManualProxy) || (type == EnvVarProxy) )
  299. noProxy = noProxyFor();
  300. if (!noProxy.isEmpty())
  301. {
  302. TQString qhost = url.host().lower();
  303. const char *host = qhost.latin1();
  304. TQString qno_proxy = noProxy.stripWhiteSpace().lower();
  305. const char *no_proxy = qno_proxy.latin1();
  306. isRevMatch = revmatch(host, no_proxy);
  307. // If no match is found and the request url has a port
  308. // number, try the combination of "host:port". This allows
  309. // users to enter host:port in the No-proxy-For list.
  310. if (!isRevMatch && url.port() > 0)
  311. {
  312. qhost += ':' + TQString::number (url.port());
  313. host = qhost.latin1();
  314. isRevMatch = revmatch (host, no_proxy);
  315. }
  316. // If the hostname does not contain a dot, check if
  317. // <local> is part of noProxy.
  318. if (!isRevMatch && host && (strchr(host, '.') == NULL))
  319. isRevMatch = revmatch("<local>", no_proxy);
  320. }
  321. if ( (!useRevProxy && !isRevMatch) || (useRevProxy && isRevMatch) )
  322. {
  323. d->url = proxy;
  324. if ( d->url.isValid() )
  325. {
  326. // The idea behind slave protocols is not applicable to http
  327. // and webdav protocols.
  328. TQString protocol = url.protocol().lower();
  329. if (protocol.startsWith("http") || protocol.startsWith("webdav"))
  330. d->protocol = protocol;
  331. else
  332. {
  333. d->protocol = d->url.protocol();
  334. kdDebug () << "slaveProtocol: " << d->protocol << endl;
  335. }
  336. d->url = url;
  337. d->proxy = proxy;
  338. return d->protocol;
  339. }
  340. }
  341. }
  342. }
  343. d->url = url;
  344. d->proxy = proxy = TQString::null;
  345. d->protocol = url.protocol();
  346. return d->protocol;
  347. }
  348. /*================================= USER-AGENT SETTINGS =====================*/
  349. TQString KProtocolManager::userAgentForHost( const TQString& hostname )
  350. {
  351. TQString sendUserAgent = TDEIO::SlaveConfig::self()->configData("http", hostname.lower(), "SendUserAgent").lower();
  352. if (sendUserAgent == "false")
  353. return TQString::null;
  354. TQString useragent = TDEIO::SlaveConfig::self()->configData("http", hostname.lower(), "UserAgent");
  355. // Return the default user-agent if none is specified
  356. // for the requested host.
  357. if (useragent.isEmpty())
  358. return defaultUserAgent();
  359. return useragent;
  360. }
  361. TQString KProtocolManager::defaultUserAgent( )
  362. {
  363. TQString modifiers = TDEIO::SlaveConfig::self()->configData("http", TQString::null, "UserAgentKeys");
  364. return defaultUserAgent(modifiers);
  365. }
  366. TQString KProtocolManager::defaultUserAgent( const TQString &_modifiers )
  367. {
  368. if (!d)
  369. d = new KProtocolManagerPrivate;
  370. TQString modifiers = _modifiers.lower();
  371. if (modifiers.isEmpty())
  372. modifiers = DEFAULT_USER_AGENT_KEYS;
  373. if (d->modifiers == modifiers)
  374. return d->useragent;
  375. TQString supp;
  376. struct utsname nam;
  377. if( uname(&nam) >= 0 )
  378. {
  379. if( modifiers.contains('o') )
  380. {
  381. supp += TQString("; %1").arg(nam.sysname);
  382. if ( modifiers.contains('v') )
  383. supp += TQString(" %1").arg(nam.release);
  384. }
  385. if( modifiers.contains('p') )
  386. {
  387. // TODO: determine this value instead of hardcoding it...
  388. supp += TQString::fromLatin1("; X11");
  389. }
  390. if( modifiers.contains('m') )
  391. {
  392. supp += TQString("; %1").arg(nam.machine);
  393. }
  394. if( modifiers.contains('l') )
  395. {
  396. TQStringList languageList = TDEGlobal::locale()->languageList();
  397. TQStringList::Iterator it = languageList.find( TQString::fromLatin1("C") );
  398. if( it != languageList.end() )
  399. {
  400. if( languageList.contains( TQString::fromLatin1("en") ) > 0 )
  401. languageList.remove( it );
  402. else
  403. (*it) = TQString::fromLatin1("en");
  404. }
  405. if( languageList.count() )
  406. supp += TQString("; %1").arg(languageList.join(", "));
  407. }
  408. }
  409. d->modifiers = modifiers;
  410. d->useragent = CFG_DEFAULT_UAGENT(supp);
  411. return d->useragent;
  412. }
  413. /*==================================== OTHERS ===============================*/
  414. bool KProtocolManager::markPartial()
  415. {
  416. TDEConfig *cfg = config();
  417. cfg->setGroup( TQString::null );
  418. return cfg->readBoolEntry( "MarkPartial", true );
  419. }
  420. int KProtocolManager::minimumKeepSize()
  421. {
  422. TDEConfig *cfg = config();
  423. cfg->setGroup( TQString::null );
  424. return cfg->readNumEntry( "MinimumKeepSize",
  425. DEFAULT_MINIMUM_KEEP_SIZE ); // 5000 byte
  426. }
  427. bool KProtocolManager::autoResume()
  428. {
  429. TDEConfig *cfg = config();
  430. cfg->setGroup( TQString::null );
  431. return cfg->readBoolEntry( "AutoResume", false );
  432. }
  433. bool KProtocolManager::persistentConnections()
  434. {
  435. TDEConfig *cfg = config();
  436. cfg->setGroup( TQString::null );
  437. return cfg->readBoolEntry( "PersistentConnections", true );
  438. }
  439. bool KProtocolManager::persistentProxyConnection()
  440. {
  441. TDEConfig *cfg = config();
  442. cfg->setGroup( TQString::null );
  443. return cfg->readBoolEntry( "PersistentProxyConnection", false );
  444. }
  445. TQString KProtocolManager::proxyConfigScript()
  446. {
  447. TDEConfig *cfg = config();
  448. cfg->setGroup( "Proxy Settings" );
  449. return cfg->readEntry( "Proxy Config Script" );
  450. }