22 #include <sys/utsname.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>
37 #include "tdeprotocolmanager.h"
40 KProtocolManagerPrivate
43 KProtocolManagerPrivate();
45 ~KProtocolManagerPrivate();
48 TDEConfig *http_config;
57 static KProtocolManagerPrivate* d = 0;
58 static KStaticDeleter<KProtocolManagerPrivate> kpmpksd;
60 KProtocolManagerPrivate::KProtocolManagerPrivate()
61 :config(0), http_config(0), init_busy(false)
63 kpmpksd.setObject(d,
this);
66 KProtocolManagerPrivate::~KProtocolManagerPrivate()
74 #define CFG_DEFAULT_UAGENT(X) \
75 TQString("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)
80 kpmpksd.destructObject();
83 TDEIO::SlaveConfig::self()->
reset ();
86 TDEConfig *KProtocolManager::config()
89 d =
new KProtocolManagerPrivate;
93 d->config =
new TDEConfig(
"tdeioslaverc",
true,
false);
98 TDEConfig *KProtocolManager::http_config()
101 d =
new KProtocolManagerPrivate;
105 d->http_config =
new TDEConfig(
"tdeio_httprc",
false,
false);
107 return d->http_config;
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);
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);
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);
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);
153 TDEConfig *cfg = config();
154 cfg->setGroup(
"Proxy Settings" );
155 return cfg->readBoolEntry(
"ReversedException",
false);
160 TDEConfig *cfg = config();
161 cfg->setGroup(
"Proxy Settings" );
162 return static_cast<ProxyType>(cfg->readNumEntry(
"ProxyType" ));
167 TDEConfig *cfg = config();
168 cfg->setGroup(
"Proxy Settings" );
169 return static_cast<ProxyAuthMode>(cfg->readNumEntry(
"AuthMode" ));
176 TDEConfig *cfg = http_config();
177 return cfg->readBoolEntry(
"UseCache",
true );
182 TDEConfig *cfg = http_config();
183 TQString tmp = cfg->readEntry(
"cache");
185 return DEFAULT_CACHE_CONTROL;
191 TDEConfig *cfg = http_config();
192 return cfg->readPathEntry(
"CacheDir", TDEGlobal::dirs()->saveLocation(
"cache",
"http"));
197 TDEConfig *cfg = http_config();
198 return cfg->readNumEntry(
"MaxCacheAge", DEFAULT_MAX_CACHE_AGE );
203 TDEConfig *cfg = http_config();
204 return cfg->readNumEntry(
"MaxCacheSize", DEFAULT_MAX_CACHE_SIZE );
209 TDEConfig *cfg = config();
210 cfg->setGroup(
"Proxy Settings" );
212 return cfg->readEntry(
"NoProxyFor" );
219 noProxy = TQString::fromLocal8Bit(getenv(noProxy.local8Bit()));
226 TQString scheme = protocol.lower();
228 if (scheme ==
"webdav")
230 else if (scheme ==
"webdavs")
233 TDEConfig *cfg = config();
234 cfg->setGroup(
"Proxy Settings" );
235 return cfg->readEntry( scheme +
"Proxy" );
247 if (!url.host().isEmpty())
250 TQString p = u.protocol().lower();
259 else if ( p ==
"webdavs" )
265 if ( p.startsWith(
"http") || p ==
"ftp" || p ==
"gopher" )
266 DCOPRef(
"kded",
"proxyscout" ).call(
"proxyForURL", u ).get( proxy );
270 proxy = TQString(TQString::fromLocal8Bit(getenv(
proxyFor(url.protocol()).local8Bit()))).stripWhiteSpace();
280 return (proxy.isEmpty() ? TQString::fromLatin1(
"DIRECT") : proxy);
285 DCOPRef(
"kded",
"proxyscout" ).send(
"blackListProxy", proxy );
293 static bool revmatch(
const char *host,
const char *nplist)
298 const char *hptr = host + strlen( host ) - 1;
299 const char *nptr = nplist + strlen( nplist ) - 1;
300 const char *shptr = hptr;
302 while ( nptr >= nplist )
304 if ( *hptr != *nptr )
309 while(--nptr>=nplist && *nptr!=
',' && *nptr!=
' ') ;
312 while(--nptr>=nplist && (*nptr==
',' || *nptr==
' ')) ;
316 if ( nptr==nplist || nptr[-1]==
',' || nptr[-1]==
' ')
333 KURL::List list = KURL::split(url);
334 KURL::List::Iterator it = list.fromLast();
339 d =
new KProtocolManagerPrivate;
350 if ((proxy !=
"DIRECT") && (!proxy.isEmpty()))
352 bool isRevMatch =
false;
359 if ( (type == ManualProxy) || (type == EnvVarProxy) )
362 if (!noProxy.isEmpty())
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);
373 if (!isRevMatch && url.port() > 0)
375 qhost +=
':' + TQString::number (url.port());
376 host = qhost.latin1();
377 isRevMatch = revmatch (host, no_proxy);
382 if (!isRevMatch && host && (strchr(host,
'.') == NULL))
383 isRevMatch = revmatch(
"<local>", no_proxy);
386 if ( (!useRevProxy && !isRevMatch) || (useRevProxy && isRevMatch) )
389 if ( d->url.isValid() )
393 TQString protocol = url.protocol().lower();
394 if (protocol.startsWith(
"http") || protocol.startsWith(
"webdav"))
395 d->protocol = protocol;
398 d->protocol = d->url.protocol();
399 kdDebug () <<
"slaveProtocol: " << d->protocol << endl;
411 d->proxy = proxy = TQString::null;
412 d->protocol = url.protocol();
420 TQString sendUserAgent = TDEIO::SlaveConfig::self()->
configData(
"http", hostname.lower(),
"SendUserAgent").lower();
421 if (sendUserAgent ==
"false")
422 return TQString::null;
424 TQString useragent = TDEIO::SlaveConfig::self()->
configData(
"http", hostname.lower(),
"UserAgent");
428 if (useragent.isEmpty())
436 TQString modifiers = TDEIO::SlaveConfig::self()->
configData(
"http", TQString::null,
"UserAgentKeys");
443 d =
new KProtocolManagerPrivate;
445 TQString modifiers = _modifiers.lower();
446 if (modifiers.isEmpty())
447 modifiers = DEFAULT_USER_AGENT_KEYS;
449 if (d->modifiers == modifiers)
454 if( uname(&nam) >= 0 )
456 if( modifiers.contains(
'o') )
458 supp += TQString(
"; %1").arg(nam.sysname);
459 if ( modifiers.contains(
'v') )
460 supp += TQString(
" %1").arg(nam.release);
462 if( modifiers.contains(
'p') )
465 supp += TQString::fromLatin1(
"; X11");
467 if( modifiers.contains(
'm') )
469 supp += TQString(
"; %1").arg(nam.machine);
471 if( modifiers.contains(
'l') )
473 TQStringList languageList = TDEGlobal::locale()->languageList();
474 TQStringList::Iterator it = languageList.find( TQString::fromLatin1(
"C") );
475 if( it != languageList.end() )
477 if( languageList.contains( TQString::fromLatin1(
"en") ) > 0 )
478 languageList.remove( it );
480 (*it) = TQString::fromLatin1(
"en");
482 if( languageList.count() )
483 supp += TQString(
"; %1").arg(languageList.join(
", "));
486 d->modifiers = modifiers;
487 d->useragent = CFG_DEFAULT_UAGENT(supp);
495 TDEConfig *cfg = config();
496 cfg->setGroup( TQString::null );
497 return cfg->readBoolEntry(
"MarkPartial",
true );
502 TDEConfig *cfg = config();
503 cfg->setGroup( TQString::null );
504 return cfg->readNumEntry(
"MinimumKeepSize",
505 DEFAULT_MINIMUM_KEEP_SIZE );
510 TDEConfig *cfg = config();
511 cfg->setGroup( TQString::null );
512 return cfg->readBoolEntry(
"AutoResume",
false );
517 TDEConfig *cfg = config();
518 cfg->setGroup( TQString::null );
519 return cfg->readBoolEntry(
"PersistentConnections",
true );
524 TDEConfig *cfg = config();
525 cfg->setGroup( TQString::null );
526 return cfg->readBoolEntry(
"PersistentProxyConnection",
false );
531 TDEConfig *cfg = config();
532 cfg->setGroup(
"Proxy Settings" );
533 return cfg->readEntry(
"Proxy Config Script" );
static void badProxy(const TQString &proxy)
Marks this proxy as bad (down).
ProxyAuthMode
Proxy authorization modes.
static bool useCache()
Returns true/false to indicate whether a cache should be used.
static bool useReverseProxy()
Returns true if the proxy settings should apply to the list returned by noProxyFor.
static int proxyConnectTimeout()
Returns the preferred timeout value for proxy connections in seconds.
static TQString defaultUserAgent()
Returns the default user-agent string.
static TQString noProxyFor()
Returns a comma-separated list of hostnames or partial host-names that should bypass any proxy settin...
static TQString proxyConfigScript()
Returns the URL of the script for automatic proxy configuration.
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,...
static TQString userAgentForHost(const TQString &hostname)
Returns the userAgent string configured for the specified host.
static bool persistentProxyConnection()
Returns true if proxy connections should be persistent.
ProxyType
Types of proxy configuration.
static bool persistentConnections()
Returns true if connections should be persistent.
static bool useProxy()
Returns true if the user specified a proxy server to make connections.
static ProxyAuthMode proxyAuthMode()
Returns the way proxy authorization should be handled.
static TQString cacheDir()
The directory which contains the cache files.
static int maxCacheSize()
Returns the maximum size that can be used for caching.
static int connectTimeout()
Returns the preferred timeout value for remote connections in seconds.
static bool autoResume()
Returns true if partial downloads should be automatically resumed.
static bool markPartial()
Returns true if partial downloads should be marked with a ".part" extension.
static TQString proxyFor(const TQString &protocol)
Returns the proxy server address for a given protocol.
static TQString proxyForURL(const KURL &url)
Returns the proxy server address for a given URL.
static TQString noProxyForRaw()
Same as above except the environment variable name is returned instead of the variable value when pro...
static int minimumKeepSize()
Returns the minimum file size for keeping aborted downloads.
static void reparseConfiguration()
Force a reload of the general config file of io-slaves ( tdeioslaverc).
static TDEIO::CacheControl cacheControl()
Returns the Cache control directive to be used.
static int maxCacheAge()
Returns the maximum age in seconds cached files should be kept before they are deleted as necessary.
static int responseTimeout()
Returns the preferred response timeout value for remote connecting in seconds.
static int readTimeout()
Returns the preferred timeout value for reading from remote connections in seconds.
static ProxyType proxyType()
Returns the type of proxy configuration that is used.
MetaData configData(const TQString &protocol, const TQString &host)
Query slave configuration for slaves of type protocol when dealing with host.
void reset()
Undo any changes made by calls to setConfigData.
TDEIO_EXPORT TDEIO::CacheControl parseCacheControl(const TQString &cacheControl)
Parses the string representation of the cache control option.
CacheControl
Specifies how to use the cache.