Exemplo n.º 1
0
        /// <summary>
        /// Update the jar with a collection cookies.
        /// </summary>
        /// <param name="collection">List of cookies to store.</param>
        /// <param name="uri">Uri cookies apply to.</param>
        public void Update(List <DreamCookie> collection, XUri uri)
        {
            if (collection == null)
            {
                throw new ArgumentNullException("collection");
            }

            // process all cookies
            foreach (DreamCookie c in collection)
            {
                DreamCookie cookie = c;
                if (!string.IsNullOrEmpty(cookie.Name))
                {
                    string[] segments = null;
                    if (uri != null)
                    {
                        // set default domain if needed
                        if (string.IsNullOrEmpty(cookie.Domain))
                        {
                            cookie = cookie.WithHostPort(uri.HostPort);
                        }
                        else if (!StringUtil.EqualsInvariantIgnoreCase(cookie.Domain, uri.HostPort))
                        {
                            // domain doesn't match, ignore cookie
                            continue;
                        }

                        // set default path if needed
                        if (string.IsNullOrEmpty(cookie.Path))
                        {
                            cookie   = cookie.WithPath(uri.Path);
                            segments = uri.Segments;
                        }
                        else
                        {
                            segments = cookie.Uri == null ? new string[0] : cookie.Uri.Segments;
                            if (!uri.PathStartsWith(segments))
                            {
                                // path doesn't match ignore cookie
                                continue;
                            }
                        }
                    }
                    if (!string.IsNullOrEmpty(cookie.Path) && !string.IsNullOrEmpty(cookie.Domain))
                    {
                        if (segments == null)
                        {
                            segments = cookie.Uri == null ? new string[0] : cookie.Uri.Segments;
                        }
                        if (cookie.Expired)
                        {
                            Delete(cookie, segments, 0);
                        }
                        else
                        {
                            Insert(cookie, segments, 0);
                        }
                    }
                }
            }
        }