Interface to Remove Cookies

View: New views
1 Messages — Rating Filter:   Alert me  

Interface to Remove Cookies

by Artyom Beilis :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

CgiCC HTTPCookies supports MaxAge that may be 0 (browser) or positive according to RFC,

Other very important options Expires is not supported. This option is mostly useless because requires
definition of absolute time. However one special case exists: Cookies Removal.

In many cases it is very useful to remove cookies by setting Expiration date in past.

I've written a little patch to fix this problem, please consider to merge it into upstream

Artyom

--- cgicc-3.2.7/cgicc/HTTPCookie.cpp 2008-07-06 18:27:18.000000000 +0300
+++ cgicc-3.2.7-patched/cgicc/HTTPCookie.cpp 2009-01-05 14:12:07.972561100 +0200
@@ -33,7 +33,8 @@
 // ============================================================
 cgicc::HTTPCookie::HTTPCookie()
   : fMaxAge(0),
-    fSecure(false)
+    fSecure(false),
+    fRemoved(false)
 {}
 
 cgicc::HTTPCookie::HTTPCookie(const std::string& name,
@@ -41,7 +42,20 @@
   : fName(name),
     fValue(value),
     fMaxAge(0),
-    fSecure(false)
+    fSecure(false),
+    fRemoved(false)
+{}
+
+cgicc::HTTPCookie::HTTPCookie(const std::string& name,
+      const std::string& domain,
+      const std::string& path,
+      bool secure)
+  : fName(name),
+    fDomain(domain),
+    fMaxAge(0),
+    fPath(path),
+    fSecure(secure),
+    fRemoved(false)
 {}
 
 cgicc::HTTPCookie::HTTPCookie(const std::string& name,
@@ -57,7 +71,8 @@
     fDomain(domain),
     fMaxAge(maxAge),
     fPath(path),
-    fSecure(secure)
+    fSecure(secure),
+    fRemoved(false)
 {}
 
 cgicc::HTTPCookie::HTTPCookie(const HTTPCookie& cookie)
@@ -94,8 +109,10 @@
     out << "; Comment=" << fComment;
   if(false == fDomain.empty())
     out << "; Domain=" << fDomain;
-  if(0 != fMaxAge)
-    out << "; Max-Age=" << fMaxAge;
+  if(fRemoved)
+    out << "; Expires=Fri, 01-Jan-1971 01:00:00 GMT;";
+  else if(0 != fMaxAge)
+      out << "; Max-Age=" << fMaxAge;
   if(false == fPath.empty())
     out << "; Path=" << fPath;
   if(true == fSecure)
--- cgicc-3.2.7/cgicc/HTTPCookie.h 2008-07-06 18:27:18.000000000 +0300
+++ cgicc-3.2.7-patched/cgicc/HTTPCookie.h 2009-01-05 14:08:30.729184100 +0200
@@ -104,6 +104,22 @@
        bool secure);
     
     /*!
+     * \brief Create a new partially-spefified HTTPCookie for deletion
+     *
+     *
+     * \param name The name of the cookie.
+     * \param domain The domain for which this cookie is valid- an empty string
+     * will use the hostname of the server which generated the cookie response.
+     * If specified, the domain <em>must</em> start with a period('.').
+     * \param path The subset of URLS in a domain for which the cookie is
+     * valid, for example \c /
+     * @param secure Specifies whether this is a secure cookie.
+     */
+    HTTPCookie(const std::string& name,
+       const std::string& domain,
+       const std::string& path,
+       bool secure);
+    /*!
      * \brief Copy constructor
      *
      * Set the name, value, comment, domain, age and path of this cookie
@@ -303,6 +319,16 @@
     setSecure(bool secure)
     { fSecure = secure; }
     //@}    
+
+    /*!
+     * \brief Mark this cookie as secure or unsecure.
+     *
+     * \param secure Whether this is a secure cookie.
+     */
+    inline void
+    remove()
+    { fRemoved = true; }
+    //@}    
     
     // ============================================================
     
@@ -320,6 +346,7 @@
     unsigned long fMaxAge;
     std::string fPath;
     bool fSecure;
+    bool fRemoved;
   };
   
 } // namespace cgicc