1
0

ResponseCookie.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using System.Web;
  3. namespace OSHttpServer
  4. {
  5. /// <summary>
  6. /// cookie being sent back to the browser.
  7. /// </summary>
  8. /// <seealso cref="ResponseCookie"/>
  9. public class ResponseCookie : RequestCookie
  10. {
  11. private const string _nullPath = "/";
  12. private bool _persistant = false;
  13. private DateTime _expires;
  14. private string _path = "/";
  15. private readonly string _domain;
  16. #region constructors
  17. /// <summary>
  18. /// Constructor.
  19. /// </summary>
  20. /// <param name="id">cookie identifier</param>
  21. /// <param name="content">cookie content</param>
  22. /// <param name="expiresAt">cookie expiration date. Use DateTime.MinValue for session cookie.</param>
  23. /// <exception cref="ArgumentNullException">id or content is null</exception>
  24. /// <exception cref="ArgumentException">id is empty</exception>
  25. public ResponseCookie(string id, string content, DateTime expiresAt)
  26. : base(id, content)
  27. {
  28. if (expiresAt != DateTime.MinValue)
  29. {
  30. _expires = expiresAt;
  31. _persistant = true;
  32. }
  33. }
  34. /// <summary>
  35. /// Create a new cookie
  36. /// </summary>
  37. /// <param name="name">name identifying the cookie</param>
  38. /// <param name="value">cookie value</param>
  39. /// <param name="expires">when the cookie expires. Setting DateTime.MinValue will delete the cookie when the session is closed.</param>
  40. /// <param name="path">Path to where the cookie is valid</param>
  41. /// <param name="domain">Domain that the cookie is valid for.</param>
  42. public ResponseCookie(string name, string value, DateTime expires, string path, string domain)
  43. : this(name, value, expires)
  44. {
  45. _domain = domain;
  46. _path = path;
  47. }
  48. /// <summary>
  49. /// Create a new cookie
  50. /// </summary>
  51. /// <param name="cookie">Name and value will be used</param>
  52. /// <param name="expires">when the cookie expires.</param>
  53. public ResponseCookie(RequestCookie cookie, DateTime expires)
  54. : this(cookie.Name, cookie.Value, expires)
  55. {}
  56. #endregion
  57. #region inherited methods
  58. /// <summary>
  59. /// Gets the cookie HTML representation.
  60. /// </summary>
  61. /// <returns>cookie string</returns>
  62. public override string ToString()
  63. {
  64. string temp = string.Format("{0}={1}; ", HttpUtility.UrlEncode(Name), HttpUtility.UrlEncode(Value));
  65. if (_persistant)
  66. {
  67. TimeSpan span = DateTime.Now - DateTime.UtcNow;
  68. DateTime utc = _expires.Subtract(span);
  69. temp += string.Format("expires={0};", utc.ToString("r"));
  70. }
  71. if (!string.IsNullOrEmpty(_path))
  72. temp += string.Format("path={0}; ", _path);
  73. if (!string.IsNullOrEmpty(_domain))
  74. temp += string.Format("domain={0}; ", _domain);
  75. return temp;
  76. }
  77. #endregion
  78. #region public properties
  79. /// <summary>
  80. /// When the cookie expires.
  81. /// DateTime.MinValue means that the cookie expires when the session do so.
  82. /// </summary>
  83. public DateTime Expires
  84. {
  85. get { return _expires; }
  86. set
  87. {
  88. _expires = value;
  89. _persistant = value != DateTime.MinValue;
  90. }
  91. }
  92. /// <summary>
  93. /// Cookie is only valid under this path.
  94. /// </summary>
  95. public string Path
  96. {
  97. get { return _path; }
  98. set
  99. {
  100. if (!string.IsNullOrEmpty(value))
  101. _path = value;
  102. else
  103. _path = _nullPath;
  104. }
  105. }
  106. #endregion
  107. }
  108. }