📄 cookies.cpp
字号:
CHXSimpleList* pCookiesFound2 = NULL; CHXSimpleList* pCookiesList = NULL; IHXValues* pValues = NULL; CHXSimpleList::Iterator i; CHXString cHostCopy; INT32 lColon; BOOL bIsPlayerCookieList = FALSE; CHXString strPlayerCookies;#ifdef REPORT_COOKIES CHXString strCookieReport;#endif pCookies = NULL; pPlayerCookies = NULL; if (!IsCookieEnabled()) { goto cleanup; } if (!m_bInitialized) { hr = Initialize(); if (HXR_OK != hr) { goto cleanup; } } else { SyncRMCookies(FALSE); } if (!pHost || !m_pCookiesHelper) { hr = HXR_FAILED; goto cleanup; } // return string to build if (!(pValues = new CHXHeader())) { hr = HXR_OUTOFMEMORY; goto cleanup; } pValues->AddRef(); cHostCopy = pHost; lColon = cHostCopy.Find(':'); if (lColon >= 0) { cHostCopy = cHostCopy.Left(lColon); } cHostCopy.MakeLower(); // search for all cookies(Netscape only for now) for (l = 0; l < 2; l++) { switch (l) { case 0: pCookiesList = m_pRMCookies; bIsPlayerCookieList = TRUE; break; case 1: pCookiesList = m_pNSCookies; bIsPlayerCookieList = FALSE; break; default: break; } if (!pCookiesList) { continue; } for (i = pCookiesList->Begin(); i != pCookiesList->End(); ++i) { pTempCookie = (CookieStruct*) (*i); if (!pTempCookie->pHost) { continue; } // check the host or domain first if(pTempCookie->bIsDomain) { if (!DoesDomainMatch(*pTempCookie->pHost, cHostCopy)) { continue; } } else if(strcasecmp((const char*)*(pTempCookie->pHost), pHost)) { // hostname matchup failed. continue; } // shorter strings always come last so there can be no // ambiquity if(pTempCookie->pPath && !strncmp(pPath, (const char*)*(pTempCookie->pPath), pTempCookie->pPath->GetLength())) { // check for expired cookies if(pTempCookie->expires && (pTempCookie->expires < cur_time)) { m_bSaveCookies = TRUE; continue; } if (!pCookiesFound1) { pCookiesFound1 = new CHXSimpleList(); pCookiesFound1->AddTail(pTempCookie); } else if (!WasCookieAdded(pCookiesFound1, pTempCookie)) { pCookiesFound1->AddTail(pTempCookie); } if(bIsPlayerCookieList) { if(!strPlayerCookies.IsEmpty()) { strPlayerCookies += "|"; } strPlayerCookies += *pTempCookie->pCookieName;#ifdef REPORT_COOKIES strCookieReport += (strCookieReport.IsEmpty() ? "" : ", "); strCookieReport += *pTempCookie->pCookieName; strCookieReport += "="; strCookieReport += *pTempCookie->pCookieValue;#endif } } } }#ifdef _WINDOWS if (!_pInternetGetCookie || m_bMemoryOnly) { goto cleanup; } host_length = strlen(pHost); if (pPath) { path_length = strlen(pPath); } pURL = new char[host_length + path_length + 8]; sprintf(pURL, "http://%s%s", pHost, pPath); /* Flawfinder: ignore */ if (_pInternetGetCookie(pURL, NULL, pData, &dwSize) && !pData && dwSize) { pData = new char[dwSize+1]; if (!_pInternetGetCookie(pURL, NULL, pData, &dwSize)) { goto cleanup; } cp = pData; while (pComma = (char*) ::HXFindChar(cp, ';')) { *pComma = '\0'; pComma++; if (pEqualSign = (char*) ::HXFindChar(cp, '=')) { *pEqualSign = '\0'; pEqualSign++; pNewCookie = new CookieStruct; bAdded = FALSE; // copy pNewCookie->pCookieValue = new CHXString(pEqualSign); pNewCookie->pCookieName = new CHXString(cp); pNewCookie->pPath = new CHXString(pPath); pNewCookie->pHost = new CHXString(pHost); pNewCookie->expires = 0; pNewCookie->bIsDomain = FALSE; pNewCookie->bMemoryOnly = FALSE; if(pNewCookie->pCookieName) { pNewCookie->pCookieName->TrimLeft(); pNewCookie->pCookieName->TrimRight(); } if (!WasCookieAdded(pCookiesFound1, pNewCookie)) { if (!pCookiesFound2) { pCookiesFound2 = new CHXSimpleList(); pCookiesFound2->AddTail(pNewCookie); bAdded = TRUE; } else if (!WasCookieAdded(pCookiesFound2, pNewCookie)) { pCookiesFound2->AddTail(pNewCookie); bAdded = TRUE; } } if (!bAdded) { HX_DELETE(pNewCookie); } } cp = pComma; } if (pEqualSign = (char*) ::HXFindChar(cp, '=')) { *pEqualSign = '\0'; pEqualSign++; pNewCookie = new CookieStruct; bAdded = FALSE; // copy pNewCookie->pCookieValue = new CHXString(pEqualSign); pNewCookie->pCookieName = new CHXString(cp); pNewCookie->pPath = NULL; pNewCookie->pHost = NULL; pNewCookie->expires = 0; pNewCookie->bIsDomain = FALSE; pNewCookie->bMemoryOnly = FALSE; if(pNewCookie->pCookieName) { pNewCookie->pCookieName->TrimLeft(); pNewCookie->pCookieName->TrimRight(); } if (!WasCookieAdded(pCookiesFound1, pNewCookie)) { if (!pCookiesFound2) { pCookiesFound2 = new CHXSimpleList(); pCookiesFound2->AddTail(pNewCookie); bAdded = TRUE; } else if (!WasCookieAdded(pCookiesFound2, pNewCookie)) { pCookiesFound2->AddTail(pNewCookie); bAdded = TRUE; } } if (!bAdded) { HX_DELETE(pNewCookie); } } }#endif /* _WINDOWS */cleanup: if (pCookiesFound1) { for (i = pCookiesFound1->Begin(); i != pCookiesFound1->End(); ++i) { pTempCookie = (CookieStruct*) (*i); if(pTempCookie->pCookieName && pTempCookie->pCookieValue) { ::SaveStringToHeader(pValues, (const char*)*(pTempCookie->pCookieName), (char*)(const char*)*(pTempCookie->pCookieValue)); } } } if (pCookiesFound2) { for (i = pCookiesFound2->Begin(); i != pCookiesFound2->End(); ++i) { pTempCookie = (CookieStruct*) (*i); if(pTempCookie->pCookieName && pTempCookie->pCookieValue) { ::SaveStringToHeader(pValues, (const char*)*(pTempCookie->pCookieName), (char*)(const char*)*(pTempCookie->pCookieValue)); } HX_DELETE(pTempCookie); } } if (m_pCookiesHelper) { hr = m_pCookiesHelper->UnPack(pValues, pCookies); }#ifdef REPORT_COOKIES // if the preference "CookieData" is set to "console" then write the cookie requests to the console IHXBuffer* pBuff = NULL; if (m_pPreferences && m_pPreferences->ReadPref("CookieData", pBuff) == HXR_OK && 0 == stricmp((const char*)pBuff->GetBuffer(), "console")) { CHXString str; str.Format("Cookies requested, getting host: %s path: %s cookies: %s", pHost, pPath, strCookieReport.IsEmpty() ? "<no cookies>" : (const char *) strCookieReport);#if defined(_CARBON) || defined(_MAC_MACHO) CFStringRef stringRef = ::CFStringCreateWithCString( kCFAllocatorDefault, (const char*) str, kCFStringEncodingUTF8 ); ::CFShow(stringRef); ::CFRelease(stringRef);#endif } HX_RELEASE(pBuff);#endif if(!strPlayerCookies.IsEmpty()) { pPlayerCookies = new CHXBuffer; pPlayerCookies->AddRef(); pPlayerCookies->Set((const UCHAR*)(const char*)strPlayerCookies, strPlayerCookies.GetLength() + 1); } HX_DELETE(pCookiesFound1); HX_DELETE(pCookiesFound2); HX_VECTOR_DELETE(pData); HX_VECTOR_DELETE(pURL); HX_RELEASE(pValues); return hr;}HX_RESULT HXCookies::PrepareCookiesPath(void){ HX_RESULT hr = HXR_OK;#ifndef _VXWORKS const char* pRMCookiesPath = NULL; IHXBuffer* pBuffer = NULL;#if !defined(_CARBON) && !defined(_MAC_MACHO) /* figured out every time at runtime on the Mac since paths are not stable between runs */ if (m_pPreferences && (m_pPreferences->ReadPref("CookiesPath", pBuffer) == HXR_OK)) { m_pRMCookiesPath = new char[pBuffer->GetSize() + 1]; strcpy(m_pRMCookiesPath, (const char*)pBuffer->GetBuffer()); /* Flawfinder: ignore */ } HX_RELEASE(pBuffer);#endif if( !m_pRMCookiesPath ) { if (m_pPreferences && (m_pPreferences->ReadPref("UserSDKDataPath", pBuffer) == HXR_OK)) { pRMCookiesPath = (char*) pBuffer->GetBuffer(); } else {#if defined(_CARBON) || defined(_MAC_MACHO) CHXString strSDKPath = CHXFileSpecUtils::GetAppDataDir(HXVER_SDK_PRODUCT).GetPathName();#elif defined(_UNIX) pRMCookiesPath = getenv("HOME"); HX_ASSERT( pRMCookiesPath ); #else pRMCookiesPath = GetDLLAccessPath()->GetPath(DLLTYPE_COMMON);#endif } if( pRMCookiesPath ) { m_pRMCookiesPath = new char[strlen(pRMCookiesPath) + strlen(RM_COOKIE_FILE_NAME)+2]; ::strcpy(m_pRMCookiesPath, pRMCookiesPath); /* Flawfinder: ignore */ if (m_pRMCookiesPath[::strlen(m_pRMCookiesPath)-1] != OS_SEPARATOR_CHAR) { strcat(m_pRMCookiesPath, OS_SEPARATOR_STRING); /* Flawfinder: ignore */ } strcat(m_pRMCookiesPath, RM_COOKIE_FILE_NAME); /* Flawfinder: ignore */ HX_RELEASE(pBuffer); pBuffer = new CHXBuffer(); pBuffer->AddRef(); pBuffer->Set((const unsigned char*)m_pRMCookiesPath, strlen(m_pRMCookiesPath) + 1); if (m_pPreferences) { m_pPreferences->WritePref("CookiesPath", pBuffer); } HX_RELEASE(pBuffer); } }#endif#ifdef _WINDOWS HKEY hMainAppKey = NULL; HKEY hBiffKey = NULL; HKEY hUsersKey = NULL; HKEY hServersKey = NULL; HKEY hCookiesKey = NULL; INT32 lSize = _MAX_PATH - 1; UINT32 ulSize = _MAX_PATH - 1; UINT32 ulType = 0; char regKey[_MAX_PATH] = {0}; /* Flawfinder: ignore */ char* pPath = NULL; char* pUser = NULL; char* pValue = NULL; char* pCursor = NULL; // Netscape Browser 4.0+ if (RegOpenKeyEx(HKEY_CURRENT_USER, OS_STRING("Software\\Netscape\\Netscape Navigator\\biff"), 0, KEY_READ, &hBiffKey) == ERROR_SUCCESS) { pUser = new char[_MAX_PATH]; memset(pUser, 0, _MAX_PATH); if (RegQueryValueEx(hBiffKey, OS_STRING("CurrentUser"), 0, &ulType, (LPBYTE)pUser, &ulSize) == ERROR_SUCCESS) { SafeSprintf(regKey, _MAX_PATH, "Software\\Netscape\\Netscape Navigator\\biff\\users\\%s", pUser); if (RegOpenKeyEx(HKEY_CURRENT_USER, OS_STRING(regKey), 0, KEY_READ, &hUsersKey) == ERROR_SUCCESS) { pValue = new char[_MAX_PATH]; memset(pValue, 0, _MAX_PATH); ulSize = _MAX_PATH - 1; if (RegQueryValueEx(hUsersKey, OS_STRING("defaultServer"), 0, &ulType, (LPBYTE)pValue, &ulSize) == ERROR_SUCCESS) { SafeSprintf(regKey, _MAX_PATH, "Software\\Netscape\\Netscape Navigator\\biff\\users\\%s\\servers\\%s", pUser, pValue); if (RegOpenKeyEx(HKEY_CURRENT_USER, OS_STRING(regKey), 0, KEY_READ, &hServersKey) == ERROR_SUCCESS) { pPath = new char[_MAX_PATH]; memset(pPath, 0, _MAX_PATH); ulSize = _MAX_PATH - 1; if (RegQueryValueEx(hServersKey, OS_STRING("popstatePath"), 0, &ulType, (LPBYTE)pPath, &ulSize) == ERROR_SUCCESS) { // pPath ="..\mail\popstate.dat" // the actual location of cookies is 1 level up if (pCursor = ::HXReverseFindChar(pPath, '\\')) { *pCursor = '\0'; if (pCursor = ::HXReverseFindChar(pPath, '\\')) { *pCursor = '\0'; m_pNSCookiesPath = new char[strlen(pPath) + 13]; sprintf(m_pNSCookiesPath, "%s\\%s", pPath, RM_COOKIE_FILE_NAME); /* Flawfinder: ignore */ goto cleanup; } } } } } } } } // Netscape Browser 3.0+ if (RegOpenKeyEx(HKEY_CURRENT_USER, OS_STRING("Software\\Netscape\\Netscape Navigator\\Cookies"), 0, KEY_READ, &hCookiesKey) == ERROR_SUCCESS) { HX_VECTOR_DELETE(pPath); pPath = new char[_MAX_PATH]; memset(pPath, 0, _MAX_PATH); ulSize = _MAX_PATH - 1; if (RegQueryValueEx(hCookiesKey, OS_STRING("Cookie File"), 0, &ulType, (LPBYTE)pPath, &ulSize) == ERROR_SUCCESS) { m_pNSCookiesPath = new char[strlen(pPath) + 1]; strcpy(m_pNSCookiesPath, pPath); /* Flawfinder: ignore */ } }cleanup: HX_RELEASE(pBuffer); if (hMainAppKey) { RegCloseKey(hMainAppKey); } if (hBiffKey) { RegCloseKey(hBiffKey); } if (hUsersKey) { RegCloseKey(hUsersKey); } if (hServersKey) { RegCloseKey(hServersKey); } if (hCookiesKey) { RegCloseKey(hCookiesKey); } HX_VECTOR_DELETE(pUser); HX_VECTOR_DELETE(pPath); HX_VECTOR_DELETE(pValue); #endif /* _WINDOWS */ return hr;}voidHXCookies::ResetCookies(CHXSimpleList* pCookieList){ CookieStruct* pCookie = NULL; while (pCookieList && pCookieList->GetCount() > 0) { pCookie = (CookieStruct*) pCookieList->RemoveHead(); HX_DELETE(pCookie); }}HX_RESULT HXCookies::FileReadLine(FILE* fp, char* pLine, UINT32 ulLineBuf, UINT32* pBytesRead){#ifdef _OPENWAVE return HXR_NOTIMPL;#else HX_RESULT hr = HXR_OK; UINT32 i = 0; UINT32 ulBytesRead = 0; char* pTmpBuf = NULL; if (!fp) { hr = HXR_FAILED; goto cleanup; } if (ulLineBuf < 1) { *pBytesRead = 0; goto cleanup; } ulBytesRead = fread(pLine, sizeof(char), ulLineBuf, fp); pTmpBuf = pLine; if (ulBytesRead)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -