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

📄 cookie.cpp

📁 vc++6.0开发网络典型应用实例导航 1. 本光盘提供了本书中所有的实例源程序文件。 2. 附录文件夹下是Winsock 函数参考以及错误码列表
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/************************************************************************/
/*																		*/
/* Function name : SetDomain											*/
/* Description   : Set the Domain attribute of the cookie.				*/
/*																		*/
/************************************************************************/
void CCookie::SetDomain(LPCTSTR lpszDomain)
{
    BOOL bRet = SetVersion(1);
    if (bRet)
	{
        AddAttribute("domain", lpszDomain);
	}
}


/************************************************************************/
/*																		*/
/* Function name : SetMaxAge											*/
/* Description   : Set the Max-Age attribute of the cookie.				*/
/*																		*/
/************************************************************************/
BOOL CCookie::SetMaxAge(UINT nMaxAge)
{
    BOOL bRet = FALSE;
    bRet = SetVersion(1);
    if (bRet)
    {
        CHAR buff[20];
        if (_itoa(nMaxAge, buff, 10))
        {
            bRet = AddAttribute("max-age", buff);
        }
    }
    return bRet;
}


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


/************************************************************************/
/*																		*/
/* Function name : SetPath												*/
/* Description   : Set the Path attribute of the cookie.				*/
/*																		*/
/************************************************************************/
void CCookie::SetPath(LPCTSTR lpszPath)
{   
    BOOL bRet = SetVersion(1);
    if (bRet)
	{
        AddAttribute("path", lpszPath);
	}
}


/************************************************************************/
/*																		*/
/* Function name : SetPort												*/
/* Description   : Set the Port attribute of the cookie.				*/
/*																		*/
/************************************************************************/
void CCookie::SetPort(LPCTSTR szPort)
{
    BOOL bRet = SetVersion(1);
    if (bRet)
	{
        bRet = AddAttribute("port", szPort);
	}
}


/************************************************************************/
/*																		*/
/* Function name : GetSecure											*/
/* Description   : Return secure attribute.								*/
/*																		*/
/************************************************************************/
BOOL CCookie::GetSecure() 
{
	CString strResult;
	if (m_Attributes.Lookup("secure", strResult))
		return TRUE;
	else
		return FALSE;
}


/************************************************************************/
/*																		*/
/* Function name : SetSecure											*/
/* Description   : Set the Secure attribute of the cookie.				*/
/*																		*/
/************************************************************************/
void CCookie::SetSecure(BOOL bSecure)
{
    BOOL bRet = FALSE;
    LPCSTR szKey = "secure";
    bRet = SetVersion(1);
    if (bRet)
    {
        if (bSecure == 0)
        {
            m_Attributes.RemoveKey(szKey);
        }
        else
        {
			m_Attributes.SetAt(szKey, " ");
        }
    }
}


/************************************************************************/
/*																		*/
/* Function name : SetVersion											*/
/* Description   : Set the Version attribute of the cookie.				*/
/*																		*/
/************************************************************************/
BOOL CCookie::SetVersion(UINT nVersion)
{
    BOOL bRet = FALSE;      
    CHAR buff[20];
    if (_itoa(nVersion, buff, 10))
    {
        bRet = AddAttribute("version", buff);
    }
    return bRet;
}


/************************************************************************/
/*																		*/
/* Function name : GetExpires											*/
/* Description   : Return expire attribute.								*/
/*																		*/
/************************************************************************/
DATE CCookie::GetExpires() 
{
	CString strResult;
	m_Attributes.Lookup("expires", strResult);
	return (DATE) 0;
}


/************************************************************************/
/*																		*/
/* Function name : SetExpires											*/
/* Description   : Set the Expires attribute of the cookie.				*/
/*																		*/
/************************************************************************/
void CCookie::SetExpires(LPCTSTR lpszExpires)
{
	// the Expires attribute specifies an absolute date and time at which this cookie
	// should be discarded by web browsers. Pass a SYSTEMTIME holding a Greenwich Mean Time (GMT)
	// value or a string in the following format:
	//		Wdy, DD-Mon-YY HH:MM:SS GMT

    AddAttribute("expires", lpszExpires);
}


/************************************************************************/
/*																		*/
/* Function name : SetExpires											*/
/* Description   : Set the Expires attribute of the cookie.				*/
/*																		*/
/************************************************************************/
void CCookie::SetExpiresDate(DATE dtExpires) 
{
	COleDateTime oleDateTimeExpires(dtExpires);

	SYSTEMTIME sysTime;
	oleDateTimeExpires.GetAsSystemTime(sysTime);
	SetExpires(sysTime);
}


