예제 #1
0
        // copy-pasted (and a bit adapted) from HttpWebResponse
        void SetCookie(string header)
        {
            string       name, val;
            Cookie       cookie = null;
            CookieParser parser = new CookieParser(header);

            while (parser.GetNextNameValue(out name, out val))
            {
                if (String.IsNullOrEmpty(name) && cookie == null)
                {
                    continue;
                }

                if (cookie == null)
                {
                    cookie = new Cookie(name, val);
                    continue;
                }

                name = name.ToUpper();
                switch (name)
                {
                case "COMMENT":
                    if (cookie.Comment == null)
                    {
                        cookie.Comment = val;
                    }
                    break;

                case "COMMENTURL":
                    if (cookie.CommentUri == null)
                    {
                        cookie.CommentUri = new Uri(val);
                    }
                    break;

                case "DISCARD":
                    cookie.Discard = true;
                    break;

                case "DOMAIN":
                    if (cookie.Domain.Length == 0)
                    {
                        cookie.Domain = val;
                    }
                    break;

                case "HTTPONLY":
                    cookie.HttpOnly = true;
                    break;

                case "MAX-AGE":                 // RFC Style Set-Cookie2
                    if (cookie.Expires == DateTime.MinValue)
                    {
                        try {
                            cookie.Expires = cookie.TimeStamp.AddSeconds(UInt32.Parse(val));
                        } catch {}
                    }
                    break;

                case "EXPIRES":                 // Netscape Style Set-Cookie
                    if (cookie.Expires != DateTime.MinValue)
                    {
                        break;
                    }

                    cookie.Expires = CookieParser.TryParseCookieExpires(val);
                    break;

                case "PATH":
                    cookie.Path = val;
                    break;

                case "PORT":
                    if (cookie.Port == null)
                    {
                        cookie.Port = val;
                    }
                    break;

                case "SECURE":
                    cookie.Secure = true;
                    break;

                case "VERSION":
                    try {
                        cookie.Version = (int)UInt32.Parse(val);
                    } catch {}
                    break;
                }
            }

            if (cookie.Domain.Length == 0)
            {
                cookie.Domain = response_uri.Host;
            }

            jar.Add(cookie);
        }