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

📄 cookie.cpp

📁 vc++6.0开发网络典型应用实例导航 1. 本光盘提供了本书中所有的实例源程序文件。 2. 附录文件夹下是Winsock 函数参考以及错误码列表
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/********************************************************************/
/*																	*/
/*  Cookie.cpp														*/
/*																	*/
/*  Implementation of the CCookie and CCookieEnum objects.			*/
/*																	*/
/*  Part of this code is based the ATL class CCookie from			*/
/*	Microsoft VC++ 7.0.												*/
/*  Modified for use with MFC by Pablo van der Meer					*/
/*																	*/
/*	http://www.pablovandermeer.nl									*/
/*																	*/
/*  Last updated: July 4, 2003										*/
/*																	*/
/********************************************************************/


#include "stdafx.h"
#include "Cookie.h"

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


/************************************************************************/
/*																		*/
/* Function name : SystemTimeToHttpDate									*/
/* Description   : Convert SYSTEMTIME to Http-date (defined in rfc2616)	*/
/*																		*/
/************************************************************************/
void SystemTimeToHttpDate(const SYSTEMTIME& st, CString &strTime)
{
	static LPCSTR szDays[] = { "Sun", "Mon", "Tue",
		"Wed", "Thu", "Fri", "Sat" };
	static LPCSTR szMonth[] = { "Jan", "Feb", "Mar", "Apr",
		"May", "Jun", "Jul", "Aug", "Sep", 
		"Oct", "Nov", "Dec" };

	strTime.Format("%s, %02d-%s-%d %02d:%02d:%02d GMT",
		szDays[st.wDayOfWeek], st.wDay, szMonth[st.wMonth-1], st.wYear,
		st.wHour, st.wMinute, st.wSecond);
}

IMPLEMENT_DYNCREATE(CCookie, CCmdTarget)

/************************************************************************/
/*																		*/
/* Function name : CCookie												*/
/* Description   : Constructor											*/
/*																		*/
/************************************************************************/
CCookie::CCookie()
{
	EnableAutomation();
}


/************************************************************************/
/*																		*/
/* Function name : ~CCookie												*/
/* Description   : Destructor											*/
/*																		*/
/************************************************************************/
CCookie::~CCookie()
{

}


/************************************************************************/
/*																		*/
/* Function name : OnFinalRelease										*/
/* Description   :														*/
/*																		*/
/************************************************************************/
void CCookie::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_DISPATCH_MAP(CCookie, CCmdTarget)
	//{{AFX_DISPATCH_MAP(CCookie)
	DISP_PROPERTY_EX(CCookie, "Path", GetPath, SetPath, VT_BSTR)
	DISP_PROPERTY_EX(CCookie, "Domain", GetDomain, SetDomain, VT_BSTR)
	DISP_PROPERTY_EX(CCookie, "Secure", GetSecure, SetSecure, VT_BOOL)
	DISP_PROPERTY_EX(CCookie, "Expires", GetExpires, SetExpiresDate, VT_DATE)
	DISP_FUNCTION(CCookie, "HasKeys", HasKeys, VT_BOOL, VTS_NONE)
	DISP_PROPERTY_PARAM(CCookie, "Item", GetItem, SetItem, VT_VARIANT, VTS_VARIANT)
	//}}AFX_DISPATCH_MAP
	DISP_PROPERTY_EX_ID(CCookie, "_NewEnum", DISPID_NEWENUM, GetNewEnum, SetNotSupported, VT_UNKNOWN)
	DISP_DEFVALUE(CCookie, "Item")
END_DISPATCH_MAP()

// Note: we add support for IID_ICookie to support typesafe binding
//  from VBA.  This IID must match the GUID that is attached to the 
//  dispinterface in the .ODL file.

// {465DC044-D0AD-481E-A828-70A68D002853}
static const IID IID_ICookie =
{ 0x465dc044, 0xd0ad, 0x481e, { 0xa8, 0x28, 0x70, 0xa6, 0x8d, 0x0, 0x28, 0x53 } };

BEGIN_INTERFACE_MAP(CCookie, CCmdTarget)
	INTERFACE_PART(CCookie, IID_ICookie, Dispatch)
END_INTERFACE_MAP()


/************************************************************************/
/*																		*/
/* Function name : CCookie												*/
/* Description   : Constructs a named cookie.							*/
/*																		*/
/************************************************************************/
CCookie::CCookie(LPCSTR szName)
{
	EnableAutomation();
	SetName(szName);
}


/************************************************************************/
/*																		*/
/* Function name : CCookie												*/
/* Description   : Constructs a single-valued cookie.					*/
/*																		*/
/************************************************************************/
CCookie::CCookie(LPCSTR szName, LPCSTR szValue)
{
	EnableAutomation();
	SetName(szName);
    SetValue(szValue);
}


/************************************************************************/
/*																		*/
/* Function name : CCookie												*/
/* Description   : Copy a cookie.										*/
/*																		*/
/************************************************************************/
CCookie::CCookie(const CCookie& thatCookie)
{
	EnableAutomation();
	Copy(thatCookie);
}


/************************************************************************/
/*																		*/
/* Function name : SetName												*/
/* Description   : Set the name of this cookie.							*/
/*																		*/
/************************************************************************/
BOOL CCookie::SetName(LPCSTR szName)
{
	// The name of a cookie cannot contain whitespace, semicolons or commas.
	// The name should not begin with a dollar sign ($) since such names are reserved for future use.
	if (szName && *szName)
	{
		m_strName = szName;
		return TRUE;
	}
    return FALSE;
}


