Click here to Skip to main content
15,896,329 members
Articles / Desktop Programming / ATL

An Asynchronous Pluggable Protocol Handler for data: URLs

Rate me:
Please Sign up or sign in to vote.
4.76/5 (21 votes)
28 Jan 2006CPOL5 min read 162.9K   2K   60  
This article describes an asynchronous pluggable protocol implementation to support the data: protocol, as described in RFC 2397, in Internet Explorer.
#pragma once

class CDataURL
{
public:
	CDataURL()
		: m_strAttribute(L"charset")
		, m_strValue(L"US-ASCII")
		, m_strMimeType(L"text/plain")
		, m_pvData(NULL)
		, m_dwDataLength(0)
	{
	}
	
	void Reset()
	{
		*this = CDataURL();
	}

	bool Parse(LPCWSTR szURL);
	
	CAtlStringA GetMimeType()
	{
		return m_strMimeType;
	}

	CAtlStringA GetCharset()
	{
		if (m_strAttribute.CompareNoCase("charset") == 0)
		{
			return m_strValue;
		}
		
		return CAtlStringA();
	}

	CAtlStringA GetAttribute()
	{
		return m_strAttribute;
	}

	CAtlStringA GetValue()
	{
		return m_strValue;
	}

	CAtlStringA GetDataString()
	{
		return m_strData;
	}

	BYTE* GetData()
	{
		return m_pvData;
	}

	DWORD GetDataLength()
	{
		return m_dwDataLength;
	}

public:
	CDataURL(const CDataURL& u)
		: m_strMimeType(u.m_strMimeType)
		, m_strAttribute(u.m_strAttribute)
		, m_strValue(u.m_strValue)
		, m_strData(u.m_strData)
		, m_dwDataLength(u.m_dwDataLength)
		, m_pvData(NULL)
	{
		if (m_dwDataLength)
		{
			m_pvData = new BYTE[u.m_dwDataLength];
			memcpy_s(m_pvData, m_dwDataLength, u.m_pvData, u.m_dwDataLength);
		}
	}
	
	CDataURL& operator = (const CDataURL& u)
	{	
		m_strMimeType = u.m_strMimeType;
		m_strAttribute = u.m_strAttribute;
		m_strValue = u.m_strValue;
		m_dwDataLength = u.m_dwDataLength;
		delete [] m_pvData;
		m_pvData = new BYTE[m_dwDataLength];
		memcpy_s(m_pvData, m_dwDataLength, u.m_pvData, u.m_dwDataLength);
		
		return *this;
	}

	~CDataURL()
	{
		delete [] m_pvData;
	}

private:
	void AssignMatchGroup(CAtlStringA& str, CAtlREMatchContext<CAtlRECharTraitsA>& ctxt, int nGroup)
	{
		CAtlREMatchContext<CAtlRECharTraitsA>::MatchGroup g;
		ctxt.GetMatch(nGroup, &g);

		intptr_t nLen = g.szEnd - g.szStart;

		if (nLen != 0)
			str = CAtlString(g.szStart, int(nLen));
	}

private:
	CAtlStringA m_strMimeType;
	CAtlStringA m_strAttribute;
	CAtlStringA m_strValue;
	CAtlStringA m_strData;
	BYTE* m_pvData;
	DWORD m_dwDataLength;

	friend bool operator == (CDataURL& u1, CDataURL& u2);
};

bool operator == (CDataURL& u1, CDataURL& u2)
{
	return (u1.m_strMimeType.CompareNoCase(u2.m_strMimeType) == 0  && 
		u1.m_dwDataLength == u2.m_dwDataLength &&
		(u1.m_pvData == u2.m_pvData ||
			memcmp(u1.m_pvData, u2.m_pvData, u1.m_dwDataLength)
		)
	);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions