/// <exception cref="System.Exception"></exception>
        public virtual void TestEncodeDecodeCookie()
        {
            PersistentCookieStore cookieStore = new PersistentCookieStore(database);
            string cookieName   = "foo";
            string cookieVal    = "bar";
            bool   isSecure     = false;
            bool   httpOnly     = false;
            string cookiePath   = "baz";
            string cookieDomain = "foo.com";
            // expiration date - 1 day from now
            Calendar cal = Calendar.GetInstance();

            cal.SetTime(new DateTime());
            int numDaysToAdd = 1;

            cal.Add(Calendar.Date, numDaysToAdd);
            DateTime          expirationDate = cal.GetTime();
            BasicClientCookie cookie         = new BasicClientCookie(cookieName, cookieVal);

            cookie.SetExpiryDate(expirationDate);
            cookie.SetSecure(isSecure);
            cookie.SetDomain(cookieDomain);
            cookie.SetPath(cookiePath);
            string encodedCookie = cookieStore.EncodeCookie(new SerializableCookie(cookie));

            Apache.Http.Cookie.Cookie fetchedCookie = cookieStore.DecodeCookie(encodedCookie);
            NUnit.Framework.Assert.AreEqual(cookieName, fetchedCookie.GetName());
            NUnit.Framework.Assert.AreEqual(cookieVal, fetchedCookie.GetValue());
            NUnit.Framework.Assert.AreEqual(expirationDate, fetchedCookie.GetExpiryDate());
            NUnit.Framework.Assert.AreEqual(cookiePath, fetchedCookie.GetPath());
            NUnit.Framework.Assert.AreEqual(cookieDomain, fetchedCookie.GetDomain());
        }
        /// <summary>Non-standard helper method, to delete cookie</summary>
        /// <param name="cookie">cookie to be removed</param>
        public virtual void DeleteCookie(Apache.Http.Cookie.Cookie cookie)
        {
            string name = cookie.GetName();

            Sharpen.Collections.Remove(cookies, name);
            DeletePersistedCookie(name);
        }
 public virtual Apache.Http.Cookie.Cookie GetCookie()
 {
     Apache.Http.Cookie.Cookie bestCookie = cookie;
     if (clientCookie != null)
     {
         bestCookie = clientCookie;
     }
     return(bestCookie);
 }
 /// <summary>Returns cookie decoded from cookie string</summary>
 /// <param name="cookieString">string of cookie as returned from http request</param>
 /// <returns>decoded cookie or null if exception occured</returns>
 internal virtual Apache.Http.Cookie.Cookie DecodeCookie(string cookieString)
 {
     Apache.Http.Cookie.Cookie cookie = null;
     try
     {
         byte[] bytes = HexStringToByteArray(cookieString);
         ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
         ObjectInputStream    objectInputStream    = new ObjectInputStream(byteArrayInputStream);
         cookie = ((SerializableCookie)objectInputStream.ReadObject()).GetCookie();
     }
     catch (Exception exception)
     {
         Log.D(Log.TagSync, string.Format("decodeCookie failed.  encoded cookie: %s", cookieString
                                          ), exception);
     }
     return(cookie);
 }
        public virtual void AddCookie(Apache.Http.Cookie.Cookie cookie)
        {
            if (omitNonPersistentCookies && !cookie.IsPersistent())
            {
                return;
            }
            string name = cookie.GetName() + cookie.GetDomain();

            // Do we already have this cookie?  If so, don't bother.
            if (cookies.ContainsKey(name) && cookies.Get(name).Equals(cookie))
            {
                return;
            }
            // Save cookie into local store, or remove if expired
            if (!cookie.IsExpired(new DateTime()))
            {
                cookies.Put(name, cookie);
            }
            else
            {
                Sharpen.Collections.Remove(cookies, name);
            }
            string encodedCookie = EncodeCookie(new SerializableCookie(cookie));
            IDictionary <string, object> cookiesDoc = GetDb().GetExistingLocalDocument(CookieLocalDocName
                                                                                       );

            if (cookiesDoc == null)
            {
                cookiesDoc = new Dictionary <string, object>();
            }
            Log.V(Log.TagSync, "Saving cookie: %s w/ encoded value: %s", name, encodedCookie);
            cookiesDoc.Put(name, encodedCookie);
            try
            {
                GetDb().PutLocalDocument(CookieLocalDocName, cookiesDoc);
            }
            catch (CouchbaseLiteException e)
            {
                Log.E(Log.TagSync, "Exception saving local doc", e);
                throw new RuntimeException(e);
            }
        }
        public virtual bool ClearExpired(DateTime date)
        {
            bool clearedAny = false;

            foreach (KeyValuePair <string, Apache.Http.Cookie.Cookie> entry in cookies.EntrySet
                         ())
            {
                string name = entry.Key;
                Apache.Http.Cookie.Cookie cookie = entry.Value;
                if (cookie.IsExpired(date))
                {
                    // Clear cookies from local store
                    Sharpen.Collections.Remove(cookies, name);
                    DeletePersistedCookie(name);
                    // We've cleared at least one
                    clearedAny = true;
                }
            }
            return(clearedAny);
        }
 private void LoadPreviouslyStoredCookies(Database db)
 {
     try
     {
         IDictionary <string, object> cookiesDoc = db.GetExistingLocalDocument(CookieLocalDocName
                                                                               );
         if (cookiesDoc == null)
         {
             return;
         }
         foreach (string name in cookiesDoc.Keys)
         {
             // ignore special couchbase lite fields like _id and _rev
             if (name.StartsWith("_"))
             {
                 continue;
             }
             string encodedCookie = (string)cookiesDoc.Get(name);
             if (encodedCookie == null)
             {
                 continue;
             }
             Apache.Http.Cookie.Cookie decodedCookie = DecodeCookie(encodedCookie);
             if (decodedCookie == null)
             {
                 continue;
             }
             if (!decodedCookie.IsExpired(new DateTime()))
             {
                 cookies.Put(name, decodedCookie);
             }
         }
     }
     catch (Exception e)
     {
         Log.E(Log.TagSync, "Exception loading previously stored cookies", e);
     }
 }
 public SerializableCookie(Apache.Http.Cookie.Cookie cookie)
 {
     this.cookie = cookie;
 }
 public SerializableCookie(Apache.Http.Cookie.Cookie cookie)
 {
     this.cookie = cookie;
 }