/************************************************************************/
/*																		*/
/* Function name : SetExpires											*/
/* Description   : Set the Expires attribute of the cookie.				*/
/*																		*/
/************************************************************************/
void CCookie::SetExpires(const SYSTEMTIME &st)
{
	CString strTime;
	SystemTimeToHttpDate(st, strTime);
	SetExpires(strTime);
}


/************************************************************************/
/*																		*/
/* Function name : Lookup												*/
/* Description   : Look up the value of a name-value pair.				*/
/*																		*/
/************************************************************************/
LPCSTR CCookie::Lookup(LPCSTR szName)
{
    if (IsEmpty())
        return NULL;

	if (m_strValue.GetLength())
	{
		ASSERT(szName == NULL);
		return m_strValue;
	}

	CString strValue;

    if (m_Values.GetCount())
    {
		if (m_Values.Lookup(szName, strValue))
		{
            return (LPCSTR)strValue;
		}
    }

    return NULL;
}


/************************************************************************/
/*																		*/
/* Function name : IsDeleted											*/
/* Description   : Check if all values are empty.						*/
/*																		*/
/************************************************************************/
BOOL CCookie::IsDeleted()
{
	if (!m_strValue.IsEmpty())
		return FALSE;

	if (m_strValue.IsEmpty() && m_Values.IsEmpty())
		return TRUE;

	CString strName, strValue;
	BOOL bDeleted = TRUE;

	POSITION pos = m_Values.GetStartPosition();
	while(pos != NULL)
	{
		m_Values.GetNextAssoc(pos, strName, strValue);
		if (!strValue.IsEmpty())
		{
			bDeleted = FALSE;
			break;
		}
	}
	return bDeleted;
}


/************************************************************************/
/*																		*/
/* Function name : Empty												*/
/* Description   : Clear the cookie of all content.						*/
/*																		*/
/************************************************************************/
void CCookie::Empty()
{
    m_strName.Empty();
    m_strValue.Empty();
    m_Attributes.RemoveAll();
    m_Values.RemoveAll();
}

 
/************************************************************************/
/*																		*/
/* Function name : ParseValue											*/
/* Description   : Create a CCookie from a buffer (HTTP_COOKIE request).*/
/*																		*/
/************************************************************************/
BOOL CCookie::ParseValue(LPCSTR lpszCookie)
{
	if (!lpszCookie)
		return FALSE;

	// could be just a value or could be an array of name=value pairs
	LPCSTR pEnd = lpszCookie;
	LPCSTR pStart = lpszCookie;
	CString name, value;

	while (*pEnd != '\0')
	{
		while (*pEnd && *pEnd != '=' && *pEnd != '&')
			pEnd++;

		if (*pEnd == '\0' || *pEnd == '&')
		{
			if (pEnd > pStart)
			{
				CopyToCString(value, pStart, pEnd);
			}
			SetValue(value);
			if (*pEnd == '&')
			{
				pEnd++;
				pStart = pEnd;
				continue;
			}
			return TRUE; // we're done;
		}
		else 
		if (*pEnd == '=' )
		{
			// we have name=value
			if (pEnd > pStart)
			{
				CopyToCString(name, pStart, pEnd);
			}
			else
			{
				pEnd++;
				pStart = pEnd;
				break;
			}

			// skip '=' and go for value
			pEnd++;
			pStart = pEnd;
			while (*pEnd && *pEnd != '&' && *pEnd != '=')
				pEnd++;
			if (pEnd > pStart)
				CopyToCString(value, pStart, pEnd);

			AddValue(name, value);

			if (*pEnd != '\0')
				pEnd++;
				pStart = pEnd;

		}
	}
	return TRUE;
}


/************************************************************************/
/*																		*/
/* Function name : Render												*/
/* Description   : Render this cookie into a buffer.					*/
/*																		*/
/************************************************************************/
BOOL CCookie::Render(LPSTR szCookieBuffer, DWORD *pdwLen)
{
	if (!pdwLen)
		return FALSE;

	CString strCookie;
    CString name, value;
    DWORD dwLenBuff = *pdwLen;
    *pdwLen = 0;

    // A cookie must have a name!
    if (!m_strName.GetLength())
    {
        *pdwLen = 0;
        return FALSE;
    }
	try
	{
		strCookie = m_strName;
		int nValSize = (int) m_Values.GetCount();
		if (nValSize)
		{
			strCookie += "=";
			POSITION pos = m_Values.GetStartPosition();
			for (int i=0; pos; i++)
			{
				m_Values.GetNextAssoc(pos, name, value);
				strCookie += name;
				if (value.GetLength())
				{
					strCookie += '=';
					strCookie += value;
				}
				if (i <= nValSize-2)
					strCookie += '&';
			}
		}
		else
		{
			strCookie += "=";
			if (m_strValue.GetLength())
				strCookie += m_strValue;

⌨️ 快捷键说明

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