diff options
author | Stef Walter <stef@thewalter.net> | 2003-09-17 19:07:23 +0000 |
---|---|---|
committer | Stef Walter <stef@thewalter.net> | 2003-09-17 19:07:23 +0000 |
commit | 3f95d417d9e623ac0c74df8ef11d7a01846392dd (patch) | |
tree | 45ec73f2dc07eafd7f41a6f62a8cdfbaa279469f /NetCmpts/parseurl.cpp |
Diffstat (limited to 'NetCmpts/parseurl.cpp')
-rw-r--r-- | NetCmpts/parseurl.cpp | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/NetCmpts/parseurl.cpp b/NetCmpts/parseurl.cpp new file mode 100644 index 0000000..786cc74 --- /dev/null +++ b/NetCmpts/parseurl.cpp @@ -0,0 +1,128 @@ + +#include <stdafx.h> +#include "parseurl.h" + +#ifndef _AFXDLL +#pragma comment(lib, "wininet.lib") +#endif + +///////////////////////////////////////////////////////////////////////////// +// Global Functions + +static bool _ParseURLWorker(LPCTSTR pstrURL, LPURL_COMPONENTS lpComponents, + DWORD& dwServiceType, INTERNET_PORT& nPort, DWORD dwFlags) +{ + // this function will return bogus stuff if lpComponents + // isn't set up to copy the components + ASSERT(lpComponents != NULL && pstrURL != NULL); + if (lpComponents == NULL || pstrURL == NULL) return false; + + ASSERT(lpComponents->dwHostNameLength == 0 || + lpComponents->lpszHostName != NULL); + ASSERT(lpComponents->dwUrlPathLength == 0 || + lpComponents->lpszUrlPath != NULL); + ASSERT(lpComponents->dwUserNameLength == 0 || + lpComponents->lpszUserName != NULL); + ASSERT(lpComponents->dwPasswordLength == 0 || + lpComponents->lpszPassword != NULL); + + LPTSTR pstrCanonicalizedURL; + TCHAR szCanonicalizedURL[INTERNET_MAX_URL_LENGTH]; + DWORD dwNeededLength = INTERNET_MAX_URL_LENGTH; + + bool bRetVal; + bool bMustFree = false; + DWORD dwCanonicalizeFlags = dwFlags & + (ICU_NO_ENCODE | ICU_DECODE | ICU_NO_META | + ICU_ENCODE_SPACES_ONLY | ICU_BROWSER_MODE); + DWORD dwCrackFlags = dwFlags & (ICU_ESCAPE | ICU_USERNAME); + + bRetVal = InternetCanonicalizeUrl(pstrURL, szCanonicalizedURL, &dwNeededLength, + dwCanonicalizeFlags) ? true : false; + + if(!bRetVal) + { + if(::GetLastError() != ERROR_INSUFFICIENT_BUFFER) + return false; + + pstrCanonicalizedURL = new TCHAR[dwNeededLength]; + bMustFree = true; + bRetVal = InternetCanonicalizeUrl(pstrURL, pstrCanonicalizedURL, &dwNeededLength, + dwCanonicalizeFlags) ? true : false; + if(!bRetVal) + { + delete [] pstrCanonicalizedURL; + return false; + } + } + else + pstrCanonicalizedURL = szCanonicalizedURL; + + // now that it's safely canonicalized, crack it + bRetVal = InternetCrackUrl(pstrCanonicalizedURL, 0, + dwCrackFlags, lpComponents) ? true : false; + + if (bMustFree) + delete [] pstrCanonicalizedURL; + + return bRetVal; +} + +bool ParseURLEx(LPCTSTR pstrURL, DWORD& dwServiceType, string& strServer, + string& strObject, INTERNET_PORT& nPort, string& strUsername, + string& strPassword, DWORD dwFlags/* = 0*/) +{ + ASSERT(pstrURL != NULL); + if (pstrURL == NULL) + return false; + + URL_COMPONENTS urlComponents; + memset(&urlComponents, 0, sizeof(URL_COMPONENTS)); + urlComponents.dwStructSize = sizeof(URL_COMPONENTS); + + urlComponents.dwHostNameLength = INTERNET_MAX_HOST_NAME_LENGTH; + urlComponents.lpszHostName = strServer.get_buffer(INTERNET_MAX_HOST_NAME_LENGTH + 1); + urlComponents.dwUrlPathLength = INTERNET_MAX_PATH_LENGTH; + urlComponents.lpszUrlPath = strObject.get_buffer(INTERNET_MAX_PATH_LENGTH + 1); + urlComponents.dwUserNameLength = INTERNET_MAX_USER_NAME_LENGTH; + urlComponents.lpszUserName = strUsername.get_buffer(INTERNET_MAX_USER_NAME_LENGTH + 1); + urlComponents.dwPasswordLength = INTERNET_MAX_PASSWORD_LENGTH; + urlComponents.lpszPassword = strPassword.get_buffer(INTERNET_MAX_PASSWORD_LENGTH + 1); + + bool bRetVal = _ParseURLWorker(pstrURL, &urlComponents, dwServiceType, nPort, + dwFlags); + + dwServiceType = urlComponents.nScheme; + strServer.release_buffer(); + strObject.release_buffer(); + strUsername.release_buffer(); + strPassword.release_buffer(); + + return bRetVal; +} + +bool ParseURL(LPCTSTR pstrURL, DWORD& dwServiceType, string& strServer, + string& strObject, INTERNET_PORT& nPort) +{ + ASSERT(pstrURL != NULL); + if (pstrURL == NULL) + return FALSE; + + URL_COMPONENTS urlComponents; + memset(&urlComponents, 0, sizeof(URL_COMPONENTS)); + urlComponents.dwStructSize = sizeof(URL_COMPONENTS); + + urlComponents.dwHostNameLength = INTERNET_MAX_URL_LENGTH; + urlComponents.lpszHostName = strServer.get_buffer(INTERNET_MAX_URL_LENGTH + 1); + urlComponents.dwUrlPathLength = INTERNET_MAX_URL_LENGTH; + urlComponents.lpszUrlPath = strObject.get_buffer(INTERNET_MAX_URL_LENGTH + 1); + + bool bRetVal = _ParseURLWorker(pstrURL, &urlComponents, dwServiceType, + nPort, ICU_BROWSER_MODE); + + dwServiceType = urlComponents.nScheme; + strServer.release_buffer(); + strObject.release_buffer(); + + return bRetVal; +}
\ No newline at end of file |