00001
00002
00003
00004
00005
00006 #include "stdafx.h"
00007 #include "vmsIETmpCookies.h"
00008 #include <inetfile/inetfile.h>
00009
00010 vmsIETmpCookies::vmsIETmpCookies()
00011 {
00012 m_mxFile = CreateMutex (NULL, FALSE, "vmsMX::fdmietmpcookiesfileops");
00013 }
00014
00015 vmsIETmpCookies::~vmsIETmpCookies()
00016 {
00017 CloseHandle (m_mxFile);
00018 }
00019
00020 int vmsIETmpCookies::Find(LPCSTR pszUrl)
00021 {
00022 fsURL url;
00023 if (IR_SUCCESS != url.Crack (pszUrl))
00024 return -1;
00025
00026 GetListOfKnownCookies ();
00027
00028
00029 fs::list <int> vFit;
00030
00031 for (int i = 0; i < m_vBeforeNavUrls.size (); i++)
00032 {
00033 if (m_vBeforeNavUrls [i] == pszUrl)
00034 {
00035
00036
00037 vFit.add (i);
00038 break;
00039 }
00040 }
00041
00042 for (int bEx = 0; bEx < 2 && vFit.size () == 0; bEx++)
00043 {
00044
00045
00046 for (int i = 0; i < m_vUrls.size (); i++)
00047 {
00048 fsURL url2;
00049 if (IR_SUCCESS != url2.Crack (m_vUrls [i]))
00050 continue;
00051
00052 if (fsIsServersEqual (url.GetHostName (), url2.GetHostName (), bEx))
00053 vFit.add (i);
00054 }
00055 }
00056
00057 if (vFit.size () == 0)
00058 {
00059
00060 fsString str1 = GetLevel2DomainName (url.GetHostName ());
00061
00062 for (int i = 0; i < m_vUrls.size (); i++)
00063 {
00064 fsURL url2;
00065 if (IR_SUCCESS != url2.Crack (m_vUrls [i]))
00066 continue;
00067
00068 fsString str2 = GetLevel2DomainName (url2.GetHostName ());
00069
00070 if (str1 == str2)
00071 vFit.add (i);
00072 }
00073 }
00074
00075 int nIndex = -1;
00076 int len = -1;
00077
00078 for (i = 0; i < vFit.size (); i++)
00079 {
00080 int n = vFit [i];
00081 int l = lstrlen (get_Cookies (n));
00082 if (l > len)
00083 {
00084 nIndex = n;
00085 len = l;
00086 }
00087 }
00088
00089 return nIndex;
00090 }
00091
00092 void vmsIETmpCookies::GetListOfKnownCookies()
00093 {
00094 m_vUrls.clear ();
00095 m_vCookies.clear ();
00096
00097 WaitForSingleObject (m_mxFile, INFINITE);
00098
00099
00100 char szTmpPath [MAX_PATH];
00101 GetTempPath (MAX_PATH, szTmpPath);
00102 lstrcat (szTmpPath, "Free Download Manager\\");
00103 char sz [MAX_PATH];
00104 lstrcpy (sz, szTmpPath);
00105 lstrcat (sz, "tic*.tmp");
00106
00107 WIN32_FIND_DATA wfd;
00108
00109 HANDLE hFind = FindFirstFile (sz, &wfd);
00110
00111 if (hFind) do
00112 {
00113 lstrcpy (sz, szTmpPath);
00114 lstrcat (sz, wfd.cFileName);
00115 ProcessFile (sz);
00116 }
00117 while (FindNextFile (hFind, &wfd));
00118
00119 if (hFind)
00120 FindClose (hFind);
00121
00122 ReleaseMutex (m_mxFile);
00123 }
00124
00125 void vmsIETmpCookies::ProcessFile(LPCSTR pszFile)
00126 {
00127 static BOOL _bIsWin9x = GetVersion () & 0x80000000;
00128
00129 DWORD dwShareMode = FILE_SHARE_READ|FILE_SHARE_WRITE;
00130 if (_bIsWin9x == FALSE)
00131 dwShareMode |= FILE_SHARE_DELETE;
00132
00133 HANDLE hFile = CreateFile (pszFile, GENERIC_READ,
00134 dwShareMode, NULL, OPEN_EXISTING, 0, NULL);
00135 if (hFile == INVALID_HANDLE_VALUE)
00136 return;
00137
00138 DWORD dwLen = GetFileSize (hFile, NULL), dw = 0;
00139 LPSTR psz = new char [dwLen + 1];
00140 ReadFile (hFile, psz, dwLen, &dw, NULL);
00141 psz [dw] = 0;
00142
00143 fsString strUrl, strCookies, strPostData, strBeforeNavUrl;
00144
00145 LPCSTR psz2 = psz;
00146 while (*psz2 && *psz2 != '\r')
00147 strUrl += *psz2++;
00148
00149 if (*psz2 != '\r')
00150 return;
00151 psz2 += 2;
00152 while (*psz2 && *psz2 != '\r')
00153 strCookies += *psz2++;
00154
00155 if (*psz2 != '\r')
00156 return;
00157 psz2 += 2;
00158 while (*psz2 && *psz2 != '\r')
00159 strPostData += *psz2++;
00160
00161 if (*psz2 != '\r')
00162 return;
00163 psz2 += 2;
00164 strBeforeNavUrl = psz2;
00165
00166 delete [] psz;
00167
00168 m_vUrls.add (strUrl);
00169 m_vCookies.add (strCookies);
00170 m_vPostDatas.add (strPostData);
00171 m_vBeforeNavUrls.add (strBeforeNavUrl);
00172
00173 CloseHandle (hFile);
00174 }
00175
00176 LPCSTR vmsIETmpCookies::get_Cookies(int nIndex)
00177 {
00178 return m_vCookies [nIndex];
00179 }
00180
00181 LPCSTR vmsIETmpCookies::get_Referer(int nIndex)
00182 {
00183 return m_vUrls [nIndex];
00184 }
00185
00186 LPCSTR vmsIETmpCookies::get_PostData(int nIndex)
00187 {
00188 return m_vPostDatas [nIndex];
00189 }
00190
00191 fsString vmsIETmpCookies::GetLevel2DomainName(LPCSTR pszHostName)
00192 {
00193 LPCSTR psz2 = strrchr (pszHostName, '.');
00194 if (psz2 == NULL)
00195 psz2 = pszHostName;
00196 else
00197 psz2--;
00198 LPCSTR psz3 = psz2;
00199 while (pszHostName != psz2 && *psz2 != '.')
00200 psz2--;
00201 if (*psz2 == '.')
00202 psz2++;
00203 fsString str = psz2;
00204 str [psz3 - psz2 + 1] = 0;
00205 return str;
00206 }