페이지

글타래

2009년 6월 12일 금요일

URL Parse for SDK Function like AfxParseUrl

필요한분 쓰시기 바람
(MFC 코드를 WIN32 모드 DLL에서 쓰기위해
MFC 의 AfxParseUrl() 함수를 WIN32 SDK에 맞게 수정함)
http://durumul.com
krkim

BOOL MyParseUrl(LPCTSTR pstrURL, DWORD& dwServiceType,
LPSTR strServer, LPSTR strObject, int &nPort,
LPSTR strUsername, LPSTR strPassword, DWORD dwFlags = 0);
#include "shlwapi.h"
#include "wininet.h"
#pragma comment (lib,"shlwapi.lib")
#pragma comment (lib,"Wininet.lib")

BOOL _myparseurl(LPCTSTR pstrURL,
LPURL_COMPONENTS lpComponents, DWORD& dwServiceType,
int & nPort, DWORD dwFlags)
{
// this function will return bogus stuff if lpComponents
// isn't set up to copy the components
LPTSTR pstrCanonicalizedURL;
TCHAR szCanonicalizedURL[INTERNET_MAX_URL_LENGTH];
DWORD dwNeededLength = INTERNET_MAX_URL_LENGTH;
BOOL bRetVal;
BOOL bMustFree = FALSE;

// Decoding is done in InternetCrackUrl/UrlUnescape
// so we don't need the ICU_DECODE flag here.

DWORD dwCanonicalizeFlags = dwFlags & (ICU_NO_ENCODE ICU_NO_META ICU_ENCODE_SPACES_ONLY ICU_BROWSER_MODE);

DWORD dwCrackFlags = 0;

BOOL bUnescape = FALSE;

if((dwFlags & (ICU_ESCAPE ICU_DECODE)) && (lpComponents->dwUrlPathLength != 0) )
{
// We use only the ICU_ESCAPE flag for decoding even if
// ICU_DECODE is passed.

// Also, if ICU_BROWSER_MODE is passed we do the unescaping
// manually because InternetCrackUrl doesn't do
// Browser mode unescaping

if (dwFlags & ICU_BROWSER_MODE)
bUnescape = TRUE;
else
dwCrackFlags = ICU_ESCAPE;
}

bRetVal = InternetCanonicalizeUrl(pstrURL, szCanonicalizedURL,
&dwNeededLength, dwCanonicalizeFlags);

if (!bRetVal)
{
if (::GetLastError() != ERROR_INSUFFICIENT_BUFFER)
return FALSE;

pstrCanonicalizedURL = new TCHAR[dwNeededLength];
if (pstrCanonicalizedURL == NULL)
return FALSE;

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(bUnescape)
{
if(FAILED(UrlUnescape(lpComponents->lpszUrlPath,NULL,NULL,URL_UNESCAPE_INPLACE URL_DONT_UNESCAPE_EXTRA_INFO)))
{
if (bMustFree)
delete [] pstrCanonicalizedURL;

return FALSE;
}

lpComponents->dwUrlPathLength = lstrlen(lpComponents->lpszUrlPath);
}

if (bMustFree)
delete [] pstrCanonicalizedURL;

if (!bRetVal)
dwServiceType = INTERNET_SCHEME_UNKNOWN;
else
{
nPort = lpComponents->nPort;
dwServiceType = lpComponents->nScheme;
}
return bRetVal;
}
//SDK available function to parser url string
//durumul.com
BOOL MyParseUrl(LPCTSTR pstrURL, DWORD& dwServiceType,
LPSTR strServer, LPSTR strObject, int & nPort,
LPSTR strUsername, LPSTR strPassword, DWORD dwFlags/* = 0*/)
{
dwServiceType = INTERNET_SCHEME_UNKNOWN;
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;
urlComponents.dwUrlPathLength = INTERNET_MAX_PATH_LENGTH;
urlComponents.lpszUrlPath = strObject;
urlComponents.dwUserNameLength = INTERNET_MAX_USER_NAME_LENGTH;
urlComponents.lpszUserName = strUsername;
urlComponents.dwPasswordLength = INTERNET_MAX_PASSWORD_LENGTH;
urlComponents.lpszPassword = strPassword;
BOOL bRetVal = _myparseurl(pstrURL, &urlComponents,dwServiceType, nPort, dwFlags);
return bRetVal;
}

댓글 없음:

댓글 쓰기