PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System.Web/System.Web/HttpCookieCollection.cs

https://bitbucket.org/danipen/mono
C# | 157 lines | 103 code | 21 blank | 33 comment | 11 complexity | a5025c151e86ff2c939e34e1b379de6b MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. //
  2. // System.Web.HttpCookieCollection.cs
  3. //
  4. // Author:
  5. // Chris Toshok (toshok@novell.com)
  6. //
  7. //
  8. // Copyright (C) 2005-2009 Novell, Inc (http://www.novell.com)
  9. //
  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:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  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.
  28. //
  29. using System.Collections;
  30. using System.Collections.Specialized;
  31. using System.Security.Permissions;
  32. namespace System.Web
  33. {
  34. // CAS - no InheritanceDemand here as the class is sealed
  35. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  36. public sealed class HttpCookieCollection : NameObjectCollectionBase
  37. {
  38. 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) : base (StringComparer.OrdinalIgnoreCase)
  41. {
  42. auto_fill = Response != null;
  43. IsReadOnly = ReadOnly;
  44. }
  45. internal HttpCookieCollection (bool auto_fill, bool read_only) : base (StringComparer.OrdinalIgnoreCase)
  46. {
  47. this.auto_fill = auto_fill;
  48. IsReadOnly = read_only;
  49. }
  50. internal HttpCookieCollection (string cookies) : base (StringComparer.OrdinalIgnoreCase)
  51. {
  52. if (String.IsNullOrEmpty (cookies))
  53. return;
  54. string[] cookie_components = cookies.Split (';');
  55. foreach (string kv in cookie_components) {
  56. int pos = kv.IndexOf ('=');
  57. if (pos == -1) {
  58. /* XXX ugh */
  59. continue;
  60. }
  61. else {
  62. string key = kv.Substring (0, pos);
  63. string val = kv.Substring (pos+1);
  64. Add (new HttpCookie (key.Trim (), val.Trim()));
  65. }
  66. }
  67. }
  68. public HttpCookieCollection () : base (StringComparer.OrdinalIgnoreCase)
  69. {
  70. }
  71. public void Add (HttpCookie cookie)
  72. {
  73. BaseAdd (cookie.Name, cookie);
  74. }
  75. public void Clear ()
  76. {
  77. BaseClear ();
  78. }
  79. public void CopyTo (Array array, int index)
  80. {
  81. /* XXX this is kind of gross and inefficient
  82. * since it makes a copy of the superclass's
  83. * list */
  84. object[] values = BaseGetAllValues();
  85. values.CopyTo (array, index);
  86. }
  87. public string GetKey (int index)
  88. {
  89. HttpCookie cookie = (HttpCookie)BaseGet (index);
  90. if (cookie == null)
  91. return null;
  92. else
  93. return cookie.Name;
  94. }
  95. public void Remove (string name)
  96. {
  97. BaseRemove (name);
  98. }
  99. public void Set (HttpCookie cookie)
  100. {
  101. BaseSet (cookie.Name, cookie);
  102. }
  103. public HttpCookie Get (int index)
  104. {
  105. return (HttpCookie)BaseGet (index);
  106. }
  107. public HttpCookie Get (string name)
  108. {
  109. return this [name];
  110. }
  111. public HttpCookie this [int index]
  112. {
  113. get {
  114. return (HttpCookie)BaseGet (index);
  115. }
  116. }
  117. public HttpCookie this [string name]
  118. {
  119. get {
  120. HttpCookie cookie = (HttpCookie)BaseGet (name);
  121. if (!IsReadOnly && auto_fill && cookie == null) {
  122. cookie = new HttpCookie (name);
  123. BaseAdd (name, cookie);
  124. }
  125. return cookie;
  126. }
  127. }
  128. public string[] AllKeys {
  129. get {
  130. string[] keys = new string [Keys.Count];
  131. ((ICollection)Keys).CopyTo (keys, 0);
  132. return keys;
  133. }
  134. }
  135. }
  136. }