Beispiel #1
0
BOOL FuzzyUrlCompare( LPCTSTR lpszUrl1, LPCTSTR lpszUrl2 )
{
	static const TCHAR ANCHOR = _T('#');
	static const TCHAR FILE_PROTOCOL [] = _T("file://");
	static const size_t FILE_PROTOCOL_LENGTH = _tcslen(FILE_PROTOCOL);

	BOOL bMatch = TRUE;

	if ( lpszUrl1 && lpszUrl2 )
	{
		TCHAR szDummy1[MAX_PATH];
		TCHAR szDummy2[MAX_PATH];

		if ( _tcsncmp( lpszUrl1, FILE_PROTOCOL, FILE_PROTOCOL_LENGTH ) == 0 )
		{
			DWORD dwLen = MAX_PATH;
			if ( PathCreateFromUrl( lpszUrl1, szDummy1, & dwLen, 0 ) == S_OK )
			{
				lpszUrl1 = szDummy1;
			}
		}

		if ( _tcsncmp( lpszUrl2, FILE_PROTOCOL, FILE_PROTOCOL_LENGTH ) == 0 )
		{
			DWORD dwLen = MAX_PATH;
			if ( PathCreateFromUrl( lpszUrl2, szDummy2, & dwLen, 0 ) == S_OK )
			{
				lpszUrl2 = szDummy2;
			}
		}

		do
		{
			if ( *lpszUrl1 != *lpszUrl2 )
			{
				if ( ( ( ANCHOR == *lpszUrl1 ) && ( 0 == *lpszUrl2 ) ) ||
					( ( ANCHOR == *lpszUrl2 ) && ( 0 == *lpszUrl1 ) ) )
				{
					bMatch = TRUE;
				}
				else
				{
					bMatch = FALSE;
				}

				break;
			}

			lpszUrl1++;
			lpszUrl2++;

		} while ( *lpszUrl1 || *lpszUrl2 );
	}

	return bMatch;
}
Beispiel #2
0
/*
	Downloads the given url and returns the webpage as dynamically allocated string.
	You need to free the returned string after use!
*/
BYTE* DownloadUrl(HINTERNET handle, std::wstring& url, DWORD* dataSize, bool forceReload)
{
	if (_wcsnicmp(url.c_str(), L"file://", 7) == 0)  // Local file
	{
		WCHAR path[MAX_PATH];
		DWORD pathLength = _countof(path);
		HRESULT hr = PathCreateFromUrl(url.c_str(), path, &pathLength, 0);
		if (FAILED(hr))
		{
			return nullptr;
		}
		
		size_t fileSize = 0;
		BYTE* buffer = FileUtil::ReadFullFile(path, &fileSize).release();
		*dataSize = (DWORD)fileSize;

		return buffer;
	}

	DWORD flags = INTERNET_FLAG_RESYNCHRONIZE;
	if (forceReload)
	{
		flags = INTERNET_FLAG_RELOAD;
	}

	HINTERNET hUrlDump = InternetOpenUrl(handle, url.c_str(), nullptr, 0, flags, 0);
	if (!hUrlDump)
	{
		return nullptr;
	}

	// Allocate buffer with 3 extra bytes for triple null termination in case the string is
	// invalid (e.g. when incorrectly using the UTF-16LE codepage for the data).
	const int CHUNK_SIZE = 8192;
	DWORD bufferSize = CHUNK_SIZE;
	BYTE* buffer = (BYTE*)malloc(bufferSize + 3);
	*dataSize = 0;

	// Read the data.
	do
	{
		DWORD readSize;
		if (!InternetReadFile(hUrlDump, buffer + *dataSize, bufferSize - *dataSize, &readSize))
		{
			free(buffer);
			InternetCloseHandle(hUrlDump);
			return nullptr;
		}
		else if (readSize == 0)
		{
			// All data read.
			break;
		}

		*dataSize += readSize;

		bufferSize += CHUNK_SIZE;
		buffer = (BYTE*)realloc(buffer, bufferSize + 3);
	}
	while (true);

	InternetCloseHandle(hUrlDump);

	// Triple null terminate the buffer.
	buffer[*dataSize] = 0;
	buffer[*dataSize + 1] = 0;
	buffer[*dataSize + 2] = 0;

	return buffer;
}