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); }
boolean CNetRequestImpl::readHeaders(Hashtable<String,String>& oHeaders) { oHeaders.clear(); CAtlStringW strHeaders; DWORD dwLen = 0; DWORD nIndex = 0; if( !HttpQueryInfo( hRequest, HTTP_QUERY_RAW_HEADERS_CRLF, null, &dwLen, &nIndex) ) { DWORD dwErr = ::GetLastError(); if ( dwErr != ERROR_INSUFFICIENT_BUFFER ) { pszErrFunction = L"HttpQueryInfo"; return false; } } if( !HttpQueryInfo( hRequest, HTTP_QUERY_RAW_HEADERS_CRLF, strHeaders.GetBuffer(dwLen), &dwLen, &nIndex) ) { pszErrFunction = L"HttpQueryInfo"; return false; } strHeaders.ReleaseBuffer(); int nStart = 0; for(int nEnd = strHeaders.Find(L"\r\n", nStart); nEnd > 0; nStart = nEnd+2, nEnd = strHeaders.Find(L"\r\n", nStart) ) { CAtlStringW strHeader = strHeaders.Mid(nStart, nEnd-nStart); int nSep = strHeader.Find(':'); if (nSep < 0 ) continue; CAtlStringW strName = strHeader.Mid(0, nSep); strName.Trim(); strName.MakeLower(); CAtlStringW strValue = strHeader.Mid(nSep+1); strValue.Trim(); oHeaders.put(common::convertToStringA(strName.GetString()),common::convertToStringA(strValue.GetString())); } return true; }
void CNetRequestImpl::ErrorMessage(LPCTSTR pszFunction) { // Retrieve the system error message for the last-error code LPTSTR pszMessage = NULL; DWORD dwLastError = GetLastError(); DWORD dwLen = FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | //FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE| FORMAT_MESSAGE_IGNORE_INSERTS, GetModuleHandle( _T("wininet.dll") ), dwLastError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&pszMessage, 0, NULL ); CAtlStringW strExtError; if ( dwLastError == ERROR_INTERNET_EXTENDED_ERROR ) { DWORD dwInetError =0, dwExtLength = 0; InternetGetLastResponseInfo( &dwInetError, NULL, &dwExtLength ); if ( dwExtLength > 0 ) { InternetGetLastResponseInfo( &dwInetError, strExtError.GetBuffer(dwExtLength+1), &dwExtLength ); strExtError.ReleaseBuffer(); } } rho::LogMessage oLogMsg(__FILE__, __LINE__, L_ERROR, LOGCONF(), getLogCategory() ); oLogMsg + "Call " + pszFunction + " failed. With code : " + dwLastError; if ( pszMessage ) oLogMsg + ".Message: " + pszMessage; if ( strExtError.GetLength() ) oLogMsg + ".Extended info: " + strExtError.GetString(); if ( pszMessage ) LocalFree(pszMessage); }
String CNetRequestImpl::makeClientCookie() { DWORD nIndex = 0; String cookie; while(true) { CAtlStringW strCookie; DWORD dwLen = 0; if( !HttpQueryInfo( hRequest, HTTP_QUERY_SET_COOKIE, null, &dwLen, &nIndex) ) { DWORD dwErr = ::GetLastError(); if ( dwErr == ERROR_HTTP_HEADER_NOT_FOUND ) break; if ( dwErr != ERROR_INSUFFICIENT_BUFFER ) { pszErrFunction = L"HttpQueryInfo"; break; } } if( !HttpQueryInfo( hRequest, HTTP_QUERY_SET_COOKIE, strCookie.GetBuffer(dwLen), &dwLen, &nIndex) ) { pszErrFunction = L"HttpQueryInfo"; break; } strCookie.ReleaseBuffer(); URI::parseCookie(common::convertToStringA(strCookie.GetString()).c_str(), cookie); } if (pszErrFunction) return ""; // if ( cookie.strAuth.length() > 0 || cookie.strSession.length() >0 ) // return cookie.strAuth + ";" + cookie.strSession + ";"; return cookie; }
CNetRequestImpl::CNetRequestImpl(CNetRequest* pParent, const char* method, const String& strUrl, IRhoSession* oSession, Hashtable<String,String>* pHeaders) { m_pParent = pParent; m_pParent->m_pCurNetRequestImpl = this; m_pHeaders = pHeaders; m_bCancel = false; m_pSession = oSession; pszErrFunction = NULL; hInet = NULL, hConnection = NULL, hRequest = NULL; memset(&uri, 0, sizeof(uri) ); m_strUrl = strUrl; CAtlStringW strUrlW(strUrl.c_str()); LOG(INFO) + "Method: " + method + ";Url: " + strUrl; do { if ( !URI::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; DWORD dwFlags = INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_NO_COOKIES; if ( uri.lpszScheme && wcsicmp(uri.lpszScheme,L"https")==0) dwFlags |= INTERNET_FLAG_SECURE; hRequest = HttpOpenRequest( hConnection, CAtlStringW(method), strReqUrlW, NULL, NULL, NULL, dwFlags, NULL ); if ( !hRequest ) { pszErrFunction = L"HttpOpenRequest"; break; } if (oSession!=null) { String strSession = oSession->getSession(); LOG(INFO) + "Cookie : " + strSession; if ( strSession.length() > 0 ) { String strHeader = "Cookie: " + strSession + "\r\n"; if ( !HttpAddRequestHeaders( hRequest, common::convertToStringW(strHeader).c_str(), -1, HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE ) ) pszErrFunction = L"HttpAddRequestHeaders"; } } }while(0); }