/////////////////////////////////////////////////////////////////////////////
// Global Functions
AFX_STATIC BOOL AFXAPI _AfxParseURLWorker(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);
ASSERT(AfxIsValidAddress(lpComponents, sizeof(URL_COMPONENTS), TRUE));
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);
if (!bRetVal)
{
if (::GetLastError() != ERROR_INSUFFICIENT_BUFFER)
return FALSE;
pstrCanonicalizedURL = new TCHAR[dwNeededLength];
bMustFree = TRUE;
bRetVal = InternetCanonicalizeUrl(pstrURL, pstrCanonicalizedURL,
&dwNeededLength, dwCanonicalizeFlags);
if (!bRetVal)
{
delete [] pstrCanonicalizedURL;
return FALSE;
}
}
else
pstrCanonicalizedURL = szCanonicalizedURL;
// now that it's safely canonicalized, crack it
bRetVal = InternetCrackUrl(pstrCanonicalizedURL, 0,
dwCrackFlags, lpComponents);
if (bMustFree)
delete [] pstrCanonicalizedURL;
// convert to MFC-style service ID
if (!bRetVal)
dwServiceType = AFX_INET_SERVICE_UNK;
else
{
nPort = lpComponents->nPort;
switch (lpComponents->nScheme)
{
case INTERNET_SCHEME_FTP:
dwServiceType = AFX_INET_SERVICE_FTP;
break;
case INTERNET_SCHEME_GOPHER:
dwServiceType = AFX_INET_SERVICE_GOPHER;
break;
case INTERNET_SCHEME_HTTP:
dwServiceType = AFX_INET_SERVICE_HTTP;
break;
case INTERNET_SCHEME_HTTPS:
dwServiceType = AFX_INET_SERVICE_HTTPS;
break;
case INTERNET_SCHEME_FILE:
dwServiceType = AFX_INET_SERVICE_FILE;
break;
case INTERNET_SCHEME_NEWS:
dwServiceType = AFX_INET_SERVICE_NNTP;
break;
case INTERNET_SCHEME_MAILTO:
dwServiceType = AFX_INET_SERVICE_MAILTO;
break;
default:
dwServiceType = AFX_INET_SERVICE_UNK;
}
}
return bRetVal;
}
BOOL AFXAPI AfxParseURLEx(LPCTSTR pstrURL, DWORD& dwServiceType,
CString& strServer, CString& strObject, INTERNET_PORT& nPort,
CString& strUsername, CString& strPassword, DWORD dwFlags/* = 0*/)
{
dwServiceType = AFX_INET_SERVICE_UNK;
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.GetBuffer(INTERNET_MAX_HOST_NAME_LENGTH+1);
urlComponents.dwUrlPathLength = INTERNET_MAX_PATH_LENGTH;
urlComponents.lpszUrlPath = strObject.GetBuffer(INTERNET_MAX_PATH_LENGTH+1);
urlComponents.dwUserNameLength = INTERNET_MAX_USER_NAME_LENGTH;
urlComponents.lpszUserName = strUsername.GetBuffer(INTERNET_MAX_USER_NAME_LENGTH+1);
urlComponents.dwPasswordLength = INTERNET_MAX_PASSWORD_LENGTH;
urlComponents.lpszPassword = strPassword.GetBuffer(INTERNET_MAX_PASSWORD_LENGTH+1);
BOOL bRetVal = _AfxParseURLWorker(pstrURL, &urlComponents,
dwServiceType, nPort, dwFlags);
strServer.ReleaseBuffer();
strObject.ReleaseBuffer();
strUsername.ReleaseBuffer();
strPassword.ReleaseBuffer();
return bRetVal;
}
BOOL AFXAPI AfxParseURL(LPCTSTR pstrURL, DWORD& dwServiceType,
CString& strServer, CString& strObject, INTERNET_PORT& nPort)
{
dwServiceType = AFX_INET_SERVICE_UNK;
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.GetBuffer(INTERNET_MAX_URL_LENGTH+1);
urlComponents.dwUrlPathLength = INTERNET_MAX_URL_LENGTH;
urlComponents.lpszUrlPath = strObject.GetBuffer(INTERNET_MAX_URL_LENGTH+1);
BOOL bRetVal = _AfxParseURLWorker(pstrURL, &urlComponents,
dwServiceType, nPort, ICU_BROWSER_MODE);
strServer.ReleaseBuffer();
strObject.ReleaseBuffer();
return bRetVal;
}