SVERROR GetFromSVServer()

in host/common/hostagenthelpers.cpp [314:501]


SVERROR GetFromSVServer( const char *pszSVServerName,
                        SV_INT HttpPort,
                        const char *pszGetURL,
                        char **ppszGetBuffer )
{
    DebugPrintf(SV_LOG_DEBUG, "Entering %s\n", __FUNCTION__);
    SVERROR rc;
    HRESULT hr = S_OK;
    DWORD dwQueryResponseLength = 0;
    char *pszQueryResponse = NULL;
    HINTERNET hinternetSession = NULL;
    HINTERNET hinternet = NULL;
    HINTERNET hinternetRequest = NULL;
    char const* SV_ROOT_KEY = "SOFTWARE\\SV Systems";
    SV_USHORT usiServerHttpPort = INTERNET_DEFAULT_HTTP_PORT;

    do
    {
        DebugPrintf( "@ LINE %d in FILE %s \n", __LINE__, __FILE__ );
        DebugPrintf( "ENTERED GetFromSVServer()...\n" );

        if( ( NULL == pszSVServerName ) ||
            ( NULL == pszGetURL ) ||
            ( NULL == ppszGetBuffer ) )
        {
            hr = E_INVALIDARG;
            DebugPrintf( "@ LINE %d in FILE %s \n", __LINE__, __FILE__ );
            DebugPrintf( hr, "FAILED GetFromSVServer()... hr = %08X\n", hr );
            break;
        }

        DebugPrintf( "@ LINE %d in FILE %s \n", __LINE__, __FILE__ );
        DebugPrintf( "CALLING InternetOpen()...\n" );

        hinternetSession = InternetOpen( "SVHostAgent",
            0, //INTERNET_OPEN_TYPE_DIRECT,
            NULL,
            NULL,
            INTERNET_FLAG_DONT_CACHE );
        if( NULL == hinternetSession )
        {
            hr = HRESULT_FROM_WIN32( GetLastError() );
            DebugPrintf( "@ LINE %d in FILE %s \n", __LINE__, __FILE__ );
            DebugPrintf( hr, "FAILED InternetOpen()... hr = %08X\n", hr );
            break;
        }

        DebugPrintf( "@ LINE %d in FILE %s \n", __LINE__, __FILE__ );
        DebugPrintf( "CALLING InternetConnect()... SVServerName: %s\n", pszSVServerName );

        hinternet = InternetConnect( hinternetSession,
            pszSVServerName,
            HttpPort,
            "",
            NULL,
            INTERNET_SERVICE_HTTP,
            INTERNET_FLAG_DONT_CACHE,
            0 );
        if( NULL == hinternet )
        {
            hr = HRESULT_FROM_WIN32( GetLastError() );
            DebugPrintf( "@ LINE %d in FILE %s \n", __LINE__, __FILE__ );
            DebugPrintf( hr, "FAILED InternetConnect()... hr = %08X\n", hr );
            break;
        }

        DebugPrintf( "@ LINE %d in FILE %s \n", __LINE__, __FILE__ );
        DebugPrintf( "CALLING HttpOpenRequest()... URL: %s\n", pszGetURL );

        hinternetRequest = HttpOpenRequest( hinternet,
            "GET",
            pszGetURL,
            NULL, // Defaults to HTTP 1.0
            NULL,
            NULL, // Accepts only text/*"
            INTERNET_FLAG_DONT_CACHE, //BUGBUG: No SSL
            0 );

        if( NULL == hinternetRequest )
        {
            hr = HRESULT_FROM_WIN32( GetLastError() );
            DebugPrintf( "@ LINE %d in FILE %s \n", __LINE__, __FILE__ );
            DebugPrintf( hr, "FAILED InternetConnect()... hr = %08X\n", hr );
            printf( "HttpOpenRequest Failed: hr: %x\n", hr );
            break;
        }

        DWORD dwTimeout = 30 * 1000;
        if(!InternetSetOption(hinternetRequest, INTERNET_OPTION_RECEIVE_TIMEOUT, &dwTimeout, sizeof(dwTimeout))) {
            DebugPrintf( "@ LINE %d in FILE %s \n", __LINE__, __FILE__ );
            DebugPrintf( "CALLING Failed Setting Timeout()...\n" );
        }

        DebugPrintf( "@ LINE %d in FILE %s \n", __LINE__, __FILE__ );
        DebugPrintf( "CALLING HttpSendRequest()...\n" );

        if( !HttpSendRequest( hinternetRequest,
            NULL,
            0,
            NULL,
            0 ) )
        {
            DWORD dwError = GetLastError();
            hr = HRESULT_FROM_WIN32( dwError );
            DebugPrintf( "@ LINE %d in FILE %s \n", __LINE__, __FILE__ );
            DebugPrintf( hr, "FAILED HttpSendRequest()... hr = %08X\n", hr );
            break;
        }

        DWORD dwIndex = 0;
        DWORD dwError = 0;

        hr = SVHttpQueryInfo( hinternetRequest,
            HTTP_QUERY_STATUS_CODE,
            NULL,
            &pszQueryResponse );

        if( FAILED( hr ) )
        {
            DebugPrintf( "@ LINE %d in FILE %s \n", __LINE__, __FILE__ );
            DebugPrintf( hr, "FAILED SVHttpQueryInfo() for HTTP status... hr = %08X\n", hr );
            break;
        }       

        if( HTTP_STATUS_OK != atoi( pszQueryResponse ) )
        {
            hr = E_FAIL;
            DebugPrintf( "@ LINE %d in FILE %s \n", __LINE__, __FILE__ );
            DebugPrintf( hr, "FAILED HTTP_STATUS: %d; hr = %08X\n", atoi( pszQueryResponse ), hr );
            break;
        }
        delete [] pszQueryResponse;
        pszQueryResponse = NULL;

        DebugPrintf( "@ LINE %d in FILE %s \n", __LINE__, __FILE__ );
        DebugPrintf( "CALLING HttpQueryInfo()...\n" );

        //
        // Use Content-Length if present; otherwise, we read till end of connection
        //
        hr = SVHttpQueryInfo( hinternetRequest,
            HTTP_QUERY_CONTENT_LENGTH,
            NULL,
            &pszQueryResponse );

        DWORD dwContentLength = 0;      
        if( SUCCEEDED( hr ) )
        {
            dwContentLength = atoi( pszQueryResponse );
        }
        delete [] pszQueryResponse;
        pszQueryResponse = NULL;


        DWORD dwBytesRead = 0;
        DebugPrintf( "@ LINE %d in FILE %s \n", __LINE__, __FILE__ );

        hr = SVInternetReadFile( hinternetRequest, ppszGetBuffer, dwContentLength, &dwBytesRead );

        if( FAILED( hr ) )
        {
            DebugPrintf( "@ LINE %d in FILE %s \n", __LINE__, __FILE__ );
            DebugPrintf( hr, "FAILED SVInternetReadFile()... hr = %08X\n", hr );
            break;
        }
    }
    while( FALSE );

    if( ( NULL != hinternetRequest ) && !InternetCloseHandle( hinternetRequest ) )
    {
        DebugPrintf( "@ LINE %d in FILE %s \n", __LINE__, __FILE__ );
        DebugPrintf( HRESULT_FROM_WIN32( GetLastError() ), "FAILED InternetCloseHandle()...\n" );
    }
    if( ( NULL != hinternet ) && !InternetCloseHandle( hinternet ) )
    {
        DebugPrintf( "@ LINE %d in FILE %s \n", __LINE__, __FILE__ );
        DebugPrintf( HRESULT_FROM_WIN32( GetLastError() ), "FAILED InternetCloseHandle()...\n" );
    }
    if( ( NULL != hinternetSession ) && !InternetCloseHandle( hinternetSession ) )
    {
        DebugPrintf( "@ LINE %d in FILE %s \n", __LINE__, __FILE__ );
        DebugPrintf( HRESULT_FROM_WIN32( GetLastError() ), "FAILED InternetCloseHandle()...\n" );
    }

    rc = hr;
    DebugPrintf(SV_LOG_DEBUG, "Exiting %s\n", __FUNCTION__);
    return( rc );
}