ResponseCookies.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace OSHttpServer
  5. {
  6. /// <summary>
  7. /// Cookies that should be set.
  8. /// </summary>
  9. public sealed class ResponseCookies : IEnumerable<ResponseCookie>
  10. {
  11. private readonly IDictionary<string, ResponseCookie> _items = new Dictionary<string, ResponseCookie>();
  12. /// <summary>
  13. /// Adds a cookie in the collection.
  14. /// </summary>
  15. /// <param name="cookie">cookie to add</param>
  16. /// <exception cref="ArgumentNullException">cookie is null</exception>
  17. public void Add(ResponseCookie cookie)
  18. {
  19. // Verifies the parameter
  20. if (cookie == null)
  21. throw new ArgumentNullException("cookie");
  22. if (cookie.Name == null || cookie.Name.Trim() == string.Empty)
  23. throw new ArgumentException("Name must be specified.");
  24. if (cookie.Value == null || cookie.Value.Trim() == string.Empty)
  25. throw new ArgumentException("Content must be specified.");
  26. if (_items.ContainsKey(cookie.Name))
  27. _items[cookie.Name] = cookie;
  28. else _items.Add(cookie.Name, cookie);
  29. }
  30. /// <summary>
  31. /// Copy a request cookie
  32. /// </summary>
  33. /// <param name="cookie"></param>
  34. /// <param name="expires">When the cookie should expire</param>
  35. public void Add(RequestCookie cookie, DateTime expires)
  36. {
  37. Add(new ResponseCookie(cookie, expires));
  38. }
  39. /// <summary>
  40. /// Gets the count of cookies in the collection.
  41. /// </summary>
  42. public int Count
  43. {
  44. get { return _items.Count; }
  45. }
  46. /// <summary>
  47. /// Gets the cookie of a given identifier (null if not existing).
  48. /// </summary>
  49. public ResponseCookie this[string id]
  50. {
  51. get
  52. {
  53. if (_items.ContainsKey(id))
  54. return _items[id];
  55. else
  56. return null;
  57. }
  58. set
  59. {
  60. if (_items.ContainsKey(id))
  61. _items[id] = value;
  62. else
  63. Add(value);
  64. }
  65. }
  66. /// <summary>
  67. /// Gets a collection enumerator on the cookie list.
  68. /// </summary>
  69. /// <returns>collection enumerator</returns>
  70. public IEnumerator GetEnumerator()
  71. {
  72. return _items.Values.GetEnumerator();
  73. }
  74. /// <summary>
  75. /// Remove all cookies
  76. /// </summary>
  77. public void Clear()
  78. {
  79. _items.Clear();
  80. }
  81. #region IEnumerable<ResponseCookie> Members
  82. ///<summary>
  83. ///Returns an enumerator that iterates through the collection.
  84. ///</summary>
  85. ///
  86. ///<returns>
  87. ///A <see cref="T:System.Collections.Generic.IEnumerator`1"></see> that can be used to iterate through the collection.
  88. ///</returns>
  89. ///<filterpriority>1</filterpriority>
  90. IEnumerator<ResponseCookie> IEnumerable<ResponseCookie>.GetEnumerator()
  91. {
  92. return _items.Values.GetEnumerator();
  93. }
  94. #endregion
  95. }
  96. }