00001
00002
00003
00004
00005
00006 #include "fsInternetURLFile.h"
00007 #include "fsInternetSession.h"
00008 #include "fsInternetFile.h"
00009 #include "fsServerConnection.h"
00010 #include "fsURL.h"
00011 #include "fsHttpConnection.h"
00012 #include "fsFtpConnection.h"
00013 #include "fsFtpFile.h"
00014 #include "fsHttpFile.h"
00015 #include <atlbase.h>
00016 #include "base64.c"
00017
00018 fsInternetURLFile::fsInternetURLFile()
00019 {
00020 m_pSession = NULL;
00021 m_bAutoDelSession = FALSE;
00022 m_pFile = NULL;
00023 m_pszLastError = NULL;
00024
00025 m_pServer = NULL;
00026 m_pFile = NULL;
00027
00028 m_bUseFile2 = false;
00029
00030 InitializeCriticalSection (&m_cs);
00031
00032 m_ftpConnection.SetDialogFunc (_InetFileDialogFunc, this, NULL);
00033 m_httpConnection.SetDialogFunc (_InetFileDialogFunc, this, NULL);
00034 m_ftpFile.SetDialogFunc (_InetFileDialogFunc, this, NULL);
00035 m_httpFile.SetDialogFunc (_InetFileDialogFunc, this, NULL);
00036 m_ifile2.SetDialogFunc (_InetFileDialogFunc, this, NULL);
00037
00038 m_bCatchFromServerResponse = FALSE;
00039 }
00040
00041 fsInternetURLFile::~fsInternetURLFile()
00042 {
00043 CloseHandle ();
00044
00045 if (m_bAutoDelSession && m_pSession)
00046 delete m_pSession;
00047
00048 DeleteCriticalSection (&m_cs);
00049 }
00050
00051 fsInternetResult fsInternetURLFile::Read(BYTE *pBuffer, DWORD dwToRead, DWORD *pdwRead)
00052 {
00053 if (m_bUseFile2)
00054 return m_ifile2.Read (pBuffer, dwToRead, pdwRead);
00055
00056 if (m_pFile == NULL)
00057 return IR_FILENOTOPENED;
00058
00059 return m_pFile->Read (pBuffer, dwToRead, pdwRead);
00060 }
00061
00062 fsInternetResult fsInternetURLFile::Initialize(fsInternetSession *pSession, BOOL bAutoDelSession)
00063 {
00064 m_pSession = pSession;
00065 m_bAutoDelSession = bAutoDelSession;
00066 return IR_SUCCESS;
00067 }
00068
00069 UINT64 fsInternetURLFile::GetFileSize()
00070 {
00071 if (m_bUseFile2)
00072 return m_ifile2.GetFileSize ();
00073
00074 if (m_pFile == NULL)
00075 return _UI64_MAX;
00076
00077 return m_pFile->GetFileSize ();
00078 }
00079
00080 void fsInternetURLFile::SetReferer(LPCSTR pszReferer)
00081 {
00082 m_httpFile.SetReferer (pszReferer);
00083 }
00084
00085 void fsInternetURLFile::UseHttp11(BOOL bUse)
00086 {
00087 m_httpFile.UseHttp11 (bUse);
00088 }
00089
00090 void fsInternetURLFile::UseCookie(BOOL bUse)
00091 {
00092 m_httpFile.UseCookie (bUse);
00093 }
00094
00095 void fsInternetURLFile::UseFtpPassiveMode(BOOL bUse)
00096 {
00097 m_ftpConnection.UsePassiveMode (bUse);
00098 }
00099
00100 void fsInternetURLFile::FtpSetTransferType(fsFtpTransferType enType)
00101 {
00102 m_ftpFile.SetTransferType (enType);
00103 }
00104
00105 void fsInternetURLFile::Close()
00106 {
00107 if (m_bUseFile2) {
00108 m_ifile2.StopDownloading ();
00109 return;
00110 }
00111
00112 if (m_pFile)
00113 m_pFile->CloseHandle ();
00114 }
00115
00116 LPCSTR fsInternetURLFile::GetLastError()
00117 {
00118 return m_pszLastError;
00119 }
00120
00121 fsInternetResult fsInternetURLFile::Open(INTERNET_SCHEME scheme, LPCSTR pszHostName, LPCSTR pszUser, LPCSTR pszPassword, INTERNET_PORT port, LPCSTR pszPath, UINT64 uStartPosition, BOOL bSendHTTPBasicAuthImmediately)
00122 {
00123 return OpenEx (scheme, pszHostName, pszUser, pszPassword, port, pszPath,
00124 uStartPosition, bSendHTTPBasicAuthImmediately, _UI64_MAX);
00125 }
00126
00127 void fsInternetURLFile::CloseHandle()
00128 {
00129 EnterCriticalSection (&m_cs);
00130
00131 if (m_bUseFile2)
00132 m_ifile2.StopDownloading ();
00133
00134 if (m_pFile)
00135 m_pFile->CloseHandle ();
00136
00137 if (m_pServer)
00138 m_pServer->CloseHandle ();
00139
00140 if (m_bAutoDelSession)
00141 m_pSession->CloseHandle ();
00142
00143 LeaveCriticalSection (&m_cs);
00144 }
00145
00146 BOOL fsInternetURLFile::GetContentType(LPSTR pszType)
00147 {
00148 if (m_bUseFile2)
00149 {
00150 LPCSTR psz = m_ifile2.get_ContentType ();
00151 lstrcpy (pszType, psz ? psz : "");
00152 return *pszType != 0;
00153 }
00154
00155 return m_pFile->GetContentType (pszType);
00156 }
00157
00158 BOOL fsInternetURLFile::GetLastModifiedDate(LPFILETIME pDate)
00159 {
00160 return m_bUseFile2 ? m_ifile2.GetLastModifiedDate (pDate) :
00161 m_pFile->GetLastModifiedDate (pDate);
00162 }
00163
00164 fsResumeSupportType fsInternetURLFile::IsResumeSupported()
00165 {
00166 if (m_bUseFile2)
00167 return m_ifile2.IsResumeSupported ();
00168
00169 if (m_pFile == NULL)
00170 return RST_UNKNOWN;
00171 return m_pFile->IsResumeSupported ();
00172 }
00173
00174 LPCSTR fsInternetURLFile::GetSuggestedFileName()
00175 {
00176 try {
00177 return m_bUseFile2 ? m_ifile2.get_SuggestedFileName () :
00178 m_pFile->GetSuggestedFileName ();
00179 }
00180 catch (...) {return NULL;}
00181 }
00182
00183 fsInternetResult fsInternetURLFile::QuerySize(INTERNET_SCHEME scheme, LPCSTR pszHostName, LPCSTR pszUser, LPCSTR pszPassword, INTERNET_PORT port, LPCSTR pszPath, BOOL bSendHTTPBasicAuthImmediately)
00184 {
00185
00186
00187 fsInternetResult ir;
00188
00189 if (scheme == INTERNET_SCHEME_HTTP || scheme == INTERNET_SCHEME_HTTPS)
00190 {
00191 ir = Open (scheme, pszHostName, pszUser, pszPassword, port, pszPath, 0, bSendHTTPBasicAuthImmediately);
00192 }
00193 else
00194 {
00195 switch (scheme)
00196 {
00197 case INTERNET_SCHEME_FTP:
00198 m_pServer = &m_ftpConnection;
00199 m_pFile = &m_ftpFile;
00200 break;
00201
00202 case INTERNET_SCHEME_FILE:
00203 m_pServer = NULL;
00204 m_pFile = &m_localFile;
00205 break;
00206
00207 default:
00208 return IR_BADURL;
00209 }
00210
00211 if (m_pServer)
00212 {
00213 ir = m_pServer->Initialize (m_pSession);
00214 if (ir != IR_SUCCESS)
00215 return ir;
00216
00217 ir = m_pServer->Connect (pszHostName, pszUser, pszPassword, port);
00218 if (ir != IR_SUCCESS)
00219 {
00220 m_pszLastError = m_pServer->GetLastError ();
00221 return ir;
00222 }
00223
00224 ir = m_pFile->Initialize (m_pServer);
00225 if (ir != IR_SUCCESS)
00226 return ir;
00227 }
00228 else
00229 {
00230 m_localFile.Initialize (pszHostName);
00231 }
00232
00233 ir = m_pFile->QuerySize (pszPath);
00234 }
00235
00236 if (ir != IR_SUCCESS)
00237 {
00238 m_pszLastError = m_pFile->GetLastError ();
00239 return ir;
00240 }
00241
00242 CloseHandle ();
00243
00244 return IR_SUCCESS;
00245 }
00246
00247 void fsInternetURLFile::FtpSetDontUseLIST(BOOL b)
00248 {
00249 m_ftpFile.SetDontUseLIST (b);
00250 }
00251
00252 void fsInternetURLFile::SetupProxyForFile2()
00253 {
00254 LPCSTR psz1, psz2, psz3;
00255 m_pSession->get_Proxy (psz1, psz2, psz3);
00256
00257 if (psz1 == NULL && *psz1 == 0)
00258 return;
00259
00260 char szProxy [1000];
00261 if (lstrcmpi (psz1, "Internet Explorer") == 0)
00262 {
00263 CRegKey key;
00264 if (ERROR_SUCCESS != key.Open (HKEY_CURRENT_USER,
00265 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
00266 KEY_READ))
00267 return;
00268
00269 DWORD dw;
00270 if (ERROR_SUCCESS != key.QueryValue (dw, "ProxyEnable"))
00271 return;
00272 if (dw == FALSE)
00273 return;
00274
00275 dw = sizeof (szProxy);
00276 if (ERROR_SUCCESS != key.QueryValue (szProxy, "ProxyServer", &dw))
00277 return;
00278
00279 psz1 = szProxy;
00280 }
00281
00282 if (psz1 && *psz1)
00283 m_ifile2.set_Proxy (psz1, psz2, psz3);
00284 }
00285
00286 void fsInternetURLFile::SetCookies(LPCSTR pszCookies)
00287 {
00288 m_httpFile.SetCookies (pszCookies);
00289 }
00290
00291 void fsInternetURLFile::SetPostData(LPCSTR pszHdrs)
00292 {
00293 m_httpFile.SetPostData (pszHdrs);
00294 }
00295
00296 fsFtpTransferType fsInternetURLFile::FtpGetTransferType()
00297 {
00298 return m_ftpFile.GetTransferType ();
00299 }
00300
00301 fsInternetResult fsInternetURLFile::OpenEx(INTERNET_SCHEME scheme, LPCSTR pszHostName, LPCSTR pszUser, LPCSTR pszPassword, INTERNET_PORT port, LPCSTR pszPath, UINT64 uStartPosition, BOOL bSendHTTPBasicAuthImmediately, UINT64 uUploadPartSize, UINT64 uUploadTotalSize)
00302 {
00303 fsInternetResult ir;
00304
00305 m_bUseFile2 = false;
00306
00307
00308
00309 switch (scheme)
00310 {
00311 case INTERNET_SCHEME_HTTP:
00312 case INTERNET_SCHEME_HTTPS:
00313 m_pServer = &m_httpConnection;
00314 m_pFile = &m_httpFile;
00315 if (bSendHTTPBasicAuthImmediately)
00316 FormHttpBasicAuthHdr (pszUser, pszPassword);
00317 break;
00318
00319 case INTERNET_SCHEME_FTP:
00320 m_pServer = &m_ftpConnection;
00321 m_pFile = &m_ftpFile;
00322 break;
00323
00324 case INTERNET_SCHEME_FILE:
00325 m_pServer = NULL;
00326 m_pFile = &m_localFile;
00327 break;
00328
00329 default:
00330 return IR_BADURL;
00331 }
00332
00333 m_httpFile.UseSecure (scheme == INTERNET_SCHEME_HTTPS);
00334
00335 if (m_pServer)
00336 {
00337 if (pszHostName)
00338 {
00339 ir = m_pServer->Initialize (m_pSession);
00340 if (ir != IR_SUCCESS)
00341 return ir;
00342
00343
00344 ir = m_pServer->Connect (pszHostName, pszUser, pszPassword, port);
00345 if (ir != IR_SUCCESS)
00346 {
00347 m_pszLastError = m_pServer->GetLastError ();
00348 return ir;
00349 }
00350
00351 ir = m_pFile->Initialize (m_pServer);
00352 if (ir != IR_SUCCESS)
00353 return ir;
00354 }
00355 }
00356 else
00357 {
00358 m_localFile.Initialize (pszHostName);
00359 }
00360
00361 m_bCatchFromServerResponse = TRUE;
00362
00363
00364 ir = m_pFile->OpenEx (pszPath, uStartPosition, uUploadPartSize, uUploadTotalSize);
00365
00366 bool bUseFile2 = false;
00367 m_ifile2.Mute (FALSE);
00368
00369 if (ir == IR_E_WININET_UNSUPP_RESOURCE)
00370 {
00371 if (uUploadTotalSize == _UI64_MAX)
00372 {
00373 bUseFile2 = true;
00374 ir = IR_SUCCESS;
00375 m_ifile2.Mute (FALSE);
00376 }
00377 else
00378 {
00379 ir = IR_ERROR;
00380 }
00381 }
00382
00383 if (ir != IR_SUCCESS)
00384 {
00385 if (m_strRespFromServer.IsEmpty () == FALSE)
00386 {
00387 Dialog (IFDD_FROMSERVER, m_strRespFromServer);
00388 m_strRespFromServer = "";
00389 }
00390 m_bCatchFromServerResponse = FALSE;
00391 m_pszLastError = m_pFile->GetLastError ();
00392 return ir;
00393 }
00394
00395 if (bUseFile2 == false)
00396 {
00397 if (uUploadTotalSize == _UI64_MAX)
00398 {
00399
00400
00401
00402 if (scheme == INTERNET_SCHEME_HTTP || scheme == INTERNET_SCHEME_HTTPS)
00403 {
00404 if (m_pFile->GetFileSize () != _UI64_MAX &&
00405 m_pFile->GetFileSize () >= 2*1024*1024*1024ui64)
00406 bUseFile2 = true;
00407 }
00408 }
00409 }
00410
00411 if (bUseFile2)
00412 {
00413 CloseHandle ();
00414 m_bUseFile2 = true;
00415 fsURL url;
00416 char szUrl [10000];
00417 DWORD dw = sizeof (szUrl);
00418 url.Create (scheme, pszHostName, port, NULL, NULL, pszPath, szUrl, &dw);
00419 m_ifile2.Initialize ();
00420 m_ifile2.set_URL (szUrl);
00421 m_ifile2.set_ResumeFrom (uStartPosition);
00422 m_ifile2.set_UserAgent (m_pSession->get_UserAgent ());
00423 m_ifile2.set_UseHttp11 (m_httpFile.get_UseHttp11 ());
00424 m_ifile2.set_Referer (m_httpFile.get_Referer ());
00425 if (pszUser && *pszUser)
00426 m_ifile2.set_Auth (pszUser, pszPassword);
00427
00428 if (m_httpFile.get_UseCookie ())
00429 {
00430 if (m_httpFile.GetCookies () && *m_httpFile.GetCookies ())
00431 {
00432 m_ifile2.set_Cookie (m_httpFile.GetCookies ());
00433 }
00434 else
00435 {
00436 char szCookie [10000]; dw = sizeof (szCookie);
00437 *szCookie = 0;
00438 InternetGetCookie (szUrl, NULL, szCookie, &dw);
00439 if (*szCookie)
00440 m_ifile2.set_Cookie (szCookie);
00441 }
00442 }
00443
00444 if (m_httpFile.GetPostData ())
00445 m_ifile2.set_PostData (m_httpFile.GetPostData ());
00446
00447 SetupProxyForFile2 ();
00448
00449 ir = m_ifile2.StartDownloading ();
00450 }
00451
00452 if (m_strRespFromServer.IsEmpty () == FALSE)
00453 {
00454 Dialog (IFDD_FROMSERVER, m_strRespFromServer);
00455 m_strRespFromServer = "";
00456 }
00457 m_bCatchFromServerResponse = FALSE;
00458
00459 return ir;
00460 }
00461
00462 fsInternetResult fsInternetURLFile::Write(LPBYTE pBuffer, DWORD dwToWrite, DWORD *pdwWritten)
00463 {
00464 if (m_bUseFile2)
00465 return IR_ERROR;
00466
00467 if (m_pFile == NULL)
00468 return IR_FILENOTOPENED;
00469
00470 return m_pFile->Write (pBuffer, dwToWrite, pdwWritten);
00471 }
00472
00473 void fsInternetURLFile::FormHttpBasicAuthHdr(LPCSTR pszUser, LPCSTR pszPassword)
00474 {
00475 if (pszUser == NULL || *pszUser == 0)
00476 return;
00477
00478 char szHdr [1000];
00479
00480 char szLogin [1000];
00481 lstrcpy (szLogin, pszUser);
00482 lstrcat (szLogin, ":");
00483 if (pszPassword)
00484 lstrcat (szLogin, pszPassword);
00485
00486 LPSTR pszL;
00487 base64_encode (szLogin, lstrlen (szLogin), &pszL);
00488
00489 sprintf (szHdr, "Authorization: Basic %s\r\n", pszL);
00490 delete [] pszL;
00491
00492 m_httpFile.SetAdditionalHeaders (szHdr);
00493 }
00494
00495 void fsInternetURLFile::set_UseMultipart(BOOL b)
00496 {
00497 m_httpFile.set_UseMultipart (b);
00498 }
00499
00500 fsInternetResult fsInternetURLFile::OpenAnotherRequestOnServer(LPCSTR pszPath, UINT64 uStartPosition, UINT64 uUploadPartSize, UINT64 uUploadTotalSize)
00501 {
00502 fsInternetResult ir;
00503
00504 if (m_pFile == NULL)
00505 return IR_NOTINITIALIZED;
00506
00507 if (m_bUseFile2)
00508 m_ifile2.StopDownloading ();
00509 m_pFile->CloseHandle ();
00510 m_bUseFile2 = false;
00511
00512
00513 ir = m_pFile->OpenEx (pszPath, uStartPosition, uUploadPartSize, uUploadTotalSize);
00514 if (ir != IR_SUCCESS)
00515 {
00516 m_pszLastError = m_pFile->GetLastError ();
00517 return ir;
00518 }
00519
00520 if (uUploadTotalSize == _UI64_MAX)
00521 {
00522
00523
00524
00525 if (m_pServer == &m_httpConnection)
00526 {
00527 if (m_pFile->GetFileSize () != _UI64_MAX &&
00528 m_pFile->GetFileSize () >= 2*1024*1024*1024ui64)
00529 {
00530 return IR_E_NOTIMPL;
00531 }
00532 }
00533 }
00534
00535 return IR_SUCCESS;
00536 }
00537
00538 fsString fsInternetURLFile::GetCookiesFromResponse()
00539 {
00540 return m_httpFile.GetCookiesFromResponse ();
00541 }
00542
00543 void fsInternetURLFile::set_EnableAutoRedirect(BOOL b)
00544 {
00545 m_httpFile.set_EnableAutoRedirect (b);
00546 }
00547
00548 void fsInternetURLFile::set_Charset(LPCSTR psz)
00549 {
00550 m_httpFile.set_Charset (psz);
00551 }
00552
00553 void fsInternetURLFile::_InetFileDialogFunc(fsInetFileDialogDirection enDir, LPCSTR pszMsg, LPVOID lp1, LPVOID )
00554 {
00555 fsInternetURLFile *pthis = (fsInternetURLFile*) lp1;
00556
00557 if (pthis->m_bCatchFromServerResponse && enDir == IFDD_FROMSERVER)
00558 {
00559 pthis->m_strRespFromServer = pszMsg;
00560 }
00561 else
00562 {
00563 pthis->Dialog (enDir, pszMsg);
00564 }
00565 }