summaryrefslogtreecommitdiff
path: root/NetCmpts/parseurl.cpp
blob: 786cc744c120a2f7a15629bc3aeb22168f259eb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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;
}