diff --git a/websocket-sharp/Net/CookieCollection.cs b/websocket-sharp/Net/CookieCollection.cs index f7becc3f..9f892d6d 100644 --- a/websocket-sharp/Net/CookieCollection.cs +++ b/websocket-sharp/Net/CookieCollection.cs @@ -43,7 +43,6 @@ using System; using System.Collections; using System.Collections.Generic; using System.Globalization; -using System.Linq; using System.Text; namespace WebSocketSharp.Net @@ -54,12 +53,6 @@ namespace WebSocketSharp.Net [Serializable] public class CookieCollection : ICollection, IEnumerable { - #region Private Static Fields - - private static CookieCollectionComparer _comparer = new CookieCollectionComparer (); - - #endregion - #region Private Fields private List _list; @@ -89,11 +82,11 @@ namespace WebSocketSharp.Net internal IEnumerable Sorted { get { - return from cookie in _list - orderby cookie.Version, - cookie.Name, - cookie.Path.Length descending - select cookie; + var list = new List (_list); + if (list.Count > 1) + list.Sort (compareCookieWithinSorted); + + return list; } } @@ -202,7 +195,7 @@ namespace WebSocketSharp.Net /// public Object SyncRoot { get { - return _sync ?? (_sync = new object ()); + return _sync ?? (_sync = ((ICollection) _list).SyncRoot); } } @@ -210,13 +203,28 @@ namespace WebSocketSharp.Net #region Private Methods + private static int compareCookieWithinSort (Cookie x, Cookie y) + { + return (x.Name.Length + x.Value.Length) - (y.Name.Length + y.Value.Length); + } + + private static int compareCookieWithinSorted (Cookie x, Cookie y) + { + var ret = 0; + return (ret = x.Version - y.Version) != 0 + ? ret + : (ret = x.Name.CompareTo (y.Name)) != 0 + ? ret + : y.Path.Length - x.Path.Length; + } + private static CookieCollection parseRequest (string value) { var cookies = new CookieCollection (); Cookie cookie = null; var version = 0; - var pairs = split (value).ToArray (); + var pairs = splitCookieHeaderValue (value); for (int i = 0; i < pairs.Length; i++) { var pair = pairs [i].Trim (); if (pair.Length == 0) @@ -277,7 +285,7 @@ namespace WebSocketSharp.Net var cookies = new CookieCollection (); Cookie cookie = null; - var pairs = split (value).ToArray (); + var pairs = splitCookieHeaderValue (value); for (int i = 0; i < pairs.Length; i++) { var pair = pairs [i].Trim (); if (pair.Length == 0) @@ -394,9 +402,9 @@ namespace WebSocketSharp.Net return -1; } - private static IEnumerable split (string value) + private static string [] splitCookieHeaderValue (string value) { - return value.SplitHeaderValue (',', ';'); + return new List (value.SplitHeaderValue (',', ';')).ToArray (); } #endregion @@ -436,8 +444,8 @@ namespace WebSocketSharp.Net internal void Sort () { - if (_list.Count > 0) - _list.Sort (_comparer); + if (_list.Count > 1) + _list.Sort (compareCookieWithinSort); } #endregion @@ -540,7 +548,7 @@ namespace WebSocketSharp.Net throw new InvalidCastException ( "The elements in this collection cannot be cast automatically to the type of the destination array."); - (_list as IList).CopyTo (array, index); + ((IList) _list).CopyTo (array, index); } /// diff --git a/websocket-sharp/Net/CookieCollectionComparer.cs b/websocket-sharp/Net/CookieCollectionComparer.cs deleted file mode 100644 index 0a3493dc..00000000 --- a/websocket-sharp/Net/CookieCollectionComparer.cs +++ /dev/null @@ -1,56 +0,0 @@ -#region License -/* - * CookieCollectionComparer.cs - * - * This code is a separation from CookieCollection.cs. - * - * The MIT License - * - * Copyright (c) 2004,2009 Novell, Inc. (http://www.novell.com) - * Copyright (c) 2014 sta.blockhead - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -#endregion - -using System; -using System.Collections.Generic; - -namespace WebSocketSharp.Net -{ - internal sealed class CookieCollectionComparer : IComparer - { - public int Compare (Cookie x, Cookie y) - { - if (x == null && y == null) - return 0; - - if (x == null) - return -1; - - if (y == null) - return 1; - - var c1 = x.Name.Length + x.Value.Length; - var c2 = y.Name.Length + y.Value.Length; - - return c1 - c2; - } - } -} diff --git a/websocket-sharp/websocket-sharp.csproj b/websocket-sharp/websocket-sharp.csproj index 63d167ac..7ade0e6d 100644 --- a/websocket-sharp/websocket-sharp.csproj +++ b/websocket-sharp/websocket-sharp.csproj @@ -130,7 +130,6 @@ -