00001
00002
00003
00004
00005
00006 #include "stdafx.h"
00007 #include "FdmApp.h"
00008 #include "fsInternetFileListMgr.h"
00009 #include "inetutil.h"
00010 #include "LoginDlg.h"
00011 #include "fsSitesMgr.h"
00012
00013 #ifdef _DEBUG
00014 #undef THIS_FILE
00015 static char THIS_FILE[]=__FILE__;
00016 #define new DEBUG_NEW
00017 #endif
00018
00019 fsInternetFileListMgr::fsInternetFileListMgr()
00020 {
00021 m_bThread = FALSE;
00022 m_pfnEvents = NULL;
00023 m_files = NULL;
00024 m_bConnected = FALSE;
00025 m_bAbort = FALSE;
00026 ReadSettings ();
00027 }
00028
00029 fsInternetFileListMgr::~fsInternetFileListMgr()
00030 {
00031 Free ();
00032 }
00033
00034 fsInternetResult fsInternetFileListMgr::GetList(LPCSTR pszUrl, LPCSTR pszUser, LPCSTR pszPassword)
00035 {
00036 if (IsRunning ())
00037 return IR_S_FALSE;
00038
00039 if (pszUser)
00040 {
00041
00042 if (m_strUser != pszUser || m_strPassword != pszPassword)
00043 {
00044
00045 Free ();
00046 m_bConnected = FALSE;
00047 }
00048 }
00049
00050 if (m_bConnected == FALSE && m_server.IsFtpServer ())
00051 Free ();
00052
00053 int iIndex = FindFiles (pszUrl);
00054 if (iIndex == -1)
00055 {
00056 m_bAbort = FALSE;
00057 _strUrl = pszUrl;
00058 _strUser = pszUser;
00059 _strPassword = pszPassword;
00060 Start ();
00061 }
00062 else
00063 {
00064 m_files = m_vFiles [iIndex];
00065 m_bCurPathIsRoot = strcmp (GetCurrentPath (), "/") == 0 || strcmp (GetCurrentPath (), "\\") == 0;
00066 m_lastError = IR_SUCCESS;
00067 m_bConnected = TRUE;
00068 Event (FLME_DONE_FROM_CACHE);
00069 }
00070
00071 return IR_SUCCESS;
00072 }
00073
00074 void fsInternetFileListMgr::Stop(BOOL bWaitStop)
00075 {
00076 m_bAbort = TRUE;
00077
00078 try {
00079 if (m_files)
00080 m_files->Abort ();
00081 }catch (...){}
00082
00083 try {m_server.CloseHandle ();} catch (...) {}
00084
00085 try {m_session.CloseHandle ();} catch (...) {}
00086
00087 m_bConnected = FALSE;
00088
00089 while (m_bThread && bWaitStop)
00090 Sleep (5);
00091 }
00092
00093 void fsInternetFileListMgr::Start()
00094 {
00095 DWORD dw;
00096 m_bThread = TRUE;
00097 CloseHandle (CreateThread (NULL, 0, _threadGetList, this, 0, &dw));
00098 }
00099
00100 DWORD WINAPI fsInternetFileListMgr::_threadGetList(LPVOID lp)
00101 {
00102 fsInternetFileListMgr* pThis = (fsInternetFileListMgr*) lp;
00103
00104 CString strProxy, strPUser, strPPassword;
00105 fsURL url;
00106
00107 if (url.Crack (pThis->_strUrl) != IR_SUCCESS)
00108 {
00109 pThis->m_lastError = IR_BADURL;
00110 goto _lExit;
00111 }
00112
00113 if (*url.GetHostName () == 0 || *url.GetPath () == 0)
00114 {
00115 pThis->m_lastError = IR_BADURL;
00116 goto _lExit;
00117 }
00118
00119 if (*url.GetUserName ())
00120 {
00121 if (pThis->m_strUser != url.GetUserName ())
00122 {
00123 pThis->m_strUser = url.GetUserName ();
00124 pThis->m_strPassword = url.GetPassword ();
00125 pThis->Free (FALSE);
00126 pThis->m_bConnected = FALSE;
00127 }
00128 }
00129 else
00130 {
00131 if (pThis->m_strUser != pThis->_strUser)
00132 {
00133 if (pThis->_strUser != NULL || fsIsServersEqual (url.GetHostName (), pThis->m_server.GetServerName ()) == FALSE)
00134 {
00135 pThis->m_strUser = pThis->_strUser;
00136 pThis->m_strPassword = pThis->_strPassword;
00137 pThis->m_bConnected = FALSE;
00138 }
00139 }
00140 }
00141
00142 pThis->m_bCurPathIsRoot = strcmp (url.GetPath (), "/") == 0 || strcmp (url.GetPath (), "\\") == 0;
00143
00144 _lConnect:
00145
00146 if (pThis->m_bConnected == FALSE ||
00147 FALSE == fsIsServersEqual (pThis->m_server.GetServerName (), url.GetHostName ()) ||
00148 pThis->m_server.GetScheme () != url.GetInternetScheme () )
00149 {
00150 pThis->Free (FALSE);
00151
00152 fsGetProxy (fsSchemeToNP (url.GetInternetScheme ()), strProxy, strPUser, strPPassword);
00153
00154 pThis->m_session.Create (_App.Agent (), _App.InternetAccessType (), strProxy,
00155 fsSchemeToNP (url.GetInternetScheme ()));
00156 pThis->m_session.SetProxyAuth (strPUser, strPPassword);
00157 pThis->m_session.SetTimeout (_App.Timeout ());
00158 pThis->m_server.Initialize (&pThis->m_session);
00159 pThis->m_server.UseFtpPassiveMode (pThis->m_bFtpPassiveMode);
00160
00161 UINT cAttempts = _App.MaxAttempts ();
00162
00163 do
00164 {
00165 pThis->Event (FLME_CONNECTING);
00166
00167 pThis->m_lastError = pThis->m_server.Connect (pThis->_strUrl, pThis->m_strUser, pThis->m_strPassword, url.GetPort ());
00168 if (pThis->m_lastError != IR_SUCCESS)
00169 {
00170 if (pThis->m_lastError != IR_S_FALSE)
00171 pThis->Event (FLME_ERROR);
00172 }
00173 else
00174 {
00175
00176 if (pThis->m_server.IsFtpServer ())
00177 pThis->Event (FLME_CONNECTED);
00178 }
00179
00180 if (pThis->m_lastError != IR_SUCCESS)
00181 {
00182 if (pThis->m_lastError == IR_LOGINFAILURE || pThis->m_lastError == IR_INVALIDUSERNAME ||
00183 pThis->m_lastError == IR_INVALIDPASSWORD)
00184 {
00185
00186
00187 fsSiteInfo *site = _SitesMgr.FindSite2 (url.GetHostName (), fsNPToSiteValidFor (fsSchemeToNP (url.GetInternetScheme ())));
00188 if (site && site->strUser)
00189 {
00190 CString strPass = site->strPassword ? site->strPassword : "";
00191 if (site->strUser != pThis->m_strUser || strPass != pThis->m_strPassword)
00192 {
00193 pThis->Event (FLME_TRYINGTOUSESITEMGRLOGIN);
00194 pThis->m_strUser = site->strUser;
00195 pThis->m_strPassword = strPass;
00196 continue;
00197 }
00198 }
00199
00200 if (pThis->AskForLogin (url.GetHostName ()))
00201 continue;
00202 else
00203 break;
00204 }
00205
00206 pThis->SleepInterval ();
00207 }
00208
00209 cAttempts--;
00210
00211 if (cAttempts == 0)
00212 break;
00213 }
00214 while (pThis->m_lastError != IR_SUCCESS && pThis->m_bAbort == FALSE);
00215
00216 if (pThis->m_lastError == IR_SUCCESS && pThis->m_bAbort == FALSE)
00217 {
00218 if (pThis->m_server.IsFtpServer ())
00219 {
00220 pThis->m_bConnected = TRUE;
00221 }
00222 }
00223 else
00224 {
00225 pThis->Event (FLME_STOPPED);
00226 goto _lExit2;
00227 }
00228 }
00229 else if (pThis->m_server.IsFtpServer () == FALSE)
00230 pThis->Event (FLME_CONNECTING);
00231
00232 UINT cAttempts;
00233 cAttempts = _App.MaxAttempts ();
00234
00235 do
00236 {
00237 if (pThis->m_server.IsFtpServer ())
00238 pThis->Event (FLME_GETTINGLIST);
00239
00240 fsnew1 (pThis->m_files, fsInternetURLFiles);
00241 pThis->m_files->SetHttpEventFunc (_HttpEvents, pThis);
00242 pThis->m_files->RetreiveInfoWhileGettingList (pThis->m_bRetreiveInfoWhileGettingList);
00243
00244 pThis->m_lastError = pThis->m_files->GetList (&pThis->m_server, url.GetPath ());
00245
00246 if (pThis->m_lastError == IR_S_REDIRECTED)
00247 pThis->m_lastError = IR_SUCCESS;
00248
00249 if (pThis->m_lastError != IR_SUCCESS && pThis->m_lastError != IR_S_REDIRECTED)
00250 {
00251 fsInternetURLFiles *files = pThis->m_files;
00252 pThis->m_files = NULL;
00253 delete files;
00254
00255 if (pThis->m_lastError != IR_S_FALSE)
00256 pThis->Event (FLME_ERROR);
00257
00258 if (pThis->m_bAbort)
00259 break;
00260
00261 switch (pThis->m_lastError)
00262 {
00263 case IR_CONNECTIONABORTED:
00264 case IR_LOSTCONNECTION:
00265 pThis->Event (FLME_CONNECTIONWASLOST_RESTORE);
00266 pThis->m_bConnected = FALSE;
00267 goto _lConnect;
00268 break;
00269
00270 case IR_LOGINFAILURE:
00271 case IR_INVALIDUSERNAME:
00272 case IR_INVALIDPASSWORD:
00273 if (pThis->AskForLogin (url.GetHostName ()))
00274 goto _lConnect;
00275 else
00276 {
00277 pThis->Event (FLME_STOPPED);
00278 goto _lExit2;
00279 }
00280 break;
00281 }
00282
00283 cAttempts--;
00284
00285 if (cAttempts == 0 || pThis->m_bAbort)
00286 break;
00287
00288 pThis->SleepInterval ();
00289
00290 if (pThis->m_bAbort == FALSE)
00291 pThis->Event (FLME_CONNECTING);
00292 }
00293 }
00294 while (pThis->m_lastError != IR_SUCCESS && pThis->m_bAbort == FALSE);
00295
00296 pThis->_strUrl = NULL;
00297
00298 if (pThis->m_lastError == IR_SUCCESS)
00299 pThis->m_vFiles.add (pThis->m_files);
00300 else
00301 {
00302 pThis->Event (FLME_STOPPED);
00303 goto _lExit2;
00304 }
00305
00306 _lExit:
00307
00308 if (pThis->m_lastError != IR_SUCCESS)
00309 {
00310 if (pThis->m_lastError != IR_S_FALSE)
00311 pThis->Event (FLME_ERROR);
00312 }
00313 else
00314 pThis->Event (FLME_DONE);
00315
00316 _lExit2:
00317
00318 pThis->m_bThread = FALSE;
00319
00320 return 0;
00321 }
00322
00323 BOOL fsInternetFileListMgr::IsRunning()
00324 {
00325 return m_bThread;
00326 }
00327
00328 void fsInternetFileListMgr::SetEventsFunc(fsFileListMgrFunc pfn, LPVOID lpParam)
00329 {
00330 m_pfnEvents = pfn;
00331 m_lpEventsParam = lpParam;
00332 }
00333
00334 void fsInternetFileListMgr::Event(fsInternetFileListMgrEvent ev)
00335 {
00336 if (m_pfnEvents)
00337 m_pfnEvents (this, ev, m_lpEventsParam);
00338 }
00339
00340 fsInternetResult fsInternetFileListMgr::GetLastError()
00341 {
00342 return m_lastError;
00343 }
00344
00345 UINT fsInternetFileListMgr::GetFileCount()
00346 {
00347 return m_files ? m_files->GetFileCount () : 0;
00348 }
00349
00350 fsFileInfo* fsInternetFileListMgr::GetFileInfo(UINT uIndex)
00351 {
00352 return m_files ? m_files->GetFileInfo (uIndex) : NULL;
00353 }
00354
00355 BOOL fsInternetFileListMgr::IsCurrentPathRoot()
00356 {
00357 return m_bCurPathIsRoot;
00358 }
00359
00360 LPCSTR fsInternetFileListMgr::GetLastErrorDesc()
00361 {
00362 return m_files ? m_files->GetLastError () : NULL;
00363 }
00364
00365 int fsInternetFileListMgr::FindFiles(LPCSTR pszFilesUrl)
00366 {
00367 fsURL url;
00368
00369 if (m_vFiles.size () == 0)
00370 return -1;
00371
00372 if (IR_SUCCESS != url.Crack (pszFilesUrl))
00373 return -1;
00374
00375 if (stricmp (url.GetHostName (), m_server.GetServerName ()) || url.GetPort () != m_server.GetServerPort () ||
00376 url.GetInternetScheme () != m_server.GetScheme () )
00377 return -1;
00378
00379 if (m_strUser.Length ())
00380 {
00381 if (strcmp (url.GetUserName (), m_strUser) || strcmp (url.GetPassword (), m_strPassword))
00382 return -1;
00383 }
00384
00385 for (int i = m_vFiles.size () - 1; i >= 0; i--)
00386 {
00387 if (strcmp (url.GetPath (), m_vFiles [i]->GetCurrentPath ()) == 0)
00388 return i;
00389 }
00390
00391 return -1;
00392 }
00393
00394 void fsInternetFileListMgr::Free(BOOL bStopBeforeFree)
00395 {
00396 if (bStopBeforeFree)
00397 Stop ();
00398
00399 m_files = NULL;
00400
00401 for (int i = m_vFiles.size () - 1; i >= 0; i--)
00402 delete m_vFiles [i];
00403 m_vFiles.clear ();
00404 }
00405
00406 LPCSTR fsInternetFileListMgr::GetCurrentPath()
00407 {
00408 return m_files ? m_files->GetCurrentPath () : NULL;
00409 }
00410
00411 fsInternetResult fsInternetFileListMgr::Refresh()
00412 {
00413 if (m_files == NULL || IsRunning ())
00414 return IR_S_FALSE;
00415
00416 char szUrl [10000];
00417
00418 GetCurrentUrl (szUrl, 10000);
00419
00420 Free ();
00421
00422 return GetList (szUrl, NULL, NULL);
00423 }
00424
00425 fsInternetResult fsInternetFileListMgr::GoParentFolder()
00426 {
00427 if (IsCurrentPathRoot () || m_files == NULL)
00428 return IR_S_FALSE;
00429
00430 if (IsRunning ())
00431 return IR_S_FALSE;
00432
00433 char szUrl [10000];
00434 GetParentFolderUrl (szUrl);
00435
00436 return GetList (szUrl, NULL, NULL);
00437 }
00438
00439 fsInternetResult fsInternetFileListMgr::GoFolder(LPCSTR pszFolder)
00440 {
00441 if (m_files == NULL)
00442 return IR_S_FALSE;
00443
00444 if (IsRunning ())
00445 return IR_S_FALSE;
00446
00447 char szUrl [10000];
00448 *szUrl = 0;
00449
00450 FolderToUrl (pszFolder, szUrl);
00451
00452 return GetList (szUrl, NULL, NULL);
00453 }
00454
00455 fsInternetResult fsInternetFileListMgr::GetFullUrl(LPCSTR pszRelOrNotUrl, fsString &strUrl)
00456 {
00457 fsURL url;
00458
00459 if (m_files == NULL)
00460 return IR_S_FALSE;
00461
00462 char szUrl [10000];
00463 GetCurrentUrl (szUrl, 10000);
00464
00465 char* pszRes;
00466
00467 fsUrlToFullUrl (szUrl, pszRelOrNotUrl, &pszRes);
00468
00469 strUrl = pszRes;
00470 delete [] pszRes;
00471
00472 return IR_SUCCESS;
00473 }
00474
00475 void fsInternetFileListMgr::GetCurrentUrl(LPSTR pszUrl, DWORD dwLen, BOOL bIncludeUser, BOOL bIncludePassword)
00476 {
00477 fsURL url;
00478
00479 if (m_files == NULL)
00480 pszUrl [0] = 0;
00481 else
00482 url.Create (m_server.GetScheme (), m_server.GetServerName (), m_server.GetServerPort (),
00483 bIncludeUser ? m_strUser : NULL, bIncludePassword ? m_strPassword : NULL, m_files->GetCurrentPath (), pszUrl, &dwLen);
00484 }
00485
00486 void fsInternetFileListMgr::SleepInterval()
00487 {
00488 if (m_bAbort)
00489 return;
00490
00491 int uInterval = _App.RetriesTime ();
00492
00493 Event (FLME_PAUSE);
00494
00495 while (uInterval > 0 && m_bAbort == FALSE)
00496 {
00497 Sleep (100);
00498 uInterval -= 100;
00499 }
00500 }
00501
00502 void fsInternetFileListMgr::_HttpEvents(fsHttpFiles* , fsHttpFilesEvent enEvent, LPVOID lpParam)
00503 {
00504 fsInternetFileListMgr *pThis = (fsInternetFileListMgr*) lpParam;
00505
00506 switch (enEvent)
00507 {
00508 case HFE_CONNECTED:
00509 pThis->m_bConnected = TRUE;
00510 pThis->Event (FLME_CONNECTED);
00511 pThis->Event (FLME_GETTINGLIST);
00512 break;
00513
00514 case HFE_FILELISTREAD:
00515 pThis->Event (FLME_HTTP_LISTREAD);
00516 break;
00517
00518 case HFE_STARTBUILDLIST:
00519 pThis->Event (FLME_HTTP_STARTBUILDLIST);
00520 break;
00521
00522 case HFE_FINISHBUILDLIST:
00523 pThis->Event (FLME_HTTP_FINISHBUILDLIST);
00524 break;
00525 }
00526 }
00527
00528 BOOL fsInternetFileListMgr::AskForLogin(LPCSTR pszHostName)
00529 {
00530 CLoginDlg dlg;
00531
00532 dlg.m_strServer = pszHostName;
00533 dlg.m_strUser = m_strUser;
00534 dlg.m_strPassword = m_strPassword;
00535
00536 if (IDCANCEL == dlg.DoModal ())
00537 return FALSE;
00538
00539 m_strUser = dlg.m_strUser;
00540 m_strPassword = dlg.m_strPassword;
00541
00542 return TRUE;
00543 }
00544
00545 BOOL fsInternetFileListMgr::IsConnected()
00546 {
00547 return m_bConnected;
00548 }
00549
00550 void fsInternetFileListMgr::RetreiveInfoWhileGettingList(BOOL b)
00551 {
00552 m_bRetreiveInfoWhileGettingList = b;
00553 }
00554
00555 BOOL fsInternetFileListMgr::RetreiveInfoWhileGettingList()
00556 {
00557 return m_bRetreiveInfoWhileGettingList;
00558 }
00559
00560 void fsInternetFileListMgr::FtpPassiveMode(BOOL b)
00561 {
00562 m_bFtpPassiveMode = b;
00563 }
00564
00565 void fsInternetFileListMgr::Disconnect()
00566 {
00567 m_bConnected = FALSE;
00568 m_server.CloseHandle ();
00569 Free (FALSE);
00570 }
00571
00572 void fsInternetFileListMgr::ReadSettings()
00573 {
00574 m_bRetreiveInfoWhileGettingList = _App.RetreiveInfoWhileGettingList ();
00575 m_bFtpPassiveMode = _App.HFE_FtpPassiveMode ();
00576 }
00577
00578 void fsInternetFileListMgr::FolderToUrl(LPCSTR pszFolder, LPSTR pszUrl)
00579 {
00580 *pszUrl = 0;
00581
00582 char* pszRes;
00583
00584 GetCurrentUrl (pszUrl, 10000);
00585 fsUrlToFullUrl (pszUrl, pszFolder, &pszRes);
00586
00587 strcpy (pszUrl, pszRes);
00588 delete [] pszRes;
00589
00590 int len = strlen (pszUrl);
00591 if (pszUrl [len - 1] != '\\' && pszUrl [len - 1] != '/')
00592 {
00593 pszUrl [len] = '/';
00594 pszUrl [len + 1] = 0;
00595 }
00596 }
00597
00598 void fsInternetFileListMgr::GetParentFolderUrl(LPSTR pszUrl)
00599 {
00600 GetCurrentUrl (pszUrl, 10000);
00601
00602
00603
00604 int pos = strlen (pszUrl) - 2;
00605
00606 while (pos > 0 && pszUrl [pos] != '\\' && pszUrl [pos] != '/')
00607 pos--;
00608
00609 pszUrl [pos+1] = 0;
00610 }