예제 #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;
}
예제 #2
0
int KProtocolProxyFTP::OpenProxy(KURL *_url, int mode, bool _reload)
{
  url = _url->url().data();

  bytesRead = 0;
  startTime.start();
  currentTime.start();
  
    // Save the parameter, we could need it if we get HTTP redirection
    // See ::ProcessHeader
    currentMode = mode;
    
	if(mode != READ) return(Error(KIO_ERROR_NotImplemented,
				              "FTP Proxy currently only supports reading",0));
	if (connected) Close();

	sock = ::socket(PF_INET,SOCK_STREAM,0);

	if (sock < 0)
	{
	    Error(KIO_ERROR_CouldNotCreateSocket,"Could not create socket",errno);
	    return(FAIL);
	}

	int do_proxy = use_proxy;
	if (do_proxy)
	{
//            char *p;
//	    if ( ( p = getenv("no_proxy") ) ) {
//	         do_proxy = !revmatch(_url->host(), p);
//	    }
       	    if ( ! noProxyForStr.isEmpty() ) 
	    {
	      // printf( "host: %s\n", _url->host() );
	      // printf( "nplist: %s\n", noProxyForStr.data() );
	        do_proxy = !revmatch( _url->host(), noProxyForStr.data() );    
	    }
	}

	if(do_proxy)
	{
	  // printf("FTP::Open: connecting to proxy %s:%d\n",
	  // inet_ntoa(proxy_name.sin_addr),
	  // ntohs(proxy_name.sin_port));
		if(::connect(sock,(struct sockaddr*)(&proxy_name),sizeof(proxy_name)))
		{
	    	Error(KIO_ERROR_CouldNotConnect,"Could not connect to proxy",errno);
	    	return(FAIL);
		}
	}
	else
	{
		struct sockaddr_in server_name;
		int port = _url->port();
		if ( port == 0 )
			port = 80;

		if(init_sockaddr(&server_name, _url->host(), port) == FAIL)
		{
    		Error(KIO_ERROR_UnknownHost, "Unknown host", errno );
			return(FAIL);
		}

                // printf("ProxyFTP::Open: connecting to %s:%d\n",
		// inet_ntoa(server_name.sin_addr),
		// ntohs(server_name.sin_port));
		if(::connect(sock,(struct sockaddr*)(&server_name),sizeof(server_name)))
		{
	    	Error(KIO_ERROR_CouldNotConnect, "Could not connect host", errno);
			return(FAIL);
		}
 	}
	connected = 1;

	fsocket = fdopen(sock,"r+");
	if(!fsocket)
	{
	    Error(KIO_ERROR_CouldNotConnect, "Could not fdopen socket", errno);
	    return(FAIL);
	}

	QString command(15              // for the constant characters
                        + strlen(_url->user())
                        + strlen(_url->host())
                        + 20            // for the port number
                        );

	if(do_proxy)
	{
		/** add hostname when using proxy **/
		int port = _url->port();
		if (! port &&  strcmp(_url->protocol(),"ftp") == 0)  // use default one
			port = 21;

		if( strlen(_url->user()) != 0 )
		    command.sprintf("GET ftp://%s@%s:%d",
		                     _url->user(), _url->host(), port);
		else
		    command.sprintf("GET ftp://%s:%d", _url->host(), port);

	} else {
		command = "GET ";
	}
	
	if ( _url->path()[0] != '/' ) command += "/";
	command += _url->httpPath(); // keep path encoded. Thanks to [email protected]

	command += " HTTP/1.0\n"; /* start header */
	command += "User-Agent: Konqueror/1.0\r\n"; /* User agent */

	if ( _reload ){ /* No caching for reload */
	  command += "Pragma: no-cache\r\n"; /* for HTTP/1.0 caches */
	  command += "Cache-control: no-cache\r\n"; /* for HTTP/>=1.1 caches */
        }
	
	command += "Host: "+QString(_url->host())+"\r\n"; /* support for HTTP 1.1 */
 
	if( strlen(_url->user()) != 0 )
	{
		char *www_auth = create_www_auth(_url->user(),_url->passwd());
		command += www_auth;
		free(www_auth);
	}

	if( do_proxy )
	{
	  if( proxy_user != "" && proxy_pass != "" )
          {
	    char *www_auth = create_generic_auth("Proxy-authorization", proxy_user, proxy_pass);
	    command += www_auth;
	    free(www_auth);
	  }
	}

	command += "\n";  /* end header */
	// fprintf(stderr,"Command=%s\n",command.data());

	// write(0, command.data(), command.length());
	write(sock, command.data(), command.length());

	return(ProcessHeader());
}