📄 dict.cpp
字号:
BOOL ret = FALSE;
PSTR pszSeperator = (m_dictType == REQUEST_COOKIE_TYPE) ? ";" : "&" ;
// Codepage Note:
// We follow IIS's lead. For POST / QueryString data we'll let
// them send it as DBCS characters, so we use the script's set codepage.
LONG lCodePage = (m_dictType == REQUEST_COOKIE_TYPE) ? CP_ACP : m_pASPState->m_lCodePage;
ASP_ERR(IDS_E_NOMEM); // only error that can occur
// DecodeFromURL mangles the string, so we work with a copy of it (this allows
// us to send raw data back to user on request)
if (NULL == (pszTrav = pszTempRawData = MySzDupA(m_pszRawData)))
myleave(1009);
while ( *pszTrav != '\0')
{
// If there is no "=" in the data sent from browser, we don't report an error
// to script writer, though obviously the data will be corrupt in such a case.
svsutil_DecodeFromURL( &pszTrav, "=", pszName = pszTrav, lCodePage);
svsutil_DecodeFromURL( &pszTrav, pszSeperator, pszValue = pszTrav, lCodePage);
if ((NULL == (wszName = MySzDupAtoW(pszName))) ||
(NULL == (wszValue = MySzDupAtoW(pszValue))))
{
MyFree(wszName); // this wouldn't get freed in error case
myleave(1003);
}
// name is already in data set
if ( Lookup(wszName, (void **) &pStrList) )
{
if (FALSE == pStrList->AddStringToArray(wszValue))
{
myleave(1001);
}
MyFree(wszName);
// Don't AddRef here!
}
else // new element
{
pStrList = CreateCRequestStrList(m_pASPState,wszValue,m_dictType);
if ( NULL == pStrList || FALSE == Set(wszName, (void *) pStrList))
{
myleave(1002);
}
// pStrList->AddRef(); Now that we use CoCreateInstance instead of new, don't do AddRef
}
}
ret = TRUE;
done:
DEBUGMSG_ERR(ZONE_ERROR,(L"ASP: ParseInput failed, err = %d\r\n",err));
if (ret)
ASP_ERR(IDS_E_DEFAULT); // set error back to default
MyFree(pszTempRawData);
return ret;
}
BOOL CRequestDictionary::WriteCookies()
{
DEBUG_CODE_INIT;
BOOL ret = FALSE;
SYSTEMTIME st;
CHAR szCookie[2048]; // buffer to hold entire cookie
CHAR szExpires[50];
PSTR pszCookie = szCookie;
DWORD cbCookieBuf = sizeof(szCookie); // size of cookie buffer
DWORD cbLen; // length of data in cookie buffer
PSTR pszTrav;
CHAR szName[512];
CHAR szValue[1024];
PSTR pszName = szName;
PSTR pszValue = szValue;
DWORD cbName;
DWORD cbValue;
DWORD cbNameBuf = sizeof(szName);
DWORD cbValueBuf = sizeof(szValue);
DWORD cbNeeded = 0;
int i;
CRequestStrList *pList = NULL;
if (m_nEntries == 0)
return TRUE;
ASP_ERR(IDS_E_NOMEM); // only error that can occur
for (i = 0; i < m_nEntries; i++)
{
cbLen = 0;
pList = (CRequestStrList *) m_pMapInfo[i].pbData;
DEBUGCHK(pList != NULL && pList->m_nEntries != 0 && pList->m_wszData);
if (0 == MyW2A(m_pMapInfo[i].wszKey, pszName, cbNameBuf))
{
if (0 == (cbNeeded = MyW2A(m_pMapInfo[i].wszKey,0,0)))
myleave(1040);
cbNameBuf = cbNeeded + 1024; // allocate needed buf + a little extra
if (pszName != szName)
pszName = MyRgReAlloc(CHAR,pszName,cbNameBuf,cbNeeded);
else
pszName = MyRgAllocNZ(CHAR,cbNameBuf);
if (!pszName)
myleave(1041);
MyW2A(m_pMapInfo[i].wszKey,pszName, cbNameBuf);
}
if (0 == MyW2A(pList->m_wszData, pszValue, cbValueBuf))
{
if (0 == (cbNeeded = MyW2A(pList->m_wszData,0,0)))
myleave(1042);
cbValueBuf = cbNeeded + 1024; // allocate needed buf + a little extra
if (pszValue != szValue)
pszValue = MyRgReAlloc(CHAR,pszValue,cbValueBuf,cbNeeded);
else
pszValue = MyRgAllocNZ(CHAR,cbValueBuf);
if (!pszValue)
myleave(1043);
MyW2A(pList->m_wszData, pszValue, cbValueBuf);
}
cbName = URLEncodeLen(pszName);
cbValue = URLEncodeLen(pszValue);
cbNeeded = cbName + cbValue + 10 +
(pList->m_pszDomain ? DBCSEncodeLen(pList->m_pszDomain) + CONSTSIZEOF(cszDomain) : 0) +
(pList->m_pszPath ? DBCSEncodeLen(pList->m_pszPath) + CONSTSIZEOF(cszPath) : CONSTSIZEOF(cszPathDefault)) +
(pList->m_dateExpires ? sizeof(szExpires) + CONSTSIZEOF(cszExpires) + 10 : 0);
// Reallocate the memory if we need to.
if (cbCookieBuf < cbNeeded)
{
cbCookieBuf = cbNeeded + 1024;
if (pszCookie != szCookie)
pszCookie = MyRgReAlloc(CHAR,pszCookie,cbCookieBuf,cbNeeded);
else
pszCookie = MyRgAllocNZ(CHAR,cbCookieBuf);
if (!pszCookie)
myleave(1048);
}
pszTrav = URLEncode(pszCookie,pszName);
*pszTrav++ = '=';
pszTrav = URLEncode(pszTrav,pszValue);
if (pList->m_pszDomain)
{
strcpy(pszTrav,cszDomain);
pszTrav += CONSTSIZEOF(cszDomain);
pszTrav = DBCSEncode(pszTrav, pList->m_pszDomain);
}
// Expires header, always an ANSI string anyway.
if (pList->m_dateExpires != 0)
{
VariantTimeToSystemTime(pList->m_dateExpires,&st);
sprintf(szExpires, cszDateOutputFmt, rgWkday[st.wDayOfWeek], st.wDay,
rgMonth[st.wMonth], st.wYear, st.wHour, st.wMinute, st.wSecond);
pszTrav += sprintf(pszTrav,"; Expires=\"%s\"",szExpires);
}
// we always have a path set, even if ASP script didn't request one
if ( pList->m_pszPath)
{
strcpy(pszTrav,cszPath);
pszTrav += CONSTSIZEOF(cszPath);
pszTrav = DBCSEncode(pszTrav, pList->m_pszPath);
}
else
{
// Setting the path to "/" will always match, so browser will always
// return this cookie data.
strcpy(pszTrav,cszPathDefault);
pszTrav += CONSTSIZEOF(cszPathDefault);
}
DEBUGCHK((pszTrav - pszCookie) < (int)cbNeeded);
// The web server tacks ":" onto end of Set-Cookie by default.
if (FALSE == m_pASPState->m_pACB->AddHeader(m_pASPState->m_pACB->ConnID,
"Set-Cookie",pszCookie))
{
myleave(1053);
}
}
ret = TRUE;
done:
if (pszName != szName)
MyFree(pszName);
if (pszValue != szValue)
MyFree(pszValue);
if (pszCookie != szCookie)
MyFree(pszCookie);
if (ret)
ASP_ERR(IDS_E_DEFAULT);
return ret;
}
/*===================================================================
DBCSEncode
DBCS Encode a string by escaping characters with the upper bit
set - Basically used to convert 8 bit data to 7 bit in contexts
where full encoding is not needed.
Parameters:
szDest - Pointer to the buffer to store the URLEncoded string
szSrc - Pointer to the source buffer
Returns:
A pointer to the NUL terminator is returned.
===================================================================*/
char *DBCSEncode(char *szDest, const char *szSrc)
{
char hex[] = "0123456789ABCDEF";
if (!szDest)
return NULL;
if (!szSrc)
{
*szDest = '\0';
return szDest;
}
while (*szSrc)
{
if ((*szSrc & 0x80) || (!isalnum(*szSrc) && !strchr("/$-_.+!*'(),", *szSrc)))
{
*szDest++ = '%';
*szDest++ = hex[BYTE(*szSrc) >> 4];
*szDest++ = hex[*szSrc++ & 0x0F];
}
else
*szDest++ = *szSrc++;
}
*szDest = '\0';
return szDest;
}
/*===================================================================
DBCSEncodeLen
Return the storage requirements for a DBCS encoded string
(url-encoding of characters with the upper bit set ONLY)
Parameters:
szSrc - Pointer to the string to URL Encode
Returns:
the number of bytes required to encode the string
===================================================================*/
int DBCSEncodeLen(const char *szSrc)
{
int cbURL = 1; // add terminator now
if (!szSrc)
return 0;
while (*szSrc)
{
cbURL += ((*szSrc & 0x80) || (!isalnum(*szSrc) && !strchr("/$-_.+!*'(),", *szSrc)))? 3 : 1;
++szSrc;
}
return cbURL;
}
STDMETHODIMP CRequestDictionary::GetServerVariables(BSTR bstrName, VARIANT* pvarReturn) {
DEBUG_CODE_INIT;
PASP_CONTROL_BLOCK pACB = m_pASPState->m_pACB;
HRESULT ret = DISP_E_EXCEPTION;
PSTR pszName = NULL;
CHAR szBuf[256];
PSTR pszValue = szBuf;
DWORD cbValue = sizeof(szBuf);
BOOL fRet;
BSTR bstr = NULL;
DEBUGMSG(ZONE_REQUEST,(L"ASP: In Server Variables!!\r\n"));
// Codepage note: We use ANSI to do this conversion, since headers are ANSI strs
pszName = MySzDupWtoA(bstrName);
if (NULL == pszName)
{
myleave(120);
}
// get length of buffer. cbValue is strlen of variable's value plus \0.
fRet = pACB->GetServerVariable(pACB->ConnID, pszName, pszValue, &cbValue);
if (!fRet)
{
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
myretleave(S_OK,121);
pszValue = MyRgAllocNZ(CHAR,cbValue);
if (NULL == pszValue)
{
myleave(122);
}
pszValue[cbValue - 1] = '\0';
if (FALSE == pACB->GetServerVariable(pACB->ConnID, pszName, pszValue, &cbValue))
{
DEBUGCHK(FALSE); // This should never happen
myleave(123);
}
}
if ( FAILED(SysAllocStringFromSz(pszValue, cbValue, &bstr, CP_ACP)))
{
myleave(124);
}
V_VT(pvarReturn) = VT_BSTR;
V_BSTR(pvarReturn) = bstr;
ret = S_OK;
done:
DEBUGMSG_ERR(ZONE_ERROR,(L"ASP: GetServerVariables failed, err = %d, GLE = %X, pszName = <<%a>>\r\n",
err, GetLastError(), pszName));
if (ret != S_OK)
{
// set it to an empty string.
m_pASPState->m_pEmptyString->QueryInterface(IID_IRequestStrList, (void **) (&V_DISPATCH(pvarReturn)));
V_VT(pvarReturn) = VT_DISPATCH;
}
if (szBuf != pszValue)
MyFree(pszValue);
MyFree(pszName);
return ret;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -