Listing 1: The Cookie class

//  Cookie.h: Interface of Cookie Class
//  Encapsulates RFC 2109 and Netscape (Version 0) Specification
//  Cookies

#if !defined(AFX_COOKIE_H_)
#define AFX_COOKIE_H_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#pragma warning (disable:4786)

#include <string>
#include <map>
#include "CookieUtil.h"

using std::string;
using std::map;
using util::toLowerStr;
using util::trimStr;

typedef map<string, string> COOKIE_MAP;
typedef map<string, string> NAME_MAP;

class Cookie  
{

public:
    // Constructor/Destructor
    Cookie(): m_nCookieVersion(0) 
    {}
    Cookie(const string& strCookie): m_nCookieVersion(0)
    { constructNameValueMap(strCookie); }
    Cookie(const Cookie& objCookie): m_nCookieVersion(0)
    { *this = objCookie; }
    virtual ~Cookie() 
    { clear(); }

    // Operators
    bool operator    == (const Cookie& objCookie);
    bool operator    < (const Cookie& objCookie);
    Cookie& operator = (const Cookie& objCookie)
    {
        this->m_mapNameValue   = objCookie.m_mapNameValue;
        this->m_nCookieVersion = objCookie.m_nCookieVersion;
        return *this;
    }

    bool isEqual(const Cookie& objCookie) const;

    // Get/Set
    string getCookieString(bool bVersionRequired = false);
    string getValue(const string& strName) const
    {
        string strVal;
        getAttribValue(strName, strVal);
        return strVal;
    }
    bool getAttribValue(const string& strName, string& strVal) const;
    string getPath() const
    { return getValue("path"); }
    string getDomain() const
    { return getValue("domain"); }
    void getCookieNames(NAME_MAP& mapNames) const;
    int getVersion() const
    { return m_nCookieVersion; }

    void setCookie(const string& strCookie)
    { constructNameValueMap(strCookie); }
    void updateCookie(const Cookie& objCookie);
    void clear()
    {
        m_mapNameValue.clear(); 
        m_nCookieVersion = 0;
    }

    bool isSecure() const
    { return doesNameExist("secure"); }
    bool isSessionCookie() const;
    bool isPersistentCookie() const
    { return !isSessionCookie(); }
    bool isRFC2109Based() const
    { return m_nCookieVersion>=1?true:false; }
    bool isCookieValid() const;
    bool hasExpired() const;
    bool doesNameExist(const string& strName) const
    {   
        string strVal;
        return getAttribValue(strName, strVal);
    }

    void updateNameValue(const string& strName,
        const string& strValue);
    void insertNameValue(const string& strName, 
        const string& strValue)
    {
        if (strName.length())
            m_mapNameValue.insert
            (COOKIE_MAP::value_type(strName, trimStr(strValue)));
    }

    // Domain/Path match
    bool doesDomainTailMatch(const string& strReqDomain) const;
    bool doesPathMatch(const string& strReqPath) const;

protected:
    // Attributes
    COOKIE_MAP m_mapNameValue;
    int m_nCookieVersion;

private:
    // Helpers
    void constructNameValueMap(const string& strCookie);
    string getExpiryInternetTime(unsigned long nSeconds) const;
    bool isDomainValid() const;
    bool isAttributeName(const string& strName) const;

};

#endif // !defined(AFX_COOKIE_H_)

— End of Listing —