2 // System.Web.HttpCookieCollection
5 // Patrik Torstensson (Patrik.Torstensson@labs2.com)
6 // (First impl Bob Smith <bob@thestuff.net>)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections.Specialized;
36 public sealed class HttpCookieCollection : NameObjectCollectionBase
38 HttpCookie [] _AllCookies;
41 HttpResponse _Response;
43 internal HttpCookieCollection(HttpResponse Response, bool ReadOnly) : base()
46 IsReadOnly = ReadOnly;
49 public HttpCookieCollection () { }
51 public string [] AllKeys
55 _AllKeys = BaseGetAllKeys ();
61 public HttpCookie this [int index]
63 get { return Get (index); }
66 public HttpCookie this [string name]
68 get { return Get (name); }
71 public void Add (HttpCookie cookie)
73 if (null != _Response)
74 _Response.GoingToChangeCookieColl ();
76 // empy performance cache
80 BaseAdd (cookie.Name, cookie);
82 if (null != _Response)
83 _Response.OnCookieAdd (cookie);
93 public void CopyTo (Array dest, int index)
95 if (null == _AllCookies) {
96 _AllCookies = new HttpCookie [Count];
98 for (int i = 0; i != Count; i++)
99 _AllCookies [i] = Get (i);
102 _AllCookies.CopyTo (dest, index);
105 public HttpCookie Get (int index)
107 return BaseGet (index) as HttpCookie;
110 public HttpCookie Get (string name)
112 HttpCookie oRet = BaseGet (name) as HttpCookie;
113 if (null == oRet && _Response != null) {
117 _Response.GoingToChangeCookieColl ();
119 oRet = new HttpCookie (name);
120 BaseAdd (name, oRet);
122 _Response.OnCookieAdd (oRet);
128 public string GetKey (int index)
130 return BaseGetKey (index);
133 public void Remove (string name)
135 if (null != _Response)
136 _Response.GoingToChangeCookieColl ();
142 if (null != _Response)
143 _Response.ChangedCookieColl ();
146 public void Set (HttpCookie cookie)
148 if (null != _Response)
149 _Response.GoingToChangeCookieColl ();
153 BaseSet (cookie.Name, cookie);
155 if (null != _Response)
156 _Response.ChangedCookieColl();
159 internal void MakeCookieExpire (string name, string path)
161 DateTime expirationTime = new DateTime (1999, 10, 12); // This is the date MS sends!
162 HttpCookie willExpire = new HttpCookie (name, String.Empty, path, expirationTime);