• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeio/tdeio
 

tdeio/tdeio

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

tdeio/tdeio

Skip menu "tdeio/tdeio"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdeio/tdeio

Skip menu "tdeio/tdeio"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeio/tdeio by doxygen 1.9.8
This website is maintained by Timothy Pearson.