22#include <sys/utsname.h>
29#include <tdeversion.h>
30#include <tdestandarddirs.h>
31#include <klibloader.h>
32#include <kstringhandler.h>
33#include <kstaticdeleter.h>
34#include <tdeio/slaveconfig.h>
35#include <tdeio/ioslave_defaults.h>
36#include <tdeio/http_slave_defaults.h>
38#include "tdeprotocolmanager.h"
41KProtocolManagerPrivate
44 KProtocolManagerPrivate();
46 ~KProtocolManagerPrivate();
49 TDEConfig *http_config;
58static KProtocolManagerPrivate* d = 0;
59static KStaticDeleter<KProtocolManagerPrivate> kpmpksd;
61KProtocolManagerPrivate::KProtocolManagerPrivate()
62 :config(0), http_config(0), init_busy(false)
64 kpmpksd.setObject(d,
this);
67KProtocolManagerPrivate::~KProtocolManagerPrivate()
75#define CFG_DEFAULT_UAGENT(X) \
76TQString("Mozilla/5.0 (compatible; Konqueror/%1.%2%3) KHTML/TDEHTML/%4.%5.%6 (like Gecko)") \
77 .arg(TDE_VERSION_MAJOR).arg(TDE_VERSION_MINOR).arg(X).arg(TDE_VERSION_MAJOR).arg(TDE_VERSION_MINOR).arg(TDE_VERSION_RELEASE)
81 kpmpksd.destructObject();
84 TDEIO::SlaveConfig::self()->
reset ();
87TDEConfig *KProtocolManager::config()
90 d =
new KProtocolManagerPrivate;
94 d->config =
new TDEConfig(
"tdeioslaverc",
true,
false);
99TDEConfig *KProtocolManager::http_config()
102 d =
new KProtocolManagerPrivate;
106 d->http_config =
new TDEConfig(
"tdeio_httprc",
false,
false);
108 return d->http_config;
115 TDEConfig *cfg = config();
116 cfg->setGroup( TQString::null );
117 int val = cfg->readNumEntry(
"ReadTimeout", DEFAULT_READ_TIMEOUT );
118 return TQMAX(MIN_TIMEOUT_VALUE, val);
123 TDEConfig *cfg = config();
124 cfg->setGroup( TQString::null );
125 int val = cfg->readNumEntry(
"ConnectTimeout", DEFAULT_CONNECT_TIMEOUT );
126 return TQMAX(MIN_TIMEOUT_VALUE, val);
131 TDEConfig *cfg = config();
132 cfg->setGroup( TQString::null );
133 int val = cfg->readNumEntry(
"ProxyConnectTimeout", DEFAULT_PROXY_CONNECT_TIMEOUT );
134 return TQMAX(MIN_TIMEOUT_VALUE, val);
139 TDEConfig *cfg = config();
140 cfg->setGroup( TQString::null );
141 int val = cfg->readNumEntry(
"ResponseTimeout", DEFAULT_RESPONSE_TIMEOUT );
142 return TQMAX(MIN_TIMEOUT_VALUE, val);
154 TDEConfig *cfg = config();
155 cfg->setGroup(
"Proxy Settings" );
156 return cfg->readBoolEntry(
"ReversedException",
false);
161 TDEConfig *cfg = config();
162 cfg->setGroup(
"Proxy Settings" );
163 return static_cast<ProxyType>(cfg->readNumEntry(
"ProxyType" ));
168 TDEConfig *cfg = config();
169 cfg->setGroup(
"Proxy Settings" );
170 return static_cast<ProxyAuthMode>(cfg->readNumEntry(
"AuthMode" ));
177 TDEConfig *cfg = http_config();
178 return cfg->readBoolEntry(
"UseCache",
true );
183 TDEConfig *cfg = http_config();
184 TQString tmp = cfg->readEntry(
"cache");
186 return DEFAULT_CACHE_CONTROL;
192 TDEConfig *cfg = http_config();
193 return cfg->readPathEntry(
"CacheDir", TDEGlobal::dirs()->saveLocation(
"cache",
"http"));
198 TDEConfig *cfg = http_config();
199 return cfg->readNumEntry(
"MaxCacheAge", DEFAULT_MAX_CACHE_AGE );
204 TDEConfig *cfg = http_config();
205 return cfg->readNumEntry(
"MaxCacheSize", DEFAULT_MAX_CACHE_SIZE );
210 TDEConfig *cfg = config();
211 cfg->setGroup(
"Proxy Settings" );
213 return cfg->readEntry(
"NoProxyFor" );
220 noProxy = TQString::fromLocal8Bit(getenv(noProxy.local8Bit()));
227 TQString scheme = protocol.lower();
229 if (scheme ==
"webdav")
231 else if (scheme ==
"webdavs")
234 TDEConfig *cfg = config();
235 cfg->setGroup(
"Proxy Settings" );
236 return cfg->readEntry( scheme +
"Proxy" );
248 if (!url.host().isEmpty())
251 TQString p = u.protocol().lower();
260 else if ( p ==
"webdavs" )
266 if ( p.startsWith(
"http") || p ==
"ftp" || p ==
"gopher" )
267 DCOPRef(
"kded",
"proxyscout" ).call(
"proxyForURL", u ).get( proxy );
271 proxy = TQString(TQString::fromLocal8Bit(getenv(
proxyFor(url.protocol()).local8Bit()))).stripWhiteSpace();
281 return (proxy.isEmpty() ? TQString::fromLatin1(
"DIRECT") : proxy);
286 DCOPRef(
"kded",
"proxyscout" ).send(
"blackListProxy", proxy );
294static bool revmatch(
const char *host,
const char *nplist)
299 const char *hptr = host + strlen( host ) - 1;
300 const char *nptr = nplist + strlen( nplist ) - 1;
301 const char *shptr = hptr;
303 while ( nptr >= nplist )
305 if ( *hptr != *nptr )
310 while(--nptr>=nplist && *nptr!=
',' && *nptr!=
' ') ;
313 while(--nptr>=nplist && (*nptr==
',' || *nptr==
' ')) ;
317 if ( nptr==nplist || nptr[-1]==
',' || nptr[-1]==
' ')
334 KURL::List list = KURL::split(url);
335 KURL::List::Iterator it = list.fromLast();
340 d =
new KProtocolManagerPrivate;
351 if ((proxy !=
"DIRECT") && (!proxy.isEmpty()))
353 bool isRevMatch =
false;
360 if ( (type == ManualProxy) || (type == EnvVarProxy) )
363 if (!noProxy.isEmpty())
365 TQString qhost = url.host().lower();
366 const char *host = qhost.latin1();
367 TQString qno_proxy = noProxy.stripWhiteSpace().lower();
368 const char *no_proxy = qno_proxy.latin1();
369 isRevMatch = revmatch(host, no_proxy);
374 if (!isRevMatch && url.port() > 0)
376 qhost +=
':' + TQString::number (url.port());
377 host = qhost.latin1();
378 isRevMatch = revmatch (host, no_proxy);
383 if (!isRevMatch && host && (strchr(host,
'.') == NULL))
384 isRevMatch = revmatch(
"<local>", no_proxy);
387 if ( (!useRevProxy && !isRevMatch) || (useRevProxy && isRevMatch) )
390 if ( d->url.isValid() )
394 TQString protocol = url.protocol().lower();
395 if (protocol.startsWith(
"http") || protocol.startsWith(
"webdav"))
396 d->protocol = protocol;
399 d->protocol = d->url.protocol();
400 kdDebug () <<
"slaveProtocol: " << d->protocol << endl;
412 d->proxy = proxy = TQString::null;
413 d->protocol = url.protocol();
421 TQString sendUserAgent = TDEIO::SlaveConfig::self()->
configData(
"http", hostname.lower(),
"SendUserAgent").lower();
422 if (sendUserAgent ==
"false")
423 return TQString::null;
425 TQString useragent = TDEIO::SlaveConfig::self()->
configData(
"http", hostname.lower(),
"UserAgent");
429 if (useragent.isEmpty())
437 TQString modifiers = TDEIO::SlaveConfig::self()->
configData(
"http", TQString::null,
"UserAgentKeys");
444 d =
new KProtocolManagerPrivate;
446 TQString modifiers = _modifiers.lower();
447 if (modifiers.isEmpty())
448 modifiers = DEFAULT_USER_AGENT_KEYS;
450 if (d->modifiers == modifiers)
455 if( uname(&nam) >= 0 )
457 if( modifiers.contains(
'o') )
459 supp += TQString(
"; %1").arg(nam.sysname);
460 if ( modifiers.contains(
'v') )
461 supp += TQString(
" %1").arg(nam.release);
463 if( modifiers.contains(
'p') )
466 supp += TQString::fromLatin1(
"; X11");
468 if( modifiers.contains(
'm') )
470 supp += TQString(
"; %1").arg(nam.machine);
472 if( modifiers.contains(
'l') )
474 TQStringList languageList = TDEGlobal::locale()->languageList();
475 TQStringList::Iterator it = languageList.find( TQString::fromLatin1(
"C") );
476 if( it != languageList.end() )
478 if( languageList.contains( TQString::fromLatin1(
"en") ) > 0 )
479 languageList.remove( it );
481 (*it) = TQString::fromLatin1(
"en");
483 if( languageList.count() )
484 supp += TQString(
"; %1").arg(languageList.join(
", "));
487 d->modifiers = modifiers;
488 d->useragent = CFG_DEFAULT_UAGENT(supp);
496 TDEConfig *cfg = config();
497 cfg->setGroup( TQString::null );
498 return cfg->readBoolEntry(
"MarkPartial",
true );
503 TDEConfig *cfg = config();
504 cfg->setGroup( TQString::null );
505 return cfg->readNumEntry(
"MinimumKeepSize",
506 DEFAULT_MINIMUM_KEEP_SIZE );
511 TDEConfig *cfg = config();
512 cfg->setGroup( TQString::null );
513 return cfg->readBoolEntry(
"AutoResume",
false );
518 TDEConfig *cfg = config();
519 cfg->setGroup( TQString::null );
520 return cfg->readBoolEntry(
"PersistentConnections",
true );
525 TDEConfig *cfg = config();
526 cfg->setGroup( TQString::null );
527 return cfg->readBoolEntry(
"PersistentProxyConnection",
false );
532 TDEConfig *cfg = config();
533 cfg->setGroup(
"Proxy Settings" );
534 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.