2 // System.Web.HttpCookieCollection.cs
5 // Chris Toshok (toshok@novell.com)
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections.Specialized;
33 namespace System.Web {
35 public sealed class HttpCookieCollection : NameObjectCollectionBase {
37 private bool auto_fill = false;
39 [Obsolete ("Don't use this constructor, use the (bool, bool) one, as it's more clear what it does")]
40 internal HttpCookieCollection (HttpResponse Response, bool ReadOnly)
42 auto_fill = Response != null;
43 IsReadOnly = ReadOnly;
46 internal HttpCookieCollection (bool auto_fill, bool read_only)
48 this.auto_fill = auto_fill;
49 IsReadOnly = read_only;
52 internal HttpCookieCollection (string cookies)
54 if (cookies == null || cookies == "")
57 string[] cookie_components = cookies.Split (';');
58 foreach (string kv in cookie_components) {
59 int pos = kv.IndexOf ('=');
65 string key = kv.Substring (0, pos);
66 string val = kv.Substring (pos+1);
68 Add (new HttpCookie (key.Trim (), val.Trim()));
73 public HttpCookieCollection ()
77 public void Add (HttpCookie cookie)
79 if (BaseGet (cookie.Name) != null)
82 BaseAdd (cookie.Name, cookie);
90 public void CopyTo (Array array, int index)
92 /* XXX this is kind of gross and inefficient
93 * since it makes a copy of the superclass's
95 object[] values = BaseGetAllValues();
96 values.CopyTo (array, index);
99 public string GetKey (int index)
101 HttpCookie cookie = (HttpCookie)BaseGet (index);
108 public void Remove (string name)
113 public void Set (HttpCookie cookie)
115 BaseSet (cookie.Name, cookie);
118 public HttpCookie Get (int index)
120 return (HttpCookie)BaseGet (index);
123 public HttpCookie Get (string name)
125 return (HttpCookie)BaseGet (name);
128 public HttpCookie this [int index]
131 return (HttpCookie)BaseGet (index);
135 public HttpCookie this [string name]
138 HttpCookie cookie = (HttpCookie)BaseGet (name);
139 if (!IsReadOnly && auto_fill && cookie == null) {
140 cookie = new HttpCookie (name);
141 BaseAdd (name, cookie);
147 public string[] AllKeys {
149 /* XXX another inefficient copy due to
150 * lack of exposure from the base
152 string[] keys = new string [Keys.Count];
153 for (int i = 0; i < Keys.Count; i ++)