BOOL CFTPCtrl::FtpDownloadFile(CString ftpFileName,CString localFilePath) { //localFilePath=CreateFilePath(ftpFileName,localFilePath); HANDLE hOpenFile = (HANDLE)CreateFile(localFilePath, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_NEW, NULL, NULL); if(hOpenFile==INVALID_HANDLE_VALUE) { if (IDYES==AfxMessageBox(_T("创建本地文件失败,指定路径下可能已存在同名文件,是否覆盖?"),MB_YESNO)) { hOpenFile = (HANDLE)CreateFile(localFilePath, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, NULL, NULL); if(hOpenFile==INVALID_HANDLE_VALUE) { AfxMessageBox(_T("创建本地文件失败,请重新选择文件路径后重试!")); CloseHandle(hOpenFile);//一定注意在函数退出之前对句柄进行释放。 return FALSE; } } else { AfxMessageBox(_T("下载文件失败!")); CloseHandle(hOpenFile);//一定注意在函数退出之前对句柄进行释放。 return FALSE; } } HINTERNET hFtpOpen = FtpOpenFile( m_hConnect, ftpFileName, GENERIC_READ, FTP_TRANSFER_TYPE_ASCII, 0 ); if ( NULL == hFtpOpen ) { AfxMessageBox(_T("Ftp远程文件打开失败!")); return FALSE; } DWORD fileSize= FtpGetFileSize(hFtpOpen,NULL);//这种调用只能获取4GB以内的文件,FtpGetFileSize该函数第二个参数64位文件大小的高32位。 DWORD readSize; char *buffer=new char[fileSize]; if (FALSE==InternetReadFile(hFtpOpen,buffer,fileSize,&readSize)) { AfxMessageBox(_T("读取Ftp文件失败!")); return FALSE; } DWORD dwBytesWritten; if (FALSE==WriteFile( hOpenFile, buffer,fileSize,&dwBytesWritten,NULL)) { AfxMessageBox(_T("下载文件失败!")); return FALSE; } CloseHandle( hOpenFile ); InternetCloseHandle(hFtpOpen); return TRUE; }
static HRESULT FtpProtocol_start_downloading(Protocol *prot) { FtpProtocol *This = impl_from_Protocol(prot); DWORD size; BOOL res; res = FtpGetFileSize(This->base.request, &size); if(res) This->base.content_length = size; else WARN("FtpGetFileSize failed: %d\n", GetLastError()); return S_OK; }
int TInetFile::CheckFileSize() { if(Error==true)return -1; char buffer[40]; unsigned long buflen=40; unsigned long index=0; if(Protokol==INET_FILE_FTP) { FSize=FtpGetFileSize(hUrl,&index); }else //Если это HTTP { if(HttpQueryInfo(hUrl, HTTP_QUERY_CONTENT_LENGTH, buffer, &buflen, &index)) { FSize=StrToInt(buffer); }else { return -1; } } return FSize; }
static DWORD WINAPI ThreadFunc(LPVOID Context) { CComPtr<IBindStatusCallback> dl; WCHAR path[MAX_PATH]; LPWSTR p; HWND Dlg = (HWND) Context; ULONG dwContentLen, dwBytesWritten, dwBytesRead, dwStatus; ULONG dwCurrentBytesRead = 0; ULONG dwStatusLen = sizeof(dwStatus); BOOL bCancelled = FALSE; BOOL bTempfile = FALSE; BOOL bCab = FALSE; HINTERNET hOpen = NULL; HINTERNET hFile = NULL; HANDLE hOut = INVALID_HANDLE_VALUE; unsigned char lpBuffer[4096]; PCWSTR lpszAgent = L"RApps/1.0"; URL_COMPONENTS urlComponents; size_t urlLength; /* built the path for the download */ p = wcsrchr(AppInfo->szUrlDownload, L'/'); if (!p) goto end; if (wcscmp(AppInfo->szUrlDownload, APPLICATION_DATABASE_URL) == 0) { bCab = TRUE; if (!GetStorageDirectory(path, sizeof(path) / sizeof(path[0]))) goto end; } else { if (FAILED(StringCbCopyW(path, sizeof(path), SettingsInfo.szDownloadDir))) goto end; } if (GetFileAttributesW(path) == INVALID_FILE_ATTRIBUTES) { if (!CreateDirectoryW(path, NULL)) goto end; } if (FAILED(StringCbCatW(path, sizeof(path), L"\\"))) goto end; if (FAILED(StringCbCatW(path, sizeof(path), p + 1))) goto end; /* download it */ bTempfile = TRUE; CDownloadDialog_Constructor(Dlg, &bCancelled, IID_PPV_ARG(IBindStatusCallback, &dl)); if (dl == NULL) goto end; switch(SettingsInfo.Proxy) { case 0: /* preconfig */ hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); break; case 1: /* direct (no proxy) */ hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0); break; case 2: /* use proxy */ hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_PROXY, SettingsInfo.szProxyServer, SettingsInfo.szNoProxyFor, 0); break; default: /* preconfig */ hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); break; } if (!hOpen) goto end; hFile = InternetOpenUrlW(hOpen, AppInfo->szUrlDownload, NULL, 0, INTERNET_FLAG_PRAGMA_NOCACHE|INTERNET_FLAG_KEEP_CONNECTION, 0); if (!hFile) goto end; if (!HttpQueryInfoW(hFile, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &dwStatus, &dwStatusLen, NULL)) goto end; if(dwStatus != HTTP_STATUS_OK) { WCHAR szMsgText[MAX_STR_LEN]; if (!LoadStringW(hInst, IDS_UNABLE_TO_DOWNLOAD, szMsgText, sizeof(szMsgText) / sizeof(WCHAR))) goto end; MessageBoxW(hMainWnd, szMsgText, NULL, MB_OK | MB_ICONERROR); goto end; } dwStatusLen = sizeof(dwStatus); memset(&urlComponents, 0, sizeof(urlComponents)); urlComponents.dwStructSize = sizeof(urlComponents); if(FAILED(StringCbLengthW(AppInfo->szUrlDownload, sizeof(AppInfo->szUrlDownload), &urlLength))) goto end; urlComponents.dwSchemeLength = urlLength*sizeof(WCHAR); urlComponents.lpszScheme = (PWSTR)malloc(urlComponents.dwSchemeLength); if(!InternetCrackUrlW(AppInfo->szUrlDownload, urlLength+1, ICU_DECODE | ICU_ESCAPE, &urlComponents)) goto end; if(urlComponents.nScheme == INTERNET_SCHEME_HTTP || urlComponents.nScheme == INTERNET_SCHEME_HTTPS) HttpQueryInfo(hFile, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &dwContentLen, &dwStatus, 0); if(urlComponents.nScheme == INTERNET_SCHEME_FTP) dwContentLen = FtpGetFileSize(hFile, &dwStatus); free(urlComponents.lpszScheme); hOut = CreateFileW(path, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, 0, NULL); if (hOut == INVALID_HANDLE_VALUE) goto end; do { if (!InternetReadFile(hFile, lpBuffer, _countof(lpBuffer), &dwBytesRead)) goto end; if (!WriteFile(hOut, &lpBuffer[0], dwBytesRead, &dwBytesWritten, NULL)) goto end; dwCurrentBytesRead += dwBytesRead; dl->OnProgress(dwCurrentBytesRead, dwContentLen, 0, AppInfo->szUrlDownload); } while (dwBytesRead); CloseHandle(hOut); hOut = INVALID_HANDLE_VALUE; if (bCancelled) goto end; ShowWindow(Dlg, SW_HIDE); /* run it */ if (!bCab) ShellExecuteW( NULL, L"open", path, NULL, NULL, SW_SHOWNORMAL ); end: if (hOut != INVALID_HANDLE_VALUE) CloseHandle(hOut); InternetCloseHandle(hFile); InternetCloseHandle(hOpen); if (bTempfile) { if (bCancelled || (SettingsInfo.bDelInstaller && !bCab)) DeleteFileW(path); } EndDialog(Dlg, 0); return 0; }
__int64 inet_get_resource_size(const wstring& url) { wstring url2=inet_canonicalize_url(url); __int64 res_size=-1; bool succ=false; HINTERNET handle_internet=NULL; HINTERNET handle_url=NULL; HINTERNET handle_connect=NULL; HINTERNET handle_file=NULL; inet_url_parts parts=inet_crack_url(url2); do { if(parts.scheme==INTERNET_SCHEME_HTTP) { handle_internet=InternetOpenW(INET_USER_AGENT.c_str(),INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0); if(!handle_internet) break; handle_url=InternetOpenUrlW(handle_internet,url2.c_str(),NULL,0,0,NULL); if(!handle_url) break; wchar_t buffer[4096]; DWORD buffer_len=sizeof(buffer); DWORD index=0; if(!HttpQueryInfoW(handle_url,HTTP_QUERY_CONTENT_LENGTH,buffer,&buffer_len,&index)) break; res_size=_wtoi64(buffer); succ=true; } else if(parts.scheme==INTERNET_SCHEME_FTP) { handle_internet=InternetOpenW(INET_USER_AGENT.c_str(),INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0); if(!handle_internet) break; handle_connect=InternetConnectW(handle_internet,parts.hostname.c_str(),parts.port,parts.username.c_str(),parts.password.c_str(),INTERNET_SERVICE_FTP,INTERNET_FLAG_PASSIVE,NULL); if(!handle_connect) break; handle_file=FtpOpenFile(handle_connect,parts.url_path.c_str(),GENERIC_READ,FTP_TRANSFER_TYPE_BINARY,NULL); if(!handle_file) break; DWORD low,high; low=FtpGetFileSize(handle_file,&high); if(low==(~0)) break; res_size=low|(((__int64)high)<<32); succ=true; } else break; }while(false); if(handle_file) InternetCloseHandle(handle_file); if(handle_connect) InternetCloseHandle(handle_connect); if(handle_url) InternetCloseHandle(handle_url); if(handle_internet) InternetCloseHandle(handle_internet); return res_size; }
// Open the file VI_FILE *ViOpenFile(char *path) { VI_FILE *f; // Validate arguments if (path == NULL) { return NULL; } if (ViIsInternetFile(path)) { HINTERNET hHttpFile; HINTERNET hInternet = InternetOpenA(DEFAULT_USER_AGENT, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); UINT size; UINT sizesize; if (hInternet == NULL) { return NULL; } hHttpFile = InternetOpenUrlA(hInternet, path, NULL, 0, INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_RELOAD, 0); if (hHttpFile == NULL) { InternetCloseHandle(hInternet); return NULL; } size = 0; sizesize = sizeof(size); if (StartWith(path, "ftp://")) { // ftp DWORD high = 0; size = FtpGetFileSize(hHttpFile, &high); } else { UINT errorcode = 0; UINT errorcode_size = sizeof(errorcode); // http if (HttpQueryInfo(hHttpFile, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &size, &sizesize, NULL) == false) { size = 0; } if (HttpQueryInfo(hHttpFile, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &errorcode, &errorcode_size, NULL) == false || (errorcode / 100) != 2) { // HTTP getting error InternetCloseHandle(hInternet); InternetCloseHandle(hHttpFile); return NULL; } } f = ZeroMalloc(sizeof(VI_FILE)); f->InternetFile = true; f->hInternet = hInternet; f->hHttpFile = hHttpFile; f->FileSize = size; return f; } else { IO *io; char fullpath[MAX_PATH]; char exedir[MAX_PATH]; GetExeDir(exedir, sizeof(exedir)); ConbinePath(fullpath, sizeof(fullpath), exedir, path); io = FileOpen(fullpath, false); if (io == NULL) { return NULL; } f = ZeroMalloc(sizeof(VI_FILE)); f->InternetFile = false; f->FileSize = FileSize(io); f->io = io; return f; } }
static DWORD WINAPI ThreadFunc(LPVOID Context) { CComPtr<IBindStatusCallback> dl; WCHAR path[MAX_PATH]; PWSTR p, q; HWND Dlg = (HWND) Context; ULONG dwContentLen, dwBytesWritten, dwBytesRead, dwStatus; ULONG dwCurrentBytesRead = 0; ULONG dwStatusLen = sizeof(dwStatus); BOOL bCancelled = FALSE; BOOL bTempfile = FALSE; BOOL bCab = FALSE; HINTERNET hOpen = NULL; HINTERNET hFile = NULL; HANDLE hOut = INVALID_HANDLE_VALUE; unsigned char lpBuffer[4096]; PCWSTR lpszAgent = L"RApps/1.0"; URL_COMPONENTS urlComponents; size_t urlLength, filenameLength; /* build the path for the download */ p = wcsrchr(AppInfo->szUrlDownload, L'/'); q = wcsrchr(AppInfo->szUrlDownload, L'?'); /* do we have a final slash separator? */ if (!p) goto end; /* prepare the tentative length of the filename, maybe we've to remove part of it later on */ filenameLength = wcslen(p) * sizeof(WCHAR); /* do we have query arguments in the target URL after the filename? account for them (e.g. https://example.org/myfile.exe?no_adware_plz) */ if (q && q > p && (q - p) > 0) filenameLength -= wcslen(q - 1) * sizeof(WCHAR); /* is this URL an update package for RAPPS? if so store it in a different place */ if (wcscmp(AppInfo->szUrlDownload, APPLICATION_DATABASE_URL) == 0) { bCab = TRUE; if (!GetStorageDirectory(path, _countof(path))) goto end; } else { if (FAILED(StringCbCopyW(path, sizeof(path), SettingsInfo.szDownloadDir))) goto end; } /* is the path valid? can we access it? */ if (GetFileAttributesW(path) == INVALID_FILE_ATTRIBUTES) { if (!CreateDirectoryW(path, NULL)) goto end; } /* append a \ to the provided file system path, and the filename portion from the URL after that */ if (FAILED(StringCbCatW(path, sizeof(path), L"\\"))) goto end; if (FAILED(StringCbCatNW(path, sizeof(path), p + 1, filenameLength))) goto end; if (!bCab && AppInfo->szSHA1[0] != 0 && GetFileAttributesW(path) != INVALID_FILE_ATTRIBUTES) { /* only open it in case of total correctness */ if (VerifyInteg(AppInfo->szSHA1, path)) goto run; } /* download it */ bTempfile = TRUE; CDownloadDialog_Constructor(Dlg, &bCancelled, IID_PPV_ARG(IBindStatusCallback, &dl)); if (dl == NULL) goto end; /* FIXME: this should just be using the system-wide proxy settings */ switch(SettingsInfo.Proxy) { case 0: /* preconfig */ hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); break; case 1: /* direct (no proxy) */ hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0); break; case 2: /* use proxy */ hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_PROXY, SettingsInfo.szProxyServer, SettingsInfo.szNoProxyFor, 0); break; default: /* preconfig */ hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); break; } if (!hOpen) goto end; hFile = InternetOpenUrlW(hOpen, AppInfo->szUrlDownload, NULL, 0, INTERNET_FLAG_PRAGMA_NOCACHE|INTERNET_FLAG_KEEP_CONNECTION, 0); if (!hFile) goto end; if (!HttpQueryInfoW(hFile, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &dwStatus, &dwStatusLen, NULL)) goto end; if(dwStatus != HTTP_STATUS_OK) { WCHAR szMsgText[MAX_STR_LEN]; if (!LoadStringW(hInst, IDS_UNABLE_TO_DOWNLOAD, szMsgText, sizeof(szMsgText) / sizeof(WCHAR))) goto end; MessageBoxW(hMainWnd, szMsgText, NULL, MB_OK | MB_ICONERROR); goto end; } dwStatusLen = sizeof(dwStatus); memset(&urlComponents, 0, sizeof(urlComponents)); urlComponents.dwStructSize = sizeof(urlComponents); if(FAILED(StringCbLengthW(AppInfo->szUrlDownload, sizeof(AppInfo->szUrlDownload), &urlLength))) goto end; urlLength /= sizeof(WCHAR); urlComponents.dwSchemeLength = urlLength + 1; urlComponents.lpszScheme = (LPWSTR)malloc(urlComponents.dwSchemeLength * sizeof(WCHAR)); urlComponents.dwHostNameLength = urlLength + 1; urlComponents.lpszHostName = (LPWSTR)malloc(urlComponents.dwHostNameLength * sizeof(WCHAR)); if(!InternetCrackUrlW(AppInfo->szUrlDownload, urlLength+1, ICU_DECODE | ICU_ESCAPE, &urlComponents)) goto end; if(urlComponents.nScheme == INTERNET_SCHEME_HTTP || urlComponents.nScheme == INTERNET_SCHEME_HTTPS) HttpQueryInfo(hFile, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &dwContentLen, &dwStatus, 0); if(urlComponents.nScheme == INTERNET_SCHEME_FTP) dwContentLen = FtpGetFileSize(hFile, &dwStatus); #ifdef USE_CERT_PINNING /* are we using HTTPS to download the RAPPS update package? check if the certificate is original */ if ((urlComponents.nScheme == INTERNET_SCHEME_HTTPS) && (wcscmp(AppInfo->szUrlDownload, APPLICATION_DATABASE_URL) == 0) && (!CertIsValid(hOpen, urlComponents.lpszHostName))) { WCHAR szMsgText[MAX_STR_LEN]; if (!LoadStringW(hInst, IDS_CERT_DOES_NOT_MATCH, szMsgText, sizeof(szMsgText) / sizeof(WCHAR))) goto end; MessageBoxW(Dlg, szMsgText, NULL, MB_OK | MB_ICONERROR); goto end; } #endif free(urlComponents.lpszScheme); free(urlComponents.lpszHostName); hOut = CreateFileW(path, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, 0, NULL); if (hOut == INVALID_HANDLE_VALUE) goto end; do { if (!InternetReadFile(hFile, lpBuffer, _countof(lpBuffer), &dwBytesRead)) { WCHAR szMsgText[MAX_STR_LEN]; if (!LoadStringW(hInst, IDS_INTERRUPTED_DOWNLOAD, szMsgText, _countof(szMsgText))) goto end; MessageBoxW(hMainWnd, szMsgText, NULL, MB_OK | MB_ICONERROR); goto end; } if (!WriteFile(hOut, &lpBuffer[0], dwBytesRead, &dwBytesWritten, NULL)) { WCHAR szMsgText[MAX_STR_LEN]; if (!LoadStringW(hInst, IDS_UNABLE_TO_WRITE, szMsgText, _countof(szMsgText))) goto end; MessageBoxW(hMainWnd, szMsgText, NULL, MB_OK | MB_ICONERROR); goto end; } dwCurrentBytesRead += dwBytesRead; dl->OnProgress(dwCurrentBytesRead, dwContentLen, 0, AppInfo->szUrlDownload); } while (dwBytesRead && !bCancelled); CloseHandle(hOut); hOut = INVALID_HANDLE_VALUE; if (bCancelled) goto end; /* if this thing isn't a RAPPS update and it has a SHA-1 checksum verify its integrity by using the native advapi32.A_SHA1 functions */ if (!bCab && AppInfo->szSHA1[0] != 0) { WCHAR szMsgText[MAX_STR_LEN]; /* change a few strings in the download dialog to reflect the verification process */ LoadStringW(hInst, IDS_INTEG_CHECK_TITLE, szMsgText, _countof(szMsgText)); SetWindowText(Dlg, szMsgText); SendMessageW(GetDlgItem(Dlg, IDC_DOWNLOAD_STATUS), WM_SETTEXT, 0, (LPARAM)path); /* this may take a while, depending on the file size */ if (!VerifyInteg(AppInfo->szSHA1, path)) { if (!LoadStringW(hInst, IDS_INTEG_CHECK_FAIL, szMsgText, _countof(szMsgText))) goto end; MessageBoxW(Dlg, szMsgText, NULL, MB_OK | MB_ICONERROR); goto end; } } ShowWindow(Dlg, SW_HIDE); run: /* run it */ if (!bCab) ShellExecuteW( NULL, L"open", path, NULL, NULL, SW_SHOWNORMAL ); end: if (hOut != INVALID_HANDLE_VALUE) CloseHandle(hOut); InternetCloseHandle(hFile); InternetCloseHandle(hOpen); if (bTempfile) { if (bCancelled || (SettingsInfo.bDelInstaller && !bCab)) DeleteFileW(path); } EndDialog(Dlg, 0); return 0; }
int FtpDownloadInet::_getFileSize(HINTERNET hRemoteFile) const { DWORD lpdwFileSizeHigh, lpdwFileSizeLow; lpdwFileSizeLow = FtpGetFileSize(hRemoteFile, &lpdwFileSizeHigh); return lpdwFileSizeLow; }
// Open the Internet file VW_FILE *VwOpenFile(char *path) { VW_FILE *f; HINTERNET hHttpFile; HINTERNET hInternet = InternetOpenA( "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); UINT size; UINT sizesize; char tmp[8]; // Validate arguments if (path == NULL) { return NULL; } if (hInternet == NULL) { return NULL; } hHttpFile = InternetOpenUrlA(hInternet, path, NULL, 0, INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_RELOAD, 0); if (hHttpFile == NULL) { InternetCloseHandle(hInternet); return NULL; } size = 0; sizesize = sizeof(size); ZeroMemory(tmp, sizeof(tmp)); if (strlen(path) >= 6) { CopyMemory(tmp, path, 6); } if (lstrcmpi(tmp, "ftp://") == 0) { // ftp DWORD high = 0; size = FtpGetFileSize(hHttpFile, &high); } else { UINT errorcode = 0; UINT errorcode_size = sizeof(errorcode); // http if (HttpQueryInfo(hHttpFile, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &size, &sizesize, NULL) == false) { size = 0; } if (HttpQueryInfo(hHttpFile, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &errorcode, &errorcode_size, NULL) == false || (errorcode / 100) != 2) { // HTTP getting error InternetCloseHandle(hInternet); InternetCloseHandle(hHttpFile); return NULL; } } f = ZeroMalloc(sizeof(VW_FILE)); f->hInternet = hInternet; f->hHttpFile = hHttpFile; f->FileSize = size; return f; }