LPBYTE DownloadToMemory(IN LPCTSTR lpszURL, OUT PDWORD_PTR lpSize) { LPBYTE lpszReturn = 0; *lpSize = 0; const HINTERNET hSession = InternetOpen(TEXT("GetGitHubRepositoryList"), INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, INTERNET_FLAG_NO_COOKIES); if (hSession) { URL_COMPONENTS uc = { 0 }; TCHAR HostName[MAX_PATH]; TCHAR UrlPath[MAX_PATH]; uc.dwStructSize = sizeof(uc); uc.lpszHostName = HostName; uc.lpszUrlPath = UrlPath; uc.dwHostNameLength = MAX_PATH; uc.dwUrlPathLength = MAX_PATH; InternetCrackUrl(lpszURL, 0, 0, &uc); const HINTERNET hConnection = InternetConnect(hSession, HostName, INTERNET_DEFAULT_HTTPS_PORT, 0, 0, INTERNET_SERVICE_HTTP, 0, 0); if (hConnection) { const HINTERNET hRequest = HttpOpenRequest(hConnection, TEXT("GET"), UrlPath, 0, 0, 0, INTERNET_FLAG_SECURE | INTERNET_FLAG_RELOAD, 0); if (hRequest) { HttpSendRequest(hRequest, 0, 0, 0, 0); lpszReturn = (LPBYTE)GlobalAlloc(GMEM_FIXED, 1); DWORD dwRead; static BYTE szBuf[1024 * 4]; LPBYTE lpTmp; for (;;) { if (!InternetReadFile(hRequest, szBuf, (DWORD)sizeof(szBuf), &dwRead) || !dwRead) break; lpTmp = (LPBYTE)GlobalReAlloc(lpszReturn, (SIZE_T)(*lpSize + dwRead), GMEM_MOVEABLE); if (lpTmp == NULL) break; lpszReturn = lpTmp; CopyMemory(lpszReturn + *lpSize, szBuf, dwRead); *lpSize += dwRead; } InternetCloseHandle(hRequest); } InternetCloseHandle(hConnection); } InternetCloseHandle(hSession); } return lpszReturn; }
bool MEmblemMgr::GetEmblemPath(char* pszFilePath, const char* pszURL) { //// Parse URL ////////////////// #define URLPATH_LEN 256 char szFileName[URLPATH_LEN] = ""; URL_COMPONENTS uc; ZeroMemory(&uc, sizeof uc); uc.dwStructSize = sizeof uc; uc.lpszUrlPath = szFileName; uc.dwUrlPathLength = URLPATH_LEN; if (!InternetCrackUrl(pszURL, lstrlen(pszURL), ICU_DECODE, &uc)) { // GetLastError() return false; } PathStripPath(szFileName); char szFullPath[_MAX_DIR]; strcpy(szFullPath, GetEmblemBaseDir()); strcat(szFullPath, "/"); strcat(szFullPath, szFileName); // out strcpy(pszFilePath, szFullPath); return true; }
void CMiniMule::_OnBeforeNavigate2(LPDISPATCH pDisp, VARIANT* URL, VARIANT* /*Flags*/, VARIANT* /*TargetFrameName*/, VARIANT* /*PostData*/, VARIANT* /*Headers*/, BOOL* Cancel) { ASSERT( GetCurrentThreadId() == _uMainThreadId ); CString strURL(V_BSTR(URL)); TRACE(_T("%hs: %s\n"), __FUNCTION__, strURL); // No external links allowed! TCHAR szScheme[INTERNET_MAX_SCHEME_LENGTH]; URL_COMPONENTS Url = {0}; Url.dwStructSize = sizeof(Url); Url.lpszScheme = szScheme; Url.dwSchemeLength = ARRSIZE(szScheme); if (InternetCrackUrl(strURL, 0, 0, &Url) && Url.dwSchemeLength) { if (Url.nScheme != INTERNET_SCHEME_UNKNOWN // <absolute local file path> && Url.nScheme != INTERNET_SCHEME_RES // res://... && Url.nScheme != INTERNET_SCHEME_FILE) // file://... { *Cancel = TRUE; return; } } OnBeforeNavigate(pDisp, strURL); }
URL::URL(const std::wstring &url) : m_iSchemeId(INTERNET_SCHEME_UNKNOWN), m_uiPortNumber(INTERNET_INVALID_PORT_NUMBER) { URL_COMPONENTS components; SecureZeroMemory(&components, sizeof(URL_COMPONENTS)); components.dwStructSize = sizeof(URL_COMPONENTS); components.dwSchemeLength = DWORD(-1); components.dwHostNameLength = DWORD(-1); components.dwUserNameLength = DWORD(-1); components.dwPasswordLength = DWORD(-1); components.dwUrlPathLength = DWORD(-1); components.dwExtraInfoLength = DWORD(-1); if(InternetCrackUrl(url.c_str(), 0, 0, &components)) { INIT_URL_STRING(0, Scheme); INIT_URL_STRING(0, HostName); INIT_URL_STRING(0, UserName); INIT_URL_STRING(0, Password); INIT_URL_STRING(1, UrlPath); INIT_URL_STRING(1, ExtraInfo); m_iSchemeId = int16_t(components.nScheme); m_uiPortNumber = components.nPort; } }
CNetRequestImpl::CNetRequestImpl(const char* method, const String& strUrl) { pszErrFunction = NULL; hInet = NULL, hConnection = NULL, hRequest = NULL; memset(&uri, 0, sizeof(uri) ); m_pInstance = this; CAtlStringW strUrlW(strUrl.c_str()); do { if ( !isLocalHost(strUrl.c_str()) && !SetupInternetConnection(strUrlW) ) { pszErrFunction = L"SetupInternetConnection"; break; } hInet = InternetOpen(_T("rhodes-wm"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, NULL ); if ( !hInet ) { pszErrFunction = L"InternetOpen"; break; } DWORD dwUrlLength = 1024; CAtlStringW strCanonicalUrlW; if ( !InternetCanonicalizeUrl( strUrlW, strCanonicalUrlW.GetBuffer(dwUrlLength), &dwUrlLength, 0) ) { pszErrFunction = _T("InternetCanonicalizeUrl"); break; } strCanonicalUrlW.ReleaseBuffer(); alloc_url_components( &uri, strCanonicalUrlW ); if( !InternetCrackUrl( strCanonicalUrlW, strCanonicalUrlW.GetLength(), 0, &uri ) ) { pszErrFunction = L"InternetCrackUrl"; break; } hConnection = InternetConnect( hInet, uri.lpszHostName, uri.nPort, _T("anonymous"), NULL, INTERNET_SERVICE_HTTP, 0, 0 ); if ( !hConnection ) { pszErrFunction = L"InternetConnect"; break; } strReqUrlW = uri.lpszUrlPath; strReqUrlW += uri.lpszExtraInfo; hRequest = HttpOpenRequest( hConnection, CAtlStringW(method), strReqUrlW, NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_NO_CACHE_WRITE, NULL ); if ( !hRequest ) { pszErrFunction = L"HttpOpenRequest"; break; } }while(0); }
void Url::_extractpath(wchar_t* url) { BOOL rslt; wchar_t szExtraInfo[1024]; memset(&m_components,0, sizeof(m_components)); m_components.dwStructSize = sizeof(m_components); m_components.lpszHostName = m_hostname; m_components.dwHostNameLength = sizeof(m_hostname); m_components.lpszUrlPath = m_path; m_components.dwUrlPathLength = sizeof(m_path); m_components.lpszExtraInfo = szExtraInfo; m_components.dwExtraInfoLength = sizeof(szExtraInfo); m_components.lpszScheme = m_lpszScheme; m_components.dwSchemeLength = sizeof(m_lpszScheme); rslt = InternetCrackUrl(url, url != NULL ? wcslen(url) : 0, ICU_ESCAPE, &m_components); if (rslt == TRUE) { _extractfilename(m_path); } else { m_filename[0] = L'\0'; m_hostname[0] = L'\0'; m_path[0] = L'\0'; } }
void GWebCacheSocket::OnConnect(int error_code) { if(error_code!=0) { Close(); p_parent->ReportBadURL(m_url.c_str()); return; } char buf[8192]; // Extract the pertinent info from this url DWORD flags=ICU_DECODE|ICU_ESCAPE; URLComponents uc; InternetCrackUrl(m_url.c_str(),0,flags,(URL_COMPONENTS *)&uc); strcpy(buf,"GET "); strcat(buf,uc.m_data.lpszUrlPath); strcat(buf,uc.m_data.lpszExtraInfo); strcat(buf," HTTP/1.1\r\n"); strcat(buf,"Accept: */*\r\n"); strcat(buf,"Accept-Language: en-us\r\n"); strcat(buf,"---------------: ----- -------\r\n"); strcat(buf,"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 1.0.3705)\r\n"); strcat(buf,"Host: "); strcat(buf,uc.m_data.lpszHostName); strcat(buf,"\r\n"); strcat(buf,"Connection: Keep-Alive\r\n"); strcat(buf,"\r\n"); SendSocketData(buf,(UINT)strlen(buf)); }
void createConnection (URL::OpenStreamProgressCallback* progressCallback, void* progressCallbackContext) { static HINTERNET sessionHandle = InternetOpen (_T("juce"), INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, 0); close(); if (sessionHandle != 0) { // break up the url.. const int fileNumChars = 65536; const int serverNumChars = 2048; const int usernameNumChars = 1024; const int passwordNumChars = 1024; HeapBlock<TCHAR> file (fileNumChars), server (serverNumChars), username (usernameNumChars), password (passwordNumChars); URL_COMPONENTS uc = { 0 }; uc.dwStructSize = sizeof (uc); uc.lpszUrlPath = file; uc.dwUrlPathLength = fileNumChars; uc.lpszHostName = server; uc.dwHostNameLength = serverNumChars; uc.lpszUserName = username; uc.dwUserNameLength = usernameNumChars; uc.lpszPassword = password; uc.dwPasswordLength = passwordNumChars; if (InternetCrackUrl (address.toWideCharPointer(), 0, 0, &uc)) openConnection (uc, sessionHandle, progressCallback, progressCallbackContext); } }
BOOL IEHistoryLog::GetLog( Json::Value &log_value ) { CComPtr<IUrlHistoryStg2> history; CComPtr<IEnumSTATURL> enum_staturl; STATURL url = {0}; url.cbSize = sizeof(url); std::map<CString, int> url_map; if(FAILED(CoCreateInstance(CLSID_CUrlHistory, NULL, CLSCTX_INPROC_SERVER, IID_IUrlHistoryStg2, (void**)&history))) { return FALSE; } if(FAILED(history->EnumUrls(&enum_staturl))) { return FALSE; } enum_staturl->Reset(); CString url_str; ULONG fetched = 0; while(SUCCEEDED(enum_staturl->Next(1, &url, &fetched))) { if(fetched == 0) { break; } if (url.pwcsUrl) { if (_wcsnicmp(url.pwcsUrl, L"http", 4) == 0) { URL_COMPONENTS url_component = {0}; url_component.dwStructSize = sizeof(url_component); TCHAR host_name[INTERNET_MAX_HOST_NAME_LENGTH] = {0}; url_component.dwHostNameLength = INTERNET_MAX_HOST_NAME_LENGTH; url_component.lpszHostName = host_name; InternetCrackUrl(url.pwcsUrl, wcslen(url.pwcsUrl), ICU_DECODE, &url_component); _tcslwr_s(host_name); url_map[host_name]++; } } if (url.pwcsTitle) { CoTaskMemFree(url.pwcsTitle); url.pwcsTitle = NULL; } if (url.pwcsUrl) { CoTaskMemFree(url.pwcsUrl); url.pwcsUrl = NULL; } } for (std::map<CString, int>::iterator it = url_map.begin(); it != url_map.end(); ++it) { Json::Value item; item["host_name"] = CT2A(it->first.GetString()).m_psz; item["count"] = it->second; log_value.append(item); } return TRUE; }
STDMETHODIMP CXMLHttpRequest::open(BSTR bstrMethod, BSTR bstrUrl,VARIANT varAsync,VARIANT bstrUser,VARIANT bstrPassword) { ATLTRACE(_T("CXMLHttpRequest::open\n")); // do not open if there is a send active if (NULL != m_hThread) { DWORD exitCode = 0; BOOL rc = ::GetExitCodeThread(m_hThread, &exitCode); if (!rc || STILL_ACTIVE == exitCode) return E_FAIL; ::CloseHandle(m_hThread); m_hThread = NULL; } if (V_VT(&varAsync) != VT_BOOL) return E_INVALIDARG; _bstr_t method = bstrMethod; if (0 == method.length()) return E_INVALIDARG; _bstr_t url = bstrUrl; if (0 == url.length()) return E_INVALIDARG; TCHAR hostName[INTERNET_MAX_PATH_LENGTH] = _T(""); TCHAR strPathName[INTERNET_MAX_PATH_LENGTH] = _T(""); URL_COMPONENTS urlComponents; memset(&urlComponents, 0, sizeof(URL_COMPONENTS)); urlComponents.dwStructSize = sizeof(URL_COMPONENTS); urlComponents.lpszHostName = hostName; urlComponents.dwHostNameLength = INTERNET_MAX_PATH_LENGTH; urlComponents.lpszUrlPath = strPathName; urlComponents.dwUrlPathLength = INTERNET_MAX_PATH_LENGTH; if (!InternetCrackUrl(url, url.length(), 0, &urlComponents)) return E_INVALIDARG; m_Method = method; m_HostName = hostName; if (urlComponents.nPort != 0) m_Port = urlComponents.nPort; m_URLPath = strPathName; m_bAsync = (VARIANT_TRUE == V_BOOL(&varAsync)) ? true : false; if (VT_BSTR == V_VT(&bstrUser)) m_User = V_BSTR(&bstrUser); else m_User = _T(""); if (VT_BSTR == V_VT(&bstrPassword) && m_User.length() > 0) m_Password = V_BSTR(&bstrPassword); else m_Password = _T(""); return S_OK; }
void CPanelDoc::SetPathName(LPCTSTR lpszPathName, BOOL bAddToMRU) { URL_COMPONENTS urlComponents; CString strTitle; urlComponents.dwStructSize = sizeof (urlComponents); urlComponents.dwSchemeLength = 1; urlComponents.lpszScheme = NULL; urlComponents.dwHostNameLength = 1; urlComponents.lpszHostName = NULL; urlComponents.dwUserNameLength = 0; urlComponents.lpszUserName = NULL; urlComponents.dwPasswordLength = 0; urlComponents.lpszPassword = NULL; urlComponents.dwUrlPathLength = 1; urlComponents.lpszUrlPath = NULL; urlComponents.dwExtraInfoLength = 1; urlComponents.lpszExtraInfo = NULL; // Do not call base classes SetPathName. There is // a restriction that path name must not be longer // than MAX_PATH characters. m_strPathName = lpszPathName; strTitle = m_strPathName; if (InternetCrackUrl (lpszPathName, strlen (lpszPathName), 0, &urlComponents)) { if (urlComponents.lpszExtraInfo != NULL) { CString strExtraInfo; if (urlComponents.dwExtraInfoLength == 0) { strExtraInfo = urlComponents.lpszExtraInfo; } else { strExtraInfo = CString (urlComponents.lpszExtraInfo).Left (urlComponents.dwExtraInfoLength); }; strTitle = strTitle.Left (strTitle.GetLength () - strExtraInfo.GetLength ()); }; }; SetTitle (strTitle); }
//注意: strLocation 只是存放的的路径名,不包含文件名 void ActualllyAddUrlDownload(const CString &strUrl, const CString & strLocation, bool bNewTask,CFileTaskItem* pFileTaskItem) { //CString strUrlEncoded = URLEncode( strUrl ); //CString urlDecoded = OptUtf8ToStr(URLDecode(strUrl)); //CString urlConverted = UrlConvert(strUrl); // VC-SearchDream[2007-04-06]: Direct HTTP and FTP DownLoad if ( (strUrl.Left(7).CompareNoCase(_T("http://")) == 0) || (strUrl.Left(6).CompareNoCase(_T("ftp://")) == 0) ) { TCHAR szScheme[INTERNET_MAX_SCHEME_LENGTH]; TCHAR szHostName[INTERNET_MAX_HOST_NAME_LENGTH]; TCHAR szUrlPath[INTERNET_MAX_PATH_LENGTH]; TCHAR szUserName[INTERNET_MAX_USER_NAME_LENGTH]; TCHAR szPassword[INTERNET_MAX_PASSWORD_LENGTH]; TCHAR szExtraInfo[INTERNET_MAX_URL_LENGTH]; // URL Split URL_COMPONENTS Url = {0}; Url.dwStructSize = sizeof(Url); Url.lpszScheme = szScheme; Url.dwSchemeLength = ARRSIZE(szScheme); Url.lpszHostName = szHostName; Url.dwHostNameLength = ARRSIZE(szHostName); Url.lpszUserName = szUserName; Url.dwUserNameLength = ARRSIZE(szUserName); Url.lpszPassword = szPassword; Url.dwPasswordLength = ARRSIZE(szPassword); Url.lpszUrlPath = szUrlPath; Url.dwUrlPathLength = ARRSIZE(szUrlPath); Url.lpszExtraInfo = szExtraInfo; Url.dwExtraInfoLength = ARRSIZE(szExtraInfo); if (InternetCrackUrl(strUrl, 0, 0, &Url) && Url.dwHostNameLength > 0) { CPartFile* pPartFile=NULL; if( CGlobalVariable::filemgr.AddDownLoadRequest(strUrl,strLocation, pPartFile,bNewTask) ) //urlConverted { ASSERT( pPartFile ); if( bNewTask ) { pPartFile->RecordUrlSource( strUrl,true,0,sfStartDown ); } else /// 任务文件丢失后恢复下载任务 { pPartFile->StopFile(); if(pFileTaskItem) { pFileTaskItem->m_metBakId = pPartFile->GetMetBakId(); pPartFile->LoadUrlSiteList( pFileTaskItem->m_lMetaLinkURLList ); } } } } } }
BOOL CUrlCrack::Crack(LPCTSTR pszUrl) { DWORD urlLen = _tcslen(pszUrl) * 2; TCHAR *pszCanUrl = new TCHAR[urlLen]; if (!InternetCanonicalizeUrl(pszUrl, pszCanUrl, &urlLen, ICU_BROWSER_MODE)) { delete[] pszCanUrl; if (GetLastError () == ERROR_INSUFFICIENT_BUFFER) { pszCanUrl = new TCHAR[urlLen + 1]; if (!InternetCanonicalizeUrl(pszUrl, pszCanUrl, &urlLen, ICU_BROWSER_MODE)) { delete[] pszCanUrl; return FALSE; } } else return FALSE; } URL_COMPONENTS url_comp; ZeroMemory(&url_comp, sizeof(url_comp)); url_comp.dwStructSize = sizeof(url_comp); url_comp.lpszHostName = m_szHost; url_comp.lpszPassword = m_szPassword; url_comp.lpszScheme = m_szScheme; url_comp.lpszUrlPath = m_szPath; url_comp.lpszUserName = m_szUser; url_comp.dwHostNameLength = UP_HOSTNAME_SIZE; url_comp.dwPasswordLength = UP_PASSWORD_SIZE; url_comp.dwSchemeLength = UP_SCHEME_SIZE; url_comp.dwUrlPathLength = UP_PATH_SIZE; url_comp.dwUserNameLength = UP_USERNAME_SIZE; if (!InternetCrackUrl(pszCanUrl, urlLen, 0, &url_comp)) { delete[] pszCanUrl; return FALSE; } delete[] pszCanUrl; // if (url_comp.nScheme != INTERNET_SCHEME_HTTP) // return FALSE; m_wPort = url_comp.nPort; return TRUE; }
void GWebCacheSocket::GetHostFile(char *url) { m_url=url; m_url+="?hostfile=1"; // Extract the pertinent info from this url DWORD flags=ICU_DECODE|ICU_ESCAPE; URLComponents uc; BOOL ret = InternetCrackUrl(m_url.c_str(),0,flags,(URL_COMPONENTS *)&uc); Create(); Connect(uc.m_data.lpszHostName,uc.m_data.nPort); }
unsigned int __stdcall speedtest(void *param) { int i; unsigned long started; unsigned long elapsed; unsigned long speed; unsigned long w; char fbuf[4096]; struct spds spd = *(struct spds *)param; struct spds *pspd = (struct spds *)param; HINTERNET ih = InternetOpen("Mozilla/4.0 (compatible)", INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, 0); HINTERNET ich; HINTERNET ftph; URL_COMPONENTS url; pspd->gotinfo = true; url.lpszUserName = (char *)malloc(128); url.dwUserNameLength = 128; url.lpszPassword = (char *)malloc(128); url.dwPasswordLength = 128; url.lpszHostName = (char *)malloc(128); url.dwHostNameLength = 128; url.dwStructSize = sizeof(url); InternetCrackUrl(spd.url, 0, 0, &url); ich = InternetConnect(ih, url.lpszHostName, url.nPort, url.lpszUserName, url.lpszPassword, INTERNET_SERVICE_FTP, 0, 0); if(ich) { ftph = FtpOpenFile(ich, "speed.test", GENERIC_WRITE, FTP_TRANSFER_TYPE_BINARY, 0); if(ftph) { memset(fbuf, 'A', sizeof(fbuf)); started = GetTickCount() / 1000; for(i = 0; i < spd.size * 1024 / 4096; i++) { InternetWriteFile(ftph, fbuf, sizeof(fbuf), &w); elapsed = GetTickCount() / 1000 - started; } speed = spd.size / ((elapsed) ? elapsed : 1); irc_privmsg(spd.target, "speedtest complete (upload speed: %luKB/s)", speed); } } InternetCloseHandle(ftph); InternetCloseHandle(ich); InternetCloseHandle(ih); free(url.lpszUserName); free(url.lpszPassword); free(url.lpszHostName); clearthread(spd.tnum); _endthreadex(0); return 0; }
HippoURLParser::HippoURLParser(const HippoBSTR &url) { ZeroMemory(&components_, sizeof(components_)); components_.dwStructSize = sizeof(components_); // The case where lpszHostName is NULL and dwHostNameLength is non-0 means // to return pointers into the passed in URL along with lengths. The // specific non-zero value is irrelevant components_.dwHostNameLength = 1; components_.dwUserNameLength = 1; components_.dwPasswordLength = 1; components_.dwUrlPathLength = 1; components_.dwExtraInfoLength = 1; ok_ = !!InternetCrackUrl(url.m_str, 0, 0, &components_); }
void CDlgInfoURL::OnOK() { //char szPath[_MAX_PATH]; //ULONG lBufLength = _MAX_PATH; // TODO: Add extra validation here /* if (FALSE==InternetCanonicalizeUrl(m_strInfoURL, szPath, &lBufLength, ICU_NO_ENCODE )) { AfxMessageBox("This must be a valid URL."); } else { */ UpdateData(); char buf[80]; DWORD length = 80; if(m_strInfoURL.GetLength()>0) { BOOL bret; URL_COMPONENTS UrlComponents; memset(&UrlComponents, 0,sizeof(URL_COMPONENTS)); UrlComponents.dwStructSize = sizeof(URL_COMPONENTS); bret = InternetCrackUrl(m_strInfoURL, m_strInfoURL.GetLength(), 0, &UrlComponents); if(bret==TRUE && (UrlComponents.nScheme!=INTERNET_SCHEME_UNKNOWN)) { bret = InternetCanonicalizeUrl(m_strInfoURL, buf, &length, ICU_DECODE); if(bret==TRUE) { m_strInfoURL = CString(buf); UpdateData(FALSE); CDialog::OnOK(); } else { AfxMessageBox("The string has invalid characters."); } } else AfxMessageBox("The string is not a valid URL."); } else CDialog::OnOK(); }
void CLauncherWindow::BeforeNavigate(Framework::Win32::CWebBrowser::BEFORENAVIGATE_INFO* beforeNavigateInfo) { const auto& navigateUrl = beforeNavigateInfo->navigateUrl; const int partSize = 256; TCHAR scheme[partSize]; TCHAR hostName[partSize]; TCHAR urlPath[partSize]; URL_COMPONENTS components; memset(&components, 0, sizeof(URL_COMPONENTS)); components.dwStructSize = sizeof(URL_COMPONENTS); components.lpszScheme = scheme; components.dwSchemeLength = partSize; components.lpszHostName = hostName; components.dwHostNameLength = partSize; components.lpszUrlPath = urlPath; components.dwUrlPathLength = partSize; BOOL result = InternetCrackUrl(navigateUrl.c_str(), 0, ICU_ESCAPE, &components); if(result == FALSE) return; if(_tcscmp(components.lpszScheme, _T("ffxiv"))) return; if(_tcscmp(components.lpszHostName, _T("login_success"))) return; auto parameters = GetUrlParameters(components.lpszUrlPath); auto sessionIdIterator = parameters.find(_T("sessionId")); if(sessionIdIterator != std::end(parameters)) { auto sessionId = string_cast<std::string>(sessionIdIterator->second); try { LaunchGame(sessionId.c_str()); } catch(const std::exception& except) { auto message = _T("Failed to launch game: ") + string_cast<std::tstring>(except.what()); MessageBox(m_hWnd, message.c_str(), APP_NAME, MB_ICONERROR); LoadLoginPage(); beforeNavigateInfo->cancel = true; return; } } }
bool UploadSettings::SetAddress(TCHAR * lpszAddress) { TCHAR szScheme[MAX_PATH]; TCHAR szUserName[MAX_PATH]; TCHAR szPassword[MAX_PATH]; TCHAR szHostName[MAX_PATH]; TCHAR szUrlPath[MAX_PATH]; TCHAR szExtraInfo[MAX_PATH]; URL_COMPONENTS url; memset(&url, 0, sizeof(URL_COMPONENTS)); url.dwStructSize = sizeof(URL_COMPONENTS); url.lpszScheme = szScheme; url.dwSchemeLength = MAX_PATH; url.lpszHostName = szHostName; url.dwHostNameLength = MAX_PATH; url.lpszUserName = szUserName; url.dwUserNameLength = MAX_PATH; url.lpszPassword = szPassword; url.dwPasswordLength = MAX_PATH; url.lpszUrlPath = szUrlPath; url.dwUrlPathLength = MAX_PATH; url.lpszExtraInfo = szExtraInfo; url.dwExtraInfoLength = MAX_PATH; if (!InternetCrackUrl(lpszAddress, 0, 0, &url)) return false; // Verify that none of the other bits got filled in: if (_tcscmp(url.lpszScheme, _T("http")) != 0) return false; if (url.nPort != INTERNET_DEFAULT_HTTP_PORT) return false; // Not supported yet. if (url.lpszUserName[0]) return false; if (url.lpszPassword[0]) return false; if (url.lpszExtraInfo[0]) return false; _tcsncpy(m_strHostName, szHostName, 1024); _tcsncpy(m_strUrlPath,szUrlPath, MAX_PATH); return true; }
void Download::postFile(const string &url, const string &file, const string &fileOut, const vector< pair<string, string> > &headers, ProgressWindow &pw) { SetLastError(0); DWORD_PTR dw = 0; URL_COMPONENTS uc; memset(&uc, 0, sizeof(uc)); uc.dwStructSize = sizeof(uc); char host[128]; char path[128]; char extra[256]; uc.lpszExtraInfo = extra; uc.dwExtraInfoLength = sizeof(extra); uc.lpszHostName = host; uc.dwHostNameLength = sizeof(host); uc.lpszUrlPath = path; uc.dwUrlPathLength = sizeof(path); InternetCrackUrl(url.c_str(), url.length(), ICU_ESCAPE, &uc); int port = INTERNET_DEFAULT_HTTP_PORT; if (uc.nScheme == INTERNET_SCHEME_HTTPS) port = INTERNET_DEFAULT_HTTPS_PORT; else if (uc.nPort>0) port = uc.nPort; HINTERNET hConnect = InternetConnect(hInternet, host, port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, dw); bool success = false; try { success = httpSendReqEx(hConnect, path, headers, file, fileOut, pw); } catch (std::exception &) { InternetCloseHandle(hConnect); throw; } InternetCloseHandle(hConnect); if (!success) { DWORD ec = GetLastError(); string error = ec != 0 ? getErrorMessage(ec) : ""; if (error.empty()) error = "Ett okänt fel inträffade."; throw std::exception(error.c_str()); } }
BOOL CInternet::CrackURL(const TCHAR *url) { //let's split the url... uc.dwStructSize = sizeof(uc); uc.lpszScheme = NULL; uc.dwSchemeLength = 0; uc.lpszHostName = NULL; uc.dwHostNameLength = 1; uc.nPort = 0; uc.lpszUserName = NULL; uc.dwUserNameLength = 0; uc.lpszPassword = NULL; uc.dwPasswordLength = 0; uc.lpszUrlPath = NULL; uc.dwUrlPathLength = 1; uc.lpszExtraInfo = NULL; uc.dwExtraInfoLength = 0; return InternetCrackUrl(url, _tcslen(url), 0, &uc); }
/* parse url into needed compnenents return a structure with size 0 if it fails */ URL_COMPONENTS parse_url(char *url){ URL_COMPONENTS cracked_url; RtlZeroMemory(&cracked_url, sizeof(URL_COMPONENTS)); cracked_url.dwStructSize = sizeof(URL_COMPONENTS); cracked_url.dwSchemeLength = INTERNET_MAX_SCHEME_LENGTH; cracked_url.dwHostNameLength = INTERNET_MAX_HOST_NAME_LENGTH; cracked_url.dwUrlPathLength = INTERNET_MAX_PATH_LENGTH; cracked_url.dwExtraInfoLength = INTERNET_MAX_PATH_LENGTH; cracked_url.lpszScheme = malloc(INTERNET_MAX_SCHEME_LENGTH); cracked_url.lpszHostName = malloc(INTERNET_MAX_HOST_NAME_LENGTH); cracked_url.lpszUrlPath = malloc(INTERNET_MAX_PATH_LENGTH); cracked_url.lpszExtraInfo = malloc(INTERNET_MAX_PATH_LENGTH); if (!InternetCrackUrl(url, strlen(url), 0, &cracked_url)){ RtlZeroMemory(&cracked_url, sizeof(URL_COMPONENTS)); } return cracked_url; }
void SplitUrl (/*[in]*/ const string & url, /*[out]*/ string & protocol, /*[out]*/ string & host) { _TCHAR szProtocol[200]; _TCHAR szHost[200]; URL_COMPONENTS url_comp = { 0 }; url_comp.dwStructSize = sizeof(url_comp); url_comp.lpszScheme = szProtocol; url_comp.dwSchemeLength = 200; url_comp.lpszHostName = szHost; url_comp.dwHostNameLength = 200; if (! InternetCrackUrl(UT_(url.c_str()), 0, 0, &url_comp)) { FATAL_WINDOWS_ERROR ("InternetCrackUrl", 0); } protocol = TU_(szProtocol); host = TU_(szHost); }
HRESULT CHttpDownloader::ParseUrl(LPCTSTR szUrlIn) { if(!lstrlen(szUrlIn)) return INET_E_INVALID_URL; URL_COMPONENTS uc; memset(&uc, 0, sizeof(uc)); uc.dwStructSize = sizeof(uc); TCHAR szServer[1024]; TCHAR szUrl[1024]; // TCHAR szScheme[1024]; uc.lpszHostName = szServer; uc.dwHostNameLength = sizeof(szServer); uc.lpszUrlPath = szUrl; uc.dwUrlPathLength = sizeof(szUrl); // uc.lpszScheme =szScheme; // uc.dwSchemeLength = sizeof(szScheme); if(!InternetCrackUrl(szUrlIn, lstrlen(szUrlIn), 0, &uc)) return INET_E_INVALID_URL; if(uc.nScheme==INTERNET_SCHEME_HTTPS) m_request.bUseSSL = TRUE; if(lstrlen(szServer)) m_request.server = szServer; if(uc.nPort != 0) m_request.port = uc.nPort; if(lstrlen(szUrl)) m_request.url = szUrl; return S_OK; }
bool HippoHTTP::parseURL(WCHAR *url, BSTR *hostReturn, INTERNET_PORT *portReturn, BSTR *targetReturn) { URL_COMPONENTS components; ZeroMemory(&components, sizeof(components)); components.dwStructSize = sizeof(components); // The case where lpszHostName is NULL and dwHostNameLength is non-0 means // to return pointers into the passed in URL along with lengths. The // specific non-zero value is irrelevant components.dwHostNameLength = 1; components.dwUserNameLength = 1; components.dwPasswordLength = 1; components.dwUrlPathLength = 1; components.dwExtraInfoLength = 1; if (!InternetCrackUrl(url, 0, 0, &components)) return false; if (components.nScheme != INTERNET_SCHEME_HTTP && components.nScheme != INTERNET_SCHEME_HTTPS) return false; HippoBSTR host(components.dwHostNameLength, components.lpszHostName); host.CopyTo(hostReturn); *portReturn = components.nPort; // We don't care about the division between the path and the query string HippoBSTR target(components.lpszUrlPath); target.CopyTo(targetReturn); return true; }
///////////////////////////////////////////// // CED2KFileLink implementation ///////////////////////////////////////////// CED2KFileLink::CED2KFileLink(const TCHAR* pszName, const TCHAR* pszSize, const TCHAR* pszHash, const CStringArray& astrParams, const TCHAR* pszSources) : m_size(pszSize) { // Here we have a little problem.. Actually the proper solution would be to decode from UTF8, // only if the string does contain escape sequences. But if user pastes a raw UTF8 encoded // string (for whatever reason), we would miss to decode that string. On the other side, // always decoding UTF8 can give flaws in case the string is valid for Unicode and UTF8 // at the same time. However, to avoid the pasting of raw UTF8 strings (which would lead // to a greater mess in the network) we always try to decode from UTF8, even if the string // did not contain escape sequences. m_name = OptUtf8ToStr(URLDecode(pszName)); m_name.Trim(); if (m_name.IsEmpty()) throw GetResString(IDS_ERR_NOTAFILELINK); SourcesList = NULL; m_hashset = NULL; m_bAICHHashValid = false; if (_tcslen(pszHash) != 32) throw GetResString(IDS_ERR_ILLFORMEDHASH); if (_tstoi64(pszSize)>=4294967295) throw GetResString(IDS_ERR_TOOLARGEFILE); if (_tstoi64(pszSize)<=0) throw GetResString(IDS_ERR_NOTAFILELINK); for (int idx = 0; idx < 16; ++idx) { m_hash[idx] = FromHexDigit(*pszHash++)*16; m_hash[idx] += FromHexDigit(*pszHash++); } bool bError = false; for (int i = 0; !bError && i < astrParams.GetCount(); i++) { const CString& strParam = astrParams.GetAt(i); ASSERT( !strParam.IsEmpty() ); CString strTok; int iPos = strParam.Find(_T('=')); if (iPos != -1) strTok = strParam.Left(iPos); if (strTok == _T("s")) { CString strURL = strParam.Mid(iPos + 1); if (!strURL.IsEmpty()) { TCHAR szScheme[INTERNET_MAX_SCHEME_LENGTH]; TCHAR szHostName[INTERNET_MAX_HOST_NAME_LENGTH]; TCHAR szUrlPath[INTERNET_MAX_PATH_LENGTH]; TCHAR szUserName[INTERNET_MAX_USER_NAME_LENGTH]; TCHAR szPassword[INTERNET_MAX_PASSWORD_LENGTH]; TCHAR szExtraInfo[INTERNET_MAX_URL_LENGTH]; URL_COMPONENTS Url = {0}; Url.dwStructSize = sizeof(Url); Url.lpszScheme = szScheme; Url.dwSchemeLength = ARRSIZE(szScheme); Url.lpszHostName = szHostName; Url.dwHostNameLength = ARRSIZE(szHostName); Url.lpszUserName = szUserName; Url.dwUserNameLength = ARRSIZE(szUserName); Url.lpszPassword = szPassword; Url.dwPasswordLength = ARRSIZE(szPassword); Url.lpszUrlPath = szUrlPath; Url.dwUrlPathLength = ARRSIZE(szUrlPath); Url.lpszExtraInfo = szExtraInfo; Url.dwExtraInfoLength = ARRSIZE(szExtraInfo); if (InternetCrackUrl(strURL, 0, 0, &Url) && Url.dwHostNameLength > 0) { SUnresolvedHostname* hostname = new SUnresolvedHostname; hostname->strURL = strURL; hostname->strHostname = szHostName; m_HostnameSourcesList.AddTail(hostname); } } else ASSERT(0); } else if (strTok == _T("p")) { CString strPartHashs = strParam.Tokenize(_T("="), iPos); if (m_hashset != NULL){ ASSERT(0); bError = true; break; } m_hashset = new CSafeMemFile(256); m_hashset->WriteHash16(m_hash); m_hashset->WriteUInt16(0); int iPartHashs = 0; int iPosPH = 0; CString strHash = strPartHashs.Tokenize(_T(":"), iPosPH); while (!strHash.IsEmpty()) { uchar aucPartHash[16]; if (!strmd4(strHash, aucPartHash)){ bError = true; break; } m_hashset->WriteHash16(aucPartHash); iPartHashs++; strHash = strPartHashs.Tokenize(_T(":"), iPosPH); } if (bError) break; m_hashset->Seek(16, CFile::begin); m_hashset->WriteUInt16(iPartHashs); m_hashset->Seek(0, CFile::begin); } else if (strTok == _T("h")) { CString strHash = strParam.Mid(iPos + 1); if (!strHash.IsEmpty()) { if (DecodeBase32(strHash, m_AICHHash.GetRawHash(), CAICHHash::GetHashSize()) == CAICHHash::GetHashSize()){ m_bAICHHashValid = true; ASSERT( m_AICHHash.GetString().CompareNoCase(strHash) == 0 ); } else ASSERT( false ); } else ASSERT( false ); } else ASSERT(0); } if (bError) { delete m_hashset; m_hashset = NULL; } if (pszSources) { TCHAR* pNewString = _tcsdup(pszSources); autoFree liberator(pNewString); TCHAR* pCh = pNewString; TCHAR* pEnd; TCHAR* pIP; TCHAR* pPort; bool bAllowSources; TCHAR date[3]; COleDateTime expirationDate; int nYear,nMonth,nDay; uint16 nCount = 0; uint32 dwID; uint16 nPort; uint32 dwServerIP = 0; uint16 nServerPort = 0; unsigned long ul; int nInvalid = 0; pCh = _tcsstr( pCh, _T("sources") ); if( pCh != NULL ) { pCh = pCh + 7; // point to char after "sources" pEnd = pCh; while( *pEnd ) pEnd++; // make pEnd point to the terminating NULL bAllowSources=true; // if there's an expiration date... if( *pCh == _T('@') && (pEnd-pCh) > 7 ) { pCh++; // after '@' date[2] = 0; // terminate the two character string date[0] = *(pCh++); date[1] = *(pCh++); nYear = _tcstol( date, 0, 10 ) + 2000; date[0] = *(pCh++); date[1] = *(pCh++); nMonth = _tcstol( date, 0, 10 ); date[0] = *(pCh++); date[1] = *(pCh++); nDay = _tcstol( date, 0, 10 ); bAllowSources = ( expirationDate.SetDate(nYear,nMonth,nDay) == 0 ); if (bAllowSources) bAllowSources=(COleDateTime::GetCurrentTime() < expirationDate); } // increment pCh to point to the first "ip:port" and check for sources if ( bAllowSources && ++pCh < pEnd ) { SourcesList = new CSafeMemFile(256); SourcesList->WriteUInt16(nCount); // init to 0, we'll fix this at the end. // for each "ip:port" source string until the end // limit to prevent overflow (uint16 due to CPartFile::AddClientSources) while( *pCh != 0 && nCount < MAXSHORT ) { pIP = pCh; // find the end of this ip:port string & start of next ip:port string. if( (pCh = _tcschr(pCh, _T(','))) != NULL ) { *pCh = 0; // terminate current "ip:port" pCh++; // point to next "ip:port" } else pCh = pEnd; // if port is not present for this ip, go to the next ip. if( (pPort = _tcschr(pIP, _T(':'))) == NULL ) { nInvalid++; continue; } *pPort = 0; // terminate ip string pPort++; // point pPort to port string. dwID = inet_addr(CStringA(pIP)); ul = _tcstoul( pPort, 0, 10 ); nPort = static_cast<uint16>(ul); // skip bad ips / ports if (ul > 0xFFFF || ul == 0 ) // port { nInvalid++; continue; } if( dwID == INADDR_NONE) { // hostname? if (_tcslen(pIP) > 512) { nInvalid++; continue; } SUnresolvedHostname* hostname = new SUnresolvedHostname; hostname->nPort = nPort; hostname->strHostname = pIP; m_HostnameSourcesList.AddTail(hostname); continue; } //TODO: This will filter out *.*.*.0 clients. Is there a nice way to fix? if( IsLowID(dwID) ) // ip { nInvalid++; continue; } SourcesList->WriteUInt32(dwID); SourcesList->WriteUInt16(nPort); SourcesList->WriteUInt32(dwServerIP); SourcesList->WriteUInt16(nServerPort); nCount++; } SourcesList->SeekToBegin(); SourcesList->WriteUInt16(nCount); SourcesList->SeekToBegin(); if (nCount==0) { delete SourcesList; SourcesList=NULL; } } } } }
/* * The servers main dispatch loop for incoming requests using SSL over TCP */ static DWORD server_dispatch_http_wininet( Remote * remote ) { LONG result = ERROR_SUCCESS; Packet * packet = NULL; THREAD * cpt = NULL; URL_COMPONENTS bits; DWORD ecount = 0; DWORD delay = 0; char tmpHostName[512]; char tmpUrlPath[1024]; if (global_expiration_timeout > 0) remote->expiration_time = current_unix_timestamp() + global_expiration_timeout; else remote->expiration_time = 0; remote->comm_timeout = global_comm_timeout; remote->start_time = current_unix_timestamp(); remote->comm_last_packet = current_unix_timestamp(); // Allocate the top-level handle if (!strcmp(global_meterpreter_proxy,"METERPRETER_PROXY")) { remote->hInternet = InternetOpen(global_meterpreter_ua, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); } else { remote->hInternet = InternetOpen(global_meterpreter_ua, INTERNET_OPEN_TYPE_PROXY, global_meterpreter_proxy, NULL, 0); } if (!remote->hInternet) { dprintf("[DISPATCH] Failed InternetOpen: %d", GetLastError()); return 0; } dprintf("[DISPATCH] Configured hInternet: 0x%.8x", remote->hInternet); // The InternetCrackUrl method was poorly designed... memset(tmpHostName, 0, sizeof(tmpHostName)); memset(tmpUrlPath, 0, sizeof(tmpUrlPath)); memset(&bits, 0, sizeof(bits)); bits.dwStructSize = sizeof(bits); bits.dwHostNameLength = sizeof(tmpHostName) -1; bits.lpszHostName = tmpHostName; bits.dwUrlPathLength = sizeof(tmpUrlPath) -1; bits.lpszUrlPath = tmpUrlPath; InternetCrackUrl(remote->url, 0, 0, &bits); remote->uri = _strdup(tmpUrlPath); dprintf("[DISPATCH] Configured URL: %s", remote->uri); dprintf("[DISPATCH] Host: %s Port: %u", tmpHostName, bits.nPort); // Allocate the connection handle remote->hConnection = InternetConnect(remote->hInternet, tmpHostName, bits.nPort, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0); if (!remote->hConnection) { dprintf("[DISPATCH] Failed InternetConnect: %d", GetLastError()); return 0; } dprintf("[DISPATCH] Configured hConnection: 0x%.8x", remote->hConnection); //authentication if (!(strcmp(global_meterpreter_proxy_username, "METERPRETER_USERNAME_PROXY") == 0)) { InternetSetOption(remote->hConnection, INTERNET_OPTION_PROXY_USERNAME, global_meterpreter_proxy_username, (DWORD)strlen(global_meterpreter_proxy_username)+1); InternetSetOption(remote->hConnection, INTERNET_OPTION_PROXY_PASSWORD, global_meterpreter_proxy_password, (DWORD)strlen(global_meterpreter_proxy_password)+1); dprintf("[DISPATCH] Proxy authentication configured : %s/%s", global_meterpreter_proxy_username, global_meterpreter_proxy_password); } // Bring up the scheduler subsystem. result = scheduler_initialize( remote ); if( result != ERROR_SUCCESS ) return result; while( TRUE ) { if (remote->comm_timeout != 0 && remote->comm_last_packet + remote->comm_timeout < current_unix_timestamp()) { dprintf("[DISPATCH] Shutting down server due to communication timeout"); break; } if (remote->expiration_time != 0 && remote->expiration_time < current_unix_timestamp()) { dprintf("[DISPATCH] Shutting down server due to hardcoded expiration time"); dprintf("Timestamp: %u Expiration: %u", current_unix_timestamp(), remote->expiration_time); break; } if( event_poll( serverThread->sigterm, 0 ) ) { dprintf( "[DISPATCH] server dispatch thread signaled to terminate..." ); break; } dprintf("[DISPATCH] Reading data from the remote side..."); result = packet_receive( remote, &packet ); if( result != ERROR_SUCCESS ) { // Update the timestamp for empty replies if (result == ERROR_EMPTY) remote->comm_last_packet = current_unix_timestamp(); if (ecount < 10) delay = 10 * ecount; else delay = 100 * ecount; ecount++; dprintf("[DISPATCH] no pending packets, sleeping for %dms...", min(10000, delay)); Sleep( min(10000, delay) ); continue; } remote->comm_last_packet = current_unix_timestamp(); // Reset the empty count when we receive a packet ecount = 0; dprintf("[DISPATCH] Returned result: %d", result); cpt = thread_create( command_process_thread, remote, packet ); if( cpt ) { dprintf( "[DISPATCH] created command_process_thread 0x%08X, handle=0x%08X", cpt, cpt->handle ); thread_run( cpt ); } } // Close WinInet handles InternetCloseHandle(remote->hConnection); InternetCloseHandle(remote->hInternet); dprintf( "[DISPATCH] calling scheduler_destroy..." ); scheduler_destroy(); dprintf( "[DISPATCH] calling command_join_threads..." ); command_join_threads(); dprintf( "[DISPATCH] leaving server_dispatch." ); return result; }
/*----------------------------------------------------------------------------- -----------------------------------------------------------------------------*/ void CWinInetEvents::OnInternetStatusCallback(HINTERNET hInternet, DWORD_PTR dwContext, DWORD dwInternetStatus, LPVOID lpvStatusInformation, DWORD dwStatusInformationLength) { if( active ) { // locate the request EnterCriticalSection(&cs); CWinInetRequest * r = NULL; winInetRequests.Lookup(hInternet, r); LeaveCriticalSection(&cs); if( r ) { switch(dwInternetStatus) { case INTERNET_STATUS_RESOLVING_NAME: { EnterCriticalSection(&cs); r->valid = true; r->fromNet = true; if( lpvStatusInformation ) r->hostName = (LPCSTR)lpvStatusInformation; if( !r->dnsStart ) QueryPerformanceCounter((LARGE_INTEGER*)&(r->dnsStart)); if( !r->start ) { r->start = r->dnsStart; r->docID = currentDoc; } LeaveCriticalSection(&cs); ATLTRACE(_T("[Pagetest] - (0x%08X) *** 0x%p - INTERNET_STATUS_RESOLVING_NAME : %s\n"), GetCurrentThreadId(), r->hRequest, (LPCTSTR)r->hostName); } break; case INTERNET_STATUS_NAME_RESOLVED: { ATLTRACE(_T("[Pagetest] - (0x%08X) *** 0x%p - INTERNET_STATUS_NAME_RESOLVED\n"), GetCurrentThreadId(), r->hRequest); __int64 now; QueryPerformanceCounter((LARGE_INTEGER*)&now); EnterCriticalSection(&cs); if( r->dnsStart ) { r->dnsEnd = now; r->tmDNS = now <= r->dnsStart ? 0 : (DWORD)((now - r->dnsStart) / msFreq); } LeaveCriticalSection(&cs); } break; case INTERNET_STATUS_CONNECTING_TO_SERVER: { EnterCriticalSection(&cs); r->valid = true; r->fromNet = true; if( !r->socketConnect ) QueryPerformanceCounter((LARGE_INTEGER*)&(r->socketConnect)); if( !r->start ) { r->start = r->socketConnect; r->docID = currentDoc; } // keep track of the current connect on this thread winInetThreadConnects.SetAt(GetCurrentThreadId(), r); LeaveCriticalSection(&cs); ATLTRACE(_T("[Pagetest] - (0x%08X) *** 0x%p - INTERNET_STATUS_CONNECTING_TO_SERVER\n"), GetCurrentThreadId(), r->hRequest); } break; case INTERNET_STATUS_CONNECTED_TO_SERVER: { __int64 now; QueryPerformanceCounter((LARGE_INTEGER*)&now); EnterCriticalSection(&cs); if( r->socketConnect ) { r->socketConnected = now; r->tmSocket = now <= r->socketConnect ? 0 : (DWORD)((now - r->socketConnect) / msFreq); UpdateRTT(r->peer.sin_addr.S_un.S_addr, r->tmSocket); } // remove the pending connect for this thread (no big deal if they don't all get removed in case of failure) winInetThreadConnects.RemoveKey(GetCurrentThreadId()); LeaveCriticalSection(&cs); ATLTRACE(_T("[Pagetest] - (0x%08X) *** 0x%p - INTERNET_STATUS_CONNECTED_TO_SERVER\n"), GetCurrentThreadId(), r->hRequest); } break; case INTERNET_STATUS_SENDING_REQUEST: { r->valid = true; r->fromNet = true; OverrideHost(r); if( !r->requestSent ) QueryPerformanceCounter((LARGE_INTEGER*)&(r->requestSent)); // see if this is the base page that is being requested if( !haveBasePage ) { r->basePage = true; haveBasePage = true; } if( !r->start ) { r->start = r->requestSent; r->docID = currentDoc; } // check if the request is secure DWORD flags = 0; DWORD len = sizeof(flags); if( InternetQueryOption(r->hRequest, INTERNET_OPTION_SECURITY_FLAGS, &flags, &len) ) if( flags & SECURITY_FLAG_SECURE ) r->secure = true; if( r->tmSSL == -1 && r->secure && r->socketConnected ) r->tmSSL = r->requestSent <= r->socketConnected ? 0 : (DWORD)((r->requestSent - r->socketConnected) / msFreq); // get the request headers TCHAR buff[10000]; len = sizeof(buff); DWORD index = 0; memset(buff, 0, len); if( r->outHeaders.IsEmpty() ) if( HttpQueryInfo(r->hRequest, HTTP_QUERY_FLAG_REQUEST_HEADERS | HTTP_QUERY_RAW_HEADERS_CRLF , buff, &len, &index) ) r->outHeaders = buff; // get some specific headers we care about if( !r->host.GetLength() ) { len = sizeof(buff); index = 0; memset(buff, 0, len); if( HttpQueryInfo(r->hRequest, HTTP_QUERY_FLAG_REQUEST_HEADERS | HTTP_QUERY_HOST , buff, &len, &index) ) r->host = buff; } ATLTRACE(_T("[Pagetest] - *** (%d) 0x%p - INTERNET_STATUS_SENDING_REQUEST, socket %d\n"), GetCurrentThreadId(), r->hRequest, r->socketId); } break; case INTERNET_STATUS_REQUEST_SENT: { ATLTRACE(_T("[Pagetest] - *** (%d) 0x%p - INTERNET_STATUS_REQUEST_SENT : %d bytes\n"), GetCurrentThreadId(), r->hRequest, r->out); EnterCriticalSection(&cs); if( dwStatusInformationLength == sizeof(DWORD) && lpvStatusInformation ) r->out += *((LPDWORD)lpvStatusInformation); EnterCriticalSection(&cs); CSocketRequest * s = NULL; winInetThreadSends.Lookup(GetCurrentThreadId(), s); if( s ) { r->linkedRequest = s; s->linkedRequest = r; // copy over the IP information r->peer.sin_addr.S_un.S_un_b.s_b1 = s->ipAddress[0]; r->peer.sin_addr.S_un.S_un_b.s_b2 = s->ipAddress[1]; r->peer.sin_addr.S_un.S_un_b.s_b3 = s->ipAddress[2]; r->peer.sin_addr.S_un.S_un_b.s_b4 = s->ipAddress[3]; r->peer.sin_port = s->port; r->socketId = s->socketId; // zero out the bytes-in s->in = 0; r->in = 0; ATLTRACE(_T("[Pagetest] INTERNET_STATUS_REQUEST_SENT - linked socket request to wininet request for %s%s\n"), (LPCTSTR)r->host, (LPCTSTR)r->object); } else { ATLTRACE(_T("[Pagetest] INTERNET_STATUS_REQUEST_SENT - Failed to link socket request to wininet request on thread %d\n"), GetCurrentThreadId()); } LeaveCriticalSection(&cs); // clean up the mapping of the request that was sending on this thread winInetThreadSends.RemoveKey(GetCurrentThreadId()); LeaveCriticalSection(&cs); } break; case INTERNET_STATUS_RECEIVING_RESPONSE: { ATLTRACE(_T("[Pagetest] - *** (%d) 0x%p - INTERNET_STATUS_RECEIVING_RESPONSE\n"), GetCurrentThreadId(), r->hRequest); } break; case INTERNET_STATUS_REDIRECT: { CString url = CA2T((LPCSTR)lpvStatusInformation); ATLTRACE(_T("[Pagetest] - *** (%d) 0x%p - INTERNET_STATUS_REDIRECT : Redirecting to %s\n"), GetCurrentThreadId(), r->hRequest, (LPCTSTR)url); // get the headers, close out the request and start a new one for the redirect r->Done(); // update the end time if( !r->ignore ) { lastRequest = r->end; lastActivity = r->end; } // update the in and out bytes from the raw socket if( r->linkedRequest ) { r->in = ((CSocketRequest *)r->linkedRequest)->in; r->out = ((CSocketRequest *)r->linkedRequest)->out; } // get the response code and headers TCHAR buff[10000]; DWORD len = sizeof(buff); DWORD index = 0; memset(buff, 0, len); if( r->inHeaders.IsEmpty() ) if( HttpQueryInfo(r->hRequest, HTTP_QUERY_RAW_HEADERS_CRLF , buff, &len, &index) ) r->inHeaders = buff; // get the redirect code DWORD code; len = sizeof(code); index = 0; if( HttpQueryInfo(r->hRequest, HTTP_QUERY_FLAG_NUMBER | HTTP_QUERY_STATUS_CODE , &code, &len, &index) ) r->result = code; // remove it from the lookup OnInternetCloseHandle(r->hRequest); // see if we need to block the new request bool block = false; POSITION pos = blockRequests.GetHeadPosition(); while (pos && !block) { CString blockRequest = blockRequests.GetNext(pos); blockRequest.Trim(); if (blockRequest.GetLength() && url.Find(blockRequest) != -1) { block = true; blockedRequests.AddTail(url); } } if (block) { InternetCloseHandle(r->hRequest); } else { // create a new request CWinInetRequest * req = new CWinInetRequest(currentDoc); // if this is for the base page, move it to the redirected request if( r->basePage ) { basePageRedirects++; r->basePage = false; req->basePage = true; } req->verb = r->verb; req->hRequest = r->hRequest; // split up the url URL_COMPONENTS parts; memset(&parts, 0, sizeof(parts)); TCHAR scheme[10000]; TCHAR host[10000]; TCHAR object[10000]; TCHAR extra[10000]; memset(scheme, 0, sizeof(scheme)); memset(host, 0, sizeof(host)); memset(object, 0, sizeof(object)); memset(extra, 0, sizeof(extra)); parts.lpszScheme = scheme; parts.dwSchemeLength = _countof(scheme); parts.lpszHostName = host; parts.dwHostNameLength = _countof(host); parts.lpszUrlPath = object; parts.dwUrlPathLength = _countof(object); parts.lpszExtraInfo = extra; parts.dwExtraInfoLength = _countof(extra); parts.dwStructSize = sizeof(parts); if( InternetCrackUrl((LPCTSTR)url, url.GetLength(), 0, &parts) ) { req->host = host; req->object = CString(object) + extra; req->scheme = scheme; if (!req->scheme.Left(5).CompareNoCase(_T("https"))) req->secure = true; } EnterCriticalSection(&cs); winInetRequests.SetAt(req->hRequest, req); winInetRequestList.AddHead(req); OverrideHost(req); LeaveCriticalSection(&cs); AddEvent(req); } } break; case INTERNET_STATUS_RESPONSE_RECEIVED: { if( dwStatusInformationLength == sizeof(DWORD) && lpvStatusInformation ) r->in += *((LPDWORD)lpvStatusInformation); r->Done(true); // update the end time if( !r->ignore ) { lastRequest = r->end; lastActivity = r->end; } // update the in and out bytes from the raw socket if( r->linkedRequest ) { r->in = ((CSocketRequest *)r->linkedRequest)->in; r->out = ((CSocketRequest *)r->linkedRequest)->out; } ATLTRACE(_T("[Pagetest] - *** (0x%08X) 0x%p - INTERNET_STATUS_RESPONSE_RECEIVED : %d bytes\n"), GetCurrentThreadId(), r->hRequest, r->in); } break; case INTERNET_STATUS_REQUEST_COMPLETE: { ATLTRACE(_T("[Pagetest] - *** (0x%08X) 0x%p - INTERNET_STATUS_REQUEST_COMPLETE\n"), GetCurrentThreadId(), r->hRequest); LPINTERNET_ASYNC_RESULT result = (LPINTERNET_ASYNC_RESULT)lpvStatusInformation; if( (!r->result || (r->result == -1))&& result && !result->dwResult) { ATLTRACE(_T("[Pagetest] - *** INTERNET_STATUS_REQUEST_COMPLETE Error - %d\n"), result->dwError); r->result = result->dwError; } // get the response code and headers TCHAR buff[10000]; DWORD len = sizeof(buff); DWORD index = 0; memset(buff, 0, len); if( r->inHeaders.IsEmpty() ) if( HttpQueryInfo(r->hRequest, HTTP_QUERY_RAW_HEADERS_CRLF , buff, &len, &index) ) { CString header = r->inHeaders = buff; // get the result code out of the header ourselves index = header.Find(_T(' ')); if( index >= 0 ) { header = header.Mid(index + 1, 10); long code = _ttol((LPCTSTR)header); if( code > 0 ) r->result = (DWORD)code; } } if( !r->result || r->result == -1 ) { DWORD code; len = sizeof(code); index = 0; if( HttpQueryInfo(r->hRequest, HTTP_QUERY_FLAG_NUMBER | HTTP_QUERY_STATUS_CODE , &code, &len, &index) ) r->result = code; } // see if it was a 304 (from cache) unsigned long reqFlags = 0; len = sizeof(reqFlags); if( InternetQueryOption(r->hRequest, INTERNET_OPTION_REQUEST_FLAGS, &reqFlags, &len) ) if( reqFlags & INTERNET_REQFLAG_FROM_CACHE ) { r->result = 304; // Save away the headers we've received previously, which were the cached // headers reported from wininet. Note that wininet does not cache all // headers, so this is just a partial set of the actual headers associated // with the response. r->cachedInHeaders = r->inHeaders; r->inHeaders = _T("HTTP/1.1 304 Not Modified\nFull response not available\n"); } // update the "done" time r->Done(); // update the end time if( !r->ignore ) { lastRequest = r->end; lastActivity = r->end; } // update the in and out bytes from the raw socket if( r->linkedRequest ) { CSocketRequest * req = (CSocketRequest *)r->linkedRequest; r->in = req->in; r->out = req->out; if( req->response.code && req->response.code != -1 ) r->result = req->response.code; } // make sure that we got an IP address for it EnterCriticalSection(&cs); if( r->s && !r->peer.sin_addr.S_un.S_addr ) { CSocketInfo * soc = NULL; openSockets.Lookup(r->s, soc); if( soc ) memcpy( &r->peer, &soc->address, sizeof(SOCKADDR_IN) ); } LeaveCriticalSection(&cs); } break; } // update the activity time if( !r->ignore ) QueryPerformanceCounter((LARGE_INTEGER *)&lastActivity); } CheckStuff(); } ATLTRACE(_T("[Pagetest] - *** (0x%08X) 0x%p - OnInternetStatusCallback - complete\n"), GetCurrentThreadId(), hInternet); }
static int download_file(IN LPCTSTR pszUrl, IN LPCTSTR pszFile OPTIONAL) { TCHAR szScheme[INTERNET_MAX_SCHEME_LENGTH + 1]; TCHAR szHostName[INTERNET_MAX_HOST_NAME_LENGTH + 1]; TCHAR szUserName[INTERNET_MAX_USER_NAME_LENGTH + 1]; TCHAR szPassWord[INTERNET_MAX_PASSWORD_LENGTH + 1]; TCHAR szUrlPath[INTERNET_MAX_PATH_LENGTH + 1]; TCHAR szExtraInfo[INTERNET_MAX_PATH_LENGTH + 1]; TCHAR szUrl[INTERNET_MAX_URL_LENGTH + 1]; DWORD dwUrlLen; LPTSTR pszFilePart; URL_COMPONENTS urlc; IBindStatusCallback *pbsc; int iRet; if (pszFile != NULL && pszFile[0] == _T('\0')) pszFile = NULL; urlc.dwStructSize = sizeof(urlc); urlc.lpszScheme = szScheme; urlc.dwSchemeLength = sizeof(szScheme) / sizeof(szScheme[0]); urlc.lpszHostName = szHostName; urlc.dwHostNameLength = sizeof(szHostName) / sizeof(szHostName[0]); urlc.lpszUserName = szUserName; urlc.dwUserNameLength = sizeof(szUserName) / sizeof(szUserName[0]); urlc.lpszPassword = szPassWord; urlc.dwPasswordLength = sizeof(szPassWord) / sizeof(szPassWord[0]); urlc.lpszUrlPath = szUrlPath; urlc.dwUrlPathLength = sizeof(szUrlPath) / sizeof(szUrlPath[0]); urlc.lpszExtraInfo = szExtraInfo; urlc.dwExtraInfoLength = sizeof(szExtraInfo) / sizeof(szExtraInfo[0]); if (!InternetCrackUrl(pszUrl, _tcslen(pszUrl), ICU_ESCAPE, &urlc)) return DWNL_E_LASTERROR; if (urlc.nScheme != INTERNET_SCHEME_FTP && urlc.nScheme != INTERNET_SCHEME_GOPHER && urlc.nScheme != INTERNET_SCHEME_HTTP && urlc.nScheme != INTERNET_SCHEME_HTTPS) { return DWNL_E_UNSUPPORTEDSCHEME; } if (urlc.nScheme == INTERNET_SCHEME_FTP && urlc.dwUserNameLength == 0 && urlc.dwPasswordLength == 0) { _tcscpy(szUserName, _T("anonymous")); urlc.dwUserNameLength = _tcslen(szUserName); } /* FIXME: Get file name from server */ if (urlc.dwUrlPathLength == 0 && pszFile == NULL) return DWNL_E_NEEDTARGETFILENAME; pszFilePart = _tcsrchr(szUrlPath, _T('/')); if (pszFilePart != NULL) pszFilePart++; if (pszFilePart == NULL && pszFile == NULL) return DWNL_E_NEEDTARGETFILENAME; if (pszFile == NULL) pszFile = pszFilePart; if (urlc.dwUserNameLength == 0) urlc.lpszUserName = NULL; if (urlc.dwPasswordLength == 0) urlc.lpszPassword = NULL; /* Generate the URL to be displayed (without a password) */ dwUrlLen = sizeof(szUrl) / sizeof(szUrl[0]); iRet = get_display_url(&urlc, szUrl, &dwUrlLen); if (iRet <= 0) return iRet; _tprintf(_T("Download `%s\'\n\t=> `%s\'\n"), szUrl, pszFile); /* Generate the URL to download */ dwUrlLen = sizeof(szUrl) / sizeof(szUrl[0]); if (!InternetCreateUrl(&urlc, ICU_ESCAPE, szUrl, &dwUrlLen)) return DWNL_E_LASTERROR; pbsc = CreateBindStatusCallback(); if (pbsc == NULL) return DWNL_E_LASTERROR; if(!SUCCEEDED(URLDownloadToFile(NULL, szUrl, pszFile, 0, pbsc))) { IBindStatusCallback_Release(pbsc); return DWNL_E_LASTERROR; /* FIXME */ } IBindStatusCallback_Release(pbsc); return 1; }
static void InternetCrackUrl_test(void) { URL_COMPONENTSA urlSrc, urlComponents; char protocol[32], hostName[1024], userName[1024]; char password[1024], extra[1024], path[1024]; BOOL ret, firstret; DWORD GLE, firstGLE; ZeroMemory(&urlSrc, sizeof(urlSrc)); urlSrc.dwStructSize = sizeof(urlSrc); urlSrc.lpszScheme = protocol; urlSrc.lpszHostName = hostName; urlSrc.lpszUserName = userName; urlSrc.lpszPassword = password; urlSrc.lpszUrlPath = path; urlSrc.lpszExtraInfo = extra; /* Tests for lpsz* members pointing to real strings while * some corresponding length members are set to zero. * As of IE7 (wininet 7.0*?) all members are checked. So we * run the first test and expect the outcome to be the same * for the first four (scheme, hostname, username and password). * The last two (path and extrainfo) are the same for all versions * of the wininet.dll. */ copy_compsA(&urlSrc, &urlComponents, 0, 1024, 1024, 1024, 2048, 1024); SetLastError(0xdeadbeef); firstret = InternetCrackUrlA(TEST_URL3, 0, ICU_DECODE, &urlComponents); firstGLE = GetLastError(); copy_compsA(&urlSrc, &urlComponents, 32, 0, 1024, 1024, 2048, 1024); SetLastError(0xdeadbeef); ret = InternetCrackUrlA(TEST_URL3, 0, ICU_DECODE, &urlComponents); GLE = GetLastError(); ok(ret==firstret && (GLE==firstGLE), "InternetCrackUrl returned %d with GLE=%d (expected to return %d)\n", ret, GetLastError(), firstret); copy_compsA(&urlSrc, &urlComponents, 32, 1024, 0, 1024, 2048, 1024); SetLastError(0xdeadbeef); ret = InternetCrackUrlA(TEST_URL3, 0, ICU_DECODE, &urlComponents); GLE = GetLastError(); ok(ret==firstret && (GLE==firstGLE), "InternetCrackUrl returned %d with GLE=%d (expected to return %d)\n", ret, GetLastError(), firstret); copy_compsA(&urlSrc, &urlComponents, 32, 1024, 1024, 0, 2048, 1024); SetLastError(0xdeadbeef); ret = InternetCrackUrlA(TEST_URL3, 0, ICU_DECODE, &urlComponents); GLE = GetLastError(); ok(ret==firstret && (GLE==firstGLE), "InternetCrackUrl returned %d with GLE=%d (expected to return %d)\n", ret, GetLastError(), firstret); copy_compsA(&urlSrc, &urlComponents, 32, 1024, 1024, 1024, 0, 1024); SetLastError(0xdeadbeef); ret = InternetCrackUrlA(TEST_URL3, 0, ICU_DECODE, &urlComponents); GLE = GetLastError(); todo_wine ok(ret==0 && (GLE==ERROR_INVALID_HANDLE || GLE==ERROR_INSUFFICIENT_BUFFER), "InternetCrackUrl returned %d with GLE=%d (expected to return 0 and ERROR_INVALID_HANDLE or ERROR_INSUFFICIENT_BUFFER)\n", ret, GLE); copy_compsA(&urlSrc, &urlComponents, 32, 1024, 1024, 1024, 2048, 0); SetLastError(0xdeadbeef); ret = InternetCrackUrlA(TEST_URL3, 0, ICU_DECODE, &urlComponents); GLE = GetLastError(); todo_wine ok(ret==0 && (GLE==ERROR_INVALID_HANDLE || GLE==ERROR_INSUFFICIENT_BUFFER), "InternetCrackUrl returned %d with GLE=%d (expected to return 0 and ERROR_INVALID_HANDLE or ERROR_INSUFFICIENT_BUFFER)\n", ret, GLE); copy_compsA(&urlSrc, &urlComponents, 0, 0, 0, 0, 0, 0); ret = InternetCrackUrlA(TEST_URL3, 0, ICU_DECODE, &urlComponents); GLE = GetLastError(); todo_wine ok(ret==0 && GLE==ERROR_INVALID_PARAMETER, "InternetCrackUrl returned %d with GLE=%d (expected to return 0 and ERROR_INVALID_PARAMETER)\n", ret, GLE); copy_compsA(&urlSrc, &urlComponents, 32, 1024, 1024, 1024, 2048, 1024); ret = InternetCrackUrl("about://host/blank", 0,0,&urlComponents); ok(ret, "InternetCrackUrl failed with %d\n", GetLastError()); ok(!strcmp(urlComponents.lpszScheme, "about"), "lpszScheme was \"%s\" instead of \"about\"\n", urlComponents.lpszScheme); ok(!strcmp(urlComponents.lpszHostName, "host"), "lpszHostName was \"%s\" instead of \"host\"\n", urlComponents.lpszHostName); ok(!strcmp(urlComponents.lpszUrlPath, "/blank"), "lpszUrlPath was \"%s\" instead of \"/blank\"\n", urlComponents.lpszUrlPath); /* try a NULL lpszUrl */ SetLastError(0xdeadbeef); copy_compsA(&urlSrc, &urlComponents, 32, 1024, 1024, 1024, 2048, 1024); ret = InternetCrackUrl(NULL, 0, 0, &urlComponents); GLE = GetLastError(); ok(ret == FALSE, "Expected InternetCrackUrl to fail\n"); ok(GLE == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GLE); /* try an empty lpszUrl, GetLastError returns 12006, whatever that means * we just need to fail and not return success */ SetLastError(0xdeadbeef); copy_compsA(&urlSrc, &urlComponents, 32, 1024, 1024, 1024, 2048, 1024); ret = InternetCrackUrl("", 0, 0, &urlComponents); GLE = GetLastError(); ok(ret == FALSE, "Expected InternetCrackUrl to fail\n"); ok(GLE != 0xdeadbeef && GLE != ERROR_SUCCESS, "Expected GLE to represent a failure\n"); /* Invalid Call: must set size of components structure (Windows only * enforces this on the InternetCrackUrlA version of the call) */ copy_compsA(&urlSrc, &urlComponents, 0, 1024, 1024, 1024, 2048, 1024); SetLastError(0xdeadbeef); urlComponents.dwStructSize = 0; ret = InternetCrackUrlA(TEST_URL, 0, 0, &urlComponents); ok(ret == FALSE, "Expected InternetCrackUrl to fail\n"); ok(GLE != 0xdeadbeef && GLE != ERROR_SUCCESS, "Expected GLE to represent a failure\n"); /* Invalid Call: size of dwStructSize must be one of the "standard" sizes * of the URL_COMPONENTS structure (Windows only enforces this on the * InternetCrackUrlA version of the call) */ copy_compsA(&urlSrc, &urlComponents, 0, 1024, 1024, 1024, 2048, 1024); SetLastError(0xdeadbeef); urlComponents.dwStructSize = sizeof(urlComponents) + 1; ret = InternetCrackUrlA(TEST_URL, 0, 0, &urlComponents); ok(ret == FALSE, "Expected InternetCrackUrl to fail\n"); ok(GLE != 0xdeadbeef && GLE != ERROR_SUCCESS, "Expected GLE to represent a failure\n"); }