⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 httpconnbase.cpp

📁 Windows CE 6.0 Server 源码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
    return ::AtomicFreeAndCopyBSTR(m_bstrSoapAction, V_BSTR(pVal));
}



////////////////////////////////////////////////////////////////////////////////////////////////////
//  function: HRESULT CHttpConnBase::get_HTTPCharset(VARIANT *pVal)
//
//  parameters:
//
//  description:
//
//  returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT CHttpConnBase::get_HTTPCharset(VARIANT *pVal)
{
    ASSERT(pVal != 0 && V_VT(pVal) == VT_EMPTY);

    V_VT(pVal)   = VT_BSTR;
    V_BSTR(pVal) = ::SysAllocString(m_bstrHTTPCharset);

    return S_OK;
}


////////////////////////////////////////////////////////////////////////////////////////////////////
//  function: HRESULT CHttpConnBase::put_HTTPCharset(VARIANT *pVal)
//
//  parameters:
//
//  description:
//
//  returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT CHttpConnBase::put_HTTPCharset(VARIANT *pVal)
{
    ASSERT(pVal != 0 && V_VT(pVal) == VT_BSTR);
    return ::AtomicFreeAndCopyBSTR(m_bstrHTTPCharset, V_BSTR(pVal));
}



////////////////////////////////////////////////////////////////////////////////////////////////////
//  function: HRESULT CHttpConnBase::PutEndPointURL(BSTR bstrUrl)
//
//  parameters:
//          
//  description:
//          
//  returns:
//          
////////////////////////////////////////////////////////////////////////////////////////////////////
inline HRESULT CHttpConnBase::PutEndPointURL(BSTR bstrUrl)
{
    HRESULT hr = m_Url.put_Url(bstrUrl);

    BEGIN_INLINE_ERROR_MAP()
        PASS_THROUGH(E_OUTOFMEMORY)
    END_INLINE_ERROR_MAP(hr, CONN_E_HTTP_BAD_URL)
    
    return hr;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//  function: static HRESULT ParseCertificateName(BSTR bstrCertName, BSTR *pLocation, BSTR *pStoreName, BSTR *pCertificate)
//
//  parameters:
//          
//  description:
//          Parses certificate name into pieces [ CURRENT_USER | LOCAL_MACHINE \ [ store-name \ ] ] cert-name
//          Default store is CURRENT_USER\MY
//  returns:
//          
////////////////////////////////////////////////////////////////////////////////////////////////////
static HRESULT ParseCertificateName(BSTR bstrCertName, BSTR *pLocation, BSTR *pStoreName, BSTR *pCertificate)
{
    ASSERT(bstrCertName && *bstrCertName);

    HRESULT hr              = S_OK;
    LPWSTR  pCertName       = bstrCertName;
    DWORD   dwLocation      = 0;
    DWORD   dwStoreName     = 0;
    DWORD   dwCertificate   = 0;
    BSTR    bstrLocation    = 0;
    BSTR    bstrStoreName   = 0;
    BSTR    bstrCertificate = 0;
    LPCWSTR pszLocation     = 0;
    LPCWSTR pszStoreName    = 0;
    LPCWSTR pszCertificate  = 0;

    while (*pCertName ++);   // Go to the end

    if (pCertName > bstrCertName + 1)
    {
        pCertName --; 
    }
    else    // Nothing to parse
    {
        goto Cleanup;
    }

    // Find certificate Name

    while (-- pCertName >= bstrCertName && *pCertName != L'\\')
    {
        dwCertificate ++;
    }

    pszCertificate = pCertName + 1;

    if (pCertName < bstrCertName)
    {
        goto Finished;
    }

    while (-- pCertName >= bstrCertName && *pCertName != L'\\')
    {
        dwStoreName ++;
    }

    pszStoreName = pCertName + 1;

    if (pCertName < bstrCertName)
    {
        goto Finished;
    }

    while (-- pCertName >= bstrCertName && *pCertName != L'\\')
    {
        dwLocation ++;
    }

    if (pCertName >= bstrCertName)
    {
        hr = CONN_E_BAD_CERTIFICATE_NAME;
        goto Cleanup;
    }

    pszLocation = pCertName + 1;

    ASSERT(pszLocation == bstrCertName);

Finished:

    ASSERT(pszCertificate);
    if (pszCertificate)
    {
        CHK_MEM(bstrCertificate = ::SysAllocStringLen(pszCertificate, dwCertificate));
    }
    else
    {
        hr = CONN_E_BAD_CERTIFICATE_NAME;
        goto Cleanup;
    }

    if (pszStoreName)
    {
        CHK_MEM(bstrStoreName = ::SysAllocStringLen(pszStoreName, dwStoreName));
    }
    else
    {
        CHK_MEM(bstrStoreName = ::SysAllocStringLen(L"MY", 2));
    }

    if (pszLocation)
    {
        CHK_MEM(bstrLocation = ::SysAllocStringLen(pszLocation, dwLocation));
    }
    else
    {
        CHK_MEM(bstrLocation = ::SysAllocStringLen(L"CURRENT_USER", 12));
    }

    *pCertificate   = bstrCertificate;
    bstrCertificate = 0;
    *pStoreName     = bstrStoreName;
    bstrStoreName   = 0;
    *pLocation      = bstrLocation;
    bstrLocation    = 0;

    hr = S_OK;

Cleanup:
    ::SysFreeString(bstrCertificate);
    ::SysFreeString(bstrStoreName);
    ::SysFreeString(bstrLocation);

    return hr;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//  function: HRESULT CHttpConnBase::GetProxyURL(BSTR *pbstrUrl)
//
//  parameters:
//          
//  description:
//          
//  returns:
//          
////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT CHttpConnBase::GetProxyURL(BSTR *pbstrUrl)
{
    HRESULT hr   = S_OK;
    LPWSTR  pUrl = 0;

    CHK_ARG(pbstrUrl != 0);

    *pbstrUrl = 0;

    if (m_dwProxyPort == 0)
    {
        pUrl = m_bstrProxyServer;
    }
    else
    {
        if (m_bstrProxyServer)
        {
            int length = ::SysStringByteLen(m_bstrProxyServer);
#ifndef UNDER_CE         
            try
#else   
            __try
#endif
            {
                pUrl = reinterpret_cast<LPWSTR>(_alloca(length + 40));
            }
#ifndef UNDER_CE
            catch (...)
#else
            __except(1)
#endif
            {
                hr = E_OUTOFMEMORY;
                goto Cleanup;
            }

#ifdef UNDER_CE
            hr = StringCbPrintf(pUrl, length + 40, L"%s:%i", m_bstrProxyServer, m_dwProxyPort);
            if(FAILED(hr))
            {
                goto Cleanup;
            }
#else
            swprintf(pUrl, L"%s:%i", m_bstrProxyServer, m_dwProxyPort);
#endif
        }
    }

    if (pUrl != 0)
    {
        *pbstrUrl = ::SysAllocString(pUrl);
    }

    hr = S_OK;

Cleanup:
    return hr;
}


////////////////////////////////////////////////////////////////////////////////////////////////////
//  function: static bool FindCommonName(WCHAR *subject, WCHAR *&name)
//
//  parameters:
//
//  description:
//        Identifies position of the Common Name (CN) part in the certificate name
//  returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
static bool FindCommonName(WCHAR *subject, WCHAR *&name)
{
    ASSERT(subject != 0);
    ASSERT(name == 0);

    enum
    {
        Initial,
        Name,
        Value,
        QValue,
        QQValue,
        NValue,
    } state = Initial;

    WCHAR *pName     = 0;
    DWORD dwNameSize = 0;

    for (; *subject != _T('\0'); subject ++)
    {
        switch (state)
        {
        case Initial:
            if (! iswspace(*subject))
            {
                state      = Name;
                pName      = subject;
                dwNameSize = 0;
            }
            break;
        case Name:
            if (*subject == _T('='))
            {
                dwNameSize = subject - pName;
                state      = Value;
            }
            break;
        case Value:
            if (*subject == _T('"'))
            {
                state = QValue;
                if (*(subject + 1) != _T('\0'))
                {
                    name = subject + 1;
                }
            }
            else
            {
                state = NValue;
                name  = subject;
            }
            break;
        case QValue:
            if (*subject == _T('"'))
            {
                state = QQValue;
            }
            break;
        case QQValue:
            if (*subject == _T('"'))
            {
                state = QValue;
            }
            else if (*subject == _T(','))
                goto Found;
            break;
        case NValue:
            if (*subject == _T(','))
            {
                goto Found;
            }
            break;
        default:
            ASSERT(FALSE);
            break;

        Found:
            *subject = _T('\0');
            if (pName != 0 && dwNameSize == 2 && wcsncmp(pName, _T("CN"), 2) == 0)
            {
                return true;
            }
            else
            {
                state   = Initial;
                name    = 0;
            }
        }
    }

    if (state == NValue)
    {
        *subject = _T('\0');
        if (pName != 0 && wcsncmp(pName, _T("CN"), dwNameSize) == 0)
        {
            return true;
        }
    }

    name   = 0;

    return false;
}


////////////////////////////////////////////////////////////////////////////////////////////////////
//  function: HRESULT CHttpConnBase::FindCertificate(BSTR bstrCertName, PCERT_CONTEXT *pCertContext)
//
//  parameters:
//          
//  description:
//          Finds certificate specified by bstrCertName
//  returns:
//          
////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT CHttpConnBase::FindCertificate(BSTR bstrCertName, PCCERT_CONTEXT *pCertContext)
{
    ASSERT(pCertContext != 0);

    HRESULT hr = S_OK;
    BSTR    bstrLocation     = 0;
    BSTR    bstrStoreName    = 0;
    BSTR    bstrCertificate  = 0;

    PCCERT_CONTEXT pContext   = 0;
    HCERTSTORE     hCertStore = 0;
    DWORD          dwFlags    = 0;

    if (bstrCertName != 0 && *bstrCertName != 0)
    {
        CHK(::ParseCertificateName(bstrCertName, &bstrLocation, &bstrStoreName, &bstrCertificate));

        if (wcscmp(bstrLocation, L"CURRENT_USER") == 0)
        {
            dwFlags = CERT_SYSTEM_STORE_CURRENT_USER;
        }
        else if (wcscmp(bstrLocation, L"LOCAL_MACHINE") == 0)
        {
            dwFlags = CERT_SYSTEM_STORE_LOCAL_MACHINE;
        }
        else
        {
            hr = CONN_E_BAD_CERTIFICATE_NAME;
            goto Cleanup;
        }

        hCertStore = ::CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0, dwFlags, bstrStoreName);
        CHK_BOOL(hCertStore, HRESULT_FROM_WIN32(::GetLastError()));

        for (;;)
        {
            pContext = ::CertFindCertificateInStore(hCertStore, X509_ASN_ENCODING, 0, CERT_FIND_ANY, 0, pContext);

            if (! pContext)
            {
                pContext = ::CertFindCertificateInStore(hCertStore, X509_ASN_ENCODING, 0, CERT_FIND_SUBJECT_STR, bstrCertificate, 0);
                CHK_BOOL(pContext, HRESULT_FROM_WIN32(::GetLastError()));
                break;
            }
#ifndef UNDER_CE
            WCHAR subject[1024] = { 0 };
            WCHAR *name  = 0;

            CHK_BOOL(::CertNameToStrW(X509_ASN_ENCODING, &pContext->pCertInfo->Subject, CERT_X500_NAME_STR, subject, sizeof(subject)), HRESULT_FROM_WIN32(::GetLastError()));
#else
            #define DEF_SZ_SUB_SIZE 1024
            WCHAR subject[DEF_SZ_SUB_SIZE] = { 0 };
            WCHAR *name  = 0;

            CHK_BOOL(::CertNameToStrW(X509_ASN_ENCODING, &pContext->pCertInfo->Subject, CERT_X500_NAME_STR, subject, DEF_SZ_SUB_SIZE), HRESULT_FROM_WIN32(::GetLastError()));

#endif

            if (! FindCommonName(subject, name))
                continue;

            if (_wcsicmp(name, bstrCertificate) == 0)
                break;
        }

    }

    *pCertContext = pContext;
    pContext      = 0;

Cleanup:

    if (hCertStore)
    {
        ::CertCloseStore(hCertStore, 0);
    }

    if (pContext)
    {
        ::CertFreeCertificateContext(pContext);
    }

    ::SysFreeString(bstrCertificate);
    ::SysFreeString(bstrStoreName);
    ::SysFreeString(bstrLocation);

    return hr;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -