Refactored Ext.cs
This commit is contained in:
		| @@ -51,11 +51,11 @@ using WebSocketSharp.Server; | |||||||
| namespace WebSocketSharp | namespace WebSocketSharp | ||||||
| { | { | ||||||
|   /// <summary> |   /// <summary> | ||||||
|   /// Provides a set of static methods for the websocket-sharp. |   /// Provides a set of static methods for websocket-sharp. | ||||||
|   /// </summary> |   /// </summary> | ||||||
|   public static class Ext |   public static class Ext | ||||||
|   { |   { | ||||||
|     #region Private Const Fields |     #region Private Fields | ||||||
|  |  | ||||||
|     private const string _tspecials = "()<>@,;:\\\"/[]?={} \t"; |     private const string _tspecials = "()<>@,;:\\\"/[]?={} \t"; | ||||||
|  |  | ||||||
| @@ -63,16 +63,15 @@ namespace WebSocketSharp | |||||||
|  |  | ||||||
|     #region Private Methods |     #region Private Methods | ||||||
|  |  | ||||||
|     private static byte [] compress (this byte [] value) |     private static byte[] compress (this byte[] data) | ||||||
|     { |     { | ||||||
|       if (value.LongLength == 0) |       if (data.LongLength == 0) | ||||||
|         //return new Byte [] { 0x00, 0x00, 0x00, 0xff, 0xff }; |         //return new Byte[] { 0x00, 0x00, 0x00, 0xff, 0xff }; | ||||||
|         return value; |         return data; | ||||||
|  |  | ||||||
|       using (var input = new MemoryStream (value)) { |       using (var input = new MemoryStream (data)) | ||||||
|         return input.compressToArray (); |         return input.compressToArray (); | ||||||
|     } |     } | ||||||
|     } |  | ||||||
|  |  | ||||||
|     private static MemoryStream compress (this Stream stream) |     private static MemoryStream compress (this Stream stream) | ||||||
|     { |     { | ||||||
| @@ -90,23 +89,22 @@ namespace WebSocketSharp | |||||||
|       } |       } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     private static byte [] compressToArray (this Stream stream) |     private static byte[] compressToArray (this Stream stream) | ||||||
|     { |     { | ||||||
|       using (var comp = stream.compress ()) { |       using (var output = stream.compress ()) { | ||||||
|         comp.Close (); |         output.Close (); | ||||||
|         return comp.ToArray (); |         return output.ToArray (); | ||||||
|       } |       } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     private static byte [] decompress (this byte [] value) |     private static byte[] decompress (this byte[] data) | ||||||
|     { |     { | ||||||
|       if (value.LongLength == 0) |       if (data.LongLength == 0) | ||||||
|         return value; |         return data; | ||||||
|  |  | ||||||
|       using (var input = new MemoryStream (value)) { |       using (var input = new MemoryStream (data)) | ||||||
|         return input.decompressToArray (); |         return input.decompressToArray (); | ||||||
|     } |     } | ||||||
|     } |  | ||||||
|  |  | ||||||
|     private static MemoryStream decompress (this Stream stream) |     private static MemoryStream decompress (this Stream stream) | ||||||
|     { |     { | ||||||
| @@ -121,15 +119,15 @@ namespace WebSocketSharp | |||||||
|       } |       } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     private static byte [] decompressToArray (this Stream stream) |     private static byte[] decompressToArray (this Stream stream) | ||||||
|     { |     { | ||||||
|       using (var decomp = stream.decompress ()) { |       using (var output = stream.decompress ()) { | ||||||
|         decomp.Close (); |         output.Close (); | ||||||
|         return decomp.ToArray (); |         return output.ToArray (); | ||||||
|       } |       } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     private static byte [] readBytes (this Stream stream, byte [] buffer, int offset, int length) |     private static byte[] readBytes (this Stream stream, byte[] buffer, int offset, int length) | ||||||
|     { |     { | ||||||
|       var len = stream.Read (buffer, offset, length); |       var len = stream.Read (buffer, offset, length); | ||||||
|       if (len < 1) |       if (len < 1) | ||||||
| @@ -150,7 +148,7 @@ namespace WebSocketSharp | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     private static bool readBytes ( |     private static bool readBytes ( | ||||||
|       this Stream stream, byte [] buffer, int offset, int length, Stream dest) |       this Stream stream, byte[] buffer, int offset, int length, Stream dest) | ||||||
|     { |     { | ||||||
|       var bytes = stream.readBytes (buffer, offset, length); |       var bytes = stream.readBytes (buffer, offset, length); | ||||||
|       var len = bytes.Length; |       var len = bytes.Length; | ||||||
| @@ -169,25 +167,25 @@ namespace WebSocketSharp | |||||||
|  |  | ||||||
|     #region Internal Methods |     #region Internal Methods | ||||||
|  |  | ||||||
|     internal static byte [] Append (this ushort code, string reason) |     internal static byte[] Append (this ushort code, string reason) | ||||||
|     { |     { | ||||||
|       using (var buffer = new MemoryStream ()) { |       using (var buff = new MemoryStream ()) { | ||||||
|         var tmp = code.ToByteArrayInternally (ByteOrder.Big); |         var tmp = code.ToByteArrayInternally (ByteOrder.Big); | ||||||
|         buffer.Write (tmp, 0, 2); |         buff.Write (tmp, 0, 2); | ||||||
|         if (reason != null && reason.Length > 0) { |         if (reason != null && reason.Length > 0) { | ||||||
|           tmp = Encoding.UTF8.GetBytes (reason); |           tmp = Encoding.UTF8.GetBytes (reason); | ||||||
|           buffer.Write (tmp, 0, tmp.Length); |           buff.Write (tmp, 0, tmp.Length); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         buffer.Close (); |         buff.Close (); | ||||||
|         return buffer.ToArray (); |         return buff.ToArray (); | ||||||
|       } |       } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     internal static string CheckIfCanRead (this Stream stream) |     internal static string CheckIfCanRead (this Stream stream) | ||||||
|     { |     { | ||||||
|       return stream == null |       return stream == null | ||||||
|              ? "'stream' must not be null." |              ? "'stream' is null." | ||||||
|              : !stream.CanRead |              : !stream.CanRead | ||||||
|                ? "'stream' cannot be read." |                ? "'stream' cannot be read." | ||||||
|                : null; |                : null; | ||||||
| @@ -247,14 +245,14 @@ namespace WebSocketSharp | |||||||
|              : null; |              : null; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     internal static string CheckIfValidControlData (this byte [] data, string paramName) |     internal static string CheckIfValidControlData (this byte[] data, string paramName) | ||||||
|     { |     { | ||||||
|       return data.Length > 125 |       return data.Length > 125 | ||||||
|              ? String.Format ("'{0}' length must be less.", paramName) |              ? String.Format ("'{0}' is greater than the allowable size.", paramName) | ||||||
|              : null; |              : null; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     internal static string CheckIfValidProtocols (this string [] protocols) |     internal static string CheckIfValidProtocols (this string[] protocols) | ||||||
|     { |     { | ||||||
|       return protocols.Contains ( |       return protocols.Contains ( | ||||||
|                protocol => protocol == null || protocol.Length == 0 || !protocol.IsToken ()) |                protocol => protocol == null || protocol.Length == 0 || !protocol.IsToken ()) | ||||||
| @@ -264,42 +262,42 @@ namespace WebSocketSharp | |||||||
|                : null; |                : null; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     internal static string CheckIfValidSendData (this byte [] data) |     internal static string CheckIfValidSendData (this byte[] data) | ||||||
|     { |     { | ||||||
|       return data == null |       return data == null | ||||||
|              ? "'data' must not be null." |              ? "'data' is null." | ||||||
|              : null; |              : null; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     internal static string CheckIfValidSendData (this FileInfo file) |     internal static string CheckIfValidSendData (this FileInfo file) | ||||||
|     { |     { | ||||||
|       return file == null |       return file == null | ||||||
|              ? "'file' must not be null." |              ? "'file' is null." | ||||||
|              : null; |              : null; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     internal static string CheckIfValidSendData (this string data) |     internal static string CheckIfValidSendData (this string data) | ||||||
|     { |     { | ||||||
|       return data == null |       return data == null | ||||||
|              ? "'data' must not be null." |              ? "'data' is null." | ||||||
|              : null; |              : null; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     internal static string CheckIfValidServicePath (this string servicePath) |     internal static string CheckIfValidServicePath (this string servicePath) | ||||||
|     { |     { | ||||||
|       return servicePath == null || servicePath.Length == 0 |       return servicePath == null || servicePath.Length == 0 | ||||||
|              ? "'servicePath' must not be null or empty." |              ? "'servicePath' is null or empty." | ||||||
|              : servicePath [0] != '/' |              : servicePath[0] != '/' | ||||||
|                ? "'servicePath' not absolute path." |                ? "'servicePath' isn't absolute path." | ||||||
|                : servicePath.IndexOfAny (new [] {'?', '#'}) != -1 |                : servicePath.IndexOfAny (new[] {'?', '#'}) != -1 | ||||||
|                  ? "'servicePath' must not contain either or both query and fragment components." |                  ? "'servicePath' contains either or both query and fragment components." | ||||||
|                  : null; |                  : null; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     internal static string CheckIfValidSessionID (this string id) |     internal static string CheckIfValidSessionID (this string id) | ||||||
|     { |     { | ||||||
|       return id == null || id.Length == 0 |       return id == null || id.Length == 0 | ||||||
|              ? "'id' must not be null or empty." |              ? "'id' is null or empty." | ||||||
|              : null; |              : null; | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @@ -316,11 +314,11 @@ namespace WebSocketSharp | |||||||
|       response.Close (HttpStatusCode.Unauthorized); |       response.Close (HttpStatusCode.Unauthorized); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     internal static byte [] Compress (this byte [] value, CompressionMethod method) |     internal static byte[] Compress (this byte[] data, CompressionMethod method) | ||||||
|     { |     { | ||||||
|       return method == CompressionMethod.Deflate |       return method == CompressionMethod.Deflate | ||||||
|              ? value.compress () |              ? data.compress () | ||||||
|              : value; |              : data; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     internal static Stream Compress (this Stream stream, CompressionMethod method) |     internal static Stream Compress (this Stream stream, CompressionMethod method) | ||||||
| @@ -330,7 +328,7 @@ namespace WebSocketSharp | |||||||
|              : stream; |              : stream; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     internal static byte [] CompressToArray (this Stream stream, CompressionMethod method) |     internal static byte[] CompressToArray (this Stream stream, CompressionMethod method) | ||||||
|     { |     { | ||||||
|       return method == CompressionMethod.Deflate |       return method == CompressionMethod.Deflate | ||||||
|              ? stream.compressToArray () |              ? stream.compressToArray () | ||||||
| @@ -346,18 +344,18 @@ namespace WebSocketSharp | |||||||
|       return false; |       return false; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     internal static bool ContainsTwice (this string [] values) |     internal static bool ContainsTwice (this string[] values) | ||||||
|     { |     { | ||||||
|       var len = values.Length; |       var len = values.Length; | ||||||
|  |  | ||||||
|       Func<int, bool> contains = null; |       Func<int, bool> contains = null; | ||||||
|       contains = index => { |       contains = idx => { | ||||||
|         if (index < len - 1) { |         if (idx < len - 1) { | ||||||
|           for (var i = index + 1; i < len; i++) |           for (var i = idx + 1; i < len; i++) | ||||||
|             if (values [i] == values [index]) |             if (values[i] == values[idx]) | ||||||
|               return true; |               return true; | ||||||
|  |  | ||||||
|           return contains (++index); |           return contains (++idx); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         return false; |         return false; | ||||||
| @@ -366,9 +364,9 @@ namespace WebSocketSharp | |||||||
|       return contains (0); |       return contains (0); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     internal static T [] Copy<T> (this T [] src, long length) |     internal static T[] Copy<T> (this T[] src, long length) | ||||||
|     { |     { | ||||||
|       var dest = new T [length]; |       var dest = new T[length]; | ||||||
|       Array.Copy (src, 0, dest, 0, length); |       Array.Copy (src, 0, dest, 0, length); | ||||||
|  |  | ||||||
|       return dest; |       return dest; | ||||||
| @@ -382,21 +380,20 @@ namespace WebSocketSharp | |||||||
|     internal static void CopyTo (this Stream src, Stream dest, bool setDefaultPosition) |     internal static void CopyTo (this Stream src, Stream dest, bool setDefaultPosition) | ||||||
|     { |     { | ||||||
|       var readLen = 0; |       var readLen = 0; | ||||||
|       var bufferLen = 256; |       var buffLen = 256; | ||||||
|       var buffer = new byte [bufferLen]; |       var buff = new byte[buffLen]; | ||||||
|       while ((readLen = src.Read (buffer, 0, bufferLen)) > 0) { |       while ((readLen = src.Read (buff, 0, buffLen)) > 0) | ||||||
|         dest.Write (buffer, 0, readLen); |         dest.Write (buff, 0, readLen); | ||||||
|       } |  | ||||||
|  |  | ||||||
|       if (setDefaultPosition) |       if (setDefaultPosition) | ||||||
|         dest.Position = 0; |         dest.Position = 0; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     internal static byte [] Decompress (this byte [] value, CompressionMethod method) |     internal static byte[] Decompress (this byte[] data, CompressionMethod method) | ||||||
|     { |     { | ||||||
|       return method == CompressionMethod.Deflate |       return method == CompressionMethod.Deflate | ||||||
|              ? value.decompress () |              ? data.decompress () | ||||||
|              : value; |              : data; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     internal static Stream Decompress (this Stream stream, CompressionMethod method) |     internal static Stream Decompress (this Stream stream, CompressionMethod method) | ||||||
| @@ -406,7 +403,7 @@ namespace WebSocketSharp | |||||||
|              : stream; |              : stream; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     internal static byte [] DecompressToArray (this Stream stream, CompressionMethod method) |     internal static byte[] DecompressToArray (this Stream stream, CompressionMethod method) | ||||||
|     { |     { | ||||||
|       return method == CompressionMethod.Deflate |       return method == CompressionMethod.Deflate | ||||||
|              ? stream.decompressToArray () |              ? stream.decompressToArray () | ||||||
| @@ -452,7 +449,7 @@ namespace WebSocketSharp | |||||||
|     /// otherwise, <see langword="null"/>. |     /// otherwise, <see langword="null"/>. | ||||||
|     /// </returns> |     /// </returns> | ||||||
|     /// <param name="uri"> |     /// <param name="uri"> | ||||||
|     /// A <see cref="Uri"/> that represents a URI to get the absolute path from. |     /// A <see cref="Uri"/> that represents the URI to get the absolute path from. | ||||||
|     /// </param> |     /// </param> | ||||||
|     internal static string GetAbsolutePath (this Uri uri) |     internal static string GetAbsolutePath (this Uri uri) | ||||||
|     { |     { | ||||||
| @@ -460,10 +457,10 @@ namespace WebSocketSharp | |||||||
|         return uri.AbsolutePath; |         return uri.AbsolutePath; | ||||||
|  |  | ||||||
|       var original = uri.OriginalString; |       var original = uri.OriginalString; | ||||||
|       if (original [0] != '/') |       if (original[0] != '/') | ||||||
|         return null; |         return null; | ||||||
|  |  | ||||||
|       var i = original.IndexOfAny (new [] {'?', '#'}); |       var i = original.IndexOfAny (new[] {'?', '#'}); | ||||||
|       return i > 0 |       return i > 0 | ||||||
|              ? original.Substring (0, i) |              ? original.Substring (0, i) | ||||||
|              : original; |              : original; | ||||||
| @@ -484,7 +481,7 @@ namespace WebSocketSharp | |||||||
|                      : code == CloseStatusCode.TooBig |                      : code == CloseStatusCode.TooBig | ||||||
|                        ? "A too big data has been received." |                        ? "A too big data has been received." | ||||||
|                        : code == CloseStatusCode.IgnoreExtension |                        : code == CloseStatusCode.IgnoreExtension | ||||||
|                          ? "WebSocket client did not receive expected extension(s)." |                          ? "WebSocket client didn't receive expected extension(s)." | ||||||
|                          : code == CloseStatusCode.ServerError |                          : code == CloseStatusCode.ServerError | ||||||
|                            ? "WebSocket server got an internal error." |                            ? "WebSocket server got an internal error." | ||||||
|                            : code == CloseStatusCode.TlsHandshakeFailure |                            : code == CloseStatusCode.TlsHandshakeFailure | ||||||
| @@ -492,7 +489,21 @@ namespace WebSocketSharp | |||||||
|                              : String.Empty; |                              : String.Empty; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     internal static string GetNameInternal (this string nameAndValue, string separator) |     /// <summary> | ||||||
|  |     /// Gets the name from the specified <see cref="string"/> that contains a pair of name and | ||||||
|  |     /// value separated by a separator character. | ||||||
|  |     /// </summary> | ||||||
|  |     /// <returns> | ||||||
|  |     /// A <see cref="string"/> that represents the name if any; otherwise, <c>null</c>. | ||||||
|  |     /// </returns> | ||||||
|  |     /// <param name="nameAndValue"> | ||||||
|  |     /// A <see cref="string"/> that contains a pair of name and value separated by a separator | ||||||
|  |     /// character. | ||||||
|  |     /// </param> | ||||||
|  |     /// <param name="separator"> | ||||||
|  |     /// A <see cref="char"/> that represents the separator character. | ||||||
|  |     /// </param> | ||||||
|  |     internal static string GetName (this string nameAndValue, char separator) | ||||||
|     { |     { | ||||||
|       var i = nameAndValue.IndexOf (separator); |       var i = nameAndValue.IndexOf (separator); | ||||||
|       return i > 0 |       return i > 0 | ||||||
| @@ -500,15 +511,29 @@ namespace WebSocketSharp | |||||||
|              : null; |              : null; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     internal static string GetValueInternal (this string nameAndValue, string separator) |     /// <summary> | ||||||
|  |     /// Gets the value from the specified <see cref="string"/> that contains a pair of name and | ||||||
|  |     /// value separated by a separator character. | ||||||
|  |     /// </summary> | ||||||
|  |     /// <returns> | ||||||
|  |     /// A <see cref="string"/> that represents the value if any; otherwise, <c>null</c>. | ||||||
|  |     /// </returns> | ||||||
|  |     /// <param name="nameAndValue"> | ||||||
|  |     /// A <see cref="string"/> that contains a pair of name and value separated by a separator | ||||||
|  |     /// character. | ||||||
|  |     /// </param> | ||||||
|  |     /// <param name="separator"> | ||||||
|  |     /// A <see cref="char"/> that represents the separator character. | ||||||
|  |     /// </param> | ||||||
|  |     internal static string GetValue (this string nameAndValue, char separator) | ||||||
|     { |     { | ||||||
|       var i = nameAndValue.IndexOf (separator); |       var i = nameAndValue.IndexOf (separator); | ||||||
|       return i >= 0 && i < nameAndValue.Length - 1 |       return i > -1 && i < nameAndValue.Length - 1 | ||||||
|              ? nameAndValue.Substring (i + 1).Trim () |              ? nameAndValue.Substring (i + 1).Trim () | ||||||
|              : null; |              : null; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     internal static string GetValue (this string nameAndValue, string separator, bool unquote) |     internal static string GetValue (this string nameAndValue, char separator, bool unquote) | ||||||
|     { |     { | ||||||
|       var i = nameAndValue.IndexOf (separator); |       var i = nameAndValue.IndexOf (separator); | ||||||
|       if (i < 0 || i == nameAndValue.Length - 1) |       if (i < 0 || i == nameAndValue.Length - 1) | ||||||
| @@ -562,7 +587,7 @@ namespace WebSocketSharp | |||||||
|     { |     { | ||||||
|       var len = value.Length; |       var len = value.Length; | ||||||
|       for (var i = 0; i < len; i++) { |       for (var i = 0; i < len; i++) { | ||||||
|         char c = value [i]; |         char c = value[i]; | ||||||
|         if (c < 0x20 && !"\r\n\t".Contains (c)) |         if (c < 0x20 && !"\r\n\t".Contains (c)) | ||||||
|           return false; |           return false; | ||||||
|  |  | ||||||
| @@ -570,7 +595,7 @@ namespace WebSocketSharp | |||||||
|           return false; |           return false; | ||||||
|  |  | ||||||
|         if (c == '\n' && ++i < len) { |         if (c == '\n' && ++i < len) { | ||||||
|           c = value [i]; |           c = value[i]; | ||||||
|           if (!" \t".Contains (c)) |           if (!" \t".Contains (c)) | ||||||
|             return false; |             return false; | ||||||
|         } |         } | ||||||
| @@ -595,50 +620,50 @@ namespace WebSocketSharp | |||||||
|              : String.Format ("\"{0}\"", value.Replace ("\"", "\\\"")); |              : String.Format ("\"{0}\"", value.Replace ("\"", "\\\"")); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     internal static byte [] ReadBytes (this Stream stream, int length) |     internal static byte[] ReadBytes (this Stream stream, int length) | ||||||
|     { |     { | ||||||
|       return stream.readBytes (new byte [length], 0, length); |       return stream.readBytes (new byte[length], 0, length); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     internal static byte [] ReadBytes (this Stream stream, long length, int bufferLength) |     internal static byte[] ReadBytes (this Stream stream, long length, int bufferLength) | ||||||
|     { |     { | ||||||
|       using (var result = new MemoryStream ()) { |       using (var res = new MemoryStream ()) { | ||||||
|         var count = length / bufferLength; |         var cnt = length / bufferLength; | ||||||
|         var rem = (int) (length % bufferLength); |         var rem = (int) (length % bufferLength); | ||||||
|  |  | ||||||
|         var buffer = new byte [bufferLength]; |         var buff = new byte[bufferLength]; | ||||||
|         var end = false; |         var end = false; | ||||||
|         for (long i = 0; i < count; i++) { |         for (long i = 0; i < cnt; i++) { | ||||||
|           if (!stream.readBytes (buffer, 0, bufferLength, result)) { |           if (!stream.readBytes (buff, 0, bufferLength, res)) { | ||||||
|             end = true; |             end = true; | ||||||
|             break; |             break; | ||||||
|           } |           } | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         if (!end && rem > 0) |         if (!end && rem > 0) | ||||||
|           stream.readBytes (new byte [rem], 0, rem, result); |           stream.readBytes (new byte[rem], 0, rem, res); | ||||||
|  |  | ||||||
|         result.Close (); |         res.Close (); | ||||||
|         return result.ToArray (); |         return res.ToArray (); | ||||||
|       } |       } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     internal static void ReadBytesAsync ( |     internal static void ReadBytesAsync ( | ||||||
|       this Stream stream, int length, Action<byte []> completed, Action<Exception> error) |       this Stream stream, int length, Action<byte[]> completed, Action<Exception> error) | ||||||
|     { |     { | ||||||
|       var buffer = new byte [length]; |       var buff = new byte[length]; | ||||||
|       stream.BeginRead ( |       stream.BeginRead ( | ||||||
|         buffer, |         buff, | ||||||
|         0, |         0, | ||||||
|         length, |         length, | ||||||
|         ar => { |         ar => { | ||||||
|           try { |           try { | ||||||
|             var len = stream.EndRead (ar); |             var len = stream.EndRead (ar); | ||||||
|             var bytes = len < 1 |             var bytes = len < 1 | ||||||
|                       ? new byte [0] |                         ? new byte[0] | ||||||
|                         : len < length |                         : len < length | ||||||
|                         ? stream.readBytes (buffer, len, length - len) |                           ? stream.readBytes (buff, len, length - len) | ||||||
|                         : buffer; |                           : buff; | ||||||
|  |  | ||||||
|             if (completed != null) |             if (completed != null) | ||||||
|               completed (bytes); |               completed (bytes); | ||||||
| @@ -651,7 +676,7 @@ namespace WebSocketSharp | |||||||
|         null); |         null); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     internal static string RemovePrefix (this string value, params string [] prefixes) |     internal static string RemovePrefix (this string value, params string[] prefixes) | ||||||
|     { |     { | ||||||
|       var i = 0; |       var i = 0; | ||||||
|       foreach (var prefix in prefixes) { |       foreach (var prefix in prefixes) { | ||||||
| @@ -666,31 +691,31 @@ namespace WebSocketSharp | |||||||
|              : value; |              : value; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     internal static T [] Reverse<T> (this T [] array) |     internal static T[] Reverse<T> (this T[] array) | ||||||
|     { |     { | ||||||
|       var len = array.Length; |       var len = array.Length; | ||||||
|       T [] reverse = new T [len]; |       var reverse = new T[len]; | ||||||
|  |  | ||||||
|       var end = len - 1; |       var end = len - 1; | ||||||
|       for (var i = 0; i <= end; i++) |       for (var i = 0; i <= end; i++) | ||||||
|         reverse [i] = array [end - i]; |         reverse[i] = array[end - i]; | ||||||
|  |  | ||||||
|       return reverse; |       return reverse; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     internal static IEnumerable<string> SplitHeaderValue ( |     internal static IEnumerable<string> SplitHeaderValue ( | ||||||
|       this string value, params char [] separator) |       this string value, params char[] separator) | ||||||
|     { |     { | ||||||
|       var len = value.Length; |       var len = value.Length; | ||||||
|       var separators = new string (separator); |       var separators = new string (separator); | ||||||
|  |  | ||||||
|       var buffer = new StringBuilder (32); |       var buff = new StringBuilder (32); | ||||||
|       var quoted = false; |       var quoted = false; | ||||||
|       var escaped = false; |       var escaped = false; | ||||||
|  |  | ||||||
|       char c; |       char c; | ||||||
|       for (var i = 0; i < len; i++) { |       for (var i = 0; i < len; i++) { | ||||||
|         c = value [i]; |         c = value[i]; | ||||||
|         if (c == '"') { |         if (c == '"') { | ||||||
|           if (escaped) |           if (escaped) | ||||||
|             escaped = !escaped; |             escaped = !escaped; | ||||||
| @@ -698,13 +723,13 @@ namespace WebSocketSharp | |||||||
|             quoted = !quoted; |             quoted = !quoted; | ||||||
|         } |         } | ||||||
|         else if (c == '\\') { |         else if (c == '\\') { | ||||||
|           if (i < len - 1 && value [i + 1] == '"') |           if (i < len - 1 && value[i + 1] == '"') | ||||||
|             escaped = true; |             escaped = true; | ||||||
|         } |         } | ||||||
|         else if (separators.Contains (c)) { |         else if (separators.Contains (c)) { | ||||||
|           if (!quoted) { |           if (!quoted) { | ||||||
|             yield return buffer.ToString (); |             yield return buff.ToString (); | ||||||
|             buffer.Length = 0; |             buff.Length = 0; | ||||||
|  |  | ||||||
|             continue; |             continue; | ||||||
|           } |           } | ||||||
| @@ -712,14 +737,14 @@ namespace WebSocketSharp | |||||||
|         else { |         else { | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         buffer.Append (c); |         buff.Append (c); | ||||||
|       } |       } | ||||||
|  |  | ||||||
|       if (buffer.Length > 0) |       if (buff.Length > 0) | ||||||
|         yield return buffer.ToString (); |         yield return buff.ToString (); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     internal static byte [] ToByteArray (this Stream stream) |     internal static byte[] ToByteArray (this Stream stream) | ||||||
|     { |     { | ||||||
|       using (var output = new MemoryStream ()) { |       using (var output = new MemoryStream ()) { | ||||||
|         stream.Position = 0; |         stream.Position = 0; | ||||||
| @@ -730,7 +755,7 @@ namespace WebSocketSharp | |||||||
|       } |       } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     internal static byte [] ToByteArrayInternally (this ushort value, ByteOrder order) |     internal static byte[] ToByteArrayInternally (this ushort value, ByteOrder order) | ||||||
|     { |     { | ||||||
|       var bytes = BitConverter.GetBytes (value); |       var bytes = BitConverter.GetBytes (value); | ||||||
|       if (!order.IsHostOrder ()) |       if (!order.IsHostOrder ()) | ||||||
| @@ -739,7 +764,7 @@ namespace WebSocketSharp | |||||||
|       return bytes; |       return bytes; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     internal static byte [] ToByteArrayInternally (this ulong value, ByteOrder order) |     internal static byte[] ToByteArrayInternally (this ulong value, ByteOrder order) | ||||||
|     { |     { | ||||||
|       var bytes = BitConverter.GetBytes (value); |       var bytes = BitConverter.GetBytes (value); | ||||||
|       if (!order.IsHostOrder ()) |       if (!order.IsHostOrder ()) | ||||||
| @@ -768,7 +793,7 @@ namespace WebSocketSharp | |||||||
|     { |     { | ||||||
|       try { |       try { | ||||||
|         var addrs = System.Net.Dns.GetHostAddresses (hostNameOrAddress); |         var addrs = System.Net.Dns.GetHostAddresses (hostNameOrAddress); | ||||||
|         return addrs [0]; |         return addrs[0]; | ||||||
|       } |       } | ||||||
|       catch { |       catch { | ||||||
|         return null; |         return null; | ||||||
| @@ -780,12 +805,12 @@ namespace WebSocketSharp | |||||||
|       return new List<TSource> (source); |       return new List<TSource> (source); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     internal static ushort ToUInt16 (this byte [] src, ByteOrder srcOrder) |     internal static ushort ToUInt16 (this byte[] src, ByteOrder srcOrder) | ||||||
|     { |     { | ||||||
|       return BitConverter.ToUInt16 (src.ToHostOrder (srcOrder), 0); |       return BitConverter.ToUInt16 (src.ToHostOrder (srcOrder), 0); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     internal static ulong ToUInt64 (this byte [] src, ByteOrder srcOrder) |     internal static ulong ToUInt64 (this byte[] src, ByteOrder srcOrder) | ||||||
|     { |     { | ||||||
|       return BitConverter.ToUInt64 (src.ToHostOrder (srcOrder), 0); |       return BitConverter.ToUInt64 (src.ToHostOrder (srcOrder), 0); | ||||||
|     } |     } | ||||||
| @@ -821,46 +846,43 @@ namespace WebSocketSharp | |||||||
|     { |     { | ||||||
|       result = null; |       result = null; | ||||||
|       if (uriString.Length == 0) { |       if (uriString.Length == 0) { | ||||||
|         message = "Must not be empty."; |         message = "Empty string."; | ||||||
|         return false; |         return false; | ||||||
|       } |       } | ||||||
|  |  | ||||||
|       var uri = uriString.ToUri (); |       var uri = uriString.ToUri (); | ||||||
|       if (!uri.IsAbsoluteUri) { |       if (!uri.IsAbsoluteUri) { | ||||||
|         message = "Must be the absolute URI: " + uriString; |         message = "Not absolute URI: " + uriString; | ||||||
|         return false; |         return false; | ||||||
|       } |       } | ||||||
|  |  | ||||||
|       var scheme = uri.Scheme; |       var schm = uri.Scheme; | ||||||
|       if (scheme != "ws" && scheme != "wss") { |       if (schm != "ws" && schm != "wss") { | ||||||
|         message = "The scheme part must be 'ws' or 'wss': " + scheme; |         message = "The scheme part isn't 'ws' or 'wss': " + uriString; | ||||||
|         return false; |         return false; | ||||||
|       } |       } | ||||||
|  |  | ||||||
|       var fragment = uri.Fragment; |       if (uri.Fragment.Length > 0) { | ||||||
|       if (fragment.Length > 0) { |         message = "Contains the fragment component: " + uriString; | ||||||
|         message = "Must not contain the fragment component: " + uriString; |  | ||||||
|         return false; |         return false; | ||||||
|       } |       } | ||||||
|  |  | ||||||
|       var port = uri.Port; |       var port = uri.Port; | ||||||
|       if (port > 0) { |       if (port > 0) { | ||||||
|         if (port > 65535) { |         if (port > 65535) { | ||||||
|           message = "The port part must be between 1 and 65535: " + port; |           message = "The port part is greater than 65535: " + uriString; | ||||||
|           return false; |           return false; | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         if ((scheme == "ws" && port == 443) || (scheme == "wss" && port == 80)) { |         if ((schm == "ws" && port == 443) || (schm == "wss" && port == 80)) { | ||||||
|           message = String.Format ( |           message = "Invalid pair of scheme and port: " + uriString; | ||||||
|             "Invalid pair of scheme and port: {0}, {1}", scheme, port); |  | ||||||
|           return false; |           return false; | ||||||
|         } |         } | ||||||
|       } |       } | ||||||
|       else { |       else { | ||||||
|         port = scheme == "ws" ? 80 : 443; |         uri = String.Format ( | ||||||
|         var url = String.Format ( |           "{0}://{1}:{2}{3}", schm, uri.Host, schm == "ws" ? 80 : 443, uri.PathAndQuery) | ||||||
|           "{0}://{1}:{2}{3}", scheme, uri.Host, port, uri.PathAndQuery); |           .ToUri (); | ||||||
|         uri = url.ToUri (); |  | ||||||
|       } |       } | ||||||
|  |  | ||||||
|       result = uri; |       result = uri; | ||||||
| @@ -871,19 +893,18 @@ namespace WebSocketSharp | |||||||
|  |  | ||||||
|     internal static string Unquote (this string value) |     internal static string Unquote (this string value) | ||||||
|     { |     { | ||||||
|       var start = value.IndexOf ('\"'); |       var start = value.IndexOf ('"'); | ||||||
|       var end = value.LastIndexOf ('\"'); |       var end = value.LastIndexOf ('"'); | ||||||
|       if (start < end) |       if (start < end) | ||||||
|         value = value.Substring (start + 1, end - start - 1).Replace ("\\\"", "\""); |         value = value.Substring (start + 1, end - start - 1).Replace ("\\\"", "\""); | ||||||
|  |  | ||||||
|       return value.Trim (); |       return value.Trim (); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     internal static void WriteBytes (this Stream stream, byte [] value) |     internal static void WriteBytes (this Stream stream, byte[] data) | ||||||
|     { |     { | ||||||
|       using (var src = new MemoryStream (value)) { |       using (var input = new MemoryStream (data)) | ||||||
|         src.CopyTo (stream); |         input.CopyTo (stream); | ||||||
|       } |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     #endregion |     #endregion | ||||||
| @@ -904,7 +925,7 @@ namespace WebSocketSharp | |||||||
|     /// <param name="chars"> |     /// <param name="chars"> | ||||||
|     /// An array of <see cref="char"/> that contains characters to find. |     /// An array of <see cref="char"/> that contains characters to find. | ||||||
|     /// </param> |     /// </param> | ||||||
|     public static bool Contains (this string value, params char [] chars) |     public static bool Contains (this string value, params char[] chars) | ||||||
|     { |     { | ||||||
|       return chars == null || chars.Length == 0 |       return chars == null || chars.Length == 0 | ||||||
|              ? true |              ? true | ||||||
| @@ -931,7 +952,7 @@ namespace WebSocketSharp | |||||||
|     { |     { | ||||||
|       return collection == null || collection.Count == 0 |       return collection == null || collection.Count == 0 | ||||||
|              ? false |              ? false | ||||||
|              : collection [name] != null; |              : collection[name] != null; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /// <summary> |     /// <summary> | ||||||
| @@ -939,9 +960,8 @@ namespace WebSocketSharp | |||||||
|     /// with the specified both <paramref name="name"/> and <paramref name="value"/>. |     /// with the specified both <paramref name="name"/> and <paramref name="value"/>. | ||||||
|     /// </summary> |     /// </summary> | ||||||
|     /// <returns> |     /// <returns> | ||||||
|     /// <c>true</c> if <paramref name="collection"/> contains the entry |     /// <c>true</c> if <paramref name="collection"/> contains the entry with both | ||||||
|     /// with both <paramref name="name"/> and <paramref name="value"/>; |     /// <paramref name="name"/> and <paramref name="value"/>; otherwise, <c>false</c>. | ||||||
|     /// otherwise, <c>false</c>. |  | ||||||
|     /// </returns> |     /// </returns> | ||||||
|     /// <param name="collection"> |     /// <param name="collection"> | ||||||
|     /// A <see cref="NameValueCollection"/> to test. |     /// A <see cref="NameValueCollection"/> to test. | ||||||
| @@ -957,12 +977,12 @@ namespace WebSocketSharp | |||||||
|       if (collection == null || collection.Count == 0) |       if (collection == null || collection.Count == 0) | ||||||
|         return false; |         return false; | ||||||
|  |  | ||||||
|       var values = collection [name]; |       var vals = collection[name]; | ||||||
|       if (values == null) |       if (vals == null) | ||||||
|         return false; |         return false; | ||||||
|  |  | ||||||
|       foreach (var v in values.Split (',')) |       foreach (var val in vals.Split (',')) | ||||||
|         if (v.Trim ().Equals (value, StringComparison.OrdinalIgnoreCase)) |         if (val.Trim ().Equals (value, StringComparison.OrdinalIgnoreCase)) | ||||||
|           return true; |           return true; | ||||||
|  |  | ||||||
|       return false; |       return false; | ||||||
| @@ -1028,7 +1048,7 @@ namespace WebSocketSharp | |||||||
|       var name = response ? "Set-Cookie" : "Cookie"; |       var name = response ? "Set-Cookie" : "Cookie"; | ||||||
|       return headers == null || !headers.Contains (name) |       return headers == null || !headers.Contains (name) | ||||||
|              ? new CookieCollection () |              ? new CookieCollection () | ||||||
|              : CookieCollection.Parse (headers [name], response); |              : CookieCollection.Parse (headers[name], response); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /// <summary> |     /// <summary> | ||||||
| @@ -1038,59 +1058,13 @@ namespace WebSocketSharp | |||||||
|     /// A <see cref="string"/> that represents the description of the HTTP status code. |     /// A <see cref="string"/> that represents the description of the HTTP status code. | ||||||
|     /// </returns> |     /// </returns> | ||||||
|     /// <param name="code"> |     /// <param name="code"> | ||||||
|     /// One of <see cref="HttpStatusCode"/> enum values, indicates the HTTP status codes. |     /// One of <see cref="HttpStatusCode"/> enum values, indicates the HTTP status code. | ||||||
|     /// </param> |     /// </param> | ||||||
|     public static string GetDescription (this HttpStatusCode code) |     public static string GetDescription (this HttpStatusCode code) | ||||||
|     { |     { | ||||||
|       return ((int) code).GetStatusDescription (); |       return ((int) code).GetStatusDescription (); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /// <summary> |  | ||||||
|     /// Gets the name from the specified <see cref="string"/> that contains a pair of name and |  | ||||||
|     /// value separated by a separator string. |  | ||||||
|     /// </summary> |  | ||||||
|     /// <returns> |  | ||||||
|     /// A <see cref="string"/> that represents the name if any; otherwise, <c>null</c>. |  | ||||||
|     /// </returns> |  | ||||||
|     /// <param name="nameAndValue"> |  | ||||||
|     /// A <see cref="string"/> that contains a pair of name and value separated by a separator |  | ||||||
|     /// string. |  | ||||||
|     /// </param> |  | ||||||
|     /// <param name="separator"> |  | ||||||
|     /// A <see cref="string"/> that represents a separator string. |  | ||||||
|     /// </param> |  | ||||||
|     public static string GetName (this string nameAndValue, string separator) |  | ||||||
|     { |  | ||||||
|       return (nameAndValue != null && nameAndValue.Length > 0) && |  | ||||||
|              (separator != null && separator.Length > 0) |  | ||||||
|              ? nameAndValue.GetNameInternal (separator) |  | ||||||
|              : null; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     /// <summary> |  | ||||||
|     /// Gets the name and value from the specified <see cref="string"/> that contains a pair of |  | ||||||
|     /// name and value separated by a separator string. |  | ||||||
|     /// </summary> |  | ||||||
|     /// <returns> |  | ||||||
|     /// A <c>KeyValuePair<string, string></c> that represents the name and value if any. |  | ||||||
|     /// </returns> |  | ||||||
|     /// <param name="nameAndValue"> |  | ||||||
|     /// A <see cref="string"/> that contains a pair of name and value separated by a separator |  | ||||||
|     /// string. |  | ||||||
|     /// </param> |  | ||||||
|     /// <param name="separator"> |  | ||||||
|     /// A <see cref="string"/> that represents a separator string. |  | ||||||
|     /// </param> |  | ||||||
|     public static KeyValuePair<string, string> GetNameAndValue ( |  | ||||||
|       this string nameAndValue, string separator) |  | ||||||
|     { |  | ||||||
|       var name = nameAndValue.GetName (separator); |  | ||||||
|       var value = nameAndValue.GetValue (separator); |  | ||||||
|       return name != null |  | ||||||
|              ? new KeyValuePair<string, string> (name, value) |  | ||||||
|              : new KeyValuePair<string, string> (null, null); |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     /// <summary> |     /// <summary> | ||||||
|     /// Gets the description of the specified HTTP status <paramref name="code"/>. |     /// Gets the description of the specified HTTP status <paramref name="code"/>. | ||||||
|     /// </summary> |     /// </summary> | ||||||
| @@ -1154,34 +1128,12 @@ namespace WebSocketSharp | |||||||
|       return String.Empty; |       return String.Empty; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /// <summary> |  | ||||||
|     /// Gets the value from the specified <see cref="string"/> that contains a pair of name and |  | ||||||
|     /// value separated by a separator string. |  | ||||||
|     /// </summary> |  | ||||||
|     /// <returns> |  | ||||||
|     /// A <see cref="string"/> that represents the value if any; otherwise, <c>null</c>. |  | ||||||
|     /// </returns> |  | ||||||
|     /// <param name="nameAndValue"> |  | ||||||
|     /// A <see cref="string"/> that contains a pair of name and value separated by a separator |  | ||||||
|     /// string. |  | ||||||
|     /// </param> |  | ||||||
|     /// <param name="separator"> |  | ||||||
|     /// A <see cref="string"/> that represents a separator string. |  | ||||||
|     /// </param> |  | ||||||
|     public static string GetValue (this string nameAndValue, string separator) |  | ||||||
|     { |  | ||||||
|       return (nameAndValue != null && nameAndValue.Length > 0) && |  | ||||||
|              (separator != null && separator.Length > 0) |  | ||||||
|              ? nameAndValue.GetValueInternal (separator) |  | ||||||
|              : null; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     /// <summary> |     /// <summary> | ||||||
|     /// Determines whether the specified <see cref="ushort"/> is in the allowable range of |     /// Determines whether the specified <see cref="ushort"/> is in the allowable range of | ||||||
|     /// the WebSocket close status code. |     /// the WebSocket close status code. | ||||||
|     /// </summary> |     /// </summary> | ||||||
|     /// <remarks> |     /// <remarks> | ||||||
|     /// Not allowable ranges are the followings. |     /// Not allowable ranges are the following: | ||||||
|     ///   <list type="bullet"> |     ///   <list type="bullet"> | ||||||
|     ///     <item> |     ///     <item> | ||||||
|     ///       <term> |     ///       <term> | ||||||
| @@ -1225,17 +1177,16 @@ namespace WebSocketSharp | |||||||
|     { |     { | ||||||
|       return value != null && |       return value != null && | ||||||
|              value.Length > 1 && |              value.Length > 1 && | ||||||
|              value [0] == c && |              value[0] == c && | ||||||
|              value [value.Length - 1] == c; |              value[value.Length - 1] == c; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /// <summary> |     /// <summary> | ||||||
|     /// Determines whether the specified <see cref="ByteOrder"/> is host |     /// Determines whether the specified <see cref="ByteOrder"/> is host (this computer | ||||||
|     /// (this computer architecture) byte order. |     /// architecture) byte order. | ||||||
|     /// </summary> |     /// </summary> | ||||||
|     /// <returns> |     /// <returns> | ||||||
|     /// <c>true</c> if <paramref name="order"/> is host byte order; |     /// <c>true</c> if <paramref name="order"/> is host byte order; otherwise, <c>false</c>. | ||||||
|     /// otherwise, <c>false</c>. |  | ||||||
|     /// </returns> |     /// </returns> | ||||||
|     /// <param name="order"> |     /// <param name="order"> | ||||||
|     /// One of the <see cref="ByteOrder"/> enum values, to test. |     /// One of the <see cref="ByteOrder"/> enum values, to test. | ||||||
| @@ -1307,7 +1258,7 @@ namespace WebSocketSharp | |||||||
|       if (value == null || value.Length < 2) |       if (value == null || value.Length < 2) | ||||||
|         return false; |         return false; | ||||||
|  |  | ||||||
|       var c = value [0]; |       var c = value[0]; | ||||||
|       if (c == 'h') |       if (c == 'h') | ||||||
|         return value == "http" || value == "https"; |         return value == "http" || value == "https"; | ||||||
|  |  | ||||||
| @@ -1318,7 +1269,7 @@ namespace WebSocketSharp | |||||||
|         return value == "file" || value == "ftp"; |         return value == "file" || value == "ftp"; | ||||||
|  |  | ||||||
|       if (c == 'n') { |       if (c == 'n') { | ||||||
|         c = value [1]; |         c = value[1]; | ||||||
|         return c == 'e' |         return c == 'e' | ||||||
|                ? value == "news" || value == "net.pipe" || value == "net.tcp" |                ? value == "news" || value == "net.pipe" || value == "net.tcp" | ||||||
|                : value == "nntp"; |                : value == "nntp"; | ||||||
| @@ -1364,7 +1315,7 @@ namespace WebSocketSharp | |||||||
|         throw new ArgumentNullException ("protocol"); |         throw new ArgumentNullException ("protocol"); | ||||||
|  |  | ||||||
|       if (protocol.Length == 0) |       if (protocol.Length == 0) | ||||||
|         throw new ArgumentException ("Must not be empty.", "protocol"); |         throw new ArgumentException ("Empty string.", "protocol"); | ||||||
|  |  | ||||||
|       return request.Headers.Contains ("Upgrade", protocol) && |       return request.Headers.Contains ("Upgrade", protocol) && | ||||||
|              request.Headers.Contains ("Connection", "Upgrade"); |              request.Headers.Contains ("Connection", "Upgrade"); | ||||||
| @@ -1415,21 +1366,21 @@ namespace WebSocketSharp | |||||||
|     /// <typeparam name="T"> |     /// <typeparam name="T"> | ||||||
|     /// The type of elements in the <paramref name="array"/>. |     /// The type of elements in the <paramref name="array"/>. | ||||||
|     /// </typeparam> |     /// </typeparam> | ||||||
|     public static T [] SubArray<T> (this T [] array, int startIndex, int length) |     public static T[] SubArray<T> (this T[] array, int startIndex, int length) | ||||||
|     { |     { | ||||||
|       if (array == null || array.Length == 0) |       if (array == null || array.Length == 0) | ||||||
|         return new T [0]; |         return new T[0]; | ||||||
|  |  | ||||||
|       if (startIndex < 0 || length <= 0) |       if (startIndex < 0 || length <= 0) | ||||||
|         return new T [0]; |         return new T[0]; | ||||||
|  |  | ||||||
|       if (startIndex + length > array.Length) |       if (startIndex + length > array.Length) | ||||||
|         return new T [0]; |         return new T[0]; | ||||||
|  |  | ||||||
|       if (startIndex == 0 && array.Length == length) |       if (startIndex == 0 && array.Length == length) | ||||||
|         return array; |         return array; | ||||||
|  |  | ||||||
|       T [] subArray = new T [length]; |       var subArray = new T[length]; | ||||||
|       Array.Copy (array, startIndex, subArray, 0, length); |       Array.Copy (array, startIndex, subArray, 0, length); | ||||||
|  |  | ||||||
|       return subArray; |       return subArray; | ||||||
| @@ -1590,7 +1541,7 @@ namespace WebSocketSharp | |||||||
|     /// <exception cref="ArgumentNullException"> |     /// <exception cref="ArgumentNullException"> | ||||||
|     /// <paramref name="src"/> is <see langword="null"/>. |     /// <paramref name="src"/> is <see langword="null"/>. | ||||||
|     /// </exception> |     /// </exception> | ||||||
|     public static T To<T> (this byte [] src, ByteOrder srcOrder) |     public static T To<T> (this byte[] src, ByteOrder srcOrder) | ||||||
|       where T : struct |       where T : struct | ||||||
|     { |     { | ||||||
|       if (src == null) |       if (src == null) | ||||||
| @@ -1600,28 +1551,28 @@ namespace WebSocketSharp | |||||||
|         return default (T); |         return default (T); | ||||||
|  |  | ||||||
|       var type = typeof (T); |       var type = typeof (T); | ||||||
|       var buffer = src.ToHostOrder (srcOrder); |       var buff = src.ToHostOrder (srcOrder); | ||||||
|  |  | ||||||
|       return type == typeof (Boolean) |       return type == typeof (Boolean) | ||||||
|              ? (T)(object) BitConverter.ToBoolean (buffer, 0) |              ? (T)(object) BitConverter.ToBoolean (buff, 0) | ||||||
|              : type == typeof (Char) |              : type == typeof (Char) | ||||||
|                ? (T)(object) BitConverter.ToChar (buffer, 0) |                ? (T)(object) BitConverter.ToChar (buff, 0) | ||||||
|                : type == typeof (Double) |                : type == typeof (Double) | ||||||
|                  ? (T)(object) BitConverter.ToDouble (buffer, 0) |                  ? (T)(object) BitConverter.ToDouble (buff, 0) | ||||||
|                  : type == typeof (Int16) |                  : type == typeof (Int16) | ||||||
|                    ? (T)(object) BitConverter.ToInt16 (buffer, 0) |                    ? (T)(object) BitConverter.ToInt16 (buff, 0) | ||||||
|                    : type == typeof (Int32) |                    : type == typeof (Int32) | ||||||
|                      ? (T)(object) BitConverter.ToInt32 (buffer, 0) |                      ? (T)(object) BitConverter.ToInt32 (buff, 0) | ||||||
|                      : type == typeof (Int64) |                      : type == typeof (Int64) | ||||||
|                        ? (T)(object) BitConverter.ToInt64 (buffer, 0) |                        ? (T)(object) BitConverter.ToInt64 (buff, 0) | ||||||
|                        : type == typeof (Single) |                        : type == typeof (Single) | ||||||
|                          ? (T)(object) BitConverter.ToSingle (buffer, 0) |                          ? (T)(object) BitConverter.ToSingle (buff, 0) | ||||||
|                          : type == typeof (UInt16) |                          : type == typeof (UInt16) | ||||||
|                            ? (T)(object) BitConverter.ToUInt16 (buffer, 0) |                            ? (T)(object) BitConverter.ToUInt16 (buff, 0) | ||||||
|                            : type == typeof (UInt32) |                            : type == typeof (UInt32) | ||||||
|                              ? (T)(object) BitConverter.ToUInt32 (buffer, 0) |                              ? (T)(object) BitConverter.ToUInt32 (buff, 0) | ||||||
|                              : type == typeof (UInt64) |                              : type == typeof (UInt64) | ||||||
|                                ? (T)(object) BitConverter.ToUInt64 (buffer, 0) |                                ? (T)(object) BitConverter.ToUInt64 (buff, 0) | ||||||
|                                : default (T); |                                : default (T); | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @@ -1640,14 +1591,14 @@ namespace WebSocketSharp | |||||||
|     /// <typeparam name="T"> |     /// <typeparam name="T"> | ||||||
|     /// The type of <paramref name="value"/>. The T must be a value type. |     /// The type of <paramref name="value"/>. The T must be a value type. | ||||||
|     /// </typeparam> |     /// </typeparam> | ||||||
|     public static byte [] ToByteArray<T> (this T value, ByteOrder order) |     public static byte[] ToByteArray<T> (this T value, ByteOrder order) | ||||||
|       where T : struct |       where T : struct | ||||||
|     { |     { | ||||||
|       var type = typeof (T); |       var type = typeof (T); | ||||||
|       var bytes = type == typeof (Boolean) |       var bytes = type == typeof (Boolean) | ||||||
|                   ? BitConverter.GetBytes ((Boolean)(object) value) |                   ? BitConverter.GetBytes ((Boolean)(object) value) | ||||||
|                   : type == typeof (Byte) |                   : type == typeof (Byte) | ||||||
|                   ? new byte [] { (Byte)(object) value } |                     ? new byte[] { (Byte)(object) value } | ||||||
|                     : type == typeof (Char) |                     : type == typeof (Char) | ||||||
|                       ? BitConverter.GetBytes ((Char)(object) value) |                       ? BitConverter.GetBytes ((Char)(object) value) | ||||||
|                       : type == typeof (Double) |                       : type == typeof (Double) | ||||||
| @@ -1666,7 +1617,7 @@ namespace WebSocketSharp | |||||||
|                                     ? BitConverter.GetBytes ((UInt32)(object) value) |                                     ? BitConverter.GetBytes ((UInt32)(object) value) | ||||||
|                                     : type == typeof (UInt64) |                                     : type == typeof (UInt64) | ||||||
|                                       ? BitConverter.GetBytes ((UInt64)(object) value) |                                       ? BitConverter.GetBytes ((UInt64)(object) value) | ||||||
|                                     : new byte [0]; |                                       : new byte[0]; | ||||||
|  |  | ||||||
|       if (bytes.Length > 1 && !order.IsHostOrder ()) |       if (bytes.Length > 1 && !order.IsHostOrder ()) | ||||||
|         Array.Reverse (bytes); |         Array.Reverse (bytes); | ||||||
| @@ -1690,7 +1641,7 @@ namespace WebSocketSharp | |||||||
|     /// <exception cref="ArgumentNullException"> |     /// <exception cref="ArgumentNullException"> | ||||||
|     /// <paramref name="src"/> is <see langword="null"/>. |     /// <paramref name="src"/> is <see langword="null"/>. | ||||||
|     /// </exception> |     /// </exception> | ||||||
|     public static byte [] ToHostOrder (this byte [] src, ByteOrder srcOrder) |     public static byte[] ToHostOrder (this byte[] src, ByteOrder srcOrder) | ||||||
|     { |     { | ||||||
|       if (src == null) |       if (src == null) | ||||||
|         throw new ArgumentNullException ("src"); |         throw new ArgumentNullException ("src"); | ||||||
| @@ -1721,7 +1672,7 @@ namespace WebSocketSharp | |||||||
|     /// <exception cref="ArgumentNullException"> |     /// <exception cref="ArgumentNullException"> | ||||||
|     /// <paramref name="array"/> is <see langword="null"/>. |     /// <paramref name="array"/> is <see langword="null"/>. | ||||||
|     /// </exception> |     /// </exception> | ||||||
|     public static string ToString<T> (this T [] array, string separator) |     public static string ToString<T> (this T[] array, string separator) | ||||||
|     { |     { | ||||||
|       if (array == null) |       if (array == null) | ||||||
|         throw new ArgumentNullException ("array"); |         throw new ArgumentNullException ("array"); | ||||||
| @@ -1733,11 +1684,11 @@ namespace WebSocketSharp | |||||||
|       if (separator == null) |       if (separator == null) | ||||||
|         separator = String.Empty; |         separator = String.Empty; | ||||||
|  |  | ||||||
|       var buffer = new StringBuilder (64); |       var buff = new StringBuilder (64); | ||||||
|       (len - 1).Times (i => buffer.AppendFormat ("{0}{1}", array [i].ToString (), separator)); |       (len - 1).Times (i => buff.AppendFormat ("{0}{1}", array[i].ToString (), separator)); | ||||||
|  |  | ||||||
|       buffer.Append (array [len - 1].ToString ()); |       buff.Append (array[len - 1].ToString ()); | ||||||
|       return buffer.ToString (); |       return buff.ToString (); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /// <summary> |     /// <summary> | ||||||
| @@ -1807,17 +1758,18 @@ namespace WebSocketSharp | |||||||
|     /// <exception cref="ArgumentNullException"> |     /// <exception cref="ArgumentNullException"> | ||||||
|     /// <paramref name="response"/> is <see langword="null"/>. |     /// <paramref name="response"/> is <see langword="null"/>. | ||||||
|     /// </exception> |     /// </exception> | ||||||
|     public static void WriteContent (this HttpListenerResponse response, byte [] content) |     public static void WriteContent (this HttpListenerResponse response, byte[] content) | ||||||
|     { |     { | ||||||
|       if (response == null) |       if (response == null) | ||||||
|         throw new ArgumentNullException ("response"); |         throw new ArgumentNullException ("response"); | ||||||
|  |  | ||||||
|       if (content == null || content.Length == 0) |       var len = 0; | ||||||
|  |       if (content == null || (len = content.Length) == 0) | ||||||
|         return; |         return; | ||||||
|  |  | ||||||
|       var output = response.OutputStream; |       var output = response.OutputStream; | ||||||
|       response.ContentLength64 = content.Length; |       response.ContentLength64 = len; | ||||||
|       output.Write (content, 0, content.Length); |       output.Write (content, 0, len); | ||||||
|       output.Close (); |       output.Close (); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -227,20 +227,20 @@ namespace WebSocketSharp.Net | |||||||
|           continue; |           continue; | ||||||
|  |  | ||||||
|         if (pair.StartsWith ("$version", StringComparison.InvariantCultureIgnoreCase)) { |         if (pair.StartsWith ("$version", StringComparison.InvariantCultureIgnoreCase)) { | ||||||
|           version = Int32.Parse (pair.GetValueInternal ("=").Trim ('"')); |           version = 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) | ||||||
|             cookie.Path = pair.GetValueInternal ("="); |             cookie.Path = pair.GetValue ('='); | ||||||
|         } |         } | ||||||
|         else if (pair.StartsWith ("$domain", StringComparison.InvariantCultureIgnoreCase)) { |         else if (pair.StartsWith ("$domain", StringComparison.InvariantCultureIgnoreCase)) { | ||||||
|           if (cookie != null) |           if (cookie != null) | ||||||
|             cookie.Domain = pair.GetValueInternal ("="); |             cookie.Domain = pair.GetValue ('='); | ||||||
|         } |         } | ||||||
|         else if (pair.StartsWith ("$port", StringComparison.InvariantCultureIgnoreCase)) { |         else if (pair.StartsWith ("$port", StringComparison.InvariantCultureIgnoreCase)) { | ||||||
|           var port = pair.Equals ("$port", StringComparison.InvariantCultureIgnoreCase) |           var port = pair.Equals ("$port", StringComparison.InvariantCultureIgnoreCase) | ||||||
|                      ? "\"\"" |                      ? "\"\"" | ||||||
|                      : pair.GetValueInternal ("="); |                      : pair.GetValue ('='); | ||||||
|  |  | ||||||
|           if (cookie != null) |           if (cookie != null) | ||||||
|             cookie.Port = port; |             cookie.Port = port; | ||||||
| @@ -289,10 +289,10 @@ namespace WebSocketSharp.Net | |||||||
|  |  | ||||||
|         if (pair.StartsWith ("version", StringComparison.InvariantCultureIgnoreCase)) { |         if (pair.StartsWith ("version", StringComparison.InvariantCultureIgnoreCase)) { | ||||||
|           if (cookie != null) |           if (cookie != null) | ||||||
|             cookie.Version = Int32.Parse (pair.GetValueInternal ("=").Trim ('"')); |             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.GetValueInternal ("="), 32); |           var buffer = new StringBuilder (pair.GetValue ('='), 32); | ||||||
|           if (i < pairs.Length - 1) |           if (i < pairs.Length - 1) | ||||||
|             buffer.AppendFormat (", {0}", pairs [++i].Trim ()); |             buffer.AppendFormat (", {0}", pairs [++i].Trim ()); | ||||||
|  |  | ||||||
| @@ -309,34 +309,34 @@ namespace WebSocketSharp.Net | |||||||
|             cookie.Expires = expires.ToLocalTime (); |             cookie.Expires = expires.ToLocalTime (); | ||||||
|         } |         } | ||||||
|         else if (pair.StartsWith ("max-age", StringComparison.InvariantCultureIgnoreCase)) { |         else if (pair.StartsWith ("max-age", StringComparison.InvariantCultureIgnoreCase)) { | ||||||
|           var max = Int32.Parse (pair.GetValueInternal ("=").Trim ('"')); |           var max = Int32.Parse (pair.GetValue ('=', true)); | ||||||
|           var expires = DateTime.Now.AddSeconds ((double) max); |           var expires = DateTime.Now.AddSeconds ((double) max); | ||||||
|           if (cookie != null) |           if (cookie != null) | ||||||
|             cookie.Expires = expires; |             cookie.Expires = expires; | ||||||
|         } |         } | ||||||
|         else if (pair.StartsWith ("path", StringComparison.InvariantCultureIgnoreCase)) { |         else if (pair.StartsWith ("path", StringComparison.InvariantCultureIgnoreCase)) { | ||||||
|           if (cookie != null) |           if (cookie != null) | ||||||
|             cookie.Path = pair.GetValueInternal ("="); |             cookie.Path = pair.GetValue ('='); | ||||||
|         } |         } | ||||||
|         else if (pair.StartsWith ("domain", StringComparison.InvariantCultureIgnoreCase)) { |         else if (pair.StartsWith ("domain", StringComparison.InvariantCultureIgnoreCase)) { | ||||||
|           if (cookie != null) |           if (cookie != null) | ||||||
|             cookie.Domain = pair.GetValueInternal ("="); |             cookie.Domain = pair.GetValue ('='); | ||||||
|         } |         } | ||||||
|         else if (pair.StartsWith ("port", StringComparison.InvariantCultureIgnoreCase)) { |         else if (pair.StartsWith ("port", StringComparison.InvariantCultureIgnoreCase)) { | ||||||
|           var port = pair.Equals ("port", StringComparison.InvariantCultureIgnoreCase) |           var port = pair.Equals ("port", StringComparison.InvariantCultureIgnoreCase) | ||||||
|                      ? "\"\"" |                      ? "\"\"" | ||||||
|                      : pair.GetValueInternal ("="); |                      : pair.GetValue ('='); | ||||||
|  |  | ||||||
|           if (cookie != null) |           if (cookie != null) | ||||||
|             cookie.Port = port; |             cookie.Port = port; | ||||||
|         } |         } | ||||||
|         else if (pair.StartsWith ("comment", StringComparison.InvariantCultureIgnoreCase)) { |         else if (pair.StartsWith ("comment", StringComparison.InvariantCultureIgnoreCase)) { | ||||||
|           if (cookie != null) |           if (cookie != null) | ||||||
|             cookie.Comment = pair.GetValueInternal ("=").UrlDecode (); |             cookie.Comment = pair.GetValue ('=').UrlDecode (); | ||||||
|         } |         } | ||||||
|         else if (pair.StartsWith ("commenturl", StringComparison.InvariantCultureIgnoreCase)) { |         else if (pair.StartsWith ("commenturl", StringComparison.InvariantCultureIgnoreCase)) { | ||||||
|           if (cookie != null) |           if (cookie != null) | ||||||
|             cookie.CommentUri = pair.GetValueInternal ("=").Trim ('"').ToUri (); |             cookie.CommentUri = pair.GetValue ('=', true).ToUri (); | ||||||
|         } |         } | ||||||
|         else if (pair.StartsWith ("discard", StringComparison.InvariantCultureIgnoreCase)) { |         else if (pair.StartsWith ("discard", StringComparison.InvariantCultureIgnoreCase)) { | ||||||
|           if (cookie != null) |           if (cookie != null) | ||||||
|   | |||||||
| @@ -522,7 +522,7 @@ namespace WebSocketSharp.Net | |||||||
|         foreach (var p in parts) { |         foreach (var p in parts) { | ||||||
|           var part = p.Trim (); |           var part = p.Trim (); | ||||||
|           if (part.StartsWith ("charset", StringComparison.OrdinalIgnoreCase)) { |           if (part.StartsWith ("charset", StringComparison.OrdinalIgnoreCase)) { | ||||||
|             var charset = part.GetValue ("=", true); |             var charset = part.GetValue ('=', true); | ||||||
|             if (charset != null && charset.Length > 0) { |             if (charset != null && charset.Length > 0) { | ||||||
|               try { |               try { | ||||||
|                 _contentEncoding = Encoding.GetEncoding (charset); |                 _contentEncoding = Encoding.GetEncoding (charset); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user