⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cookiecollection.cpp

📁 vc++6.0开发网络典型应用实例导航 1. 本光盘提供了本书中所有的实例源程序文件。 2. 附录文件夹下是Winsock 函数参考以及错误码列表
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/********************************************************************/
/*																	*/
/*  CookieCollection.cpp											*/
/*																	*/
/*  Implementation of the CCookieCollection and						*/
/*	CCookieCollectionEnum objects.									*/
/*																	*/
/*	This is an attempt to duplicate the behaviour of the			*/
/*  IRequestDictionary interface.									*/
/*																	*/
/*  Programmed by Pablo van der Meer								*/
/*	This code is stolen from: http://www.pablovandermeer.nl			*/
/*																	*/
/*  Last updated: July 4, 2003										*/
/*																	*/
/********************************************************************/

#include "stdafx.h"
#include "server.h"
#include "CookieCollection.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


IMPLEMENT_DYNCREATE(CCookieCollection, CCmdTarget)

CCookieCollection::CCookieCollection()
{
	EnableAutomation();
}

CCookieCollection::~CCookieCollection()
{
	CCookie *pCookie;
	CString strKey;
	POSITION pos = m_CookieMap.GetStartPosition();
	while (pos != NULL)
	{
		m_CookieMap.GetNextAssoc(pos, strKey, (CObject *&)pCookie);
		delete pCookie;
	}
}


void CCookieCollection::OnFinalRelease()
{
	// When the last reference for an automation object is released
	// OnFinalRelease is called.  The base class will automatically
	// deletes the object.  Add additional cleanup required for your
	// object before calling the base class.

	CCmdTarget::OnFinalRelease();
}


BEGIN_MESSAGE_MAP(CCookieCollection, CCmdTarget)
	//{{AFX_MSG_MAP(CCookieCollection)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

BEGIN_DISPATCH_MAP(CCookieCollection, CCmdTarget)
	//{{AFX_DISPATCH_MAP(CCookieCollection)
	DISP_PROPERTY_EX(CCookieCollection, "Count", GetCount, SetNotSupported, VT_I4)
	DISP_PROPERTY_PARAM(CCookieCollection, "Item", GetItem, SetItem, VT_VARIANT, VTS_VARIANT)	
	DISP_PROPERTY_PARAM(CCookieCollection, "Key", GetKey, SetNotSupported, VT_VARIANT, VTS_VARIANT)
	//}}AFX_DISPATCH_MAP
	DISP_DEFVALUE(CCookieCollection, "Item")
	DISP_PROPERTY_EX_ID(CCookieCollection, "_NewEnum", DISPID_NEWENUM, GetNewEnum, SetNotSupported, VT_UNKNOWN)
END_DISPATCH_MAP()

// {0851C7AF-1D2B-45E0-A89B-F9096A5BE638}
static const IID IID_ICookieCollection =
{ 0x851c7af, 0x1d2b, 0x45e0, { 0xa8, 0x9b, 0xf9, 0x9, 0x6a, 0x5b, 0xe6, 0x38 } };

BEGIN_INTERFACE_MAP(CCookieCollection, CCmdTarget)
	INTERFACE_PART(CCookieCollection, IID_ICookieCollection, Dispatch)
END_INTERFACE_MAP()


/************************************************************************/
/*																		*/
/* Function name : GetCount												*/
/* Description   : Return the number of elements in the Dictionary.		*/
/*																		*/
/************************************************************************/
long CCookieCollection::GetCount()
{
	return m_CookieMap.GetCount();
}


/************************************************************************/
/*																		*/
/* Function name : GetItem												*/
/* Description   : Retrieves the dictionary value for the specified key */
/*																		*/
/************************************************************************/
VARIANT CCookieCollection::GetItem(const VARIANT FAR& Name) 
{
	VARIANT vaResult;
	VariantInit(&vaResult);

	COleVariant vaName(Name);

	// make sure it's a BSTR
	vaName.ChangeType(VT_BSTR);

	CString strName = vaName.bstrVal;
	
	CCookie *pCookie = NULL;

	// lookup value of specified name
	if (!m_CookieMap.Lookup(strName, (CObject *&)pCookie))
	{
		// create an empty cookie
		pCookie = Add(strName, "");
	} 

	if (pCookie)
	{
		vaResult.pdispVal = pCookie->GetIDispatch(TRUE);
		vaResult.vt = VT_DISPATCH;
	}
	return vaResult;
}


