using System; using System.Collections; using System.Collections.Generic; namespace OSHttpServer { /// /// Cookies that should be set. /// public sealed class ResponseCookies : IEnumerable { private readonly IDictionary _items = new Dictionary(); /// /// Adds a cookie in the collection. /// /// cookie to add /// cookie is null public void Add(ResponseCookie cookie) { // Verifies the parameter if (cookie == null) throw new ArgumentNullException("cookie"); if (cookie.Name == null || cookie.Name.Trim().Length == 0) throw new ArgumentException("Name must be specified."); if (cookie.Value == null || cookie.Value.Trim().Length == 0) throw new ArgumentException("Content must be specified."); if (_items.ContainsKey(cookie.Name)) _items[cookie.Name] = cookie; else _items.Add(cookie.Name, cookie); } /// /// Copy a request cookie /// /// /// When the cookie should expire public void Add(RequestCookie cookie, DateTime expires) { Add(new ResponseCookie(cookie, expires)); } /// /// Gets the count of cookies in the collection. /// public int Count { get { return _items.Count; } } /// /// Gets the cookie of a given identifier (null if not existing). /// public ResponseCookie this[string id] { get { if (_items.ContainsKey(id)) return _items[id]; else return null; } set { if (_items.ContainsKey(id)) _items[id] = value; else Add(value); } } /// /// Gets a collection enumerator on the cookie list. /// /// collection enumerator public IEnumerator GetEnumerator() { return _items.Values.GetEnumerator(); } /// /// Remove all cookies /// public void Clear() { _items.Clear(); } #region IEnumerable Members /// ///Returns an enumerator that iterates through the collection. /// /// /// ///A that can be used to iterate through the collection. /// ///1 IEnumerator IEnumerable.GetEnumerator() { return _items.Values.GetEnumerator(); } #endregion } }