Refactored CookieCollection.cs

This commit is contained in:
sta 2014-08-14 22:43:09 +09:00
parent c8bd8e9522
commit 11298565a4

View File

@ -148,12 +148,12 @@ namespace WebSocketSharp.Net
/// <exception cref="ArgumentOutOfRangeException"> /// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="index"/> is out of allowable range of indexes for the collection. /// <paramref name="index"/> is out of allowable range of indexes for the collection.
/// </exception> /// </exception>
public Cookie this [int index] { public Cookie this[int index] {
get { get {
if (index < 0 || index >= _list.Count) if (index < 0 || index >= _list.Count)
throw new ArgumentOutOfRangeException ("index"); throw new ArgumentOutOfRangeException ("index");
return _list [index]; return _list[index];
} }
} }
@ -170,7 +170,7 @@ namespace WebSocketSharp.Net
/// <exception cref="ArgumentNullException"> /// <exception cref="ArgumentNullException">
/// <paramref name="name"/> is <see langword="null"/>. /// <paramref name="name"/> is <see langword="null"/>.
/// </exception> /// </exception>
public Cookie this [string name] { public Cookie this[string name] {
get { get {
if (name == null) if (name == null)
throw new ArgumentNullException ("name"); throw new ArgumentNullException ("name");
@ -219,15 +219,15 @@ namespace WebSocketSharp.Net
var cookies = new CookieCollection (); var cookies = new CookieCollection ();
Cookie cookie = null; Cookie cookie = null;
var version = 0; var ver = 0;
var pairs = splitCookieHeaderValue (value); var pairs = splitCookieHeaderValue (value);
for (int i = 0; i < pairs.Length; i++) { for (var i = 0; i < pairs.Length; i++) {
var pair = pairs [i].Trim (); var pair = pairs[i].Trim ();
if (pair.Length == 0) if (pair.Length == 0)
continue; continue;
if (pair.StartsWith ("$version", StringComparison.InvariantCultureIgnoreCase)) { if (pair.StartsWith ("$version", StringComparison.InvariantCultureIgnoreCase)) {
version = Int32.Parse (pair.GetValue ('=', true)); ver = Int32.Parse (pair.GetValue ('=', true));
} }
else if (pair.StartsWith ("$path", StringComparison.InvariantCultureIgnoreCase)) { else if (pair.StartsWith ("$path", StringComparison.InvariantCultureIgnoreCase)) {
if (cookie != null) if (cookie != null)
@ -265,8 +265,8 @@ namespace WebSocketSharp.Net
} }
cookie = new Cookie (name, val); cookie = new Cookie (name, val);
if (version != 0) if (ver != 0)
cookie.Version = version; cookie.Version = ver;
} }
} }
@ -282,8 +282,8 @@ namespace WebSocketSharp.Net
Cookie cookie = null; Cookie cookie = null;
var pairs = splitCookieHeaderValue (value); var pairs = splitCookieHeaderValue (value);
for (int i = 0; i < pairs.Length; i++) { for (var i = 0; i < pairs.Length; i++) {
var pair = pairs [i].Trim (); var pair = pairs[i].Trim ();
if (pair.Length == 0) if (pair.Length == 0)
continue; continue;
@ -292,14 +292,14 @@ namespace WebSocketSharp.Net
cookie.Version = Int32.Parse (pair.GetValue ('=', true)); cookie.Version = Int32.Parse (pair.GetValue ('=', true));
} }
else if (pair.StartsWith ("expires", StringComparison.InvariantCultureIgnoreCase)) { else if (pair.StartsWith ("expires", StringComparison.InvariantCultureIgnoreCase)) {
var buffer = new StringBuilder (pair.GetValue ('='), 32); var buff = new StringBuilder (pair.GetValue ('='), 32);
if (i < pairs.Length - 1) if (i < pairs.Length - 1)
buffer.AppendFormat (", {0}", pairs [++i].Trim ()); buff.AppendFormat (", {0}", pairs[++i].Trim ());
DateTime expires; DateTime expires;
if (!DateTime.TryParseExact ( if (!DateTime.TryParseExact (
buffer.ToString (), buff.ToString (),
new [] { "ddd, dd'-'MMM'-'yyyy HH':'mm':'ss 'GMT'", "r" }, new[] { "ddd, dd'-'MMM'-'yyyy HH':'mm':'ss 'GMT'", "r" },
CultureInfo.CreateSpecificCulture ("en-US"), CultureInfo.CreateSpecificCulture ("en-US"),
DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal,
out expires)) out expires))
@ -384,21 +384,21 @@ namespace WebSocketSharp.Net
var name = cookie.Name; var name = cookie.Name;
var path = cookie.Path; var path = cookie.Path;
var domain = cookie.Domain; var domain = cookie.Domain;
var version = cookie.Version; var ver = cookie.Version;
for (int i = _list.Count - 1; i >= 0; i--) { for (var i = _list.Count - 1; i >= 0; i--) {
var c = _list [i]; var c = _list[i];
if (c.Name.Equals (name, StringComparison.InvariantCultureIgnoreCase) && if (c.Name.Equals (name, StringComparison.InvariantCultureIgnoreCase) &&
c.Path.Equals (path, StringComparison.InvariantCulture) && c.Path.Equals (path, StringComparison.InvariantCulture) &&
c.Domain.Equals (domain, StringComparison.InvariantCultureIgnoreCase) && c.Domain.Equals (domain, StringComparison.InvariantCultureIgnoreCase) &&
c.Version == version) c.Version == ver)
return i; return i;
} }
return -1; return -1;
} }
private static string [] splitCookieHeaderValue (string value) private static string[] splitCookieHeaderValue (string value)
{ {
return new List<string> (value.SplitHeaderValue (',', ';')).ToArray (); return new List<string> (value.SplitHeaderValue (',', ';')).ToArray ();
} }
@ -425,7 +425,7 @@ namespace WebSocketSharp.Net
} }
if (!cookie.Expired) { if (!cookie.Expired) {
_list [pos] = cookie; _list[pos] = cookie;
return; return;
} }
@ -468,7 +468,7 @@ namespace WebSocketSharp.Net
return; return;
} }
_list [pos] = cookie; _list[pos] = cookie;
} }
/// <summary> /// <summary>
@ -567,7 +567,7 @@ namespace WebSocketSharp.Net
/// The number of elements in the collection is greater than the available space from /// The number of elements in the collection is greater than the available space from
/// <paramref name="index"/> to the end of the destination <paramref name="array"/>. /// <paramref name="index"/> to the end of the destination <paramref name="array"/>.
/// </exception> /// </exception>
public void CopyTo (Cookie [] array, int index) public void CopyTo (Cookie[] array, int index)
{ {
if (array == null) if (array == null)
throw new ArgumentNullException ("array"); throw new ArgumentNullException ("array");