/************************************************************************/
/*																		*/
/* Function name : GetName												*/
/* Description   : Retrieve the name of this cookie.					*/
/*																		*/
/************************************************************************/
BOOL CCookie::GetName(CString &strName)
{
	strName = m_strName;
    return TRUE;
}


/************************************************************************/
/*																		*/
/* Function name : GetName												*/
/* Description   : Set the value of this cookie (pass NULL to remove)	*/
/*																		*/
/************************************************************************/
BOOL CCookie::SetValue(LPCSTR szValue)
{
	// already dictionary values in the cookie ?
	if (m_Values.GetCount())
		return FALSE; 

	if (!szValue)
		m_strValue.Empty();
	else 
		m_strValue = szValue;

	return TRUE;
}


/************************************************************************/
/*																		*/
/* Function name : GetValue												*/
/* Description   : Retrieve the value of this cookie.					*/
/*																		*/
/************************************************************************/
BOOL CCookie::GetValue(CString &strValue)
{
	strValue = m_strValue;
	return TRUE;
}


/************************************************************************/
/*																		*/
/* Function name : AddValue												*/
/* Description   : Add a name-value pair to the cookie.					*/
/*																		*/
/************************************************************************/
BOOL CCookie::AddValue(LPCSTR szName, LPCSTR szValue)
{
    if (m_strValue.GetLength())
        return FALSE;

	m_Values.SetAt(szName, szValue);
	return TRUE;
}


/************************************************************************/
/*																		*/
/* Function name : ModifyValue											*/
/* Description   : Modify a name-value pair associated with the cookie.	*/
/*																		*/
/************************************************************************/
BOOL CCookie::ModifyValue(LPCSTR szName, LPCSTR szValue)
{
    return AddValue(szName, szValue);
}


/************************************************************************/
/*																		*/
/* Function name : RemoveValue											*/
/* Description   : Remove a name-value pair from the collection.		*/
/*																		*/
/************************************************************************/
BOOL CCookie::RemoveValue(LPCSTR szName)
{
    return m_Values.RemoveKey(szName);
}


/************************************************************************/
/*																		*/
/* Function name : RemoveValue											*/
/* Description   : Remove all the name-value pairs from the collection.	*/
/*																		*/
/************************************************************************/
void CCookie::RemoveAllValues()
{
    m_Values.RemoveAll();
}


/************************************************************************/
/*																		*/
/* Function name : AddAttribute											*/
/* Description   : Add an attribute-value pair to the collection of		*/
/*				   attributes for this cookie.							*/
/*																		*/
/************************************************************************/
BOOL CCookie::AddAttribute(LPCSTR szName, LPCSTR szValue)
{
    if (!szName || !*szName || !szValue)
        return FALSE;

	m_Attributes.SetAt(szName, szValue);
	return TRUE;
}


/************************************************************************/
/*																		*/
/* Function name : ModifyAttribute										*/
/* Description   : Modify an attribute-value pair associated with the	*/
/*				   cookie.												*/
/*																		*/
/************************************************************************/
BOOL CCookie::ModifyAttribute(LPCSTR szName, LPCSTR szValue)
{
    return AddAttribute(szName, szValue);
}


/************************************************************************/
/*																		*/
/* Function name : RemoveAttribute										*/
/* Description   : Remove an attribute-value pair from the collection	*/
/*				   of attributes managed by this cookie.				*/
/*																		*/
/************************************************************************/
BOOL CCookie::RemoveAttribute(LPCSTR szName)
{
    return m_Attributes.RemoveKey(szName);
}


/************************************************************************/
/*																		*/
/* Function name : RemoveAllAttributes									*/
/* Description   : Remove all the attribute-value pairs from the		*/
/*				   collection of attributes managed by this cookie.		*/
/*																		*/
/************************************************************************/
void CCookie::RemoveAllAttributes()
{
    m_Attributes.RemoveAll();
}


/************************************************************************/
/*																		*/
/* Function name : SetComment											*/
/* Description   : Set the Comment attribute of the cookie.				*/
/*																		*/
/************************************************************************/
BOOL CCookie::SetComment(LPCSTR szComment)
{
    BOOL bRet = SetVersion(1);
    if (bRet)
        bRet = AddAttribute("comment", szComment);
    return bRet;
}


/************************************************************************/
/*																		*/
/* Function name : SetCommentUrl										*/
/* Description   : Set the CommentUrl attribute of the cookie.			*/
/*																		*/
/************************************************************************/
BOOL CCookie::SetCommentUrl(LPCSTR szUrl)
{
    BOOL bRet = SetVersion(1);
    if (bRet)
        bRet = AddAttribute("commenturl", szUrl);
    return bRet;
}


/************************************************************************/
/*																		*/
/* Function name : SetDiscard											*/
/* Description   : Add or remove the Discard attribute of the cookie.	*/
/*																		*/
/************************************************************************/
BOOL CCookie::SetDiscard(BOOL bDiscard)
{
    BOOL bRet = FALSE;
    LPCSTR szKey = "Discard";
    bRet = SetVersion(1);
    if (bRet)
    {
        if (bDiscard == 0)
        {
            bRet = m_Attributes.RemoveKey(szKey);
        }
        else
        {
			m_Attributes.SetAt(szKey, " ");
			bRet = TRUE;
        }
    }
    return bRet;
}


/************************************************************************/
/*																		*/
/* Function name : GetDomain											*/
/* Description   : Return domain attribute.								*/
/*																		*/
/************************************************************************/
BSTR CCookie::GetDomain() 
{
	CString strResult;
	m_Attributes.Lookup("domain", strResult);
	return strResult.AllocSysString();
}


⌨️ 快捷键说明

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