00001
00002
00003
00004
00005
00006 #include "stdafx.h"
00007 #include "DownloadProperties.h"
00008 #include "Hash\vmsHash.h"
00009
00010 BOOL fsFillBuffer (LPSTR *ppszBuffer, UINT* pnSize, LPCSTR pszFrom, BOOL bAllocate)
00011 {
00012 if (pszFrom == NULL)
00013 return FALSE;
00014
00015 UINT len = strlen (pszFrom);
00016
00017
00018 if (len >= *pnSize && !bAllocate)
00019 return FALSE;
00020
00021 *pnSize = len+1;
00022
00023 if (bAllocate)
00024 fsnew (*ppszBuffer, CHAR, len+1);
00025
00026 if (*ppszBuffer)
00027 strcpy (*ppszBuffer, pszFrom);
00028
00029 return TRUE;
00030 }
00031
00032 BOOL fsDNP_GetDefaults (fsDownload_NetworkProperties *pDNP, fsDNP_BuffersInfo* pBuffs, BOOL bAllocate)
00033 {
00034 BOOL bResult = TRUE;
00035
00036 if (pDNP == NULL || pBuffs == NULL)
00037 {
00038 SetLastError (ERROR_INVALID_PARAMETER);
00039 return FALSE;
00040 }
00041
00042 #ifndef FDM_DLDR__RAWCODEONLY
00043 pDNP->wRollBackSize = _App.RollBackSize ();
00044 pDNP->enAccType = _App.InternetAccessType ();
00045 pDNP->dwFtpFlags = _App.FtpFlags ();
00046 pDNP->bUseHttp11 = _App.UseHttp11 ();
00047 pDNP->enFtpTransferType = _App.FtpTransferType ();
00048 pDNP->bUseCookie = _App.UseCookie ();
00049 pDNP->dwFlags = _App.DNPFlags ();
00050 pDNP->wLowSpeed_Duration = _App.LowSpeed_Duration ();
00051 pDNP->wLowSpeed_Factor = _App.LowSpeed_Factor ();
00052
00053 bResult &= fsFillBuffer (&pDNP->pszAgent, &pBuffs->nAgentSize, _App.Agent (), bAllocate);
00054 bResult &= fsFillBuffer (&pDNP->pszPassword, &pBuffs->nPasswordSize, _App.UserPassword (), bAllocate);
00055 bResult &= fsFillBuffer (&pDNP->pszReferer, &pBuffs->nRefferSize, _App.Referer (), bAllocate);
00056 bResult &= fsFillBuffer (&pDNP->pszUserName, &pBuffs->nUserNameSize, _App.UserName (), bAllocate);
00057 bResult &= fsFillBuffer (&pDNP->pszASCIIExts, &pBuffs->nTransferTypeExtsSize, _App.ASCIIExts (), bAllocate);
00058 bResult &= fsFillBuffer (&pDNP->pszCookies, &pBuffs->nCookiesSize, "", bAllocate);
00059 bResult &= fsFillBuffer (&pDNP->pszPostData, &pBuffs->nPostDataSize, "", bAllocate);
00060
00061 #else
00062 pDNP->wRollBackSize = 3500;
00063 pDNP->enAccType = IATE_PRECONFIGPROXY;
00064 pDNP->dwFtpFlags = FTP_USEPASSIVEMODE;
00065 pDNP->bUseHttp11 = FALSE;
00066 pDNP->enFtpTransferType = FTT_BINARY;
00067 pDNP->bUseCookie = TRUE;
00068 pDNP->dwFlags = 0;
00069 pDNP->wLowSpeed_Duration = 1;
00070 pDNP->wLowSpeed_Factor = 3;
00071
00072 bResult &= fsFillBuffer (&pDNP->pszAgent, &pBuffs->nAgentSize, "FDMuiless", bAllocate);
00073 bResult &= fsFillBuffer (&pDNP->pszPassword, &pBuffs->nPasswordSize, "", bAllocate);
00074 bResult &= fsFillBuffer (&pDNP->pszReferer, &pBuffs->nRefferSize, "", bAllocate);
00075 bResult &= fsFillBuffer (&pDNP->pszUserName, &pBuffs->nUserNameSize, "", bAllocate);
00076 bResult &= fsFillBuffer (&pDNP->pszASCIIExts, &pBuffs->nTransferTypeExtsSize, "", bAllocate);
00077 bResult &= fsFillBuffer (&pDNP->pszCookies, &pBuffs->nCookiesSize, "", bAllocate);
00078 bResult &= fsFillBuffer (&pDNP->pszPostData, &pBuffs->nPostDataSize, "", bAllocate);
00079 #endif
00080
00081 if (!bResult)
00082 SetLastError (ERROR_INSUFFICIENT_BUFFER);
00083
00084 return bResult;
00085 }
00086
00087 void fsDNP_GetDefaults_Free (fsDownload_NetworkProperties *pDNP)
00088 {
00089 SAFE_DELETE_ARRAY (pDNP->pszPassword);
00090 SAFE_DELETE_ARRAY (pDNP->pszProxyName);
00091 SAFE_DELETE_ARRAY (pDNP->pszProxyPassword);
00092 SAFE_DELETE_ARRAY (pDNP->pszProxyUserName);
00093 SAFE_DELETE_ARRAY (pDNP->pszReferer);
00094 SAFE_DELETE_ARRAY (pDNP->pszUserName);
00095 SAFE_DELETE_ARRAY (pDNP->pszAgent);
00096 SAFE_DELETE_ARRAY (pDNP->pszASCIIExts);
00097 SAFE_DELETE_ARRAY (pDNP->pszCookies);
00098 SAFE_DELETE_ARRAY (pDNP->pszPostData);
00099 }
00100
00101 BOOL fsDP_GetDefaults (fsDownload_Properties *pDP, fsDP_BuffersInfo* pBuffs, BOOL bAllocate)
00102 {
00103 if (pDP == NULL)
00104 {
00105 SetLastError (ERROR_INVALID_PARAMETER);
00106 return FALSE;
00107 }
00108
00109 if (pDP->wStructSize != sizeof (fsDownload_Properties))
00110 {
00111 SetLastError (ERROR_INVALID_DATA);
00112 return FALSE;
00113 }
00114
00115 #ifndef FDM_DLDR__RAWCODEONLY
00116 pDP->bIgnoreRestrictions = _App.IgnoreRestrictions ();
00117 pDP->bRestartSpeedLow = _App.RestartSpeedLow ();
00118 pDP->uMaxAttempts = _App.MaxAttempts ();
00119 pDP->uMaxSections = _App.MaxSections ();
00120 pDP->uSectionMinSize = _App.SectionMinSize ();
00121 pDP->uTrafficRestriction = _App.TrafficRestriction ();
00122 pDP->uRetriesTime = _App.RetriesTime ();
00123 pDP->bReserveDiskSpace = _App.ReserveDiskSpace ();
00124 pDP->uTimeout = _App.Timeout ();
00125
00126 pDP->aEP [DFE_NOTFOUND] = _App.NotFoundReaction ();
00127 pDP->aEP [DFE_ACCDENIED] = _App.AccDeniedReaction ();
00128
00129 pDP->enAER = _App.AlreadyExistReaction ();
00130 pDP->enSCR = _App.SizeChangeReaction ();
00131
00132 pDP->dwFlags = _App.DownloadFlags ();
00133
00134 if (_App.UseZipPreview ())
00135 pDP->dwFlags |= DPF_USEZIPPREVIEW;
00136 else
00137 pDP->dwFlags &= ~DPF_USEZIPPREVIEW;
00138
00139 pDP->bCheckIntegrityWhenDone = _App.Download_CheckIntegrityWhenDone ();
00140 pDP->dwIntegrityCheckAlgorithm = HA_MD5;
00141 pDP->enICFR = (vmsIntegrityCheckFailedReaction)_App.Download_IntegrityCheckFailedReaction ();
00142 pDP->pszCheckSum = new char [1]; *pDP->pszCheckSum = 0;
00143
00144 #else
00145 pDP->bIgnoreRestrictions = FALSE;
00146 pDP->bRestartSpeedLow = TRUE;
00147 pDP->uMaxAttempts = UINT_MAX;
00148 pDP->uMaxSections = 1;
00149 pDP->uSectionMinSize = 300*1024;
00150 pDP->uTrafficRestriction = UINT_MAX;
00151 pDP->uRetriesTime = 5*1000;
00152 pDP->bReserveDiskSpace = TRUE;
00153 pDP->uTimeout = 2*60*1000;
00154
00155 pDP->aEP [DFE_NOTFOUND] = DFEP_STOP;
00156 pDP->aEP [DFE_ACCDENIED] = DFEP_STOP;
00157
00158 pDP->enAER = AER_REWRITE;
00159 pDP->enSCR = SCR_STOP;
00160
00161 pDP->dwFlags = 0;
00162
00163 pDP->bCheckIntegrityWhenDone = FALSE;
00164 pDP->pszCheckSum = new char [1]; *pDP->pszCheckSum = 0;
00165 #endif
00166
00167 if (bAllocate)
00168 pDP->pszCheckSum = new char [1];
00169 *pDP->pszCheckSum = 0;
00170
00171 BOOL bResult = TRUE;
00172
00173 #ifndef FDM_DLDR__RAWCODEONLY
00174 bResult &= fsFillBuffer (&pDP->pszAdditionalExt, &pBuffs->nAdditionalExtSize, _App.AdditionalExtension (), bAllocate);
00175 bResult &= fsFillBuffer (&pDP->pszCreateExt, &pBuffs->nAdditionalExtSize, _App.Download_CreateExt (), bAllocate);
00176 #else
00177 bResult &= fsFillBuffer (&pDP->pszAdditionalExt, &pBuffs->nAdditionalExtSize, "", bAllocate);
00178 bResult &= fsFillBuffer (&pDP->pszCreateExt, &pBuffs->nAdditionalExtSize, "", bAllocate);
00179 #endif
00180
00181 return bResult ? IR_SUCCESS : IR_ERROR;
00182 }
00183
00184 fsInternetResult fsDNP_GetByUrl (fsDownload_NetworkProperties *pDNP, fsDNP_BuffersInfo* pBuffs, BOOL bAllocate, LPCSTR pszUrl)
00185 {
00186
00187 if (!fsDNP_GetDefaults (pDNP, pBuffs, bAllocate))
00188 return IR_ERROR;
00189
00190 fsURL url;
00191 BOOL bOk = TRUE;
00192
00193 fsInternetResult ir = url.Crack (pszUrl);
00194 if (ir != IR_SUCCESS)
00195 {
00196 fsDNP_GetDefaults_Free (pDNP);
00197 return ir;
00198 }
00199
00200 pDNP->enProtocol = fsSchemeToNP (url.GetInternetScheme ());
00201
00202
00203 fsGetProxyByNP (pDNP, pBuffs, bAllocate);
00204
00205 pDNP->uServerPort = url.GetPort ();
00206
00207 bOk &= fsFillBuffer (&pDNP->pszServerName, &pBuffs->nServerNameSize, url.GetHostName (), bAllocate);
00208 bOk &= fsFillBuffer (&pDNP->pszPathName, &pBuffs->nPathNameSize, url.GetPath (), bAllocate);
00209
00210 LPCSTR pszUser = url.GetUserName ();
00211 LPCSTR pszPass = url.GetPassword ();
00212
00213 if (*pszUser)
00214 {
00215 if (bAllocate)
00216 SAFE_DELETE_ARRAY (pDNP->pszUserName);
00217
00218 bOk &= fsFillBuffer (&pDNP->pszUserName, &pBuffs->nUserNameSize, pszUser, bAllocate);
00219 }
00220
00221 if (*pszPass)
00222 {
00223 if (bAllocate)
00224 SAFE_DELETE_ARRAY (pDNP->pszPassword);
00225
00226 bOk &= fsFillBuffer (&pDNP->pszPassword, &pBuffs->nPasswordSize, pszPass, bAllocate);
00227 }
00228
00229 return IR_SUCCESS;
00230 }
00231
00232 void fsDNP_SetAuth (fsDownload_NetworkProperties* dnp, LPCSTR pszUser, LPCSTR pszPassword)
00233 {
00234 SAFE_DELETE_ARRAY (dnp->pszUserName);
00235 SAFE_DELETE_ARRAY (dnp->pszPassword);
00236
00237 dnp->pszUserName = new char [strlen (pszUser) + 1];
00238 strcpy (dnp->pszUserName, pszUser);
00239
00240 dnp->pszPassword = new char [strlen (pszPassword) + 1];
00241 strcpy (dnp->pszPassword, pszPassword);
00242 }
00243
00244 fsInternetResult fsDNP_ApplyUrl (fsDownload_NetworkProperties *dnp, LPCSTR pszUrl)
00245 {
00246 fsURL url;
00247 fsInternetResult ir;
00248
00249 ir = url.Crack (pszUrl);
00250 if (ir != IR_SUCCESS)
00251 return ir;
00252
00253 fsNetworkProtocol np = fsSchemeToNP (url.GetInternetScheme ());
00254
00255 if (dnp->enProtocol != np)
00256 {
00257 fsDNP_BuffersInfo buffs;
00258
00259 SAFE_DELETE_ARRAY (dnp->pszProxyName);
00260 SAFE_DELETE_ARRAY (dnp->pszProxyUserName);
00261 SAFE_DELETE_ARRAY (dnp->pszProxyPassword);
00262
00263 dnp->enProtocol = np;
00264
00265 fsGetProxyByNP (dnp, &buffs, TRUE);
00266 }
00267
00268 dnp->uServerPort = url.GetPort ();
00269
00270 SAFE_DELETE_ARRAY (dnp->pszPathName);
00271 fsnew (dnp->pszPathName, char, strlen (url.GetPath ()) + 1);
00272 strcpy (dnp->pszPathName, url.GetPath ());
00273
00274 SAFE_DELETE_ARRAY (dnp->pszServerName);
00275 fsnew (dnp->pszServerName, char, strlen (url.GetHostName ()) + 1);
00276 strcpy (dnp->pszServerName, url.GetHostName ());
00277
00278 if (*url.GetUserName ())
00279 {
00280 SAFE_DELETE_ARRAY (dnp->pszUserName);
00281 fsnew (dnp->pszUserName, char, strlen (url.GetUserName ()) + 1);
00282 strcpy (dnp->pszUserName, url.GetUserName ());
00283
00284 SAFE_DELETE_ARRAY (dnp->pszPassword);
00285 fsnew (dnp->pszPassword, char, strlen (url.GetPassword ()) + 1);
00286 strcpy (dnp->pszPassword, url.GetPassword ());
00287 }
00288 else
00289 {
00290 *dnp->pszUserName = 0;
00291 *dnp->pszPassword = 0;
00292 }
00293
00294 return IR_SUCCESS;
00295 }
00296
00297 void fsDNP_GetByUrl_Free (fsDownload_NetworkProperties *pDNP)
00298 {
00299 fsDNP_GetDefaults_Free (pDNP);
00300 SAFE_DELETE_ARRAY (pDNP->pszPathName);
00301 SAFE_DELETE_ARRAY (pDNP->pszServerName);
00302 }
00303
00304 fsNetworkProtocol fsSchemeToNP (INTERNET_SCHEME scheme)
00305 {
00306 switch (scheme)
00307 {
00308 case INTERNET_SCHEME_FTP:
00309 return NP_FTP;
00310
00311 case INTERNET_SCHEME_HTTP:
00312 return NP_HTTP;
00313
00314 case INTERNET_SCHEME_HTTPS:
00315 return NP_HTTPS;
00316
00317 case INTERNET_SCHEME_FILE:
00318 return NP_FILE;
00319 }
00320
00321 ASSERT (FALSE);
00322 return (fsNetworkProtocol) -1;
00323 }
00324
00325 INTERNET_SCHEME fsNPToScheme (fsNetworkProtocol np)
00326 {
00327 switch (np)
00328 {
00329 case NP_HTTP:
00330 return INTERNET_SCHEME_HTTP;
00331
00332 case NP_HTTPS:
00333 return INTERNET_SCHEME_HTTPS;
00334
00335 case NP_FTP:
00336 return INTERNET_SCHEME_FTP;
00337
00338 case NP_FILE:
00339 return INTERNET_SCHEME_FILE;
00340
00341 default:
00342 ASSERT (0);
00343 return INTERNET_SCHEME_UNKNOWN;
00344 }
00345 }
00346
00347 fsInternetResult fsGetProxyByNP (fsDownload_NetworkProperties *pDNP, fsDNP_BuffersInfo* pBuffs, BOOL bAllocate)
00348 {
00349 BOOL bOk = TRUE;
00350
00351 switch (pDNP->enProtocol)
00352 {
00353 case NP_FTP:
00354 #ifndef FDM_DLDR__RAWCODEONLY
00355 bOk &= fsFillBuffer (&pDNP->pszProxyName, &pBuffs->nProxyNameSize, _App.FtpProxy_Name (), bAllocate);
00356 bOk &= fsFillBuffer (&pDNP->pszProxyPassword, &pBuffs->nProxyPasswordSize, _App.FtpProxy_Password (), bAllocate);
00357 bOk &= fsFillBuffer (&pDNP->pszProxyUserName, &pBuffs->nProxyUserNameSize, _App.FtpProxy_UserName (), bAllocate);
00358 #else
00359 bOk &= fsFillBuffer (&pDNP->pszProxyName, &pBuffs->nProxyNameSize, "", bAllocate);
00360 bOk &= fsFillBuffer (&pDNP->pszProxyPassword, &pBuffs->nProxyPasswordSize, "", bAllocate);
00361 bOk &= fsFillBuffer (&pDNP->pszProxyUserName, &pBuffs->nProxyUserNameSize, "", bAllocate);
00362 #endif
00363 break;
00364
00365 case NP_HTTP:
00366 #ifndef FDM_DLDR__RAWCODEONLY
00367 bOk &= fsFillBuffer (&pDNP->pszProxyName, &pBuffs->nProxyNameSize, _App.HttpProxy_Name (), bAllocate);
00368 bOk &= fsFillBuffer (&pDNP->pszProxyPassword, &pBuffs->nProxyPasswordSize, _App.HttpProxy_Password (), bAllocate);
00369 bOk &= fsFillBuffer (&pDNP->pszProxyUserName, &pBuffs->nProxyUserNameSize, _App.HttpProxy_UserName (), bAllocate);
00370 #else
00371 bOk &= fsFillBuffer (&pDNP->pszProxyName, &pBuffs->nProxyNameSize, "", bAllocate);
00372 bOk &= fsFillBuffer (&pDNP->pszProxyPassword, &pBuffs->nProxyPasswordSize, "", bAllocate);
00373 bOk &= fsFillBuffer (&pDNP->pszProxyUserName, &pBuffs->nProxyUserNameSize, "", bAllocate);
00374 #endif
00375 break;
00376
00377 case NP_HTTPS:
00378 #ifndef FDM_DLDR__RAWCODEONLY
00379 bOk &= fsFillBuffer (&pDNP->pszProxyName, &pBuffs->nProxyNameSize, _App.HttpsProxy_Name (), bAllocate);
00380 bOk &= fsFillBuffer (&pDNP->pszProxyPassword, &pBuffs->nProxyPasswordSize, _App.HttpsProxy_Password (), bAllocate);
00381 bOk &= fsFillBuffer (&pDNP->pszProxyUserName, &pBuffs->nProxyUserNameSize, _App.HttpsProxy_UserName (), bAllocate);
00382 #else
00383 bOk &= fsFillBuffer (&pDNP->pszProxyName, &pBuffs->nProxyNameSize, "", bAllocate);
00384 bOk &= fsFillBuffer (&pDNP->pszProxyPassword, &pBuffs->nProxyPasswordSize, "", bAllocate);
00385 bOk &= fsFillBuffer (&pDNP->pszProxyUserName, &pBuffs->nProxyUserNameSize, "", bAllocate);
00386 #endif
00387 break;
00388
00389 case NP_FILE:
00390 bOk &= fsFillBuffer (&pDNP->pszProxyName, &pBuffs->nProxyNameSize, "", bAllocate);
00391 bOk &= fsFillBuffer (&pDNP->pszProxyPassword, &pBuffs->nProxyPasswordSize, "", bAllocate);
00392 bOk &= fsFillBuffer (&pDNP->pszProxyUserName, &pBuffs->nProxyUserNameSize, "", bAllocate);
00393 break;
00394
00395 default:
00396 return IR_BADURL;
00397 }
00398
00399 return bOk ? IR_SUCCESS : IR_ERROR;
00400 }
00401
00402 BOOL fsGetProxy (fsNetworkProtocol np, CString& strProxy, CString& strUser, CString& strPassword)
00403 {
00404 switch (np)
00405 {
00406 case NP_HTTP:
00407 #ifndef FDM_DLDR__RAWCODEONLY
00408 strProxy = _App.HttpProxy_Name ();
00409 strUser = _App.HttpProxy_UserName ();
00410 strPassword = _App.HttpProxy_Password ();
00411 #else
00412 strProxy = "";
00413 strUser = "";
00414 strPassword = "";
00415 #endif
00416 break;
00417
00418 case NP_HTTPS:
00419 #ifndef FDM_DLDR__RAWCODEONLY
00420 strProxy = _App.HttpsProxy_Name ();
00421 strUser = _App.HttpsProxy_UserName ();
00422 strPassword = _App.HttpsProxy_Password ();
00423 #else
00424 strProxy = "";
00425 strUser = "";
00426 strPassword = "";
00427 #endif
00428 break;
00429
00430 case NP_FTP:
00431 #ifndef FDM_DLDR__RAWCODEONLY
00432 strProxy = _App.FtpProxy_Name ();
00433 strUser = _App.FtpProxy_UserName ();
00434 strPassword = _App.FtpProxy_Password ();
00435 #else
00436 strProxy = "";
00437 strUser = "";
00438 strPassword = "";
00439 #endif
00440 break;
00441
00442 case NP_FILE:
00443 strProxy = "";
00444 strUser = "";
00445 strPassword = "";
00446 break;
00447
00448 default:
00449 return FALSE;
00450 }
00451
00452 return TRUE;
00453 }
00454
00455 BOOL fsIsSameProtocols (fsNetworkProtocol np1, fsNetworkProtocol np2)
00456 {
00457 return np1 == np2 ||
00458 (
00459 (np1 == NP_HTTP || np1 == NP_HTTPS) && (np2 == NP_HTTP || np2 == NP_HTTPS)
00460 );
00461 }
00462
00463 BOOL fsDNP_CloneSettings (fsDownload_NetworkProperties *dst, fsDownload_NetworkProperties *src)
00464 {
00465 if (fsIsSameProtocols (src->enProtocol, dst->enProtocol) == FALSE)
00466 return FALSE;
00467
00468 dst->wRollBackSize = src->wRollBackSize;
00469 dst->enAccType = src->enAccType;
00470 dst->dwFtpFlags = src->dwFtpFlags;
00471 dst->bUseHttp11 = src->bUseHttp11;
00472 dst->enFtpTransferType = src->enFtpTransferType;
00473 dst->bUseCookie = src->bUseCookie;
00474
00475 UINT u;
00476
00477 SAFE_DELETE_ARRAY (dst->pszAgent);
00478 SAFE_DELETE_ARRAY (dst->pszASCIIExts);
00479
00480 fsFillBuffer (&dst->pszAgent, &u, src->pszAgent, TRUE);
00481 fsFillBuffer (&dst->pszASCIIExts, &u, src->pszASCIIExts, TRUE);
00482
00483 SAFE_DELETE_ARRAY (dst->pszProxyName);
00484 SAFE_DELETE_ARRAY (dst->pszProxyUserName);
00485 SAFE_DELETE_ARRAY (dst->pszProxyPassword);
00486
00487 fsFillBuffer (&dst->pszProxyName, &u, src->pszProxyName, TRUE);
00488 fsFillBuffer (&dst->pszProxyUserName, &u, src->pszProxyUserName, TRUE);
00489 fsFillBuffer (&dst->pszProxyPassword, &u, src->pszProxyPassword, TRUE);
00490
00491 return TRUE;
00492 }
00493
00494 void fsDNP_GetURL (fsDownload_NetworkProperties* dnp, LPSTR pszURL)
00495 {
00496 DWORD dw = 10000;
00497 fsURL url;
00498
00499 if (IR_SUCCESS != url.Create (fsNPToScheme (dnp->enProtocol), dnp->pszServerName,
00500 dnp->uServerPort, dnp->pszUserName, dnp->pszPassword, dnp->pszPathName, pszURL, &dw))
00501 *pszURL = 0;
00502 }