/************************************************************************/
/*																		*/
/* Function name : SetItem												*/
/* Description   : Add (or modify) the cookie value(s)					*/
/*																		*/
/************************************************************************/
void CCookieCollection::SetItem(const VARIANT FAR& Name, const VARIANT FAR& Value) 
{
	COleVariant vaName(Name);
	COleVariant vaValue(Value);

	// make sure they're BSTR's
	vaName.ChangeType(VT_BSTR);
	vaValue.ChangeType(VT_BSTR);

	CString strName = vaName.bstrVal;
	CString strValue = vaValue.bstrVal;

	// add or modify value
	Add(strName, strValue);
}


/************************************************************************/
/*																		*/
/* Function name : GetKey												*/
/* Description   : Retrieves the identifier of the item to be retrieved */
/*				   from the collection.									*/
/*																		*/
/************************************************************************/
VARIANT CCookieCollection::GetKey(const VARIANT FAR& VarKey) 
{
	VARIANT vaResult;
	VariantInit(&vaResult);

	COleVariant vaKey(VarKey);

	// make sure it's a long
	vaKey.ChangeType(VT_I4);

	int nIndex = 0;
	CString strKey;
	CCookie *pCookie;

	POSITION pos = m_CookieMap.GetStartPosition();
	while (pos != NULL)
	{
		m_CookieMap.GetNextAssoc(pos, strKey, (CObject *&)pCookie);
		if (nIndex == vaKey.lVal)
		{
			// stuff it into a variant
			vaResult.bstrVal = strKey.AllocSysString();
			vaResult.vt = VT_BSTR;
			break;
		}
		nIndex++;
	}
	return vaResult;
}


/************************************************************************/
/*																		*/
/* Function name : GetNewEnum											*/
/* Description   : Implementation of the enumerator (for each ...)		*/
/*																		*/
/************************************************************************/
LPUNKNOWN CCookieCollection::GetNewEnum()
{
	CCookieCollectionEnum* pEnum = new CCookieCollectionEnum(this);
    if (pEnum)
    {
        pEnum->m_xEnumVARIANT.Reset();
        return &pEnum->m_xEnumVARIANT;
    }
    
    return NULL;
}


/************************************************************************/
/*																		*/
/* Function name : Add													*/
/* Description   : Add cookie to the collection							*/
/*																		*/
/************************************************************************/
CCookie *CCookieCollection::Add(LPCTSTR lpszName, LPCTSTR lpszValue)
{
	CCookie *pCookie = NULL;
	try
	{
		if (!m_CookieMap.Lookup(lpszName, (CObject *&)pCookie))
		{
			pCookie = new CCookie;
			m_CookieMap.SetAt(lpszName, pCookie);
		}
		pCookie->SetName(lpszName);
		pCookie->SetValue(lpszValue);
	}
	catch(...)
	{
	}
	return pCookie;
}


/************************************************************************/
/*																		*/
/* Function name : Add													*/
/* Description   : Add cookie to the collection							*/
/*																		*/
/************************************************************************/
CCookie *CCookieCollection::Add(LPCTSTR lpszName, CCookie &cookie)
{
	CCookie *pCookie = NULL;
	try
	{
		// does it already exist ?
		if (!m_CookieMap.Lookup(lpszName, (CObject *&)pCookie))
		{
			// create new cookie
			pCookie = new CCookie;
			
			// add to cookie collection
			m_CookieMap.SetAt(lpszName, pCookie);
		}
		// copy contents
		*pCookie = cookie;
	}
	catch(...)
	{
	}
	return pCookie;
}


/********************************************************************/
/*																	*/
/* Function name : RenderCookies									*/
/* Description   : Return collected cookies as string				*/
/*																	*/
/********************************************************************/
BOOL CCookieCollection::RenderCookies(CString &strCookies)
{
	strCookies = "";

	CCookie *pCookie;
	CString strKey;
	
	POSITION pos = m_CookieMap.GetStartPosition();
	while (pos != NULL)
	{
		m_CookieMap.GetNextAssoc(pos, strKey, (CObject *&)pCookie);

		char szCookie[2048];
		DWORD dwBuffSize = 2048;

		if (pCookie->IsDeleted())
		{
			pCookie->SetMaxAge(0);
		}

		if (pCookie->Render(szCookie, &dwBuffSize))
		{
			strCookies += "Set-Cookie";
			strCookies += ": ";
			strCookies += szCookie;
			strCookies += "\r\n";
		}
	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -