CookieHelper.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Net;
  5. using System.Text;
  6. namespace SocketHttpListener.Net
  7. {
  8. public static class CookieHelper
  9. {
  10. internal static CookieCollection Parse(string value, bool response)
  11. {
  12. return response
  13. ? parseResponse(value)
  14. : null;
  15. }
  16. private static string[] splitCookieHeaderValue(string value)
  17. {
  18. return new List<string>(value.SplitHeaderValue(',', ';')).ToArray();
  19. }
  20. private static CookieCollection parseResponse(string value)
  21. {
  22. var cookies = new CookieCollection();
  23. Cookie cookie = null;
  24. var pairs = splitCookieHeaderValue(value);
  25. for (int i = 0; i < pairs.Length; i++)
  26. {
  27. var pair = pairs[i].Trim();
  28. if (pair.Length == 0)
  29. continue;
  30. if (pair.StartsWith("version", StringComparison.OrdinalIgnoreCase))
  31. {
  32. if (cookie != null)
  33. cookie.Version = int.Parse(pair.GetValueInternal("=").Trim('"'));
  34. }
  35. else if (pair.StartsWith("expires", StringComparison.OrdinalIgnoreCase))
  36. {
  37. var buffer = new StringBuilder(pair.GetValueInternal("="), 32);
  38. if (i < pairs.Length - 1)
  39. buffer.AppendFormat(", {0}", pairs[++i].Trim());
  40. if (!DateTime.TryParseExact(
  41. buffer.ToString(),
  42. new[] { "ddd, dd'-'MMM'-'yyyy HH':'mm':'ss 'GMT'", "r" },
  43. new CultureInfo("en-US"),
  44. DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal,
  45. out var expires))
  46. expires = DateTime.Now;
  47. if (cookie != null && cookie.Expires == DateTime.MinValue)
  48. cookie.Expires = expires.ToLocalTime();
  49. }
  50. else if (pair.StartsWith("max-age", StringComparison.OrdinalIgnoreCase))
  51. {
  52. var max = int.Parse(pair.GetValueInternal("=").Trim('"'));
  53. var expires = DateTime.Now.AddSeconds((double)max);
  54. if (cookie != null)
  55. cookie.Expires = expires;
  56. }
  57. else if (pair.StartsWith("path", StringComparison.OrdinalIgnoreCase))
  58. {
  59. if (cookie != null)
  60. cookie.Path = pair.GetValueInternal("=");
  61. }
  62. else if (pair.StartsWith("domain", StringComparison.OrdinalIgnoreCase))
  63. {
  64. if (cookie != null)
  65. cookie.Domain = pair.GetValueInternal("=");
  66. }
  67. else if (pair.StartsWith("port", StringComparison.OrdinalIgnoreCase))
  68. {
  69. var port = pair.Equals("port", StringComparison.OrdinalIgnoreCase)
  70. ? "\"\""
  71. : pair.GetValueInternal("=");
  72. if (cookie != null)
  73. cookie.Port = port;
  74. }
  75. else if (pair.StartsWith("comment", StringComparison.OrdinalIgnoreCase))
  76. {
  77. if (cookie != null)
  78. cookie.Comment = pair.GetValueInternal("=").UrlDecode();
  79. }
  80. else if (pair.StartsWith("commenturl", StringComparison.OrdinalIgnoreCase))
  81. {
  82. if (cookie != null)
  83. cookie.CommentUri = pair.GetValueInternal("=").Trim('"').ToUri();
  84. }
  85. else if (pair.StartsWith("discard", StringComparison.OrdinalIgnoreCase))
  86. {
  87. if (cookie != null)
  88. cookie.Discard = true;
  89. }
  90. else if (pair.StartsWith("secure", StringComparison.OrdinalIgnoreCase))
  91. {
  92. if (cookie != null)
  93. cookie.Secure = true;
  94. }
  95. else if (pair.StartsWith("httponly", StringComparison.OrdinalIgnoreCase))
  96. {
  97. if (cookie != null)
  98. cookie.HttpOnly = true;
  99. }
  100. else
  101. {
  102. if (cookie != null)
  103. cookies.Add(cookie);
  104. string name;
  105. string val = string.Empty;
  106. var pos = pair.IndexOf('=');
  107. if (pos == -1)
  108. {
  109. name = pair;
  110. }
  111. else if (pos == pair.Length - 1)
  112. {
  113. name = pair.Substring(0, pos).TrimEnd(' ');
  114. }
  115. else
  116. {
  117. name = pair.Substring(0, pos).TrimEnd(' ');
  118. val = pair.Substring(pos + 1).TrimStart(' ');
  119. }
  120. cookie = new Cookie(name, val);
  121. }
  122. }
  123. if (cookie != null)
  124. cookies.Add(cookie);
  125. return cookies;
  126. }
  127. }
  128. }