示例#1
0
HX_RESULT HXCloakedV2TCPSocket::_DoProxyAuthentication(HTTPResponseMessage* pMess)
{
    HX_RESULT                  res              = HXR_FAIL;
    IHXCommonClassFactory*     pCCF             = NULL;
    CHXString                  getStr           = "";
    IHXKeyValueList*           pRespHeaders     = NULL;
    MIMEHeaderValue*           pHeaderValue     = NULL;
    MIMEHeader*                pHeader          = NULL;
    CHXString                  strHeader        = "";
    CHXString                  strTemp          = "";
    IHXBuffer*                 pBuffer          = NULL;
    IHXRequest*                pRequest         = NULL;
    IHXValues*                 pValues          = NULL;             
    IHXClientAuthConversation* pClientAuth      = NULL;
    IUnknown*                  pUnkConfig       = NULL;
    IHXObjectConfiguration*    pConfig          = NULL;


    //////////////////////
    //
    // XXXgfw I really don't think we need all this authentication
    // garbage.  It really seems we only need to TLC to just give us a
    // dialog to do a user/pass request. It sure seems like this code
    // below is way overkill. Just check out ResponseReady(), the only
    // thing is needs from all this is just that one string. Oh well,
    // until I have time to figure out exactly how this all works (all
    // the calls through the core and into the TLC from here) I will
    // just leave it like the original cloaking.
    //
    //////////////////////
    
    res = m_pContext->QueryInterface(IID_IHXCommonClassFactory, (void**)&pCCF);
    if( SUCCEEDED(res) )
    {
        //pRequest will be freed in the ResponseReady() method.
        res = pCCF->CreateInstance(CLSID_IHXRequest, (void**)&pRequest);
        if( SUCCEEDED(res) )
        {
            getStr = _GenerateGetRequest();
            pRequest->SetURL((const char*)getStr); //Always returns HXR_OK;
            
            res = pCCF->CreateInstance(CLSID_IHXKeyValueList, (void**)&pRespHeaders);
            if( SUCCEEDED(res) )
            {
                
                pHeader = pMess->getFirstHeader();
                while (pHeader)
                {
                    pHeaderValue = pHeader->getFirstHeaderValue();
                    strHeader = "";
                    while( pHeaderValue )
                    {
                        pHeaderValue->asString(strTemp);
                        strHeader += strTemp;
                        pHeaderValue = pHeader->getNextHeaderValue();
                        if (pHeaderValue)
                        {
                            strHeader += ", ";
                        }
                    }
                    pBuffer = NULL;
                    CHXBuffer::FromCharArray((const char*)strHeader, &pBuffer);
                    if( pBuffer )
                    {
                        pRespHeaders->AddKeyValue(pHeader->name(), pBuffer);
                    }
                    HX_RELEASE(pBuffer);
                    pHeader = pMess->getNextHeader();
                }

                pValues = NULL;
                if (HXR_OK == pRespHeaders->QueryInterface(IID_IHXValues, (void**)&pValues))
                {
                    pRequest->SetResponseHeaders(pValues);
                }
                HX_RELEASE(pValues);
            }

            HX_RELEASE(pRespHeaders);

            if (HXR_OK == pRequest->GetResponseHeaders(pValues))
            {
                res = pCCF->CreateInstance(CLSID_IHXBuffer, (void**)&pBuffer);
                if (SUCCEEDED(res))
                {
                    pBuffer->Set((UCHAR*)m_pszProxyName, strlen(m_pszProxyName)+1);
                    pValues->SetPropertyCString("_server", pBuffer);
                    HX_RELEASE(pBuffer);
                }

                // Add the protocol to the response headers because TLC needs it
                if (SUCCEEDED(pCCF->CreateInstance(CLSID_IHXBuffer, (void**)&pBuffer)))
                {
                    pBuffer->Set((UCHAR*)"http", strlen("http") + 1);
                    pValues->SetPropertyCString("_protocol", pBuffer);
                    HX_RELEASE(pBuffer);
                }

                HX_RELEASE(pValues);
            }

            //We now need a CCF from the cloaking context, not our core's context.
            HX_RELEASE(pCCF);


            res = m_pCloakContext->QueryInterface(IID_IHXCommonClassFactory, (void**)&pCCF);
            if(SUCCEEDED(res))
            {
                res = pCCF->CreateInstance( CLSID_CHXClientAuthenticator, (void**)&pUnkConfig );
                if(SUCCEEDED(res))
                {
                    res = pUnkConfig->QueryInterface(IID_IHXClientAuthConversation, 
                                                     (void**)&pClientAuth);
                    if (SUCCEEDED(res))
                    {
                        res = pUnkConfig->QueryInterface(IID_IHXObjectConfiguration,
                                                         (void**)&pConfig);
                        if (SUCCEEDED(res))
                        {
                            pConfig->SetContext(m_pCloakContext);
                            HX_RELEASE(pConfig);
                        }

                        if ( pClientAuth && !pClientAuth->IsDone() )
                        {
                            HX_ASSERT(pRequest);
                            //After this if/else, we continue in ResponseReady() after
                            //the TLC calls us back.
                            if (pRequest)
                            {
                                res = pClientAuth->MakeResponse(this, pRequest);
                            }
                            else
                            {
                                // Auth Failed!
                                pClientAuth->Authenticated(FALSE);
                                res = HXR_NOT_AUTHORIZED;
                                res = ResponseReady(res, pRequest);
                            }
                        }
                    }
                    HX_RELEASE(pUnkConfig);
                }
                HX_RELEASE(pCCF);
            }
        }
    }

    //We return this in all cases. the connection will be started over in
    //the ResponseReady() method.
    res = HXR_NOT_AUTHORIZED;
    return res;
}