예제 #1
0
파일: manager.c 프로젝트: lessc0de/ncbi-vdb
static
void KNSManagerHttpProxyInit ( KNSManager * self )
{
    const KConfigNode * proxy;
    rc_t rc = KConfigOpenNodeRead ( self -> kfg, & proxy, "http/proxy" );
    if ( rc == 0 )
    {
        const KConfigNode * proxy_path;
        rc = KConfigNodeOpenNodeRead ( proxy, & proxy_path, "path" );
        if ( rc == 0 )
        {
            String * path;
            rc = KConfigNodeReadString ( proxy_path, & path );
            if ( rc == 0 )
            {
                rc = KNSManagerSetHTTPProxyPath ( self, "%S", path );
                if ( rc == 0 )
                {
                    const KConfigNode * proxy_enabled;
                    rc = KConfigNodeOpenNodeRead ( proxy, & proxy_enabled, "enabled" );
                    if ( rc == 0 )
                    {
                        rc = KConfigNodeReadBool ( proxy_enabled, & self -> http_proxy_enabled );
                        KConfigNodeRelease ( proxy_enabled );
                    }
                    else if ( GetRCState ( rc ) == rcNotFound )
                    {
                        rc = 0;
                    }

                    if ( rc != 0 )
                    {
                        KNSManagerSetHTTPProxyPath ( self, NULL );
                        assert ( self -> http_proxy_enabled == false );
                    }
                }

                StringWhack ( path );
            }

            KConfigNodeRelease ( proxy_path );
        }

        KConfigNodeRelease ( proxy );
    }
}
예제 #2
0
void CVDBMgr::x_Init(void)
{
    if ( rc_t rc = VDBManagerMakeRead(x_InitPtr(), 0) ) {
        *x_InitPtr() = 0;
        NCBI_THROW2(CSraException, eInitFailed,
                    "Cannot open VDBManager", rc);
    }
    uint32_t sdk_ver;
    if ( rc_t rc = VDBManagerVersion(*this, &sdk_ver) ) {
        NCBI_THROW2(CSraException, eInitFailed,
                    "Cannot get VDBManager version", rc);
    }
    CKNSManager kns_mgr(CVFSManager(*this));
    CNcbiOstrstream str;
    CNcbiApplication* app = CNcbiApplication::Instance();
    if ( app ) {
        str << app->GetAppName() << ": " << app->GetVersion().Print() << "; ";
    }
#if NCBI_PACKAGE
    str << "Package: " << NCBI_PACKAGE_NAME << ' ' <<
        NCBI_PACKAGE_VERSION << "; ";
#endif
    str << "C++ ";
#ifdef NCBI_PRODUCTION_VER
    str << NCBI_PRODUCTION_VER << "/";
#endif
#ifdef NCBI_DEVELOPMENT_VER
    str << NCBI_DEVELOPMENT_VER;
#endif
    string prefix = CNcbiOstrstreamToString(str);
    KNSManagerSetUserAgent(kns_mgr, "%s; SRA Toolkit %V",
                           prefix.c_str(),
                           sdk_ver);

    // redirect VDB log to C++ Toolkit
    if ( s_GetDiagHandler() ) {
        KLogInit();
        KLogLevelSet(klogDebug);
        KLogLibHandlerSet(VDBLogWriter, 0);
    }

    if ( app ) {
        string host = app->GetConfig().GetString("CONN", "HTTP_PROXY_HOST", kEmptyStr);
        int port = app->GetConfig().GetInt("CONN", "HTTP_PROXY_PORT", 0);
        if ( !host.empty() && port != 0 ) {
            if ( rc_t rc = KNSManagerSetHTTPProxyPath(kns_mgr, "%s:%d", host.c_str(), port) ) {
                NCBI_THROW2(CSraException, eInitFailed,
                            "Cannot set KNSManager proxy parameters", rc);
            }
            KNSManagerSetHTTPProxyEnabled(kns_mgr, true);
        }
    }
}
예제 #3
0
파일: proxy.c 프로젝트: ncbi/ncbi-vdb
KNSProxies * KNSManagerKNSProxiesMake ( struct KNSManager * mgr,
                                        const KConfig * kfg )
{
    rc_t rc = 0;

    int i = 0;
    int n = 2;

    typedef enum {
        eEnv,
        eKfg,
    } EType;

    EType type [ 2 ] = { eKfg, eEnv };

    KNSProxies * self = calloc ( 1, sizeof * self ); 
    if ( self == NULL )
        return NULL;

    assert ( self );

    BSTreeInit ( & self -> proxie_tree );

    rc = KConfigReadBool
        ( kfg, "/http/proxy/enabled", & self -> http_proxy_enabled );
    if ( rc != 0 ) {
        if ( GetRCState ( rc ) == rcNotFound )
            rc = 0;
        else {
            KNSManagerSetHTTPProxyPath ( mgr, NULL );
            assert ( self -> http_proxy_enabled == false );
        }
    }
    else if ( ! self -> http_proxy_enabled )
        return self;

    {
        bool proxy_only = false;
        rc_t rc = KConfigReadBool ( kfg, "/http/proxy/only",  & proxy_only );
        if ( rc == 0 && proxy_only )
            self-> http_proxy_only = true;
    }

    {
        String * result = NULL;
        rc = KConfigReadString ( kfg, "/http/proxy/use", & result );
        if ( rc == 0 ) {
            if ( StringCmp ( result, "env") ) {
                n = 1;
                type [ 0 ] = eEnv;
            } else if ( StringCmp ( result, "kfg") ) {
                n = 1;
                type [ 0 ] = eKfg;
            } else if ( StringCmp ( result, "none") ) {
                n = 0;
            } else if ( StringCmp ( result, "env,kfg") ) {
                n = 2;
                type [ 0 ] = eEnv;
                type [ 1 ] = eKfg;
            } else if ( StringCmp ( result, "kfg,env") ) {
                n = 2;
                type [ 0 ] = eKfg;
                type [ 1 ] = eEnv;
            }
        }
        RELEASE ( String, result );
    }

    for ( i = 0; i < n; ++ i ) {
        switch ( type [ i ] ) {
            case eEnv:
                KNSProxiesHttpProxyInitFromEnv ( self );
                break;
            case eKfg:
                KNSProxiesHttpProxyInitFromKfg ( self, kfg );
                break;
            default:
                assert ( 0 );
                break;
        }
    }

    BSTreeForEach ( & self -> proxie_tree, false, KNSProxiesBSTreeCount,
                    & self -> http_proxies_cnt );

    if ( self -> http_proxies_cnt > 0 ) {
        self -> http_proxies = calloc ( self -> http_proxies_cnt,
                                        sizeof * self -> http_proxies );
        if ( self -> http_proxies == NULL )
            return NULL;
        DBGMSG ( DBG_KNS, DBG_FLAG ( DBG_KNS_PROXY ),
            ( "Will use %zu proxy spec%s%s\n", self -> http_proxies_cnt,
              self -> http_proxies_cnt > 1 ? "s" : "",
              self -> http_proxy_only ? "" : " and direct connection") );
    }

    self -> tmpS = 0;
    n = self -> http_proxies_cnt;
    srand ( time ( NULL ) );
    while ( n > 0 ) {
        self -> rand = rand () % n;
        self -> tmpI = 0;
        if ( ! BSTreeDoUntil ( & self -> proxie_tree, false,
                               KNSProxiesBSTreeSetRand, self ) )
        {
            BSTreeForEach ( & self -> proxie_tree, false,
                               KNSProxiesBSTreeInit, self ) ;
            n = 0;
        }
        else {
            const BSTItem * item = ( BSTItem * ) self -> tmpB;
            self -> http_proxies [ self -> tmpS ++ ] = item -> proxy;
            BSTreeUnlink ( & self -> proxie_tree, self -> tmpB );
            BSTItemWhack ( self -> tmpB, NULL );
            self -> tmpB = NULL;
            -- n;
        }
    }

/* BSTreeForEach ( & self -> proxie_tree, false, KNSProxiesBSTreeInit, self );*/

    for ( self -> tmpS = 1; self -> tmpS < self ->http_proxies_cnt;
       ++ self -> tmpS )
    {
        self -> http_proxies [ self -> tmpS - 1 ] -> next
            = self -> http_proxies [ self -> tmpS ];
    }

    return self;
}