[Common-dev] CR-Client: Cookies support for Firefox
Jeff Leitner jeffl at real.comBob, Typically the shell functions are used to get the standard paths in windows. For example: SHGetSpecialFolderPath (This function works all the way back to Win95 w/IE4) which is sufficient for the OSes we support. You can also look into SHGetFolderPath for a newer function. I wouldn't recommend relying on the environment variable to be set. jeff At 08:55 AM 8/10/2005, Bob Clark wrote: >Synopsis: > Add support for Firefox cookies in HXCookies. Also allow for > context-less use of HXCookies. > >Overview: > HXCookies currently supports reading Netscape cookie files. The file > format of cookies.txt is identical for Firefox, but the location of the > cookies file is different on Firefox. This change add support for Firefox > cookies by adding the ability to locate Firefox's profile directories. > (This is documented at http://www.mozilla.org/support/firefox/profile.) > Although that accounts for the bulk of the diff, there are also a few > checks for a valid m_pContext, which allows for context-less use of > HXCookies, which I found useful for dropping in HXCookies as a utility > class for reading IE/Netscape/Firefox cookies. > >Files Modified: > common/util/pub/cookies.h > common/util/cookies.cpp > >Branches: > head > 150cay > >--Bob > >Index: pub/cookies.h >=================================================================== >RCS file: /cvsroot/common/util/pub/cookies.h,v >retrieving revision 1.3 >diff -u -w -r1.3 cookies.h >--- pub/cookies.h 9 Jul 2004 18:23:37 -0000 1.3 >+++ pub/cookies.h 10 Aug 2005 15:48:21 -0000 >@@ -99,10 +99,12 @@ > BOOL m_bSaveCookies; > BOOL m_bMemoryOnly; > char* m_pNSCookiesPath; >+ char* m_pFFCookiesPath; > char* m_pRMCookiesPath; > time_t m_lastModification; > > CHXSimpleList* m_pNSCookies; >+ CHXSimpleList* m_pFFCookies; > CHXSimpleList* m_pRMCookies; > > IHXPreferences* m_pPreferences; > >Index: cookies.cpp >=================================================================== >RCS file: /cvsroot/common/util/cookies.cpp,v >retrieving revision 1.25.6.6 >diff -u -w -r1.25.6.6 cookies.cpp >--- cookies.cpp 17 May 2005 22:32:38 -0000 1.25.6.6 >+++ cookies.cpp 10 Aug 2005 15:48:21 -0000 >@@ -141,9 +141,11 @@ > , m_bSaveCookies(FALSE) > , m_bMemoryOnly(bMemoryOnly) > , m_pNSCookiesPath(NULL) >+ , m_pFFCookiesPath(NULL) > , m_pRMCookiesPath(NULL) > , m_lastModification(0) > , m_pNSCookies(NULL) >+ , m_pFFCookies(NULL) > , m_pRMCookies(NULL) > , m_pPreferences(NULL) > , m_pCookiesHelper(NULL) >@@ -228,13 +230,16 @@ > { > HX_RESULT hr = HXR_OK; > >- if (!m_pContext || !IsCookieEnabled()) >+ if (!IsCookieEnabled()) > { > hr = HXR_FAILED; > goto cleanup; > } > >+ if (m_pContext) >+ { > m_pContext->QueryInterface(IID_IHXCookiesHelper, (void**) > &m_pCookiesHelper); >+ } > if (!m_pCookiesHelper) > { > HXCookiesHelper* pHelper = new HXCookiesHelper(m_pContext); >@@ -261,6 +266,11 @@ > OpenCookies(m_pNSCookiesPath, FALSE, m_pNSCookies); > } > >+ if (m_pFFCookiesPath) >+ { >+ OpenCookies(m_pFFCookiesPath, TRUE, m_pFFCookies); >+ } >+ > if (m_pRMCookiesPath) > { > OpenCookies(m_pRMCookiesPath, TRUE, m_pRMCookies); >@@ -284,12 +294,15 @@ > } > > ResetCookies(m_pNSCookies); >+ ResetCookies(m_pFFCookies); > ResetCookies(m_pRMCookies); > > HX_DELETE(m_pNSCookies); >+ HX_DELETE(m_pFFCookies); > HX_DELETE(m_pRMCookies); > > HX_VECTOR_DELETE(m_pNSCookiesPath); >+ HX_VECTOR_DELETE(m_pFFCookiesPath); > HX_VECTOR_DELETE(m_pRMCookiesPath); > > HX_RELEASE(m_pPreferences); >@@ -626,8 +639,8 @@ > } > cHostCopy.MakeLower(); > >- // search for all cookies(Netscape only for now) >- for (l = 0; l < 2; l++) >+ // search for all cookies(Netscape/Firefox only for now) >+ for (l = 0; l < 3; l++) > { > switch (l) > { >@@ -639,6 +652,10 @@ > pCookiesList = m_pNSCookies; > bIsPlayerCookieList = FALSE; > break; >+ case 2: >+ pCookiesList = m_pFFCookies; >+ bIsPlayerCookieList = FALSE; >+ break; > default: > break; > } >@@ -1093,6 +1110,121 @@ > HX_VECTOR_DELETE(pValue); > #endif /* _WINDOWS */ > >+#ifdef _WINDOWS >+ // Grabbing Firefox Cookies Path. See >http://www.mozilla.org/support/firefox/profile >+ // for information on finding the path to profiles.ini on various >platforms. >+ >+ // first find the profiles.ini file: >+ // on Windows XP/2000, %AppData%\Mozilla\Firefox\ >+ // on Windows 95/98/Me, C:\WINDOWS\Application Data\Mozilla\Firefox\ >+ // on Linux, ~/.mozilla/firefox >+ // on Mac OS X, ~/Library/Application Support/Firefox/ >+ >+ char* pAppDataPath = NULL; >+ char* pFFProfilesDirPath = NULL; >+ char* pFFProfilesIniPath = NULL; >+ >+ pAppDataPath = new char[_MAX_PATH]; >+ >+ if (!pAppDataPath) >+ { >+ hr = HXR_OUTOFMEMORY; >+ goto ffcleanup; >+ } >+ >+ DWORD datapathSize = _MAX_PATH - 1; >+ DWORD bytesRead = ::GetEnvironmentVariable("APPDATA", pAppDataPath, >datapathSize); >+ >+ if (bytesRead) >+ { >+ // hmmm, this means that the env var exists! What if it doesn't? >+ >+ pFFProfilesDirPath = new char[_MAX_PATH]; >+ if (!pFFProfilesDirPath) >+ { >+ hr = HXR_OUTOFMEMORY; >+ goto ffcleanup; >+ } >+ >+ pFFProfilesIniPath = new char[_MAX_PATH]; >+ if (!pFFProfilesIniPath) >+ { >+ hr = HXR_OUTOFMEMORY; >+ goto ffcleanup; >+ } >+ >+ SafeSprintf(pFFProfilesDirPath, _MAX_PATH, >"%s\\Mozilla\\Firefox\\", pAppDataPath); >+ SafeSprintf(pFFProfilesIniPath, _MAX_PATH, "%sprofiles.ini", >pFFProfilesDirPath); >+ } >+ >+ if (!pFFProfilesIniPath) >+ { >+ // the environment variable didn't exist; we're likely on Windows >95/98/ME, so >+ // try the 95/98/ME path. >+ >+ pFFProfilesIniPath = new char[_MAX_PATH]; >+ SafeSprintf(pFFProfilesIniPath, _MAX_PATH, >"C:\\WINDOWS\\Application Data\\Mozilla\\Firefox\\profiles.ini"); >+ } >+ >+ if (pFFProfilesIniPath) >+ { >+ // Once we've found profiles.ini, iterate through >[profile0]-[profileN] looking for >+ // the default profile. This will include the path to the profile >directory, which >+ // will include a file cookies.txt. >+ char szIniSection[20]; >+ char szProfilePath[_MAX_PATH]; >+ int nCurProfile = 0; >+ do >+ { >+ DWORD profilepathSize = _MAX_PATH - 1; >+ SafeSprintf(szIniSection, 20, "profile%d", nCurProfile); >+ bytesRead = ::GetPrivateProfileString(szIniSection, "Path", >NULL, szProfilePath, _MAX_PATH - 1, pFFProfilesIniPath); >+ if (bytesRead) >+ { >+ // see (1) whether this is the default and (2) whether >it's a relative or absolute path >+ char defaultBuf[256]; >+ DWORD appBytesRead = >::GetPrivateProfileString(szIniSection, "Default", NULL, defaultBuf, 256, >pFFProfilesIniPath); >+ >+ if (appBytesRead) >+ { >+ if (!strcmp(defaultBuf, "1")) >+ { >+ // a ha! this is the default! >+ char relativeBuf[256]; >+ HXBOOL bIsRelativePath = FALSE; >+ DWORD relBytesRead = >::GetPrivateProfileString(szIniSection, "IsRelative", NULL, relativeBuf, >256, pFFProfilesIniPath); >+ if (relBytesRead && !strcmp(relativeBuf, "1")) >+ { >+ bIsRelativePath = TRUE; >+ } >+ >+ char szCookiePath[_MAX_PATH]; >+ >+ if (bIsRelativePath) >+ { >+ SafeSprintf(szCookiePath, _MAX_PATH, >"%s%s\\cookies.txt", pFFProfilesDirPath, szProfilePath); >+ } >+ else >+ { >+ SafeSprintf(szCookiePath, _MAX_PATH, >"%s\\cookies.txt", szProfilePath); >+ } >+ >+ m_pFFCookiesPath = new char[strlen(szCookiePath)+1]; >+ strcpy(m_pFFCookiesPath, szCookiePath); /* >Flawfinder: ignore */ >+ } >+ } >+ } >+ nCurProfile++; >+ } while (bytesRead); >+ } >+ >+ffcleanup: >+ HX_VECTOR_DELETE(pAppDataPath); >+ HX_VECTOR_DELETE(pFFProfilesIniPath); >+ HX_VECTOR_DELETE(pFFProfilesDirPath); >+ >+#endif >+ > return hr; > } > >@@ -1721,7 +1853,7 @@ > BOOL bResult = TRUE; > IHXBuffer* pBuffer = NULL; > >- if (!m_pPreferences) >+ if (!m_pPreferences && m_pContext) > { > if (HXR_OK != m_pContext->QueryInterface(IID_IHXPreferences, > (void**)&m_pPreferences)) > { >@@ -1907,7 +2039,7 @@ > CookieStruct* pCookie = NULL; > CHXSimpleList::Iterator i; > >- for (int l = 0; l < 2; l++) >+ for (int l = 0; l < 3; l++) > { > switch (l) > { >@@ -1915,6 +2047,9 @@ > pCookies = m_pNSCookies; > break; > case 1: >+ pCookies = m_pFFCookies; >+ break; >+ case 2: > pCookies = m_pRMCookies; > break; > default: >@@ -2017,8 +2152,8 @@ > } > cHostCopy.MakeLower(); > >- // search for all cookies(Netscape only for now) >- for (l = 0; l < 2; l++) >+ // search for all cookies(Netscape/Firefox only for now) >+ for (l = 0; l < 3; l++) > { > switch (l) > { >@@ -2028,6 +2163,9 @@ > case 1: > pCookiesList = m_pNSCookies; > break; >+ case 2: >+ pCookiesList = m_pFFCookies; >+ break; > default: > break; > } > > > >_______________________________________________ >Common-dev mailing list >Common-dev at helixcommunity.org >http://lists.helixcommunity.org/mailman/listinfo/common-dev -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.helixcommunity.org/pipermail/common-dev/attachments/20050810/4e7d5927/attachment-0001.html