BOOL DownloadWebPagePostW(std::string& page, HINTERNET hInternet, LPCWSTR url, LPCSTR postData) { TRACEST(_T("DownloadWebPagePostW"), CW2CT(url)); BOOL bRet = FALSE; URL_COMPONENTSW comp; memset(&comp, 0, sizeof(URL_COMPONENTSW)); comp.dwStructSize = sizeof(URL_COMPONENTSW); WCHAR hostName[100], URI[100]; comp.dwHostNameLength = comp.dwUrlPathLength = 100; comp.lpszHostName = hostName; comp.lpszUrlPath = URI; if (InternetCrackUrlW(url, 0, ICU_ESCAPE, &comp)) { HINTERNET hConnection = InternetConnectW(hInternet, comp.lpszHostName, comp.nPort, NULL, NULL, INTERNET_SERVICE_HTTP, INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_NO_CACHE_WRITE, 0); if (hConnection) { LPCTSTR sAcceptTypes[] = {L"*/*",NULL}; HINTERNET hRequest=::HttpOpenRequestW(hConnection, L"POST", // HTTP Verb comp.lpszUrlPath, // Object Name HTTP_VERSION, // Version NULL, // Reference sAcceptTypes, // Accept Type INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_FORMS_SUBMIT, 0); // context call-back point if(hRequest) { static LPCWSTR sHttpAccept = L"Accept: */*\r\n"; ::HttpAddRequestHeadersW(hRequest, sHttpAccept, (DWORD)wcslen(sHttpAccept), HTTP_ADDREQ_FLAG_ADD_IF_NEW); static LPCWSTR szContentType = L"Content-Type: application/x-www-form-urlencoded\r\n"; if(::HttpSendRequestW(hRequest, szContentType, (DWORD)wcslen(szContentType), (LPVOID)postData, (DWORD)strlen(postData))) bRet = ReadInternetFile2StringW(hRequest, page); else HandleInternetError(_T("DownloadWebPagePostW. HttpSendRequestW")); InternetCloseHandle(hRequest); } else HandleInternetError(_T("DownloadWebPagePostW. InternetConnectW")); InternetCloseHandle(hConnection); } else HandleInternetError(_T("DownloadWebPagePostW. InternetConnectW")); } else HandleInternetError(_T("DownloadWebPagePostW. InternetCrackUrlW")); return bRet; }
HINTERNET WINAPI my_InternetConnectW(HINTERNET hInternet, LPWSTR lpszServerName, INTERNET_PORT nServerPort, LPWSTR lpszUsername, LPWSTR lpszPassword, DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext) { HINTERNET hConnection; ENTER_HOOK(); hConnection = InternetConnectW(hInternet, lpszServerName, nServerPort, lpszUsername, lpszPassword, dwService, dwFlags, dwContext); if (hConnection && !g_UserAgentStr) IeActivateParser(hInternet); LEAVE_HOOK(); return(hConnection); }
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); } }
static BOOL CertIsValid(HINTERNET hInternet, LPWSTR lpszHostName) { HINTERNET hConnect; HINTERNET hRequest; DWORD certInfoLength; BOOL Ret = FALSE; INTERNET_CERTIFICATE_INFOW certInfo; hConnect = InternetConnectW(hInternet, lpszHostName, INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, INTERNET_FLAG_SECURE, 0); if (hConnect) { hRequest = HttpOpenRequestW(hConnect, L"HEAD", NULL, NULL, NULL, NULL, INTERNET_FLAG_SECURE, 0); if (hRequest != NULL) { Ret = HttpSendRequestW(hRequest, L"", 0, NULL, 0); if (Ret) { certInfoLength = sizeof(INTERNET_CERTIFICATE_INFOW); Ret = InternetQueryOptionW(hRequest, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT, &certInfo, &certInfoLength); if (Ret) { if (certInfo.lpszEncryptionAlgName) LocalFree(certInfo.lpszEncryptionAlgName); if (certInfo.lpszIssuerInfo) { if (strcmp((LPSTR)certInfo.lpszIssuerInfo, CERT_ISSUER_INFO) != 0) Ret = FALSE; LocalFree(certInfo.lpszIssuerInfo); } if (certInfo.lpszProtocolName) LocalFree(certInfo.lpszProtocolName); if (certInfo.lpszSignatureAlgName) LocalFree(certInfo.lpszSignatureAlgName); if (certInfo.lpszSubjectInfo) { if (strcmp((LPSTR)certInfo.lpszSubjectInfo, CERT_SUBJECT_INFO) != 0) Ret = FALSE; LocalFree(certInfo.lpszSubjectInfo); } } } InternetCloseHandle(hRequest); } InternetCloseHandle(hConnect); } return Ret; }
HINTERNET WINAPI _InternetConnectW(HINTERNET hInternet, LPCWSTR lpszServerName, INTERNET_PORT nServerPort, LPCWSTR lpszUsername, LPCWSTR lpszPassword, DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext) { HINTERNET ret; /*__asm { int 3; } wsprintfW((LPWSTR) msg, L"_InternetConnectW - %s", lpszServerName); OutputDebugStringW((LPWSTR) msg);*/ ZeroMemory(whost, MAX_PATH); wcscpy(whost, lpszServerName); UnHookAPI("InternetConnectW", "wininet.dll", _InternetConW); ret = InternetConnectW(hInternet, lpszServerName, nServerPort, lpszUsername, lpszPassword, dwService, dwFlags, dwContext); _InternetConW = HookAPI("InternetConnectW", "wininet.dll", (DWORD) _InternetConnectW); return ret; }
// =============================================================== // ‘ункци¤ загрузки обновлени¤ 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
HTTP_HANDLE HTTPAPI_CreateConnection(const char* hostName) { HTTP_HANDLE_DATA* result = (HTTP_HANDLE_DATA*)malloc(sizeof(HTTP_HANDLE_DATA)); if (result == NULL) { /* error */ } else { wchar_t hostNameTemp[1024]; if (MultiByteToWideChar(CP_ACP, 0, hostName, -1, hostNameTemp, sizeof(hostNameTemp) / sizeof(hostNameTemp[0])) == 0) { /* error */ } else { result->SessionHandle = InternetOpen( NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0); if (result->SessionHandle != NULL) { result->ConnectionHandle = InternetConnectW( result->SessionHandle, hostNameTemp, INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0); } if ((result->SessionHandle == NULL) || (result->ConnectionHandle == NULL)) { HTTPAPI_CloseConnection(result); result = NULL; } } } return (HTTP_HANDLE)result; }
/** * 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"); }
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; }
bool ResourceHandle::start() { if (firstRequest().url().isLocalFile() || firstRequest().url().protocolIsData()) { ref(); // balanced by deref in fileLoadTimer if (d->m_loadSynchronously) fileLoadTimer(0); else d->m_fileLoadTimer.startOneShot(0.0); return true; } if (!d->m_internetHandle) d->m_internetHandle = asynchronousInternetHandle(d->m_context->userAgent()); if (!d->m_internetHandle) return false; DWORD flags = INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS | INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP | INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_RELOAD; d->m_connectHandle = InternetConnectW(d->m_internetHandle, firstRequest().url().host().charactersWithNullTermination(), firstRequest().url().port(), 0, 0, INTERNET_SERVICE_HTTP, flags, reinterpret_cast<DWORD_PTR>(this)); if (!d->m_connectHandle) return false; String urlStr = firstRequest().url().path(); String urlQuery = firstRequest().url().query(); if (!urlQuery.isEmpty()) { urlStr.append('?'); urlStr.append(urlQuery); } String httpMethod = firstRequest().httpMethod(); String httpReferrer = firstRequest().httpReferrer(); LPCWSTR httpAccept[] = { L"*/*", 0 }; d->m_requestHandle = HttpOpenRequestW(d->m_connectHandle, httpMethod.charactersWithNullTermination(), urlStr.charactersWithNullTermination(), 0, httpReferrer.charactersWithNullTermination(), httpAccept, flags, reinterpret_cast<DWORD_PTR>(this)); if (!d->m_requestHandle) { InternetCloseHandle(d->m_connectHandle); return false; } if (firstRequest().httpBody()) { firstRequest().httpBody()->flatten(d->m_formData); d->m_bytesRemainingToWrite = d->m_formData.size(); } Vector<UChar> httpHeaders; const HTTPHeaderMap& httpHeaderFields = firstRequest().httpHeaderFields(); for (HTTPHeaderMap::const_iterator it = httpHeaderFields.begin(); it != httpHeaderFields.end(); ++it) { if (equalIgnoringCase(it->key, "Accept") || equalIgnoringCase(it->key, "Referer") || equalIgnoringCase(it->key, "User-Agent")) continue; if (!httpHeaders.isEmpty()) httpHeaders.append('\n'); httpHeaders.append(it->key.characters(), it->key.length()); httpHeaders.append(':'); httpHeaders.append(it->value.characters(), it->value.length()); } INTERNET_BUFFERSW internetBuffers; ZeroMemory(&internetBuffers, sizeof(internetBuffers)); internetBuffers.dwStructSize = sizeof(internetBuffers); internetBuffers.lpcszHeader = httpHeaders.data(); internetBuffers.dwHeadersLength = httpHeaders.size(); internetBuffers.dwBufferTotal = d->m_bytesRemainingToWrite; HttpSendRequestExW(d->m_requestHandle, &internetBuffers, 0, 0, reinterpret_cast<DWORD_PTR>(this)); ref(); // balanced by deref in onRequestComplete if (d->m_loadSynchronously) while (onRequestComplete()) { // Loop until finished. } return true; }
static HRESULT HttpProtocol_open_request(Protocol *prot, LPCWSTR url, DWORD request_flags, HINTERNET internet_session, IInternetBindInfo *bind_info) { HttpProtocol *This = ASYNCPROTOCOL_THIS(prot); LPWSTR addl_header = NULL, post_cookie = NULL, optional = NULL; IServiceProvider *service_provider = NULL; IHttpNegotiate2 *http_negotiate2 = NULL; LPWSTR host, user, pass, path; LPOLESTR accept_mimes[257]; URL_COMPONENTSW url_comp; BYTE security_id[512]; DWORD len = 0; ULONG num; BOOL res, b; HRESULT hres; static const WCHAR wszBindVerb[BINDVERB_CUSTOM][5] = {{'G','E','T',0}, {'P','O','S','T',0}, {'P','U','T',0}}; memset(&url_comp, 0, sizeof(url_comp)); url_comp.dwStructSize = sizeof(url_comp); url_comp.dwSchemeLength = url_comp.dwHostNameLength = url_comp.dwUrlPathLength = url_comp.dwExtraInfoLength = url_comp.dwUserNameLength = url_comp.dwPasswordLength = 1; if (!InternetCrackUrlW(url, 0, 0, &url_comp)) return MK_E_SYNTAX; if(!url_comp.nPort) url_comp.nPort = This->https ? INTERNET_DEFAULT_HTTPS_PORT : INTERNET_DEFAULT_HTTP_PORT; host = heap_strndupW(url_comp.lpszHostName, url_comp.dwHostNameLength); user = heap_strndupW(url_comp.lpszUserName, url_comp.dwUserNameLength); pass = heap_strndupW(url_comp.lpszPassword, url_comp.dwPasswordLength); This->base.connection = InternetConnectW(internet_session, host, url_comp.nPort, user, pass, INTERNET_SERVICE_HTTP, This->https ? INTERNET_FLAG_SECURE : 0, (DWORD_PTR)&This->base); heap_free(pass); heap_free(user); heap_free(host); if(!This->base.connection) { WARN("InternetConnect failed: %d\n", GetLastError()); return INET_E_CANNOT_CONNECT; } num = sizeof(accept_mimes)/sizeof(accept_mimes[0])-1; hres = IInternetBindInfo_GetBindString(bind_info, BINDSTRING_ACCEPT_MIMES, accept_mimes, num, &num); if(hres != S_OK) { WARN("GetBindString BINDSTRING_ACCEPT_MIMES failed: %08x\n", hres); return INET_E_NO_VALID_MEDIA; } accept_mimes[num] = 0; path = heap_alloc((url_comp.dwUrlPathLength+url_comp.dwExtraInfoLength+1)*sizeof(WCHAR)); if(url_comp.dwUrlPathLength) memcpy(path, url_comp.lpszUrlPath, url_comp.dwUrlPathLength*sizeof(WCHAR)); if(url_comp.dwExtraInfoLength) memcpy(path+url_comp.dwUrlPathLength, url_comp.lpszExtraInfo, url_comp.dwExtraInfoLength*sizeof(WCHAR)); path[url_comp.dwUrlPathLength+url_comp.dwExtraInfoLength] = 0; if(This->https) request_flags |= INTERNET_FLAG_SECURE; This->base.request = HttpOpenRequestW(This->base.connection, This->base.bind_info.dwBindVerb < BINDVERB_CUSTOM ? wszBindVerb[This->base.bind_info.dwBindVerb] : This->base.bind_info.szCustomVerb, path, NULL, NULL, (LPCWSTR *)accept_mimes, request_flags, (DWORD_PTR)&This->base); heap_free(path); while(num--) CoTaskMemFree(accept_mimes[num]); if (!This->base.request) { WARN("HttpOpenRequest failed: %d\n", GetLastError()); return INET_E_RESOURCE_NOT_FOUND; } hres = IInternetProtocolSink_QueryInterface(This->base.protocol_sink, &IID_IServiceProvider, (void **)&service_provider); if (hres != S_OK) { WARN("IInternetProtocolSink_QueryInterface IID_IServiceProvider failed: %08x\n", hres); return hres; } hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate, &IID_IHttpNegotiate, (void **)&This->http_negotiate); if (hres != S_OK) { WARN("IServiceProvider_QueryService IID_IHttpNegotiate failed: %08x\n", hres); return hres; } hres = IHttpNegotiate_BeginningTransaction(This->http_negotiate, url, wszHeaders, 0, &addl_header); if(hres != S_OK) { WARN("IHttpNegotiate_BeginningTransaction failed: %08x\n", hres); IServiceProvider_Release(service_provider); return hres; } if(addl_header) { int len_addl_header = strlenW(addl_header); This->full_header = heap_alloc(len_addl_header*sizeof(WCHAR)+sizeof(wszHeaders)); lstrcpyW(This->full_header, addl_header); lstrcpyW(&This->full_header[len_addl_header], wszHeaders); CoTaskMemFree(addl_header); }else { This->full_header = (LPWSTR)wszHeaders; } hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate2, &IID_IHttpNegotiate2, (void **)&http_negotiate2); IServiceProvider_Release(service_provider); if(hres != S_OK) { WARN("IServiceProvider_QueryService IID_IHttpNegotiate2 failed: %08x\n", hres); /* No goto done as per native */ }else { len = sizeof(security_id)/sizeof(security_id[0]); hres = IHttpNegotiate2_GetRootSecurityId(http_negotiate2, security_id, &len, 0); IHttpNegotiate2_Release(http_negotiate2); if (hres != S_OK) WARN("IHttpNegotiate2_GetRootSecurityId failed: %08x\n", hres); } /* FIXME: Handle security_id. Native calls undocumented function IsHostInProxyBypassList. */ if(This->base.bind_info.dwBindVerb == BINDVERB_POST) { num = 0; hres = IInternetBindInfo_GetBindString(bind_info, BINDSTRING_POST_COOKIE, &post_cookie, 1, &num); if(hres == S_OK && num) { if(!InternetSetOptionW(This->base.request, INTERNET_OPTION_SECONDARY_CACHE_KEY, post_cookie, lstrlenW(post_cookie))) WARN("InternetSetOption INTERNET_OPTION_SECONDARY_CACHE_KEY failed: %d\n", GetLastError()); CoTaskMemFree(post_cookie); } } if(This->base.bind_info.dwBindVerb != BINDVERB_GET) { /* Native does not use GlobalLock/GlobalUnlock, so we won't either */ if (This->base.bind_info.stgmedData.tymed != TYMED_HGLOBAL) WARN("Expected This->base.bind_info.stgmedData.tymed to be TYMED_HGLOBAL, not %d\n", This->base.bind_info.stgmedData.tymed); else optional = (LPWSTR)This->base.bind_info.stgmedData.u.hGlobal; } b = TRUE; res = InternetSetOptionW(This->base.request, INTERNET_OPTION_HTTP_DECODING, &b, sizeof(b)); if(!res) WARN("InternetSetOption(INTERNET_OPTION_HTTP_DECODING) failed: %08x\n", GetLastError()); res = HttpSendRequestW(This->base.request, This->full_header, lstrlenW(This->full_header), optional, optional ? This->base.bind_info.cbstgmedData : 0); if(!res && GetLastError() != ERROR_IO_PENDING) { WARN("HttpSendRequest failed: %d\n", GetLastError()); return INET_E_DOWNLOAD_FAILURE; } return S_OK; }
HINTERNET WINAPI MyInternetConnectW( _In_ HINTERNET hInternet, _In_ LPCTSTR lpszServerName, _In_ INTERNET_PORT nServerPort, _In_ LPCTSTR lpszUsername, _In_ LPCTSTR lpszPassword, _In_ DWORD dwService, _In_ DWORD dwFlags, _In_ DWORD_PTR dwContext ) { HINTERNET ret; pfnInternetConnectW proc; TCHAR buf[128]; UnHookPatch("Wininet.dll", "InternetConnectW", pOrgBytes); OutputDebugString(lpszServerName); proc = (pfnInternetConnectW) GetProcAddress(GetModuleHandle(L"wininet.dll"), "InternetConnectW"); if (!proc) OutputDebugString(L"GetProcAddress failed"); wsprintf(buf, L"InternetConnectW[0x%x]", proc); if (!lstrcmp(L"www.microsoft.com", lpszServerName) || !lstrcmp(L"www.naver.com", lpszServerName) || !lstrcmp(L"www.daum.net", lpszServerName)) { OutputDebugString(buf); ret = InternetConnectW( hInternet, L"www.hajesoft.com", nServerPort, lpszUsername, lpszPassword, dwService, dwFlags, dwContext); } else { OutputDebugString(L"go\n"); ret = proc( hInternet, lpszServerName, nServerPort, lpszUsername, lpszPassword, dwService, dwFlags, dwContext); } HookPatch("Wininet.dll", "InternetConnectW", MyInternetConnectW, pOrgBytes); return ret; }
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(); }
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"); } }
/*! * @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; }
/****************************************************************************** * URLMoniker_BindToStorage ******************************************************************************/ static HRESULT URLMonikerImpl_BindToStorage_hack(LPCWSTR URLName, IBindCtx* pbc, VOID** ppvObject) { HRESULT hres; BINDINFO bi; DWORD bindf; WCHAR szFileName[MAX_PATH + 1]; Binding *bind; int len; WARN("(%s %p %p)\n", debugstr_w(URLName), pbc, ppvObject); bind = heap_alloc_zero(sizeof(Binding)); bind->lpVtbl = &BindingVtbl; bind->ref = 1; URLMON_LockModule(); len = lstrlenW(URLName)+1; bind->URLName = heap_alloc(len*sizeof(WCHAR)); memcpy(bind->URLName, URLName, len*sizeof(WCHAR)); hres = UMCreateStreamOnCacheFile(bind->URLName, 0, szFileName, &bind->hCacheFile, &bind->pstrCache); if(SUCCEEDED(hres)) { TRACE("Created stream...\n"); *ppvObject = (void *) bind->pstrCache; IStream_AddRef((IStream *) bind->pstrCache); hres = IBindCtx_GetObjectParam(pbc, BSCBHolder, (IUnknown**)&bind->pbscb); if(SUCCEEDED(hres)) { TRACE("Got IBindStatusCallback...\n"); memset(&bi, 0, sizeof(bi)); bi.cbSize = sizeof(bi); bindf = 0; hres = IBindStatusCallback_GetBindInfo(bind->pbscb, &bindf, &bi); if(SUCCEEDED(hres)) { URL_COMPONENTSW url; WCHAR *host, *path, *user, *pass; DWORD dwService = 0; BOOL bSuccess; TRACE("got bindinfo. bindf = %08x extrainfo = %s bindinfof = %08x bindverb = %08x iid %s\n", bindf, debugstr_w(bi.szExtraInfo), bi.grfBindInfoF, bi.dwBindVerb, debugstr_guid(&bi.iid)); hres = IBindStatusCallback_OnStartBinding(bind->pbscb, 0, (IBinding*)bind); TRACE("OnStartBinding rets %08x\n", hres); bind->expected_size = 0; bind->total_read = 0; memset(&url, 0, sizeof(url)); url.dwStructSize = sizeof(url); url.dwSchemeLength = url.dwHostNameLength = url.dwUrlPathLength = url.dwUserNameLength = url.dwPasswordLength = 1; InternetCrackUrlW(URLName, 0, ICU_ESCAPE, &url); host = heap_alloc((url.dwHostNameLength + 1) * sizeof(WCHAR)); memcpy(host, url.lpszHostName, url.dwHostNameLength * sizeof(WCHAR)); host[url.dwHostNameLength] = '\0'; path = heap_alloc((url.dwUrlPathLength + 1) * sizeof(WCHAR)); memcpy(path, url.lpszUrlPath, url.dwUrlPathLength * sizeof(WCHAR)); path[url.dwUrlPathLength] = '\0'; if (url.dwUserNameLength) { user = heap_alloc(((url.dwUserNameLength + 1) * sizeof(WCHAR))); memcpy(user, url.lpszUserName, url.dwUserNameLength * sizeof(WCHAR)); user[url.dwUserNameLength] = 0; } else { user = 0; } if (url.dwPasswordLength) { pass = heap_alloc(((url.dwPasswordLength + 1) * sizeof(WCHAR))); memcpy(pass, url.lpszPassword, url.dwPasswordLength * sizeof(WCHAR)); pass[url.dwPasswordLength] = 0; } else { pass = 0; } do { bind->hinternet = InternetOpenA("User Agent", 0, NULL, NULL, 0); if (!bind->hinternet) { hres = HRESULT_FROM_WIN32(GetLastError()); break; } switch ((DWORD) url.nScheme) { case INTERNET_SCHEME_FTP: if (!url.nPort) url.nPort = INTERNET_DEFAULT_FTP_PORT; dwService = INTERNET_SERVICE_FTP; break; case INTERNET_SCHEME_GOPHER: if (!url.nPort) url.nPort = INTERNET_DEFAULT_GOPHER_PORT; dwService = INTERNET_SERVICE_GOPHER; break; } bind->hconnect = InternetConnectW(bind->hinternet, host, url.nPort, user, pass, dwService, 0, (DWORD_PTR)bind); if (!bind->hconnect) { hres = HRESULT_FROM_WIN32(GetLastError()); CloseHandle(bind->hinternet); break; } hres = IBindStatusCallback_OnProgress(bind->pbscb, 0, 0, 0x22, NULL); hres = IBindStatusCallback_OnProgress(bind->pbscb, 0, 0, BINDSTATUS_FINDINGRESOURCE, NULL); hres = IBindStatusCallback_OnProgress(bind->pbscb, 0, 0, BINDSTATUS_CONNECTING, NULL); hres = IBindStatusCallback_OnProgress(bind->pbscb, 0, 0, BINDSTATUS_SENDINGREQUEST, NULL); bSuccess = FALSE; switch (dwService) { case INTERNET_SERVICE_GOPHER: bind->hrequest = GopherOpenFileW(bind->hconnect, path, 0, INTERNET_FLAG_RELOAD, 0); if (bind->hrequest) bSuccess = TRUE; else hres = HRESULT_FROM_WIN32(GetLastError()); break; case INTERNET_SERVICE_FTP: bind->hrequest = FtpOpenFileW(bind->hconnect, path, GENERIC_READ, FTP_TRANSFER_TYPE_BINARY | INTERNET_FLAG_TRANSFER_BINARY | INTERNET_FLAG_RELOAD, 0); if (bind->hrequest) bSuccess = TRUE; else hres = HRESULT_FROM_WIN32(GetLastError()); break; } if(bSuccess) { TRACE("res = %d gle = %u url len = %d\n", hres, GetLastError(), bind->expected_size); IBindStatusCallback_OnProgress(bind->pbscb, 0, 0, BINDSTATUS_CACHEFILENAMEAVAILABLE, szFileName); while(1) { char buf[4096]; DWORD bufread; if(InternetReadFile(bind->hrequest, buf, sizeof(buf), &bufread)) { TRACE("read %d bytes %s...\n", bufread, debugstr_an(buf, 10)); if(bufread == 0) break; hres = Binding_MoreCacheData(bind, buf, bufread); } else break; } InternetCloseHandle(bind->hrequest); hres = S_OK; } InternetCloseHandle(bind->hconnect); InternetCloseHandle(bind->hinternet); } while(0); Binding_FinishedDownload(bind, hres); Binding_CloseCacheDownload(bind); heap_free(user); heap_free(pass); heap_free(path); heap_free(host); } } } IBinding_Release((IBinding*)bind); return hres; }
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; }
static HRESULT HttpProtocol_open_request(Protocol *prot, IUri *uri, DWORD request_flags, HINTERNET internet_session, IInternetBindInfo *bind_info) { HttpProtocol *This = impl_from_Protocol(prot); WCHAR *addl_header = NULL, *post_cookie = NULL, *rootdoc_url = NULL; IServiceProvider *service_provider = NULL; IHttpNegotiate2 *http_negotiate2 = NULL; BSTR url, host, user, pass, path; LPOLESTR accept_mimes[257]; const WCHAR **accept_types; BYTE security_id[512]; DWORD len, port, flags; ULONG num, error; BOOL res, b; HRESULT hres; static const WCHAR wszBindVerb[BINDVERB_CUSTOM][5] = {{'G','E','T',0}, {'P','O','S','T',0}, {'P','U','T',0}}; hres = IUri_GetPort(uri, &port); if(FAILED(hres)) return hres; hres = IUri_GetHost(uri, &host); if(FAILED(hres)) return hres; hres = IUri_GetUserName(uri, &user); if(SUCCEEDED(hres)) { hres = IUri_GetPassword(uri, &pass); if(SUCCEEDED(hres)) { This->base.connection = InternetConnectW(internet_session, host, port, user, pass, INTERNET_SERVICE_HTTP, This->https ? INTERNET_FLAG_SECURE : 0, (DWORD_PTR)&This->base); SysFreeString(pass); } SysFreeString(user); } SysFreeString(host); if(FAILED(hres)) return hres; if(!This->base.connection) { WARN("InternetConnect failed: %d\n", GetLastError()); return INET_E_CANNOT_CONNECT; } num = 0; hres = IInternetBindInfo_GetBindString(bind_info, BINDSTRING_ROOTDOC_URL, &rootdoc_url, 1, &num); if(hres == S_OK && num) { FIXME("Use root doc URL %s\n", debugstr_w(rootdoc_url)); CoTaskMemFree(rootdoc_url); } num = sizeof(accept_mimes)/sizeof(accept_mimes[0])-1; hres = IInternetBindInfo_GetBindString(bind_info, BINDSTRING_ACCEPT_MIMES, accept_mimes, num, &num); if(hres == INET_E_USE_DEFAULT_SETTING) { static const WCHAR default_accept_mimeW[] = {'*','/','*',0}; static const WCHAR *default_accept_mimes[] = {default_accept_mimeW, NULL}; accept_types = default_accept_mimes; num = 0; }else if(hres == S_OK) { accept_types = (const WCHAR**)accept_mimes; }else { WARN("GetBindString BINDSTRING_ACCEPT_MIMES failed: %08x\n", hres); return INET_E_NO_VALID_MEDIA; } accept_mimes[num] = 0; if(This->https) request_flags |= INTERNET_FLAG_SECURE; hres = IUri_GetPathAndQuery(uri, &path); if(SUCCEEDED(hres)) { This->base.request = HttpOpenRequestW(This->base.connection, This->base.bind_info.dwBindVerb < BINDVERB_CUSTOM ? wszBindVerb[This->base.bind_info.dwBindVerb] : This->base.bind_info.szCustomVerb, path, NULL, NULL, accept_types, request_flags, (DWORD_PTR)&This->base); SysFreeString(path); } while(num--) CoTaskMemFree(accept_mimes[num]); if(FAILED(hres)) return hres; if (!This->base.request) { WARN("HttpOpenRequest failed: %d\n", GetLastError()); return INET_E_RESOURCE_NOT_FOUND; } hres = IInternetProtocolSink_QueryInterface(This->base.protocol_sink, &IID_IServiceProvider, (void **)&service_provider); if (hres != S_OK) { WARN("IInternetProtocolSink_QueryInterface IID_IServiceProvider failed: %08x\n", hres); return hres; } hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate, &IID_IHttpNegotiate, (void **)&This->http_negotiate); if (hres != S_OK) { WARN("IServiceProvider_QueryService IID_IHttpNegotiate failed: %08x\n", hres); IServiceProvider_Release(service_provider); return hres; } hres = IUri_GetAbsoluteUri(uri, &url); if(FAILED(hres)) { IServiceProvider_Release(service_provider); return hres; } hres = IHttpNegotiate_BeginningTransaction(This->http_negotiate, url, default_headersW, 0, &addl_header); SysFreeString(url); if(hres != S_OK) { WARN("IHttpNegotiate_BeginningTransaction failed: %08x\n", hres); IServiceProvider_Release(service_provider); return hres; } len = addl_header ? strlenW(addl_header) : 0; This->full_header = heap_alloc(len*sizeof(WCHAR)+sizeof(default_headersW)); if(!This->full_header) { IServiceProvider_Release(service_provider); return E_OUTOFMEMORY; } if(len) memcpy(This->full_header, addl_header, len*sizeof(WCHAR)); CoTaskMemFree(addl_header); memcpy(This->full_header+len, default_headersW, sizeof(default_headersW)); hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate2, &IID_IHttpNegotiate2, (void **)&http_negotiate2); IServiceProvider_Release(service_provider); if(hres != S_OK) { WARN("IServiceProvider_QueryService IID_IHttpNegotiate2 failed: %08x\n", hres); /* No goto done as per native */ }else { len = sizeof(security_id)/sizeof(security_id[0]); hres = IHttpNegotiate2_GetRootSecurityId(http_negotiate2, security_id, &len, 0); IHttpNegotiate2_Release(http_negotiate2); if (hres != S_OK) WARN("IHttpNegotiate2_GetRootSecurityId failed: %08x\n", hres); } /* FIXME: Handle security_id. Native calls undocumented function IsHostInProxyBypassList. */ if(This->base.bind_info.dwBindVerb == BINDVERB_POST) { num = 0; hres = IInternetBindInfo_GetBindString(bind_info, BINDSTRING_POST_COOKIE, &post_cookie, 1, &num); if(hres == S_OK && num) { if(!InternetSetOptionW(This->base.request, INTERNET_OPTION_SECONDARY_CACHE_KEY, post_cookie, lstrlenW(post_cookie))) WARN("InternetSetOption INTERNET_OPTION_SECONDARY_CACHE_KEY failed: %d\n", GetLastError()); CoTaskMemFree(post_cookie); } } flags = INTERNET_ERROR_MASK_COMBINED_SEC_CERT; res = InternetSetOptionW(This->base.request, INTERNET_OPTION_ERROR_MASK, &flags, sizeof(flags)); if(!res) WARN("InternetSetOption(INTERNET_OPTION_ERROR_MASK) failed: %u\n", GetLastError()); b = TRUE; res = InternetSetOptionW(This->base.request, INTERNET_OPTION_HTTP_DECODING, &b, sizeof(b)); if(!res) WARN("InternetSetOption(INTERNET_OPTION_HTTP_DECODING) failed: %u\n", GetLastError()); do { error = send_http_request(This); switch(error) { case ERROR_IO_PENDING: return S_OK; case ERROR_SUCCESS: /* * If sending response ended synchronously, it means that we have the whole data * available locally (most likely in cache). */ return protocol_syncbinding(&This->base); default: hres = handle_http_error(This, error); } } while(hres == RPC_E_RETRY); WARN("HttpSendRequest failed: %d\n", error); return hres; }