diff --git a/websocket-sharp/Ext.cs b/websocket-sharp/Ext.cs index a2abd501..71170945 100644 --- a/websocket-sharp/Ext.cs +++ b/websocket-sharp/Ext.cs @@ -114,7 +114,9 @@ namespace WebSocketSharp stream.Position = 0; using (var ds = new DeflateStream (stream, CompressionMode.Decompress, true)) { - ds.CopyTo (output, true); + ds.CopyTo (output); + output.Position = 0; + return output; } } @@ -133,13 +135,12 @@ namespace WebSocketSharp if (len < 1) return buffer.SubArray (0, offset); - var tmp = 0; while (len < length) { - tmp = stream.Read (buffer, offset + len, length - len); - if (tmp < 1) + var readLen = stream.Read (buffer, offset + len, length - len); + if (readLen < 1) break; - len += tmp; + len += readLen; } return len < length @@ -148,19 +149,19 @@ namespace WebSocketSharp } 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 destination) { var bytes = stream.readBytes (buffer, offset, length); var len = bytes.Length; - dest.Write (bytes, 0, len); + destination.Write (bytes, 0, len); return len == offset + length; } - private static void times (this ulong n, Action act) + private static void times (this ulong n, Action action) { for (ulong i = 0; i < n; i++) - act (); + action (); } #endregion @@ -170,11 +171,11 @@ namespace WebSocketSharp internal static byte[] Append (this ushort code, string reason) { using (var buff = new MemoryStream ()) { - var tmp = code.ToByteArrayInternally (ByteOrder.Big); - buff.Write (tmp, 0, 2); + var bytes = code.InternalToByteArray (ByteOrder.Big); + buff.Write (bytes, 0, 2); if (reason != null && reason.Length > 0) { - tmp = Encoding.UTF8.GetBytes (reason); - buff.Write (tmp, 0, tmp.Length); + bytes = Encoding.UTF8.GetBytes (reason); + buff.Write (bytes, 0, bytes.Length); } buff.Close (); @@ -283,14 +284,14 @@ namespace WebSocketSharp : null; } - internal static string CheckIfValidServicePath (this string servicePath) + internal static string CheckIfValidServicePath (this string path) { - return servicePath == null || servicePath.Length == 0 - ? "'servicePath' is null or empty." - : servicePath[0] != '/' - ? "'servicePath' isn't absolute path." - : servicePath.IndexOfAny (new[] {'?', '#'}) != -1 - ? "'servicePath' contains either or both query and fragment components." + return path == null || path.Length == 0 + ? "'path' is null or empty." + : path[0] != '/' + ? "'path' isn't absolute path." + : path.IndexOfAny (new[] { '?', '#' }) > -1 + ? "'path' contains either or both query and fragment components." : null; } @@ -364,29 +365,21 @@ namespace WebSocketSharp return contains (0); } - internal static T[] Copy (this T[] src, long length) + internal static T[] Copy (this T[] source, long length) { var dest = new T[length]; - Array.Copy (src, 0, dest, 0, length); + Array.Copy (source, 0, dest, 0, length); return dest; } - internal static void CopyTo (this Stream src, Stream dest) + internal static void CopyTo (this Stream source, Stream destination) { - src.CopyTo (dest, false); - } - - internal static void CopyTo (this Stream src, Stream dest, bool setDefaultPosition) - { - var readLen = 0; var buffLen = 256; var buff = new byte[buffLen]; - while ((readLen = src.Read (buff, 0, buffLen)) > 0) - dest.Write (buff, 0, readLen); - - if (setDefaultPosition) - dest.Position = 0; + var readLen = 0; + while ((readLen = source.Read (buff, 0, buffLen)) > 0) + destination.Write (buff, 0, readLen); } internal static byte[] Decompress (this byte[] data, CompressionMethod method) @@ -460,7 +453,7 @@ namespace WebSocketSharp if (original[0] != '/') return null; - var i = original.IndexOfAny (new[] {'?', '#'}); + var i = original.IndexOfAny (new[] { '?', '#' }); return i > 0 ? original.Substring (0, i) : original; @@ -540,15 +533,37 @@ namespace WebSocketSharp return null; var val = nameAndValue.Substring (i + 1).Trim (); - return unquote && val.Length > 1 + return unquote ? val.Unquote () : val; } internal static TcpListenerWebSocketContext GetWebSocketContext ( - this TcpClient client, string protocol, bool secure, X509Certificate cert, Logger logger) + this TcpClient tcpClient, + string protocol, + bool secure, + X509Certificate certificate, + Logger logger) { - return new TcpListenerWebSocketContext (client, protocol, secure, cert, logger); + return new TcpListenerWebSocketContext (tcpClient, protocol, secure, certificate, logger); + } + + internal static byte[] InternalToByteArray (this ushort value, ByteOrder order) + { + var bytes = BitConverter.GetBytes (value); + if (!order.IsHostOrder ()) + Array.Reverse (bytes); + + return bytes; + } + + internal static byte[] InternalToByteArray (this ulong value, ByteOrder order) + { + var bytes = BitConverter.GetBytes (value); + if (!order.IsHostOrder ()) + Array.Reverse (bytes); + + return bytes; } internal static bool IsCompressionExtension (this string value) @@ -581,7 +596,7 @@ namespace WebSocketSharp { var len = value.Length; for (var i = 0; i < len; i++) { - char c = value[i]; + var c = value[i]; if (c < 0x20 && !"\r\n\t".Contains (c)) return false; @@ -600,7 +615,7 @@ namespace WebSocketSharp internal static bool IsToken (this string value) { - foreach (char c in value) + foreach (var c in value) if (c < 0x20 || c >= 0x7f || _tspecials.Contains (c)) return false; @@ -696,18 +711,17 @@ namespace WebSocketSharp } internal static IEnumerable SplitHeaderValue ( - this string value, params char[] separator) + this string value, params char[] separators) { var len = value.Length; - var separators = new string (separator); + var seps = new string (separators); var buff = new StringBuilder (32); - var quoted = false; var escaped = false; + var quoted = false; - char c; for (var i = 0; i < len; i++) { - c = value[i]; + var c = value[i]; if (c == '"') { if (escaped) escaped = !escaped; @@ -718,7 +732,7 @@ namespace WebSocketSharp if (i < len - 1 && value[i + 1] == '"') escaped = true; } - else if (separators.Contains (c)) { + else if (seps.Contains (c)) { if (!quoted) { yield return buff.ToString (); buff.Length = 0; @@ -747,24 +761,6 @@ namespace WebSocketSharp } } - internal static byte[] ToByteArrayInternally (this ushort value, ByteOrder order) - { - var bytes = BitConverter.GetBytes (value); - if (!order.IsHostOrder ()) - Array.Reverse (bytes); - - return bytes; - } - - internal static byte[] ToByteArrayInternally (this ulong value, ByteOrder order) - { - var bytes = BitConverter.GetBytes (value); - if (!order.IsHostOrder ()) - Array.Reverse (bytes); - - return bytes; - } - internal static CompressionMethod ToCompressionMethod (this string value) { foreach (CompressionMethod method in Enum.GetValues (typeof (CompressionMethod))) @@ -797,14 +793,14 @@ namespace WebSocketSharp return new List (source); } - internal static ushort ToUInt16 (this byte[] src, ByteOrder srcOrder) + internal static ushort ToUInt16 (this byte[] source, ByteOrder sourceOrder) { - return BitConverter.ToUInt16 (src.ToHostOrder (srcOrder), 0); + return BitConverter.ToUInt16 (source.ToHostOrder (sourceOrder), 0); } - internal static ulong ToUInt64 (this byte[] src, ByteOrder srcOrder) + internal static ulong ToUInt64 (this byte[] source, ByteOrder sourceOrder) { - return BitConverter.ToUInt64 (src.ToHostOrder (srcOrder), 0); + return BitConverter.ToUInt64 (source.ToHostOrder (sourceOrder), 0); } internal static string TrimEndSlash (this string value) @@ -886,6 +882,9 @@ namespace WebSocketSharp internal static string Unquote (this string value) { var start = value.IndexOf ('"'); + if (start < 0) + return value; + var end = value.LastIndexOf ('"'); var len = end - start - 1; @@ -896,9 +895,9 @@ namespace WebSocketSharp : value.Substring (start + 1, len).Replace ("\\\"", "\""); } - internal static void WriteBytes (this Stream stream, byte[] data) + internal static void WriteBytes (this Stream stream, byte[] bytes) { - using (var input = new MemoryStream (data)) + using (var input = new MemoryStream (bytes)) input.CopyTo (stream); } @@ -926,7 +925,7 @@ namespace WebSocketSharp ? true : value == null || value.Length == 0 ? false - : value.IndexOfAny (chars) != -1; + : value.IndexOfAny (chars) > -1; } /// @@ -945,9 +944,9 @@ namespace WebSocketSharp /// public static bool Contains (this NameValueCollection collection, string name) { - return collection == null || collection.Count == 0 - ? false - : collection[name] != null; + return collection != null && collection.Count > 0 + ? collection[name] != null + : false; } /// @@ -1041,9 +1040,9 @@ namespace WebSocketSharp public static CookieCollection GetCookies (this NameValueCollection headers, bool response) { var name = response ? "Set-Cookie" : "Cookie"; - return headers == null || !headers.Contains (name) - ? new CookieCollection () - : CookieCollection.Parse (headers[name], response); + return headers != null && headers.Contains (name) + ? CookieCollection.Parse (headers[name], response) + : new CookieCollection (); } /// @@ -1387,13 +1386,13 @@ namespace WebSocketSharp /// /// An is the number of times to execute. /// - /// + /// /// An delegate that references the method(s) to execute. /// - public static void Times (this int n, Action act) + public static void Times (this int n, Action action) { - if (n > 0 && act != null) - ((ulong) n).times (act); + if (n > 0 && action != null) + ((ulong) n).times (action); } /// @@ -1402,13 +1401,13 @@ namespace WebSocketSharp /// /// A is the number of times to execute. /// - /// + /// /// An delegate that references the method(s) to execute. /// - public static void Times (this long n, Action act) + public static void Times (this long n, Action action) { - if (n > 0 && act != null) - ((ulong) n).times (act); + if (n > 0 && action != null) + ((ulong) n).times (action); } /// @@ -1417,13 +1416,13 @@ namespace WebSocketSharp /// /// A is the number of times to execute. /// - /// + /// /// An delegate that references the method(s) to execute. /// - public static void Times (this uint n, Action act) + public static void Times (this uint n, Action action) { - if (n > 0 && act != null) - ((ulong) n).times (act); + if (n > 0 && action != null) + ((ulong) n).times (action); } /// @@ -1432,13 +1431,13 @@ namespace WebSocketSharp /// /// A is the number of times to execute. /// - /// + /// /// An delegate that references the method(s) to execute. /// - public static void Times (this ulong n, Action act) + public static void Times (this ulong n, Action action) { - if (n > 0 && act != null) - n.times (act); + if (n > 0 && action != null) + n.times (action); } /// @@ -1447,16 +1446,16 @@ namespace WebSocketSharp /// /// An is the number of times to execute. /// - /// + /// /// An Action<int> delegate that references the method(s) to execute. /// An parameter to pass to the method(s) is the zero-based count /// of iteration. /// - public static void Times (this int n, Action act) + public static void Times (this int n, Action action) { - if (n > 0 && act != null) + if (n > 0 && action != null) for (int i = 0; i < n; i++) - act (i); + action (i); } /// @@ -1465,16 +1464,16 @@ namespace WebSocketSharp /// /// A is the number of times to execute. /// - /// + /// /// An Action<long> delegate that references the method(s) to execute. /// A parameter to pass to the method(s) is the zero-based count /// of iteration. /// - public static void Times (this long n, Action act) + public static void Times (this long n, Action action) { - if (n > 0 && act != null) + if (n > 0 && action != null) for (long i = 0; i < n; i++) - act (i); + action (i); } /// @@ -1483,16 +1482,16 @@ namespace WebSocketSharp /// /// A is the number of times to execute. /// - /// + /// /// An Action<uint> delegate that references the method(s) to execute. /// A parameter to pass to the method(s) is the zero-based count /// of iteration. /// - public static void Times (this uint n, Action act) + public static void Times (this uint n, Action action) { - if (n > 0 && act != null) + if (n > 0 && action != null) for (uint i = 0; i < n; i++) - act (i); + action (i); } /// @@ -1501,52 +1500,52 @@ namespace WebSocketSharp /// /// A is the number of times to execute. /// - /// + /// /// An Action<ulong> delegate that references the method(s) to execute. /// A parameter to pass to this method(s) is the zero-based count /// of iteration. /// - public static void Times (this ulong n, Action act) + public static void Times (this ulong n, Action action) { - if (n > 0 && act != null) + if (n > 0 && action != null) for (ulong i = 0; i < n; i++) - act (i); + action (i); } /// /// Converts the specified array of to the specified type data. /// /// - /// A T converted from , or a default value of T if - /// is an empty array of or if the + /// A T converted from , or a default value of T if + /// is an empty array of or if the /// type of T isn't , , , /// , , , , /// , , or . /// - /// + /// /// An array of to convert. /// - /// + /// /// One of the enum values, indicates the byte order of - /// . + /// . /// /// /// The type of the return. The T must be a value type. /// /// - /// is . + /// is . /// - public static T To (this byte[] src, ByteOrder srcOrder) + public static T To (this byte[] source, ByteOrder sourceOrder) where T : struct { - if (src == null) - throw new ArgumentNullException ("src"); + if (source == null) + throw new ArgumentNullException ("source"); - if (src.Length == 0) + if (source.Length == 0) return default (T); var type = typeof (T); - var buff = src.ToHostOrder (srcOrder); + var buff = source.ToHostOrder (sourceOrder); return type == typeof (Boolean) ? (T)(object) BitConverter.ToBoolean (buff, 0) @@ -1624,26 +1623,26 @@ namespace WebSocketSharp /// Converts the order of the specified array of to the host byte order. /// /// - /// An array of converted from . + /// An array of converted from . /// - /// + /// /// An array of to convert. /// - /// + /// /// One of the enum values, indicates the byte order of - /// . + /// . /// /// - /// is . + /// is . /// - public static byte[] ToHostOrder (this byte[] src, ByteOrder srcOrder) + public static byte[] ToHostOrder (this byte[] source, ByteOrder sourceOrder) { - if (src == null) - throw new ArgumentNullException ("src"); + if (source == null) + throw new ArgumentNullException ("source"); - return src.Length > 1 && !srcOrder.IsHostOrder () - ? src.Reverse () - : src; + return source.Length > 1 && !sourceOrder.IsHostOrder () + ? source.Reverse () + : source; } /// @@ -1717,9 +1716,9 @@ namespace WebSocketSharp /// public static string UrlDecode (this string value) { - return value == null || value.Length == 0 - ? value - : HttpUtility.UrlDecode (value); + return value != null && value.Length > 0 + ? HttpUtility.UrlDecode (value) + : value; } /// @@ -1734,9 +1733,9 @@ namespace WebSocketSharp /// public static string UrlEncode (this string value) { - return value == null || value.Length == 0 - ? value - : HttpUtility.UrlEncode (value); + return value != null && value.Length > 0 + ? HttpUtility.UrlEncode (value) + : value; } /// diff --git a/websocket-sharp/Server/HttpServer.cs b/websocket-sharp/Server/HttpServer.cs index 8edfb596..4f1184d6 100644 --- a/websocket-sharp/Server/HttpServer.cs +++ b/websocket-sharp/Server/HttpServer.cs @@ -410,7 +410,7 @@ namespace WebSocketSharp.Server } _services.Stop ( - ((ushort) CloseStatusCode.ServerError).ToByteArrayInternally (ByteOrder.Big), true); + ((ushort) CloseStatusCode.ServerError).InternalToByteArray (ByteOrder.Big), true); _listener.Abort (); _state = ServerState.Stop; diff --git a/websocket-sharp/Server/WebSocketServer.cs b/websocket-sharp/Server/WebSocketServer.cs index d8c84555..ee22f9ea 100644 --- a/websocket-sharp/Server/WebSocketServer.cs +++ b/websocket-sharp/Server/WebSocketServer.cs @@ -466,7 +466,7 @@ namespace WebSocketSharp.Server _listener.Stop (); _services.Stop ( - ((ushort) CloseStatusCode.ServerError).ToByteArrayInternally (ByteOrder.Big), true); + ((ushort) CloseStatusCode.ServerError).InternalToByteArray (ByteOrder.Big), true); _state = ServerState.Stop; } diff --git a/websocket-sharp/Server/WebSocketServiceManager.cs b/websocket-sharp/Server/WebSocketServiceManager.cs index 5cf70f93..73c587ea 100644 --- a/websocket-sharp/Server/WebSocketServiceManager.cs +++ b/websocket-sharp/Server/WebSocketServiceManager.cs @@ -295,7 +295,7 @@ namespace WebSocketSharp.Server if (host.Sessions.State == ServerState.Start) host.Sessions.Stop ( - ((ushort) CloseStatusCode.Away).ToByteArrayInternally (ByteOrder.Big), true); + ((ushort) CloseStatusCode.Away).InternalToByteArray (ByteOrder.Big), true); return true; } diff --git a/websocket-sharp/WebSocketFrame.cs b/websocket-sharp/WebSocketFrame.cs index 58ce15f1..31a38b4f 100644 --- a/websocket-sharp/WebSocketFrame.cs +++ b/websocket-sharp/WebSocketFrame.cs @@ -108,11 +108,11 @@ namespace WebSocketSharp } else if (len < 0x010000) { _payloadLength = (byte) 126; - _extPayloadLength = ((ushort) len).ToByteArrayInternally (ByteOrder.Big); + _extPayloadLength = ((ushort) len).InternalToByteArray (ByteOrder.Big); } else { _payloadLength = (byte) 127; - _extPayloadLength = len.ToByteArrayInternally (ByteOrder.Big); + _extPayloadLength = len.InternalToByteArray (ByteOrder.Big); } if (mask == Mask.Mask) { @@ -640,7 +640,7 @@ Extended Payload Length: {7} header = (header << 4) + (int) _opcode; header = (header << 1) + (int) _mask; header = (header << 7) + (int) _payloadLength; - buff.Write (((ushort) header).ToByteArrayInternally (ByteOrder.Big), 0, 2); + buff.Write (((ushort) header).InternalToByteArray (ByteOrder.Big), 0, 2); if (_payloadLength > 125) buff.Write (_extPayloadLength, 0, _extPayloadLength.Length);