Пример #1
0
QString KProtocolManager::slaveProtocol(const KURL &url, QString &proxy)
{
    if(url.hasSubURL()) // We don't want the suburl's protocol
    {
        KURL::List list = KURL::split(url);
        KURL::List::Iterator it = list.fromLast();
        return slaveProtocol(*it, proxy);
    }

    if(!d)
        d = new KProtocolManagerPrivate;

    if(d->url == url)
    {
        proxy = d->proxy;
        return d->protocol;
    }

    if(useProxy())
    {
        proxy = proxyForURL(url);
        if((proxy != "DIRECT") && (!proxy.isEmpty()))
        {
            bool isRevMatch = false;
            KProtocolManager::ProxyType type = proxyType();
            bool useRevProxy = ((type == ManualProxy) && useReverseProxy());

            QString noProxy;
            // Check no proxy information iff the proxy type is either
            // ManualProxy or EnvVarProxy
            if((type == ManualProxy) || (type == EnvVarProxy))
                noProxy = noProxyFor();

            if(!noProxy.isEmpty())
            {
                QString qhost = url.host().lower();
                const char *host = qhost.latin1();
                QString qno_proxy = noProxy.stripWhiteSpace().lower();
                const char *no_proxy = qno_proxy.latin1();
                isRevMatch = revmatch(host, no_proxy);

                // If no match is found and the request url has a port
                // number, try the combination of "host:port". This allows
                // users to enter host:port in the No-proxy-For list.
                if(!isRevMatch && url.port() > 0)
                {
                    qhost += ':' + QString::number(url.port());
                    host = qhost.latin1();
                    isRevMatch = revmatch(host, no_proxy);
                }

                // If the hostname does not contain a dot, check if
                // <local> is part of noProxy.
                if(!isRevMatch && host && (strchr(host, '.') == NULL))
                    isRevMatch = revmatch("<local>", no_proxy);
            }

            if((!useRevProxy && !isRevMatch) || (useRevProxy && isRevMatch))
            {
                d->url = proxy;
                if(d->url.isValid())
                {
                    // The idea behind slave protocols is not applicable to http
                    // and webdav protocols.
                    QString protocol = url.protocol().lower();
                    if(protocol.startsWith("http") || protocol.startsWith("webdav"))
                        d->protocol = protocol;
                    else
                    {
                        d->protocol = d->url.protocol();
                        kdDebug() << "slaveProtocol: " << d->protocol << endl;
                    }

                    d->url = url;
                    d->proxy = proxy;
                    return d->protocol;
                }
            }
        }
    }

    d->url = url;
    d->proxy = proxy = QString::null;
    d->protocol = url.protocol();
    return d->protocol;
}