📄 exchangerequest.cpp
字号:
hr = m_pClient->BypassOWALoginPage(piHttpRequest);
//If we successfully bypassed the login page, send the request again
//otherwise we fail
if (FAILED(hr))
{
DEBUGMSG(ZONE_OWAEC_ERROR, (L"OWAExchangeClient:: Bypassing the owa login page failed hr = 0x%x", hr));
UpdateStatus(e_ecrsFailedToBypassAuthPage);
goto error;
}
if (Canceled())
goto cancel;
hr = SendRequest(piHttpRequest);
if (FAILED(hr))
{
UpdateStatus(e_ecrsFailedToSend);
goto error;
}
if (ResponseIsOWALoginPage(piHttpRequest))
{
//then the credentials are not valid!
m_lHttpStatus = HTTP_UNAUTHORIZED;
UpdateStatus(e_ecrsHttpFailure);
hr = E_FAIL;
goto error;
}
}
//If we succeeded, get the resulting status and try to parse the results
hr = piHttpRequest->get_status(&m_lHttpStatus);
if (FAILED(hr))
{
RETAILMSG(1, (L"OWAExchangeClient:: Unexpected - could not get status from http request (0x%x)", hr));
goto error;
}
//only status' in [200-299] are successful
if (m_lHttpStatus < HTTP_SUCCESS_RANGE_START || m_lHttpStatus > HTTP_SUCCESS_RANGE_END)
{
UpdateStatus(e_ecrsHttpFailure);
fSuccessfulResponse = FALSE;
goto error;
}
DEBUGMSG(ZONE_OWAEC_TRACING_INFORMATIONAL, (L"Request returns with status %d", m_lHttpStatus));
//if we got a succeessful response, parse the results
hr = piHttpRequest->get_responseText(&bstrResponse);
if (FAILED(hr))
{
RETAILMSG(1, (L"OWAExchangeClient:: Unexpected - could not get text from http request (0x%x)", hr));
ASSERT(FALSE);
goto error;
}
if (Canceled())
goto cancel;
//The constructor for a refcounteddatarecordlist add's a reference
m_pDataRecordList = new RefCountedDataRecordList(m_piFormatHandler->GetDataRecordSize());
if (m_pDataRecordList == NULL)
{
DEBUGMSG(ZONE_OWAEC_ERROR, (L"OWAExchangeClient:: Out of memory!"));
UpdateStatus(e_ecrsOutOfMemory);
hr = E_OUTOFMEMORY;
goto error;
}
(VOID)pParser->ClearMapping();
hr = m_piFormatHandler->SetParsingFormat(pParser);
if (FAILED(hr))
{
RETAILMSG(1, (L"OWAExchangeClient:: Unexpected - could not set parsing format (0x%x)", hr));
ASSERT(FALSE);
goto error;
}
hr = pParser->Parse(
bstrResponse,
m_pDataRecordList
);
if (FAILED(hr))
{
//if parsing failed after we passed the OWA Login screen, then we didn't log in successfully
//simulate an "Unauthorized" response
if (fTryLoginPage)
{
DEBUGMSG(ZONE_OWAEC_ERROR, (L"OWAExchangeClient:: Parsing failed indicating we couldn't log in - bad credentials - hr = 0x%x", hr));
//simulate a 401 - unauthorized
m_lHttpStatus = HTTP_UNAUTHORIZED;
UpdateStatus(e_ecrsHttpFailure);
}
else
{
DEBUGMSG(ZONE_OWAEC_ERROR, (L"OWAExchangeClient:: Parsing failed hr = 0x%x", hr));
UpdateStatus(e_ecrsParseFailed);
}
goto error;
}
UpdateStatus(e_ecrsSucceeded);
exit:
if (piHttpRequest)
{
//force a cleanup of memory associated with the request
piHttpRequest->abort();
}
if (FAILED(hr))
{
SafeRelease(m_pDataRecordList);
}
return hr;
cancel:
(VOID)UpdateStatus(e_ecrsCancelled);
goto exit;
error:
//ensure we report the necessary failures
if (SUCCEEDED(hr))
{
hr = E_FAIL;
}
if (hr == E_OUTOFMEMORY)
{
UpdateStatus(e_ecrsOutOfMemory);
}
switch (m_Status)
{
case e_ecrsInProgress:
case e_ecrsPending:
case e_ecrsSending:
RETAILMSG(1, (L"OWAExchangeClient:: Unknown general failure - setting failure to e_ecrsFailedToSend"));
UpdateStatus(e_ecrsFailedToSend);
break;
default:
break;
}
goto exit;
}
/*------------------------------------------------------------------------------
CExchangeClientRequest::Cancel
Cancels this request
Returns (HRESULT): S_OK - the request was cancelled successfully
S_FALSE - the request has already run or is currently running
------------------------------------------------------------------------------*/
HRESULT STDMETHODCALLTYPE CExchangeClientRequest::Cancel()
{
//Stabilize ourself
AddRef();
//Have the client remove 'this' from the request queue
HRESULT hr = m_pClient->CancelMe(this);
if (hr == S_FALSE)
{
//Syncronously set the value of canceled to true (from false)
InterlockedCompareExchange(&m_fCanceled, TRUE, FALSE);
}
Release();
return hr;
}
/*------------------------------------------------------------------------------
CExchangeClientRequest::SendRequest
Use the format handler to send the request to the server
------------------------------------------------------------------------------*/
HRESULT CExchangeClientRequest::SendRequest(
IXMLHTTPRequest *piHttpRequest
)
{
HRESULT hr = S_OK;
ce::auto_bstr bstrMethod, bstrUrl, bstrBody;
(VOID)UpdateStatus(e_ecrsSending);
//Have the handler get the method, url and body for this request
hr = m_piFormatHandler->GetFormattedHttpParameters(
&bstrMethod,
&bstrUrl,
&bstrBody
);
if (SUCCEEDED(hr))
{
VARIANT vAsync = {0},
vUName = {0},
vPWord = {0};
vAsync.vt = VT_BOOL; vAsync.bVal = FALSE;
//Use cached credentials
vUName.vt = VT_BSTR; vUName.bstrVal = NULL;
vPWord.vt = VT_BSTR; vPWord.bstrVal = NULL;
hr = piHttpRequest->open(
bstrMethod,
bstrUrl,
vAsync,
vUName,
vPWord
);
ASSERT(SUCCEEDED(hr));
}
if (SUCCEEDED(hr))
{
//Have the handler attach additional headers as appropriate
hr = m_piFormatHandler->SetAdditionalHeaders(
piHttpRequest
);
}
if (SUCCEEDED(hr))
{
VARIANT vBody = {0};
vBody.vt = VT_BSTR;
vBody.bstrVal = bstrBody;
//Send the request *syncronously*
hr = piHttpRequest->send(vBody);
}
return hr;
}
/*------------------------------------------------------------------------------
CExchangeClientRequest::ResponseIsOWALoginPage
Determine whether or not the response of a request is the OWA login page
Returns (BOOL): TRUE if the response is the OWA login page - FALSE otherwise
------------------------------------------------------------------------------*/
BOOL CExchangeClientRequest::ResponseIsOWALoginPage(
IXMLHTTPRequest *piHttpRequest
)
{
PREFAST_ASSERT(piHttpRequest);
HRESULT hr = S_OK;
LONG lStatus = 0;
ce::auto_bstr bstrResponse;
hr = piHttpRequest->get_status(&lStatus);
if (lStatus == HTTP_LOGIN_TIMEOUT)
{
DEBUGMSG(ZONE_OWAEC_TRACING_INFORMATIONAL, (L"OWAExchangeClient:: Request %08x received a 440 from the server", this));
return TRUE;
}
//get the text
hr = piHttpRequest->get_responseText(&bstrResponse);
if (FAILED(hr))
{
return FALSE;
}
//does the login guid exist on this webpage?
BOOL fRet = (wcsstr(bstrResponse, (const WCHAR*)c_SettingAuthLoginPageGUID) != NULL);
if (fRet)
{
DEBUGMSG(ZONE_OWAEC_TRACING_INFORMATIONAL, (L"OWAExchangeClient:: Request %08x received a response containing the OWA Login Page GUID", this));
}
return fRet;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -