Example #1
0
        void AddCookie(Cookie cookie)
        {
            if (cookie.Domain.Length == 0)
            {
                throw new ArgumentException("Cookie domain not set.", "cookie.Domain");
            }

            if (cookie.Value.Length > maxCookieSize)
            {
                throw new Exception("value is larger than MaxCookieSize.");
            }
//
//			if ((cookie.Version == 1) && (cookie.Domain[0] != '.'))
//				throw new CookieException ("Invalid cookie domain: " + cookie.Domain);
//
//			if (cookie.HasDomain && !CheckPublicRoots (cookie.Domain))
//				throw new CookieException ("Invalid cookie domain: " + cookie.Domain);

            if (cookies == null)
            {
                cookies = new CookieCollection2();
            }

            if (cookies.Count >= capacity)
            {
                RemoveOldest(null);
            }

            // try to avoid counting per-domain
            if (cookies.Count >= perDomainCapacity)
            {
                if (CountDomain(cookie.Domain) >= perDomainCapacity)
                {
                    RemoveOldest(cookie.Domain);
                }
            }

            // clone the important parts of the cookie
            Cookie c = new Cookie(cookie.Name, cookie.Value);

            c.Path   = cookie.Path;
            c.Domain = cookie.Domain;
//			c.HasDomain = cookie.HasDomain;
            c.Version    = cookie.Version;
            c.Expires    = cookie.Expires;
            c.CommentUri = cookie.CommentUri;
            c.Comment    = cookie.Comment;
            c.Discard    = cookie.Discard;
            c.HttpOnly   = cookie.HttpOnly;
            c.Secure     = cookie.Secure;

            cookies.Add(c);
            CheckExpiration();
        }
Example #2
0
        public CookieCollection GetCookies(Uri uri)
        {
            if (uri == null)
            {
                throw new ArgumentNullException("uri");
            }

            CheckExpiration();
            CookieCollection2 coll = new CookieCollection2();

            if (cookies == null)
            {
                return(new CookieCollection());
            }

            foreach (Cookie cookie in cookies)
            {
                string domain = cookie.Domain;
                if (cookie.Version == 1)
                {
                    if (!CheckDomain_RFC2109(domain, uri.Host))
                    {
                        continue;
                    }
                }
                else
                {
                    if (!CheckDomain(domain, uri.Host, false))
                    {
                        continue;
                    }
                }


                string path    = cookie.Path;
                string uripath = uri.AbsolutePath;
                if (path != "" && path != "/")
                {
                    if (uripath != path)
                    {
                        if (!uripath.StartsWith(path))
                        {
                            continue;
                        }

                        if (path [path.Length - 1] != '/' && uripath.Length > path.Length &&
                            uripath [path.Length] != '/')
                        {
                            continue;
                        }
                    }
                }

                if (cookie.Secure && uri.Scheme != "https")
                {
                    continue;
                }

                coll.Add(cookie);
            }

            coll.Sort();
            var result = new CookieCollection();

            foreach (var item in coll.List)
            {
                result.Add(item);
            }

            return(result);
        }