static inline HINTERNET createInternetHandle(const String& userAgent, bool asynchronous) { String userAgentString = userAgent; HINTERNET internetHandle = InternetOpenW(userAgentString.charactersWithNullTermination(), INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, asynchronous ? INTERNET_FLAG_ASYNC : 0); if (asynchronous) InternetSetStatusCallback(internetHandle, &ResourceHandle::internetStatusCallback); return internetHandle; }
static void HttpGet(const wxString& url, std::shared_ptr<wxString>& str) { wxString host(url), req; // Usuń z adresu http[s]:// if (host.substr(0, 4) == "http") { host = host.substr(7); } // Wyciągnij wszystko po pierwszym '/' const size_t idx = host.find("/"); if (idx != wxString::npos) { req = host.substr(idx); host = host.substr(0, idx); } HINTERNET hInternet = InternetOpenW(ua.wc_str(), INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0); if (hInternet) { HINTERNET hConnect = InternetConnectW(hInternet, host.wc_str(), 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0); if (hConnect) { const wchar_t* parrAcceptTypes[] = { L"text/*", NULL }; HINTERNET hRequest = HttpOpenRequestW(hConnect, L"GET", req.wc_str(), NULL, NULL, parrAcceptTypes, 0, 0); if (hRequest) { BOOL bRequestSent = HttpSendRequestW(hRequest, NULL, 0, NULL, 0); if (bRequestSent) { const int nBuffSize = 10240; // 10 kB char buff[nBuffSize]; BOOL bKeepReading = true; DWORD dwBytesRead = -1; while (bKeepReading && dwBytesRead != 0) { bKeepReading = InternetReadFile(hRequest, buff, nBuffSize, &dwBytesRead); str->append(buff, dwBytesRead); } } InternetCloseHandle(hRequest); } InternetCloseHandle(hConnect); } InternetCloseHandle(hInternet); } }
// =============================================================== // ‘ункци¤ загрузки обновлени¤ void FtpDownload ( const std::wstring& serverName, const std::wstring& pathTo ) { // —оединение с сервером wprintf ( L"Connecting to server %s...\n", (L"ftp://" + serverName + L"/" + pathTo).c_str() ); HINTERNET hInet = InternetOpenW( L"GameInc updater agent", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0 ); HINTERNET hFtp = InternetConnectW( hInet, serverName.c_str(), INTERNET_DEFAULT_FTP_PORT, L"anonymous", L"*****@*****.**", INTERNET_SERVICE_FTP, 0, 0 ); if ( !hInet || !hFtp ) wprintf( L"Server are not availble. Goodbay\n" ); else if ( !DownloadFiles ( hFtp, pathTo ) ) wprintf( L"Some errors while update\n" ); // ќсвобождение ресурсов InternetCloseHandle( hFtp ); InternetCloseHandle( hInet ); } // ThreadProc
/** * Constructs a CWebService object and immediately establishes a connection to the "testman" Web Service. */ CWebService::CWebService() { /* Zero-initialize variables */ m_hHTTP = NULL; m_hHTTPRequest = NULL; m_TestID = NULL; /* Establish an internet connection to the "testman" server */ m_hInet = InternetOpenW(L"rosautotest", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); if(!m_hInet) FATAL("InternetOpenW failed\n"); m_hHTTP = InternetConnectW(m_hInet, szHostname, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0); if(!m_hHTTP) FATAL("InternetConnectW failed\n"); }
CString GetHttpString( LPCWSTR pszUrl ) { HINTERNET hInternet1 = NULL; HINTERNET hInternet2 = NULL; CString strPageContent; do { hInternet1 = InternetOpenW(NULL,INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,NULL); if (NULL == hInternet1) { break; } BOOL bOption = TRUE; BOOL bSetRes = InternetSetOption(hInternet1,INTERNET_OPTION_HTTP_DECODING,&bOption,sizeof(BOOL)); WCHAR szHeaderAdd[] = L"Accept-Encoding: gzip, deflate"; HINTERNET hInternet2 = InternetOpenUrlW(hInternet1,pszUrl,szHeaderAdd,wcslen(szHeaderAdd),INTERNET_FLAG_NO_CACHE_WRITE,NULL); if (NULL == hInternet2) { break; } DWORD dwReadDataLength = NULL; BOOL bRet = TRUE; do { CHAR chReadBuffer[4097]; bRet = InternetReadFile(hInternet2,chReadBuffer,4096,&dwReadDataLength); chReadBuffer[dwReadDataLength] = 0; strPageContent+=chReadBuffer; } while (bRet && NULL != dwReadDataLength); } while (FALSE); InternetCloseHandle(hInternet2); InternetCloseHandle(hInternet1); return strPageContent; }
static HINTERNET create_internet_session(IInternetBindInfo *bind_info) { LPWSTR global_user_agent = NULL; LPOLESTR user_agent = NULL; ULONG size = 0; HINTERNET ret; HRESULT hres; hres = IInternetBindInfo_GetBindString(bind_info, BINDSTRING_USER_AGENT, &user_agent, 1, &size); if(hres != S_OK || !size) global_user_agent = get_useragent(); ret = InternetOpenW(user_agent ? user_agent : global_user_agent, 0, NULL, NULL, INTERNET_FLAG_ASYNC); heap_free(global_user_agent); CoTaskMemFree(user_agent); if(!ret) { WARN("InternetOpen failed: %d\n", GetLastError()); return NULL; } InternetSetStatusCallbackW(ret, internet_status_callback); return ret; }
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; }
bool inet_get_resource_range(const wstring& url,__int64 start,__int64 length,inet_reader& reader) { wstring url2=inet_canonicalize_url(url); 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; wstring header; if(length<0) header=FORMATW(L"Range: bytes=%I64d-",start); else header=FORMATW(L"Range: bytes=%I64d-%I64d",start,start+length-1); handle_url=InternetOpenUrlW(handle_internet,url2.c_str(),header.c_str(),0,0,NULL); if(!handle_url) break; wchar_t buffer[4096]; DWORD buffer_len=sizeof(buffer); DWORD index=0; __int64 len=-1; if(HttpQueryInfoW(handle_url,HTTP_QUERY_CONTENT_LENGTH,buffer,&buffer_len,&index)) len=_wtoi64(buffer); reader.run(handle_url); } 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; wstring command=FORMATW(L"REST %I64d",start); FtpCommandW(handle_connect,FALSE,FTP_TRANSFER_TYPE_BINARY,command.c_str(),NULL,NULL); handle_file=FtpOpenFileW(handle_connect,parts.url_path.c_str(),GENERIC_READ,FTP_TRANSFER_TYPE_BINARY,NULL); if(!handle_file) break; reader.run(handle_file); } 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 true; }
__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; }
static void test_null(void) { HINTERNET hi, hc; static const WCHAR szServer[] = { 's','e','r','v','e','r',0 }; static const WCHAR szEmpty[] = { 0 }; static const WCHAR szUrl[] = { 'h','t','t','p',':','/','/','a','.','b','.','c',0 }; static const WCHAR szExpect[] = { 's','e','r','v','e','r',';',' ','s','e','r','v','e','r',0 }; WCHAR buffer[0x20]; BOOL r; DWORD sz; hi = InternetOpenW(NULL, 0, NULL, NULL, 0); ok(hi != NULL, "open failed\n"); hc = InternetConnectW(hi, NULL, 0, NULL, NULL, 0, 0, 0); ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error\n"); ok(hc == NULL, "connect failed\n"); hc = InternetConnectW(hi, NULL, 0, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0); ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error\n"); ok(hc == NULL, "connect failed\n"); hc = InternetConnectW(hi, NULL, 0, NULL, NULL, INTERNET_SERVICE_FTP, 0, 0); ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error\n"); ok(hc == NULL, "connect failed\n"); hc = InternetConnectW(NULL, szServer, 0, NULL, NULL, INTERNET_SERVICE_FTP, 0, 0); ok(GetLastError() == ERROR_INVALID_HANDLE, "wrong error\n"); ok(hc == NULL, "connect failed\n"); hc = InternetOpenUrlW(hi, NULL, NULL, 0, 0, 0); ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error\n"); ok(hc == NULL, "connect failed\n"); hc = InternetOpenUrlW(hi, szServer, NULL, 0, 0, 0); ok(GetLastError() == ERROR_INTERNET_UNRECOGNIZED_SCHEME, "wrong error\n"); ok(hc == NULL, "connect failed\n"); InternetCloseHandle(hi); r = InternetSetCookieW(NULL, NULL, NULL); ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error\n"); ok(r == FALSE, "return wrong\n"); r = InternetSetCookieW(szServer, NULL, NULL); ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error\n"); ok(r == FALSE, "return wrong\n"); r = InternetSetCookieW(szUrl, szServer, NULL); ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error\n"); ok(r == FALSE, "return wrong\n"); r = InternetSetCookieW(szUrl, szServer, szServer); ok(r == TRUE, "return wrong\n"); todo_wine { r = InternetSetCookieW(szUrl, NULL, szServer); ok(r == TRUE, "return wrong\n"); } r = InternetSetCookieW(szUrl, szServer, szEmpty); ok(r == TRUE, "return wrong\n"); r = InternetSetCookieW(szServer, NULL, szServer); todo_wine { ok(GetLastError() == ERROR_INTERNET_UNRECOGNIZED_SCHEME, "wrong error\n"); } ok(r == FALSE, "return wrong\n"); sz = 0; r = InternetGetCookieW(NULL, NULL, NULL, &sz); ok(GetLastError() == ERROR_INTERNET_UNRECOGNIZED_SCHEME, "wrong error\n"); ok( r == FALSE, "return wrong\n"); r = InternetGetCookieW(szServer, NULL, NULL, &sz); todo_wine { ok(GetLastError() == ERROR_INTERNET_UNRECOGNIZED_SCHEME, "wrong error\n"); } ok( r == FALSE, "return wrong\n"); sz = 0; r = InternetGetCookieW(szUrl, szServer, NULL, &sz); ok( r == TRUE, "return wrong\n"); todo_wine { ok( sz == 30, "sz wrong\n"); } sz = 0x20; memset(buffer, 0, sizeof buffer); r = InternetGetCookieW(szUrl, szServer, buffer, &sz); ok( r == TRUE, "return wrong\n"); todo_wine { ok( sz == lstrlenW(buffer), "sz wrong\n"); ok( !lstrcmpW(szExpect, buffer), "cookie data wrong\n"); } }
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; }
int64_t Downloader::downloadWin(Job* job, const Request& request, Downloader::Response* response) { QUrl url = request.url; QString verb = request.httpMethod; QFile* file = request.file; QString* mime = &response->mimeType; QString* contentDisposition = &response->contentDisposition; HWND parentWindow = defaultPasswordWindow; QString* sha1 = &response->hashSum; bool useCache = request.useCache; QCryptographicHash::Algorithm alg = request.alg; bool keepConnection = request.keepConnection; int timeout = request.timeout; bool interactive = request.interactive; QString initialTitle = job->getTitle(); job->setTitle(initialTitle + " / " + QObject::tr("Connecting")); if (sha1) sha1->clear(); QString server = url.host(); QString resource = url.path(); QString encQuery = url.query(QUrl::FullyEncoded); if (!encQuery.isEmpty()) resource.append('?').append(encQuery); QString agent("Npackd/"); agent.append(NPACKD_VERSION); agent += " (compatible; MSIE 9.0)"; HINTERNET internet = InternetOpenW((WCHAR*) agent.utf16(), INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, 0); if (internet == 0) { QString errMsg; WPMUtils::formatMessage(GetLastError(), &errMsg); job->setErrorMessage(errMsg); } if (job->shouldProceed()) { // change the timeout to 5 minutes DWORD rec_timeout = timeout * 1000; InternetSetOption(internet, INTERNET_OPTION_RECEIVE_TIMEOUT, &rec_timeout, sizeof(rec_timeout)); // enable automatic gzip decoding const DWORD INTERNET_OPTION_HTTP_DECODING = 65; BOOL b = TRUE; InternetSetOption(internet, INTERNET_OPTION_HTTP_DECODING, &b, sizeof(b)); job->setProgress(0.01); } HINTERNET hConnectHandle = 0; if (job->shouldProceed()) { INTERNET_PORT port = url.port(url.scheme() == "https" ? INTERNET_DEFAULT_HTTPS_PORT: INTERNET_DEFAULT_HTTP_PORT); hConnectHandle = InternetConnectW(internet, (WCHAR*) server.utf16(), port, 0, 0, INTERNET_SERVICE_HTTP, 0, 0); if (hConnectHandle == 0) { QString errMsg; WPMUtils::formatMessage(GetLastError(), &errMsg); job->setErrorMessage(errMsg); } } // flags: http://msdn.microsoft.com/en-us/library/aa383661(v=vs.85).aspx // We support accepting any mime file type since this is a simple download // of a file HINTERNET hResourceHandle = 0; if (job->shouldProceed()) { LPCTSTR ppszAcceptTypes[2]; ppszAcceptTypes[0] = L"*/*"; ppszAcceptTypes[1] = NULL; DWORD flags = (url.scheme() == "https" ? INTERNET_FLAG_SECURE : 0); if (keepConnection) flags |= INTERNET_FLAG_KEEP_CONNECTION; flags |= INTERNET_FLAG_RESYNCHRONIZE; if (!useCache) flags |= INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_RELOAD; hResourceHandle = HttpOpenRequestW(hConnectHandle, reinterpret_cast<LPCWSTR>(verb.utf16()), (WCHAR*) resource.utf16(), 0, 0, ppszAcceptTypes, flags, 0); if (hResourceHandle == 0) { QString errMsg; WPMUtils::formatMessage(GetLastError(), &errMsg); job->setErrorMessage(errMsg); } } if (job->shouldProceed()) { job->checkOSCall(HttpAddRequestHeadersW(hResourceHandle, L"Accept-Encoding: gzip, deflate", -1, HTTP_ADDREQ_FLAG_ADD)); } DWORD dwStatus, dwStatusSize = sizeof(dwStatus); // qDebug() << "download.5"; int callNumber = 0; while (job->shouldProceed()) { // qDebug() << "download.5.1"; DWORD sendRequestError = 0; if (!HttpSendRequestW(hResourceHandle, reinterpret_cast<LPCWSTR>(request.headers.utf16()), -1, const_cast<char*>(request.postData.data()), request.postData.length())) { sendRequestError = GetLastError(); } // http://msdn.microsoft.com/en-us/library/aa384220(v=vs.85).aspx if (!HttpQueryInfo(hResourceHandle, HTTP_QUERY_FLAG_NUMBER | HTTP_QUERY_STATUS_CODE, &dwStatus, &dwStatusSize, NULL)) { QString errMsg; WPMUtils::formatMessage(GetLastError(), &errMsg); job->setErrorMessage(errMsg); break; } /* qDebug() << callNumber << sendRequestError << dwStatus << request.httpMethod << request.url.toString(); */ // 2XX if (sendRequestError == 0) { DWORD hundreds = dwStatus / 100; if (hundreds == 2 || hundreds == 5) break; } // the InternetErrorDlg calls below can either handle // sendRequestError <> 0 or HTTP error code <> 2xx void* p = 0; // both calls to InternetErrorDlg should be enclosed by one // mutex, so that only one dialog will be shown loginDialogMutex.lock(); DWORD r; if (callNumber == 0) { r = InternetErrorDlg(0, hResourceHandle, sendRequestError, FLAGS_ERROR_UI_FILTER_FOR_ERRORS | FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS | FLAGS_ERROR_UI_FLAGS_GENERATE_DATA | FLAGS_ERROR_UI_FLAGS_NO_UI, &p); if (r == ERROR_SUCCESS && interactive) r = ERROR_INTERNET_FORCE_RETRY; } else if (interactive) { if (parentWindow) { r = InternetErrorDlg(parentWindow, hResourceHandle, sendRequestError, FLAGS_ERROR_UI_FILTER_FOR_ERRORS | FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS | FLAGS_ERROR_UI_FLAGS_GENERATE_DATA, &p); } else { QString e = inputPassword(hConnectHandle, dwStatus); //qDebug() << "inputPassword: "******"HTTP status code %1")).arg(dwStatus)); } } else if (r == ERROR_INTERNET_FORCE_RETRY) { // nothing } else if (r == ERROR_CANCELLED) { job->setErrorMessage(QObject::tr("Cancelled by the user")); } else if (r == ERROR_INVALID_HANDLE) { job->setErrorMessage(QObject::tr("Invalid handle")); } else { job->setErrorMessage(QString( QObject::tr("Unknown error %1 from InternetErrorDlg")).arg(r)); } loginDialogMutex.unlock(); if (!job->shouldProceed()) break; // read all the data before re-sending the request char smallBuffer[4 * 1024]; while (true) { DWORD read; if (!InternetReadFile(hResourceHandle, &smallBuffer, sizeof(smallBuffer), &read)) { QString errMsg; WPMUtils::formatMessage(GetLastError(), &errMsg); job->setErrorMessage(errMsg); goto out; } // qDebug() << "read some bytes " << read; if (read == 0) break; } callNumber++; }; // while (job->shouldProceed()) out: if (job->shouldProceed()) { // http://msdn.microsoft.com/en-us/library/aa384220(v=vs.85).aspx if (!HttpQueryInfo(hResourceHandle, HTTP_QUERY_FLAG_NUMBER | HTTP_QUERY_STATUS_CODE, &dwStatus, &dwStatusSize, NULL)) { QString errMsg; WPMUtils::formatMessage(GetLastError(), &errMsg); job->setErrorMessage(errMsg); } else { // 2XX if (dwStatus / 100 != 2) { job->setErrorMessage(QString( QObject::tr("HTTP status code %1")).arg(dwStatus)); } } } if (job->shouldProceed()) { job->setProgress(0.03); job->setTitle(initialTitle + " / " + QObject::tr("Downloading")); } // MIME type if (job->shouldProceed()) { if (mime) { WCHAR mimeBuffer[1024]; DWORD bufferLength = sizeof(mimeBuffer); DWORD index = 0; if (!HttpQueryInfoW(hResourceHandle, HTTP_QUERY_CONTENT_TYPE, &mimeBuffer, &bufferLength, &index)) { QString errMsg; WPMUtils::formatMessage(GetLastError(), &errMsg); job->setErrorMessage(errMsg); } else { mime->setUtf16((ushort*) mimeBuffer, bufferLength / 2); } } } bool gzip = false; // Content-Encoding if (job->shouldProceed()) { WCHAR contentEncodingBuffer[1024]; DWORD bufferLength = sizeof(contentEncodingBuffer); DWORD index = 0; if (HttpQueryInfoW(hResourceHandle, HTTP_QUERY_CONTENT_ENCODING, &contentEncodingBuffer, &bufferLength, &index)) { QString contentEncoding; contentEncoding.setUtf16((ushort*) contentEncodingBuffer, bufferLength / 2); gzip = contentEncoding == "gzip" || contentEncoding == "deflate"; } job->setProgress(0.04); } // Content-Disposition if (job->shouldProceed()) { if (contentDisposition) { WCHAR cdBuffer[1024]; wcscpy(cdBuffer, L"Content-Disposition"); DWORD bufferLength = sizeof(cdBuffer); DWORD index = 0; if (HttpQueryInfoW(hResourceHandle, HTTP_QUERY_CUSTOM, &cdBuffer, &bufferLength, &index)) { contentDisposition->setUtf16((ushort*) cdBuffer, bufferLength / 2); } } } int64_t contentLength = -1; // content length if (job->shouldProceed()) { WCHAR contentLengthBuffer[100]; DWORD bufferLength = sizeof(contentLengthBuffer); DWORD index = 0; if (HttpQueryInfoW(hResourceHandle, HTTP_QUERY_CONTENT_LENGTH, contentLengthBuffer, &bufferLength, &index)) { QString s; s.setUtf16((ushort*) contentLengthBuffer, bufferLength / 2); bool ok; contentLength = s.toLongLong(&ok, 10); if (!ok) contentLength = 0; } job->setProgress(0.05); } if (job->shouldProceed()) { Job* sub = job->newSubJob(0.95, QObject::tr("Reading the data")); readData(sub, hResourceHandle, file, sha1, gzip, contentLength, alg); if (!sub->getErrorMessage().isEmpty()) job->setErrorMessage(sub->getErrorMessage()); } if (hResourceHandle) InternetCloseHandle(hResourceHandle); if (hConnectHandle) InternetCloseHandle(hConnectHandle); if (internet) InternetCloseHandle(internet); if (job->shouldProceed()) job->setProgress(1); job->setTitle(initialTitle); job->complete(); return contentLength; }
void WinHttpStream::initRequest() { TextParser<wchar_t> parser; if (!parser(L"%1://%[*-_.a-zA-Z0-9:@]2%%[/](*)*3",url)) throw FileOpenError(THISLOCATION,ERROR_FILE_NOT_FOUND,url); String protocol = parser[1].str(); String hostident = parser[2].str(); String::SplitIterator hostidentsplit = hostident.split('@'); String ident; String domain; domain = hostidentsplit.getNext(); while (hostidentsplit.hasItems()) { ident = ident + domain + ConstStrW('@'); domain = hostidentsplit.getNext(); } String path = parser[3].str(); natural port; String username; String password; bool secure; if (parser( L"%1:%u2",domain)) { domain = parser[1].str(); port = parser[2]; secure = false; } else if (protocol == ConstStrW(L"http")) { port = INTERNET_DEFAULT_HTTP_PORT; secure = false; } else if (protocol == ConstStrW(L"https")) { port = INTERNET_DEFAULT_HTTPS_PORT; secure = true; } else throw FileOpenError(THISLOCATION,ERROR_NOT_FOUND,url); if (!ident.empty()) { if (parser(L"%1:%2@",ident)) { username = parser[1].str(); password = parser[2].str(); } else { throw FileMsgException(THISLOCATION,0,url,"Invalid identification field in the url"); } } DWORD accessType; switch (settings.proxyMode) { case pmManual: accessType = INTERNET_OPEN_TYPE_PROXY; case pmDirect: accessType = INTERNET_OPEN_TYPE_DIRECT; case pmAuto: accessType = INTERNET_OPEN_TYPE_PRECONFIG; } TextFormatBuff<wchar_t> fmt; String proxyName; if (accessType == INTERNET_OPEN_TYPE_PROXY) { fmt(L"%1:%2") << settings.proxyAddr << settings.proxyPort; proxyName = fmt.write(); } if (hInternet) InternetCloseHandle(hInternet); hInternet = InternetOpenW(settings.userAgent.cStr(),accessType,proxyName.cStr(),0,0); if (hInternet == 0) throw FileMsgException(THISLOCATION,GetLastError(),url,"Cannot initialize WinInet"); if (hConnect) InternetCloseHandle(hConnect); hConnect = InternetConnectW(hInternet,domain.cStr(),(INTERNET_PORT)port, username.empty()?0:username.cStr(), password.empty()?0:password.cStr(), INTERNET_SERVICE_HTTP ,0,0); if (hConnect == 0) throw FileMsgException(THISLOCATION,GetLastError(),url,"Cannot connect remote site"); DWORD reqFlags = INTERNET_FLAG_NO_UI |INTERNET_FLAG_HYPERLINK ; if (redirDisabled) reqFlags|=INTERNET_FLAG_NO_AUTO_REDIRECT ; if (!settings.cookiesEnabled) reqFlags|=INTERNET_FLAG_NO_COOKIES; if (secure) reqFlags|=INTERNET_FLAG_SECURE; hHTTPConn = HttpOpenRequestW(hConnect,String(method).cStr(),path.cStr(), 0,0,0,reqFlags,0); if (hHTTPConn == 0) throw FileMsgException(THISLOCATION,GetLastError(),url,"Cannot connect remote site"); AutoArray<wchar_t> hdrall; for (HeaderMap::Iterator iter = hdrmap.getFwIter(); iter.hasItems();) { const HeaderMap::Entity &e = iter.getNext(); fmt(L"%1: %2\n") << e.key << e.value; hdrall.append(fmt.write()); } if (!hdrall.empty() && !HttpAddRequestHeadersW(hHTTPConn,hdrall.data(),(DWORD)hdrall.length(),HTTP_ADDREQ_FLAG_REPLACE|HTTP_ADDREQ_FLAG_ADD)) throw FileMsgException(THISLOCATION,GetLastError(),url,"AddRequest failed"); if (!HttpSendRequestW(hHTTPConn,0,0,postBuffer.data(),(DWORD)postBuffer.length())) { bool stillError = true; DWORD dwError = GetLastError(); if (dwError == ERROR_INTERNET_INVALID_CA && settings.allowUntrustedCert) { DWORD dwFlags; DWORD dwBuffLen = sizeof(dwFlags); InternetQueryOption (hHTTPConn, INTERNET_OPTION_SECURITY_FLAGS, (LPVOID)&dwFlags, &dwBuffLen); dwFlags |= SECURITY_FLAG_IGNORE_UNKNOWN_CA; InternetSetOption (hHTTPConn, INTERNET_OPTION_SECURITY_FLAGS, &dwFlags, sizeof (dwFlags) ); if (HttpSendRequestW(hHTTPConn,0,0,postBuffer.data(),(DWORD)postBuffer.length())) stillError = false; } if (stillError) throw FileMsgException(THISLOCATION,GetLastError(),url,"Failed to SendRequest"); } postBuffer.clear(); }
/*! * @brief Initialise the HTTP(S) connection. * @param remote Pointer to the remote instance with the HTTP(S) transport details wired in. * @param sock Reference to the original socket FD passed to metsrv (ignored); * @return Indication of success or failure. */ static BOOL server_init_wininet(Transport* transport) { URL_COMPONENTS bits; wchar_t tmpHostName[URL_SIZE]; wchar_t tmpUrlPath[URL_SIZE]; HttpTransportContext* ctx = (HttpTransportContext*)transport->ctx; dprintf("[WININET] Initialising ..."); // configure proxy if (ctx->proxy) { dprintf("[DISPATCH] Configuring with proxy: %S", ctx->proxy); ctx->internet = InternetOpenW(ctx->ua, INTERNET_OPEN_TYPE_PROXY, ctx->proxy, NULL, 0); } else { ctx->internet = InternetOpenW(ctx->ua, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); } if (!ctx->internet) { dprintf("[DISPATCH] Failed InternetOpenW: %d", GetLastError()); return FALSE; } dprintf("[DISPATCH] Configured hInternet: 0x%.8x", ctx->internet); // The InternetCrackUrl method was poorly designed... ZeroMemory(tmpHostName, sizeof(tmpHostName)); ZeroMemory(tmpUrlPath, sizeof(tmpUrlPath)); ZeroMemory(&bits, sizeof(bits)); bits.dwStructSize = sizeof(bits); bits.dwHostNameLength = URL_SIZE - 1; bits.lpszHostName = tmpHostName; bits.dwUrlPathLength = URL_SIZE - 1; bits.lpszUrlPath = tmpUrlPath; dprintf("[DISPATCH] About to crack URL: %S", transport->url); InternetCrackUrlW(transport->url, 0, 0, &bits); SAFE_FREE(ctx->uri); ctx->uri = _wcsdup(tmpUrlPath); transport->comms_last_packet = current_unix_timestamp(); dprintf("[DISPATCH] Configured URI: %S", ctx->uri); dprintf("[DISPATCH] Host: %S Port: %u", tmpHostName, bits.nPort); // Allocate the connection handle ctx->connection = InternetConnectW(ctx->internet, tmpHostName, bits.nPort, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0); if (!ctx->connection) { dprintf("[DISPATCH] Failed InternetConnect: %d", GetLastError()); return FALSE; } if (ctx->proxy) { if (ctx->proxy_user) { InternetSetOptionW(ctx->connection, INTERNET_OPTION_PROXY_USERNAME, ctx->proxy_user, (DWORD)wcslen(ctx->proxy_user)); } if (ctx->proxy_pass) { InternetSetOptionW(ctx->connection, INTERNET_OPTION_PROXY_PASSWORD, ctx->proxy_pass, (DWORD)wcslen(ctx->proxy_pass)); } } dprintf("[DISPATCH] Configured hConnection: 0x%.8x", ctx->connection); return TRUE; }