Refactored CookieCollection.cs
This commit is contained in:
parent
6645a88418
commit
6909489f4c
@ -43,7 +43,6 @@ using System;
|
|||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace WebSocketSharp.Net
|
namespace WebSocketSharp.Net
|
||||||
@ -54,12 +53,6 @@ namespace WebSocketSharp.Net
|
|||||||
[Serializable]
|
[Serializable]
|
||||||
public class CookieCollection : ICollection, IEnumerable
|
public class CookieCollection : ICollection, IEnumerable
|
||||||
{
|
{
|
||||||
#region Private Static Fields
|
|
||||||
|
|
||||||
private static CookieCollectionComparer _comparer = new CookieCollectionComparer ();
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Private Fields
|
#region Private Fields
|
||||||
|
|
||||||
private List<Cookie> _list;
|
private List<Cookie> _list;
|
||||||
@ -89,11 +82,11 @@ namespace WebSocketSharp.Net
|
|||||||
|
|
||||||
internal IEnumerable<Cookie> Sorted {
|
internal IEnumerable<Cookie> Sorted {
|
||||||
get {
|
get {
|
||||||
return from cookie in _list
|
var list = new List<Cookie> (_list);
|
||||||
orderby cookie.Version,
|
if (list.Count > 1)
|
||||||
cookie.Name,
|
list.Sort (compareCookieWithinSorted);
|
||||||
cookie.Path.Length descending
|
|
||||||
select cookie;
|
return list;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -202,7 +195,7 @@ namespace WebSocketSharp.Net
|
|||||||
/// </value>
|
/// </value>
|
||||||
public Object SyncRoot {
|
public Object SyncRoot {
|
||||||
get {
|
get {
|
||||||
return _sync ?? (_sync = new object ());
|
return _sync ?? (_sync = ((ICollection) _list).SyncRoot);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -210,13 +203,28 @@ namespace WebSocketSharp.Net
|
|||||||
|
|
||||||
#region Private Methods
|
#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)
|
private static CookieCollection parseRequest (string value)
|
||||||
{
|
{
|
||||||
var cookies = new CookieCollection ();
|
var cookies = new CookieCollection ();
|
||||||
|
|
||||||
Cookie cookie = null;
|
Cookie cookie = null;
|
||||||
var version = 0;
|
var version = 0;
|
||||||
var pairs = split (value).ToArray ();
|
var pairs = splitCookieHeaderValue (value);
|
||||||
for (int i = 0; i < pairs.Length; i++) {
|
for (int i = 0; i < pairs.Length; i++) {
|
||||||
var pair = pairs [i].Trim ();
|
var pair = pairs [i].Trim ();
|
||||||
if (pair.Length == 0)
|
if (pair.Length == 0)
|
||||||
@ -277,7 +285,7 @@ namespace WebSocketSharp.Net
|
|||||||
var cookies = new CookieCollection ();
|
var cookies = new CookieCollection ();
|
||||||
|
|
||||||
Cookie cookie = null;
|
Cookie cookie = null;
|
||||||
var pairs = split (value).ToArray ();
|
var pairs = splitCookieHeaderValue (value);
|
||||||
for (int i = 0; i < pairs.Length; i++) {
|
for (int i = 0; i < pairs.Length; i++) {
|
||||||
var pair = pairs [i].Trim ();
|
var pair = pairs [i].Trim ();
|
||||||
if (pair.Length == 0)
|
if (pair.Length == 0)
|
||||||
@ -394,9 +402,9 @@ namespace WebSocketSharp.Net
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static IEnumerable<string> split (string value)
|
private static string [] splitCookieHeaderValue (string value)
|
||||||
{
|
{
|
||||||
return value.SplitHeaderValue (',', ';');
|
return new List<string> (value.SplitHeaderValue (',', ';')).ToArray ();
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
@ -436,8 +444,8 @@ namespace WebSocketSharp.Net
|
|||||||
|
|
||||||
internal void Sort ()
|
internal void Sort ()
|
||||||
{
|
{
|
||||||
if (_list.Count > 0)
|
if (_list.Count > 1)
|
||||||
_list.Sort (_comparer);
|
_list.Sort (compareCookieWithinSort);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
@ -540,7 +548,7 @@ namespace WebSocketSharp.Net
|
|||||||
throw new InvalidCastException (
|
throw new InvalidCastException (
|
||||||
"The elements in this collection cannot be cast automatically to the type of the destination array.");
|
"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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -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<Cookie>
|
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -130,7 +130,6 @@
|
|||||||
<Compile Include="Net\HttpDigestIdentity.cs" />
|
<Compile Include="Net\HttpDigestIdentity.cs" />
|
||||||
<Compile Include="Net\NetworkCredential.cs" />
|
<Compile Include="Net\NetworkCredential.cs" />
|
||||||
<Compile Include="Server\WebSocketServiceManager.cs" />
|
<Compile Include="Server\WebSocketServiceManager.cs" />
|
||||||
<Compile Include="Net\CookieCollectionComparer.cs" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
Loading…
Reference in New Issue
Block a user