diff --git a/Example/Example.pidb b/Example/Example.pidb index dfdd464d..7fe02fa9 100644 Binary files a/Example/Example.pidb and b/Example/Example.pidb differ diff --git a/Example/bin/Debug/example.exe b/Example/bin/Debug/example.exe index 1c6eac38..e2cec72d 100755 Binary files a/Example/bin/Debug/example.exe and b/Example/bin/Debug/example.exe differ diff --git a/Example/bin/Debug/example.exe.mdb b/Example/bin/Debug/example.exe.mdb index 53190d84..88f9473f 100644 Binary files a/Example/bin/Debug/example.exe.mdb and b/Example/bin/Debug/example.exe.mdb differ diff --git a/Example/bin/Debug/websocket-sharp.dll b/Example/bin/Debug/websocket-sharp.dll index ddfadd45..431b9c16 100755 Binary files a/Example/bin/Debug/websocket-sharp.dll and b/Example/bin/Debug/websocket-sharp.dll differ diff --git a/Example/bin/Debug/websocket-sharp.dll.mdb b/Example/bin/Debug/websocket-sharp.dll.mdb index 773f7fbb..c9ec20ff 100644 Binary files a/Example/bin/Debug/websocket-sharp.dll.mdb and b/Example/bin/Debug/websocket-sharp.dll.mdb differ diff --git a/Example/bin/Debug_Ubuntu/example.exe b/Example/bin/Debug_Ubuntu/example.exe index 163e6c66..82f91787 100755 Binary files a/Example/bin/Debug_Ubuntu/example.exe and b/Example/bin/Debug_Ubuntu/example.exe differ diff --git a/Example/bin/Debug_Ubuntu/example.exe.mdb b/Example/bin/Debug_Ubuntu/example.exe.mdb index 1d33080e..16443663 100644 Binary files a/Example/bin/Debug_Ubuntu/example.exe.mdb and b/Example/bin/Debug_Ubuntu/example.exe.mdb differ diff --git a/Example/bin/Debug_Ubuntu/websocket-sharp.dll b/Example/bin/Debug_Ubuntu/websocket-sharp.dll index a5583810..5d5fa248 100755 Binary files a/Example/bin/Debug_Ubuntu/websocket-sharp.dll and b/Example/bin/Debug_Ubuntu/websocket-sharp.dll differ diff --git a/Example/bin/Debug_Ubuntu/websocket-sharp.dll.mdb b/Example/bin/Debug_Ubuntu/websocket-sharp.dll.mdb index bf32ab3e..0803f12c 100644 Binary files a/Example/bin/Debug_Ubuntu/websocket-sharp.dll.mdb and b/Example/bin/Debug_Ubuntu/websocket-sharp.dll.mdb differ diff --git a/Example/bin/Release/example.exe b/Example/bin/Release/example.exe index 3b5d4030..43a13b64 100755 Binary files a/Example/bin/Release/example.exe and b/Example/bin/Release/example.exe differ diff --git a/Example/bin/Release/websocket-sharp.dll b/Example/bin/Release/websocket-sharp.dll index 69e31c51..37bc66e7 100755 Binary files a/Example/bin/Release/websocket-sharp.dll and b/Example/bin/Release/websocket-sharp.dll differ diff --git a/Example/bin/Release_Ubuntu/example.exe b/Example/bin/Release_Ubuntu/example.exe index ebdef96e..760ee9f8 100755 Binary files a/Example/bin/Release_Ubuntu/example.exe and b/Example/bin/Release_Ubuntu/example.exe differ diff --git a/Example/bin/Release_Ubuntu/websocket-sharp.dll b/Example/bin/Release_Ubuntu/websocket-sharp.dll index 6e9ae203..393f4f9a 100755 Binary files a/Example/bin/Release_Ubuntu/websocket-sharp.dll and b/Example/bin/Release_Ubuntu/websocket-sharp.dll differ diff --git a/Example1/bin/Debug/example1.exe b/Example1/bin/Debug/example1.exe index b3050be4..3e7fb7a7 100755 Binary files a/Example1/bin/Debug/example1.exe and b/Example1/bin/Debug/example1.exe differ diff --git a/Example1/bin/Debug/example1.exe.mdb b/Example1/bin/Debug/example1.exe.mdb index a86f46b3..ab1b79a9 100644 Binary files a/Example1/bin/Debug/example1.exe.mdb and b/Example1/bin/Debug/example1.exe.mdb differ diff --git a/Example1/bin/Debug/websocket-sharp.dll b/Example1/bin/Debug/websocket-sharp.dll index ddfadd45..431b9c16 100755 Binary files a/Example1/bin/Debug/websocket-sharp.dll and b/Example1/bin/Debug/websocket-sharp.dll differ diff --git a/Example1/bin/Debug/websocket-sharp.dll.mdb b/Example1/bin/Debug/websocket-sharp.dll.mdb index 773f7fbb..c9ec20ff 100644 Binary files a/Example1/bin/Debug/websocket-sharp.dll.mdb and b/Example1/bin/Debug/websocket-sharp.dll.mdb differ diff --git a/Example1/bin/Debug_Ubuntu/example1.exe b/Example1/bin/Debug_Ubuntu/example1.exe index c44f0f3c..d107f8ea 100755 Binary files a/Example1/bin/Debug_Ubuntu/example1.exe and b/Example1/bin/Debug_Ubuntu/example1.exe differ diff --git a/Example1/bin/Debug_Ubuntu/example1.exe.mdb b/Example1/bin/Debug_Ubuntu/example1.exe.mdb index df708073..cbc75fda 100644 Binary files a/Example1/bin/Debug_Ubuntu/example1.exe.mdb and b/Example1/bin/Debug_Ubuntu/example1.exe.mdb differ diff --git a/Example1/bin/Debug_Ubuntu/websocket-sharp.dll b/Example1/bin/Debug_Ubuntu/websocket-sharp.dll index a5583810..5d5fa248 100755 Binary files a/Example1/bin/Debug_Ubuntu/websocket-sharp.dll and b/Example1/bin/Debug_Ubuntu/websocket-sharp.dll differ diff --git a/Example1/bin/Debug_Ubuntu/websocket-sharp.dll.mdb b/Example1/bin/Debug_Ubuntu/websocket-sharp.dll.mdb index bf32ab3e..0803f12c 100644 Binary files a/Example1/bin/Debug_Ubuntu/websocket-sharp.dll.mdb and b/Example1/bin/Debug_Ubuntu/websocket-sharp.dll.mdb differ diff --git a/Example1/bin/Release/example1.exe b/Example1/bin/Release/example1.exe index 3d3be0bf..83f6bafe 100755 Binary files a/Example1/bin/Release/example1.exe and b/Example1/bin/Release/example1.exe differ diff --git a/Example1/bin/Release/websocket-sharp.dll b/Example1/bin/Release/websocket-sharp.dll index 69e31c51..37bc66e7 100755 Binary files a/Example1/bin/Release/websocket-sharp.dll and b/Example1/bin/Release/websocket-sharp.dll differ diff --git a/Example1/bin/Release_Ubuntu/example1.exe b/Example1/bin/Release_Ubuntu/example1.exe index 3909fa89..3dc27c2d 100755 Binary files a/Example1/bin/Release_Ubuntu/example1.exe and b/Example1/bin/Release_Ubuntu/example1.exe differ diff --git a/Example1/bin/Release_Ubuntu/websocket-sharp.dll b/Example1/bin/Release_Ubuntu/websocket-sharp.dll index 6e9ae203..393f4f9a 100755 Binary files a/Example1/bin/Release_Ubuntu/websocket-sharp.dll and b/Example1/bin/Release_Ubuntu/websocket-sharp.dll differ diff --git a/Example2/Example2.pidb b/Example2/Example2.pidb index f07c6064..8dfcf1b3 100644 Binary files a/Example2/Example2.pidb and b/Example2/Example2.pidb differ diff --git a/Example2/bin/Debug/example2.exe b/Example2/bin/Debug/example2.exe index 03acf4c0..a312f5c1 100755 Binary files a/Example2/bin/Debug/example2.exe and b/Example2/bin/Debug/example2.exe differ diff --git a/Example2/bin/Debug/example2.exe.mdb b/Example2/bin/Debug/example2.exe.mdb index b13e7585..d8e1c542 100644 Binary files a/Example2/bin/Debug/example2.exe.mdb and b/Example2/bin/Debug/example2.exe.mdb differ diff --git a/Example2/bin/Debug/websocket-sharp.dll b/Example2/bin/Debug/websocket-sharp.dll index ddfadd45..431b9c16 100755 Binary files a/Example2/bin/Debug/websocket-sharp.dll and b/Example2/bin/Debug/websocket-sharp.dll differ diff --git a/Example2/bin/Debug/websocket-sharp.dll.mdb b/Example2/bin/Debug/websocket-sharp.dll.mdb index 773f7fbb..c9ec20ff 100644 Binary files a/Example2/bin/Debug/websocket-sharp.dll.mdb and b/Example2/bin/Debug/websocket-sharp.dll.mdb differ diff --git a/Example2/bin/Debug_Ubuntu/example2.exe b/Example2/bin/Debug_Ubuntu/example2.exe index 75b24179..109cde99 100755 Binary files a/Example2/bin/Debug_Ubuntu/example2.exe and b/Example2/bin/Debug_Ubuntu/example2.exe differ diff --git a/Example2/bin/Debug_Ubuntu/example2.exe.mdb b/Example2/bin/Debug_Ubuntu/example2.exe.mdb index dcaa14fd..86e4dfa4 100644 Binary files a/Example2/bin/Debug_Ubuntu/example2.exe.mdb and b/Example2/bin/Debug_Ubuntu/example2.exe.mdb differ diff --git a/Example2/bin/Debug_Ubuntu/websocket-sharp.dll b/Example2/bin/Debug_Ubuntu/websocket-sharp.dll index a5583810..5d5fa248 100755 Binary files a/Example2/bin/Debug_Ubuntu/websocket-sharp.dll and b/Example2/bin/Debug_Ubuntu/websocket-sharp.dll differ diff --git a/Example2/bin/Debug_Ubuntu/websocket-sharp.dll.mdb b/Example2/bin/Debug_Ubuntu/websocket-sharp.dll.mdb index bf32ab3e..0803f12c 100644 Binary files a/Example2/bin/Debug_Ubuntu/websocket-sharp.dll.mdb and b/Example2/bin/Debug_Ubuntu/websocket-sharp.dll.mdb differ diff --git a/Example2/bin/Release/example2.exe b/Example2/bin/Release/example2.exe index b2cead4c..a4aeb68c 100755 Binary files a/Example2/bin/Release/example2.exe and b/Example2/bin/Release/example2.exe differ diff --git a/Example2/bin/Release/websocket-sharp.dll b/Example2/bin/Release/websocket-sharp.dll index 69e31c51..37bc66e7 100755 Binary files a/Example2/bin/Release/websocket-sharp.dll and b/Example2/bin/Release/websocket-sharp.dll differ diff --git a/Example2/bin/Release_Ubuntu/example2.exe b/Example2/bin/Release_Ubuntu/example2.exe index 49199852..7af43297 100755 Binary files a/Example2/bin/Release_Ubuntu/example2.exe and b/Example2/bin/Release_Ubuntu/example2.exe differ diff --git a/Example2/bin/Release_Ubuntu/websocket-sharp.dll b/Example2/bin/Release_Ubuntu/websocket-sharp.dll index 6e9ae203..393f4f9a 100755 Binary files a/Example2/bin/Release_Ubuntu/websocket-sharp.dll and b/Example2/bin/Release_Ubuntu/websocket-sharp.dll differ diff --git a/Example3/bin/Debug/Example3.exe b/Example3/bin/Debug/Example3.exe index 4b7bccf3..21a7cbdd 100755 Binary files a/Example3/bin/Debug/Example3.exe and b/Example3/bin/Debug/Example3.exe differ diff --git a/Example3/bin/Debug/Example3.exe.mdb b/Example3/bin/Debug/Example3.exe.mdb index 3db1eb4f..3244c62b 100644 Binary files a/Example3/bin/Debug/Example3.exe.mdb and b/Example3/bin/Debug/Example3.exe.mdb differ diff --git a/Example3/bin/Debug/websocket-sharp.dll b/Example3/bin/Debug/websocket-sharp.dll index ddfadd45..431b9c16 100755 Binary files a/Example3/bin/Debug/websocket-sharp.dll and b/Example3/bin/Debug/websocket-sharp.dll differ diff --git a/Example3/bin/Debug/websocket-sharp.dll.mdb b/Example3/bin/Debug/websocket-sharp.dll.mdb index 773f7fbb..c9ec20ff 100644 Binary files a/Example3/bin/Debug/websocket-sharp.dll.mdb and b/Example3/bin/Debug/websocket-sharp.dll.mdb differ diff --git a/Example3/bin/Debug_Ubuntu/Example3.exe b/Example3/bin/Debug_Ubuntu/Example3.exe index 970015cd..411dce92 100755 Binary files a/Example3/bin/Debug_Ubuntu/Example3.exe and b/Example3/bin/Debug_Ubuntu/Example3.exe differ diff --git a/Example3/bin/Debug_Ubuntu/Example3.exe.mdb b/Example3/bin/Debug_Ubuntu/Example3.exe.mdb index 016f363d..0604a51d 100644 Binary files a/Example3/bin/Debug_Ubuntu/Example3.exe.mdb and b/Example3/bin/Debug_Ubuntu/Example3.exe.mdb differ diff --git a/Example3/bin/Debug_Ubuntu/websocket-sharp.dll b/Example3/bin/Debug_Ubuntu/websocket-sharp.dll index a5583810..5d5fa248 100755 Binary files a/Example3/bin/Debug_Ubuntu/websocket-sharp.dll and b/Example3/bin/Debug_Ubuntu/websocket-sharp.dll differ diff --git a/Example3/bin/Debug_Ubuntu/websocket-sharp.dll.mdb b/Example3/bin/Debug_Ubuntu/websocket-sharp.dll.mdb index bf32ab3e..0803f12c 100644 Binary files a/Example3/bin/Debug_Ubuntu/websocket-sharp.dll.mdb and b/Example3/bin/Debug_Ubuntu/websocket-sharp.dll.mdb differ diff --git a/Example3/bin/Release/Example3.exe b/Example3/bin/Release/Example3.exe index 5aa8a885..470666d1 100755 Binary files a/Example3/bin/Release/Example3.exe and b/Example3/bin/Release/Example3.exe differ diff --git a/Example3/bin/Release/websocket-sharp.dll b/Example3/bin/Release/websocket-sharp.dll index 69e31c51..37bc66e7 100755 Binary files a/Example3/bin/Release/websocket-sharp.dll and b/Example3/bin/Release/websocket-sharp.dll differ diff --git a/Example3/bin/Release_Ubuntu/Example3.exe b/Example3/bin/Release_Ubuntu/Example3.exe index 919a5e2b..bf0bce4d 100755 Binary files a/Example3/bin/Release_Ubuntu/Example3.exe and b/Example3/bin/Release_Ubuntu/Example3.exe differ diff --git a/Example3/bin/Release_Ubuntu/websocket-sharp.dll b/Example3/bin/Release_Ubuntu/websocket-sharp.dll index 6e9ae203..393f4f9a 100755 Binary files a/Example3/bin/Release_Ubuntu/websocket-sharp.dll and b/Example3/bin/Release_Ubuntu/websocket-sharp.dll differ diff --git a/README.md b/README.md index 82010116..1b8cd326 100644 --- a/README.md +++ b/README.md @@ -294,8 +294,8 @@ Please access [http://localhost:4649](http://localhost:4649) to do WebSocket Ech **websocket-sharp** supports **[RFC 6455]**. -- **[branch: hybi-00]** supports older draft-ietf-hybi-thewebsocketprotocol-00 (**[hybi-00]**). -- **[branch: draft75]** supports even more old draft-hixie-thewebsocketprotocol-75 (**[hixie-75]**). +- **[branch: hybi-00]** supports older draft-ietf-hybi-thewebsocketprotocol-00 ( **[hybi-00]** ). +- **[branch: draft75]** supports even more old draft-hixie-thewebsocketprotocol-75 ( **[hixie-75]** ). ## Reference ## diff --git a/websocket-sharp/Ext.cs b/websocket-sharp/Ext.cs index cfd58339..534f5869 100644 --- a/websocket-sharp/Ext.cs +++ b/websocket-sharp/Ext.cs @@ -6,13 +6,18 @@ * * The MIT License * - * (C) 2001 Garrett Rooney (System.Uri) - * (C) 2003 Ian MacLean (System.Uri) - * (C) 2003 Ben Maurer (System.Uri) - * Copyright (C) 2003, 2005, 2009 Novell, Inc. (http://www.novell.com) (System.Uri, System.Net.HttpListenerResponse) - * Copyright (c) 2009 Stephane Delcroix (System.Uri) * Copyright (c) 2010-2012 sta.blockhead * + * System.Uri.cs + * (C) 2001 Garrett Rooney + * (C) 2003 Ian MacLean + * (C) 2003 Ben Maurer + * Copyright (C) 2003, 2005, 2009 Novell, Inc. (http://www.novell.com) + * Copyright (c) 2009 Stephane Delcroix + * + * System.Net.HttpListenerResponse.cs + * Copyright (C) 2003, 2005, 2009 Novell, Inc. (http://www.novell.com) + * * 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 @@ -45,10 +50,46 @@ using WebSocketSharp.Net.Sockets; namespace WebSocketSharp { + /// + /// Provides a set of static methods for the websocket-sharp. + /// public static class Ext { + #region Private Methods + + private static void times(this ulong n, Action act) + { + for (ulong i = 0; i < n; i++) + act(); + } + + private static void times(this ulong n, Action act) + { + for (ulong i = 0; i < n; i++) + act(i); + } + + #endregion + + #region Public Methods + + /// + /// Accept a WebSocket connection by the . + /// + /// + /// A that contains a WebSocket connection. + /// + /// + /// A that contains a TCP connection to accept a WebSocket connection from. + /// + /// + /// Is thrown when the parameter passed to a method is invalid because it is . + /// public static TcpListenerWebSocketContext AcceptWebSocket(this TcpClient client) { + if (client.IsNull()) + throw new ArgumentNullException("client"); + return new TcpListenerWebSocketContext(client); } @@ -56,10 +97,10 @@ namespace WebSocketSharp { /// Emit the specified delegate if is not . /// /// - /// An to be emitted. + /// An to emit. /// /// - /// An that emits this . + /// An that emits the . /// /// /// An that contains no event data. @@ -75,10 +116,10 @@ namespace WebSocketSharp { /// Emit the specified delegate if is not . /// /// - /// An to be emitted. + /// An to emit. /// /// - /// An that emits this . + /// An that emits the . /// /// /// An that contains the event data. @@ -94,26 +135,76 @@ namespace WebSocketSharp { eventHandler(sender, e); } + /// + /// Determines whether the specified equals the specified as . + /// And save this specified as to the specified . + /// + /// + /// true if the parameter equals the parameter as ; otherwise, false. + /// + /// + /// An to compare. + /// + /// + /// A to compare. + /// + /// + /// A to save the as . + /// + /// + /// Is thrown when the parameter passed to a method is invalid because it is outside the allowable range of values as . + /// public static bool EqualsAndSaveTo(this int value, char c, List dest) { - if (value < 0) + if (value < 0 || value > 255) throw new ArgumentOutOfRangeException("value"); - byte b = (byte)value; + var b = (byte)value; dest.Add(b); + return b == Convert.ToByte(c); } - public static bool Exists(this NameValueCollection collections, string name) + /// + /// Determines whether the entry with the specified key exists in the specified . + /// + /// + /// true if the entry with the exists in the ; otherwise, false. + /// + /// + /// A that contains the entries. + /// + /// + /// A that contains the key of the entry to find. + /// + public static bool Exists(this NameValueCollection collection, string name) { - return collections[name] != null - ? true - : false; + return collection.IsNull() + ? false + : !collection[name].IsNull(); } - public static bool Exists(this NameValueCollection collections, string name, string value) + /// + /// Determines whether the entry with the specified both key and value exists in the specified . + /// + /// + /// true if the entry with the both and exists in the ; otherwise, false. + /// + /// + /// A that contains the entries. + /// + /// + /// A that contains the key of the entry to find. + /// + /// + /// A that contains the value of the entry to find. + /// + public static bool Exists(this NameValueCollection collection, string name, string value) { - var values = collections[name]; + if (collection.IsNull()) + return false; + + var values = collection[name]; if (values.IsNull()) return false; @@ -124,33 +215,104 @@ namespace WebSocketSharp { return false; } + /// + /// Gets the absolute path from the specified . + /// + /// + /// A that contains the absolute path if got successfully; otherwise, . + /// + /// + /// A that contains the URI to get the absolute path from. + /// public static string GetAbsolutePath(this Uri uri) { + if (uri.IsNull()) + return null; + if (uri.IsAbsoluteUri) return uri.AbsolutePath; var uriString = uri.OriginalString; - var i = uriString.IndexOf('/'); if (i != 0) - { - var msg = "Not absolute path: " + uriString; - throw new ArgumentException(msg, "uri"); - } + return null; - var j = uriString.IndexOfAny(new []{'?', '#'}); - if (j > 0) - return uriString.Substring(0, j); - - return uriString; + i = uriString.IndexOfAny(new []{'?', '#'}); + return i > 0 + ? uriString.Substring(0, i) + : uriString; } + /// + /// Gets the description of the HTTP status code using the specified code. + /// + /// + /// A that contains the description of the . + /// + /// + /// One of values that contains the HTTP status code. + /// public static string GetDescription(this HttpStatusCode code) { return ((int)code).GetStatusDescription(); } - // Derived from System.Net.HttpListenerResponse.GetStatusDescription method + /// + /// Gets the name from the specified that contains a pair of name and value are separated by a separator string. + /// + /// + /// A that contains the name if any; otherwise, null. + /// + /// + /// A that contains a pair of name and value are separated by a separator string. + /// + /// + /// A that contains a separator string. + /// + public static string GetName(this string nameAndValue, string separator) + { + if (nameAndValue.IsNullOrEmpty()) + return null; + + if (separator.IsNullOrEmpty()) + return null; + + var i = nameAndValue.IndexOf(separator); + return i > 0 + ? nameAndValue.Substring(0, i).Trim() + : null; + } + + /// + /// Gets the name and value from the specified that contains a pair of name and value are separated by a separator string. + /// + /// + /// A that contains the name and value if any. + /// + /// + /// A that contains a pair of name and value are separated by a separator string. + /// + /// + /// A that contains a separator string. + /// + public static KeyValuePair GetNameAndValue(this string nameAndValue, string separator) + { + var name = nameAndValue.GetName(separator); + var value = nameAndValue.GetValue(separator); + return !name.IsNull() + ? new KeyValuePair(name, value) + : new KeyValuePair(null, null); + } + + /// + /// Gets the description of the HTTP status code using the specified code. + /// + /// + /// A that contains the description of the . + /// + /// + /// An that contains the HTTP status code. + /// public static string GetStatusDescription(this int code) { switch (code) @@ -206,29 +368,8 @@ namespace WebSocketSharp { return String.Empty; } - public static string GetName(this string nameAndValue, string separator) - { - var i = nameAndValue.IndexOf(separator); - if (i <= 0) - return null; - - return nameAndValue.Substring(0, i).Trim(); - } - - public static KeyValuePair GetNameAndValue(this string nameAndValue, string separator) - { - var i = nameAndValue.IndexOf(separator); - if (i <= 0 || i == nameAndValue.Length - 1) - return new KeyValuePair(null, null); - - var name = nameAndValue.Substring(0, i).Trim(); - var value = nameAndValue.Substring(i + 1).Trim(); - - return new KeyValuePair(name, value); - } - /// - /// Gets the value from a that contains a pair of name and value are separated by a separator string. + /// Gets the value from the specified that contains a pair of name and value are separated by a separator string. /// /// /// A that contains the value if any; otherwise, null. @@ -241,30 +382,23 @@ namespace WebSocketSharp { /// public static string GetValue(this string nameAndValue, string separator) { - var i = nameAndValue.IndexOf(separator); - if (i == -1 || i == nameAndValue.Length - 1) + if (nameAndValue.IsNullOrEmpty()) return null; - return nameAndValue.Substring(i + 1).Trim(); - } + if (separator.IsNullOrEmpty()) + return null; - public static bool IsHostOrder(this ByteOrder order) - { - if (BitConverter.IsLittleEndian ^ (order == ByteOrder.LITTLE)) - {// true ^ false or false ^ true - return false; - } - else - {// true ^ true or false ^ false - return true; - } + var i = nameAndValue.IndexOf(separator); + return i >= 0 && i < nameAndValue.Length - 1 + ? nameAndValue.Substring(i + 1).Trim() + : null; } /// - /// Determines whether the specified is . + /// Determines whether the specified is a . /// /// - /// true if the is ; otherwise, false. + /// true if the parameter is a ; otherwise, false. /// /// /// A to test. @@ -274,17 +408,33 @@ namespace WebSocketSharp { return value == String.Empty ? true : false; } + /// + /// Determines whether the specified is host (this computer architecture) byte order. + /// + /// + /// true if the parameter is host byte order; otherwise, false. + /// + /// + /// A to test. + /// + public static bool IsHostOrder(this ByteOrder order) + { + // true : !(true ^ true) or !(false ^ false) + // false: !(true ^ false) or !(false ^ true) + return !(BitConverter.IsLittleEndian ^ (order == ByteOrder.LITTLE)); + } + /// /// Determines whether the specified object is . /// /// - /// true if the specified object is ; otherwise, false. + /// true if the parameter is ; otherwise, false. /// /// - /// An to test. + /// A to test. /// /// - /// The type of this . + /// The type of the parameter. /// public static bool IsNull(this T obj) where T : class @@ -292,6 +442,22 @@ namespace WebSocketSharp { return obj == null ? true : false; } + /// + /// Determines whether the specified object is . + /// And invokes the specified delegate if the specified object is . + /// + /// + /// true if the parameter is ; otherwise, false. + /// + /// + /// A to test. + /// + /// + /// An delegate that contains the method(s) called if the is . + /// + /// + /// The type of the parameter. + /// public static bool IsNullDo(this T obj, Action act) where T : class { @@ -308,7 +474,7 @@ namespace WebSocketSharp { /// Determines whether the specified is or . /// /// - /// true if the specified is or ; otherwise, false. + /// true if the parameter is or ; otherwise, false. /// /// /// A to test. @@ -318,40 +484,21 @@ namespace WebSocketSharp { return String.IsNullOrEmpty(value); } - public static bool IsValidAbsolutePath(this string absPath, out string message) - { - if (absPath.IsEmpty()) - { - message = "Must not be empty."; - return false; - } - - var i = absPath.IndexOf('/'); - if (i != 0) - { - message = "Not absolute path: " + absPath; - return false; - } - - var j = absPath.IndexOfAny(new []{'?', '#'}); - if (j != -1) - { - message = "Must not contain either or both query and fragment components: " + absPath; - return false; - } - - message = String.Empty; - return true; - } - - // Derived from System.Uri.IsPredefinedScheme method + /// + /// Determines whether the specified is predefined scheme. + /// + /// + /// true if the parameter is the predefined scheme; otherwise, false. + /// + /// + /// A to test. + /// public static bool IsPredefinedScheme(this string scheme) { if (scheme.IsNull() && scheme.Length < 2) return false; char c = scheme[0]; - if (c == 'h') return (scheme == "http" || scheme == "https"); @@ -364,10 +511,9 @@ namespace WebSocketSharp { if (c == 'n') { c = scheme[1]; - if (c == 'e') return (scheme == "news" || scheme == "net.pipe" || scheme == "net.tcp"); - + if (scheme == "nntp") return true; @@ -380,9 +526,58 @@ namespace WebSocketSharp { return false; } - // Derived from System.Uri.MaybeUri method + /// + /// Determines whether the specified is valid absolute path. + /// + /// + /// true if the parameter is valid absolute path; otherwise, false. + /// + /// + /// A to test. + /// + /// + /// A that receives a message if the is invalid. + /// + public static bool IsValidAbsolutePath(this string absPath, out string message) + { + if (absPath.IsNullOrEmpty()) + { + message = "Must not be null or empty."; + return false; + } + + var i = absPath.IndexOf('/'); + if (i != 0) + { + message = "Not absolute path: " + absPath; + return false; + } + + i = absPath.IndexOfAny(new []{'?', '#'}); + if (i != -1) + { + message = "Must not contain either or both query and fragment components: " + absPath; + return false; + } + + message = String.Empty; + return true; + } + + /// + /// Determines whether the specified is a URI string. + /// + /// + /// true if the parameter is maybe a URI string; otherwise, false. + /// + /// + /// A to test. + /// public static bool MaybeUri(this string uriString) { + if (uriString.IsNullOrEmpty()) + return false; + int p = uriString.IndexOf(':'); if (p == -1) return false; @@ -393,6 +588,21 @@ namespace WebSocketSharp { return uriString.Substring(0, p).IsPredefinedScheme(); } + /// + /// Determines whether two specified objects don't have the same value. + /// + /// + /// true if the value of parameter isn't the same as the value of parameter; otherwise, false. + /// + /// + /// The first to compare. + /// + /// + /// The second to compare. + /// + /// + /// A that indicates a case-sensitive or insensitive comparison. (true indicates a case-insensitive comparison.) + /// public static bool NotEqual(this string expected, string actual, bool ignoreCase) { return String.Compare(expected, actual, ignoreCase) != 0 @@ -400,19 +610,73 @@ namespace WebSocketSharp { : false; } + /// + /// Reads a block of bytes from the specified stream and returns the read data in an array of . + /// + /// + /// An array of that receives the read data. + /// + /// + /// A that contains the data to read. + /// + /// + /// An that contains the number of bytes to read. + /// public static byte[] ReadBytes(this Stream stream, int length) { - if (length <= 0) + if (stream.IsNull() || length <= 0) return new byte[]{}; var buffer = new byte[length]; var readLen = stream.Read(buffer, 0, length); - return readLen == length ? buffer : null; + return readLen == length + ? buffer + : readLen > 0 + ? buffer.SubArray(0, readLen) + : new byte[]{}; } + /// + /// Reads a block of bytes from the specified stream and returns the read data in an array of . + /// + /// + /// An array of that receives the read data. + /// + /// + /// A that contains the data to read. + /// + /// + /// A that contains the number of bytes to read. + /// + public static byte[] ReadBytes(this Stream stream, long length) + { + return stream.ReadBytes(length, 1024); + } + + /// + /// Reads a block of bytes from the specified stream and returns the read data in an array of . + /// + /// + /// An array of that receives the read data. + /// + /// + /// A that contains the data to read. + /// + /// + /// A that contains the number of bytes to read. + /// + /// + /// An that contains the buffer size in bytes of each internal read. + /// public static byte[] ReadBytes(this Stream stream, long length, int bufferLength) { + if (stream.IsNull() || length <= 0) + return new byte[]{}; + + if (bufferLength <= 0) + bufferLength = 1024; + var count = length / bufferLength; var rem = length % bufferLength; var readData = new List(); @@ -436,18 +700,42 @@ namespace WebSocketSharp { }); if (rem > 0) - { - readBuffer = new byte[rem]; - read(readBuffer); - } + read(new byte[rem]); - return readLen == length + return readLen > 0 ? readData.ToArray() - : null; + : new byte[]{}; } + /// + /// Retrieves a sub-array from the specified . A sub-array starts at the specified element position. + /// + /// + /// An array of T that receives a sub-array, or an empty array of T if any problems with the parameters. + /// + /// + /// An array of T that contains the data to retrieve a sub-array. + /// + /// + /// An that contains the zero-based starting position of a sub-array in the . + /// + /// + /// An that contains the number of elements to retrieve a sub-array. + /// + /// + /// The type of elements in the . + /// public static T[] SubArray(this T[] array, int startIndex, int length) { + if (array.IsNull() || array.Length == 0) + return new T[]{}; + + if (startIndex < 0 || length <= 0) + return new T[]{}; + + if (startIndex + length > array.Length) + return new T[]{}; + if (startIndex == 0 && array.Length == length) return array; @@ -457,144 +745,253 @@ namespace WebSocketSharp { return subArray; } + /// + /// Executes the specified delegate times. + /// + /// + /// An that contains the number of times to execute. + /// + /// + /// An delegate that contains the method(s) to execute. + /// public static void Times(this int n, Action act) { - ((ulong)n).Times(act); - } - - public static void Times(this uint n, Action act) - { - ((ulong)n).Times(act); + if (n > 0 && !act.IsNull()) + ((ulong)n).times(act); } + /// + /// Executes the specified delegate times. + /// + /// + /// A that contains the number of times to execute. + /// + /// + /// An delegate that contains the method(s) to execute. + /// public static void Times(this long n, Action act) { - ((ulong)n).Times(act); + if (n > 0 && !act.IsNull()) + ((ulong)n).times(act); } + /// + /// Executes the specified delegate times. + /// + /// + /// A that contains the number of times to execute. + /// + /// + /// An delegate that contains the method(s) to execute. + /// + public static void Times(this uint n, Action act) + { + if (n > 0 && !act.IsNull()) + ((ulong)n).times(act); + } + + /// + /// Executes the specified delegate times. + /// + /// + /// A that contains the number of times to execute. + /// + /// + /// An delegate that contains the method(s) to execute. + /// public static void Times(this ulong n, Action act) { - for (ulong i = 0; i < n; i++) - act(); + if (n > 0 && !act.IsNull()) + n.times(act); } + /// + /// Executes the specified delegate times. + /// + /// + /// An that contains the number of times to execute. + /// + /// + /// An delegate that contains the method(s) to execute. + /// A parameter to pass to this method(s) contains the zero-based count of iteration. + /// public static void Times(this int n, Action act) { - ((ulong)n).Times(act); - } - - public static void Times(this uint n, Action act) - { - ((ulong)n).Times(act); + if (n > 0 && !act.IsNull()) + ((ulong)n).times(act); } + /// + /// Executes the specified delegate times. + /// + /// + /// A that contains the number of times to execute. + /// + /// + /// An delegate that contains the method(s) to execute. + /// A parameter to pass to this method(s) contains the zero-based count of iteration. + /// public static void Times(this long n, Action act) { - ((ulong)n).Times(act); + if (n > 0 && !act.IsNull()) + ((ulong)n).times(act); } + /// + /// Executes the specified delegate times. + /// + /// + /// A that contains the number of times to execute. + /// + /// + /// An delegate that contains the method(s) to execute. + /// A parameter to pass to this method(s) contains the zero-based count of iteration. + /// + public static void Times(this uint n, Action act) + { + if (n > 0 && !act.IsNull()) + ((ulong)n).times(act); + } + + /// + /// Executes the specified delegate times. + /// + /// + /// A that contains the number of times to execute. + /// + /// + /// An delegate that contains the method(s) to execute. + /// A parameter to pass to this method(s) contains the zero-based count of iteration. + /// public static void Times(this ulong n, Action act) { - for (ulong i = 0; i < n; i++) - act(i); + if (n > 0 && !act.IsNull()) + n.times(act); } + /// + /// Converts the specified array of to the specified type data. + /// + /// + /// A T converted from the , or a default value of T + /// if the is an empty array of + /// or if the types of T aren't the , , , + /// , , , , + /// , , . + /// + /// + /// An array of to convert. + /// + /// + /// A that indicates the byte order of the . + /// + /// + /// The type of the return value. The T must be a value type. + /// + /// + /// Is thrown when the parameter passed to a method is invalid because it is . + /// public static T To(this byte[] src, ByteOrder srcOrder) where T : struct { - T dest; - byte[] buffer = src.ToHostOrder(srcOrder); + if (src.IsNull()) + throw new ArgumentNullException("src"); - if (typeof(T) == typeof(Boolean)) - { - dest = (T)(object)BitConverter.ToBoolean(buffer, 0); - } - else if (typeof(T) == typeof(Char)) - { - dest = (T)(object)BitConverter.ToChar(buffer, 0); - } - else if (typeof(T) == typeof(Double)) - { - dest = (T)(object)BitConverter.ToDouble(buffer, 0); - } - else if (typeof(T) == typeof(Int16)) - { - dest = (T)(object)BitConverter.ToInt16(buffer, 0); - } - else if (typeof(T) == typeof(Int32)) - { - dest = (T)(object)BitConverter.ToInt32(buffer, 0); - } - else if (typeof(T) == typeof(Int64)) - { - dest = (T)(object)BitConverter.ToInt64(buffer, 0); - } - else if (typeof(T) == typeof(Single)) - { - dest = (T)(object)BitConverter.ToSingle(buffer, 0); - } - else if (typeof(T) == typeof(UInt16)) - { - dest = (T)(object)BitConverter.ToUInt16(buffer, 0); - } - else if (typeof(T) == typeof(UInt32)) - { - dest = (T)(object)BitConverter.ToUInt32(buffer, 0); - } - else if (typeof(T) == typeof(UInt64)) - { - dest = (T)(object)BitConverter.ToUInt64(buffer, 0); - } - else - { - dest = default(T); - } + if (src.Length == 0) + return default(T); - return dest; + var type = typeof(T); + var buffer = src.ToHostOrder(srcOrder); + if (type == typeof(Boolean)) + return (T)(object)BitConverter.ToBoolean(buffer, 0); + + if (type == typeof(Char)) + return (T)(object)BitConverter.ToChar(buffer, 0); + + if (type == typeof(Double)) + return (T)(object)BitConverter.ToDouble(buffer, 0); + + if (type == typeof(Int16)) + return (T)(object)BitConverter.ToInt16(buffer, 0); + + if (type == typeof(Int32)) + return (T)(object)BitConverter.ToInt32(buffer, 0); + + if (type == typeof(Int64)) + return (T)(object)BitConverter.ToInt64(buffer, 0); + + if (type == typeof(Single)) + return (T)(object)BitConverter.ToSingle(buffer, 0); + + if (type == typeof(UInt16)) + return (T)(object)BitConverter.ToUInt16(buffer, 0); + + if (type == typeof(UInt32)) + return (T)(object)BitConverter.ToUInt32(buffer, 0); + + if (type == typeof(UInt64)) + return (T)(object)BitConverter.ToUInt64(buffer, 0); + + return default(T); } + /// + /// Converts the specified data to an array of . + /// + /// + /// An array of converted from the . + /// + /// + /// A T to convert. + /// + /// + /// A that indicates the byte order of the return. + /// + /// + /// The type of the . The T must be a value type. + /// public static byte[] ToBytes(this T value, ByteOrder order) where T : struct { + var type = typeof(T); byte[] buffer; - - if (typeof(T) == typeof(Boolean)) + if (type == typeof(Boolean)) { buffer = BitConverter.GetBytes((Boolean)(object)value); } - else if (typeof(T) == typeof(Char)) + else if (type == typeof(Char)) { buffer = BitConverter.GetBytes((Char)(object)value); } - else if (typeof(T) == typeof(Double)) + else if (type == typeof(Double)) { buffer = BitConverter.GetBytes((Double)(object)value); } - else if (typeof(T) == typeof(Int16)) + else if (type == typeof(Int16)) { buffer = BitConverter.GetBytes((Int16)(object)value); } - else if (typeof(T) == typeof(Int32)) + else if (type == typeof(Int32)) { buffer = BitConverter.GetBytes((Int32)(object)value); } - else if (typeof(T) == typeof(Int64)) + else if (type == typeof(Int64)) { buffer = BitConverter.GetBytes((Int64)(object)value); } - else if (typeof(T) == typeof(Single)) + else if (type == typeof(Single)) { buffer = BitConverter.GetBytes((Single)(object)value); } - else if (typeof(T) == typeof(UInt16)) + else if (type == typeof(UInt16)) { buffer = BitConverter.GetBytes((UInt16)(object)value); } - else if (typeof(T) == typeof(UInt32)) + else if (type == typeof(UInt32)) { buffer = BitConverter.GetBytes((UInt32)(object)value); } - else if (typeof(T) == typeof(UInt64)) + else if (type == typeof(UInt64)) { buffer = BitConverter.GetBytes((UInt64)(object)value); } @@ -603,45 +1000,98 @@ namespace WebSocketSharp { buffer = new byte[]{}; } - return order.IsHostOrder() + return buffer.Length == 0 || order.IsHostOrder() ? buffer : buffer.Reverse().ToArray(); } + /// + /// Converts the order of the specified array of to the host byte order. + /// + /// + /// An array of converted from the . + /// + /// + /// An array of to convert. + /// + /// + /// A that indicates the byte order of the . + /// + /// + /// Is thrown when the parameter passed to a method is invalid because it is . + /// public static byte[] ToHostOrder(this byte[] src, ByteOrder srcOrder) { - byte[] buffer = new byte[src.Length]; - src.CopyTo(buffer, 0); + if (src.IsNull()) + throw new ArgumentNullException("src"); - return srcOrder.IsHostOrder() - ? buffer - : buffer.Reverse().ToArray(); + return src.Length == 0 || srcOrder.IsHostOrder() + ? src + : src.Reverse().ToArray(); } - public static string ToString(this T[] array, string separater) + /// + /// Converts the specified array to a concatenated the specified separator string + /// between each element of this array. + /// + /// + /// A converted from the parameter, or a + /// if the length of the is zero. + /// + /// + /// An array of T to convert. + /// + /// + /// A that contains a separator string. + /// + /// + /// The type of elements in the . + /// + /// + /// Is thrown when the parameter passed to a method is invalid because it is . + /// + public static string ToString(this T[] array, string separator) { + if (array.IsNull()) + throw new ArgumentNullException("array"); + var len = array.Length; if (len == 0) return String.Empty; - var sb = new StringBuilder(); - for (int i = 0; i < len - 1; i++) - sb.AppendFormat("{0}{1}", array[i].ToString(), separater); - sb.Append(array[len - 1].ToString()); + if (separator.IsNull()) + separator = String.Empty; + var sb = new StringBuilder(); + (len - 1).Times(i => + sb.AppendFormat("{0}{1}", array[i].ToString(), separator) + ); + + sb.Append(array[len - 1].ToString()); return sb.ToString(); } + /// + /// Converts the specified to a object. + /// + /// + /// A converted from the parameter, or + /// if the is or . + /// + /// + /// A to convert. + /// public static Uri ToUri(this string uriString) { - if (!uriString.MaybeUri()) - return new Uri(uriString, UriKind.Relative); - - return new Uri(uriString); + return uriString.IsNullOrEmpty() + ? null + : uriString.MaybeUri() + ? new Uri(uriString) + : new Uri(uriString, UriKind.Relative); } /// - /// Tries to create a new WebSocket using . + /// Tries to create a new WebSocket using the specified . /// /// /// true if the WebSocket was successfully created; otherwise, false. @@ -650,13 +1100,13 @@ namespace WebSocketSharp { /// A that contains a WebSocket URI. /// /// - /// When this method returns, contains a created WebSocket if is valid WebSocket URI; otherwise, . + /// When this method returns, contains a created WebSocket if the parameter is valid WebSocket URI; otherwise, . /// /// - /// When this method returns, contains a error message if is invalid WebSocket URI; otherwise, String.Empty. + /// When this method returns, contains a error message if the parameter is invalid WebSocket URI; otherwise, String.Empty. /// /// - /// Is thrown when passed to a method is invalid because it is . + /// Is thrown when the parameter passed to a method is invalid because it is . /// public static bool TryCreateWebSocketUri(this string uriString, out Uri result, out string message) { @@ -709,22 +1159,66 @@ namespace WebSocketSharp { return true; } + /// + /// URL-decodes the specified . + /// + /// + /// A that receives a decoded string, or the parameter + /// if the is or . + /// + /// + /// A to decode. + /// public static string UrlDecode(this string s) { - return HttpUtility.UrlDecode(s); + return s.IsNullOrEmpty() + ? s + : HttpUtility.UrlDecode(s); } + /// + /// URL-encodes the specified . + /// + /// + /// A that receives a encoded string, or the parameter + /// if the is or . + /// + /// + /// A to encode. + /// public static string UrlEncode(this string s) { - return HttpUtility.UrlEncode(s); + return s.IsNullOrEmpty() + ? s + : HttpUtility.UrlEncode(s); } + /// + /// Writes the specified content data using the specified . + /// + /// + /// A that contains a network stream to write a content data. + /// + /// + /// An array of that contains a content data to write. + /// + /// + /// Is thrown when the parameter passed to a method is invalid because it is . + /// public static void WriteContent(this HttpListenerResponse response, byte[] content) { + if (response.IsNull()) + throw new ArgumentNullException("response"); + + if (content.IsNull() || content.Length == 0) + return; + var output = response.OutputStream; response.ContentLength64 = content.Length; output.Write(content, 0, content.Length); output.Close(); } + + #endregion } } diff --git a/websocket-sharp/Frame/PayloadData.cs b/websocket-sharp/Frame/PayloadData.cs index cde2eb48..9d599337 100644 --- a/websocket-sharp/Frame/PayloadData.cs +++ b/websocket-sharp/Frame/PayloadData.cs @@ -32,14 +32,53 @@ using System.Collections.Generic; using System.Linq; using System.Text; -namespace WebSocketSharp.Frame -{ +namespace WebSocketSharp.Frame { + public class PayloadData : IEnumerable { + #region Field - #region Public Static Fields + public const ulong MaxLength = long.MaxValue; - public static readonly ulong MaxLength; + #endregion + + #region Public Constructors + + public PayloadData(string appData) + : this(Encoding.UTF8.GetBytes(appData)) + { + } + + public PayloadData(byte[] appData) + : this(new byte[]{}, appData) + { + } + + public PayloadData(byte[] appData, bool masked) + : this(new byte[]{}, appData, masked) + { + } + + public PayloadData(byte[] extData, byte[] appData) + : this(extData, appData, false) + { + } + + public PayloadData(byte[] extData, byte[] appData, bool masked) + { + if (extData.IsNull()) + throw new ArgumentNullException("extData"); + + if (appData.IsNull()) + throw new ArgumentNullException("appData"); + + if ((ulong)extData.LongLength + (ulong)appData.LongLength > MaxLength) + throw new ArgumentOutOfRangeException("Plus 'extData' length and 'appData' lenght must be less than MaxLength."); + + ExtensionData = extData; + ApplicationData = appData; + IsMasked = masked; + } #endregion @@ -50,82 +89,25 @@ namespace WebSocketSharp.Frame public bool IsMasked { get; private set; } - public ulong Length - { - get - { + public ulong Length { + get { return (ulong)(ExtensionData.LongLength + ApplicationData.LongLength); } } #endregion - #region Static Constructor - - static PayloadData() - { - MaxLength = long.MaxValue; - } - - #endregion - - #region Public Constructors - - public PayloadData(string appData) - : this(Encoding.UTF8.GetBytes(appData)) - { - } - - public PayloadData(byte[] appData) - : this(new byte[]{}, appData) - { - } - - public PayloadData(byte[] appData, bool masked) - : this(new byte[]{}, appData, masked) - { - } - - public PayloadData(byte[] extData, byte[] appData) - : this(extData, appData, false) - { - } - - public PayloadData(byte[] extData, byte[] appData, bool masked) - { - Func func = s => () => - { - string message = String.Format("{0} must not be null.", s); - throw new ArgumentNullException(message); - }; - extData.IsNullDo(func("extData")); - appData.IsNullDo(func("appData")); - - if ((ulong)extData.LongLength + (ulong)appData.LongLength > MaxLength) - { - throw new ArgumentOutOfRangeException("Plus extData length and appData lenght must be less than MaxLength."); - } - - ExtensionData = extData; - ApplicationData = appData; - IsMasked = masked; - } - - #endregion - #region Private Methods + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + private void mask(byte[] src, byte[] key) { - if (key.Length != 4) - { - throw new ArgumentOutOfRangeException("key length must be 4."); - } - for (long i = 0; i < src.LongLength; i++) - { src[i] = (byte)(src[i] ^ key[i % 4]); - } } #endregion @@ -135,45 +117,34 @@ namespace WebSocketSharp.Frame public IEnumerator GetEnumerator() { foreach (byte b in ExtensionData) - { yield return b; - } - foreach (byte b in ApplicationData) - { - yield return b; - } - } - System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() - { - return GetEnumerator(); + foreach (byte b in ApplicationData) + yield return b; } public void Mask(byte[] maskingKey) { + if (maskingKey.IsNull()) + throw new ArgumentNullException("maskingKey"); + + if (maskingKey.Length != 4) + throw new ArgumentOutOfRangeException("maskingKey", "'maskingKey' length must be 4."); + if (ExtensionData.LongLength > 0) - { mask(ExtensionData, maskingKey); - } if (ApplicationData.LongLength > 0) - { mask(ApplicationData, maskingKey); - } IsMasked = !IsMasked; } public byte[] ToBytes() { - if (ExtensionData.LongLength > 0) - { - return ExtensionData.Concat(ApplicationData).ToArray(); - } - else - { - return ApplicationData; - } + return ExtensionData.LongLength > 0 + ? ExtensionData.Concat(ApplicationData).ToArray() + : ApplicationData; } public override string ToString() diff --git a/websocket-sharp/Frame/WsFrame.cs b/websocket-sharp/Frame/WsFrame.cs index dfc545ca..8891a658 100644 --- a/websocket-sharp/Frame/WsFrame.cs +++ b/websocket-sharp/Frame/WsFrame.cs @@ -38,16 +38,7 @@ namespace WebSocketSharp.Frame { { #region Field - private static readonly int _readBufferLen; - - #endregion - - #region Static Constructor - - static WsFrame() - { - _readBufferLen = 1024; - } + private const int _readBufferLen = 1024; #endregion @@ -171,16 +162,18 @@ namespace WebSocketSharp.Frame { { var length = frame.PayloadLen <= 125 ? 0 - : frame.PayloadLen == 126 ? 2 : 8; + : frame.PayloadLen == 126 + ? 2 + : 8; - if (length > 0) - { - var extLength = stream.ReadBytes(length); - if (extLength == null) - throw new IOException(); + if (length == 0) + return; - frame.ExtPayloadLen = extLength; - } + var extLen = stream.ReadBytes(length); + if (extLen.Length != length) + throw new IOException(); + + frame.ExtPayloadLen = extLen; } private static WsFrame readHeader(byte[] header) @@ -213,14 +206,14 @@ namespace WebSocketSharp.Frame { private static void readMaskingKey(Stream stream, WsFrame frame) { - if (frame.Masked == Mask.MASK) - { - var maskingKey = stream.ReadBytes(4); - if (maskingKey == null) - throw new IOException(); + if (frame.Masked == Mask.UNMASK) + return; - frame.MaskingKey = maskingKey; - } + var maskingKey = stream.ReadBytes(4); + if (maskingKey.Length != 4) + throw new IOException(); + + frame.MaskingKey = maskingKey; } private static void readPayloadData(Stream stream, WsFrame frame, bool unmask) @@ -231,27 +224,31 @@ namespace WebSocketSharp.Frame { ? frame.ExtPayloadLen.To(ByteOrder.BIG) : frame.ExtPayloadLen.To(ByteOrder.BIG); + if (length == 0) + { + frame.PayloadData = new PayloadData(new byte[]{}); + return; + } + + if (frame.PayloadLen > 126 && length > PayloadData.MaxLength) + throw new WsReceivedTooBigMessageException(); + var buffer = length <= (ulong)_readBufferLen ? stream.ReadBytes((int)length) : stream.ReadBytes((long)length, _readBufferLen); - if (buffer == null) + if (buffer.LongLength != (long)length) throw new IOException(); - PayloadData payloadData; - if (frame.Masked == Mask.MASK) + var payloadData = frame.Masked == Mask.MASK + ? new PayloadData(buffer, true) + : new PayloadData(buffer); + + if (frame.Masked == Mask.MASK && unmask) { - payloadData = new PayloadData(buffer, true); - if (unmask == true) - { - payloadData.Mask(frame.MaskingKey); - frame.Masked = Mask.UNMASK; - frame.MaskingKey = new byte[]{}; - } - } - else - { - payloadData = new PayloadData(buffer); + payloadData.Mask(frame.MaskingKey); + frame.Masked = Mask.UNMASK; + frame.MaskingKey = new byte[]{}; } frame.PayloadData = payloadData; @@ -462,7 +459,7 @@ namespace WebSocketSharp.Frame { { var buffer = new List(); - int header = (int)Fin; + var header = (int)Fin; header = (header << 1) + (int)Rsv1; header = (header << 1) + (int)Rsv2; header = (header << 1) + (int)Rsv3; diff --git a/websocket-sharp/Net/HttpStatusCode.cs b/websocket-sharp/Net/HttpStatusCode.cs index 0338ab01..81f03051 100644 --- a/websocket-sharp/Net/HttpStatusCode.cs +++ b/websocket-sharp/Net/HttpStatusCode.cs @@ -1,6 +1,6 @@ // // HttpStatusCode.cs -// Copied from System.Net.HttpStatusCode +// Copied from System.Net.HttpStatusCode.cs // // This code was automatically generated from // ECMA CLI XML Library Specification. @@ -34,6 +34,7 @@ namespace WebSocketSharp.Net { public enum HttpStatusCode { + Continue = 100, SwitchingProtocols = 101, OK = 200, diff --git a/websocket-sharp/bin/Debug/websocket-sharp.dll b/websocket-sharp/bin/Debug/websocket-sharp.dll index ddfadd45..431b9c16 100755 Binary files a/websocket-sharp/bin/Debug/websocket-sharp.dll and b/websocket-sharp/bin/Debug/websocket-sharp.dll differ diff --git a/websocket-sharp/bin/Debug/websocket-sharp.dll.mdb b/websocket-sharp/bin/Debug/websocket-sharp.dll.mdb index 773f7fbb..c9ec20ff 100644 Binary files a/websocket-sharp/bin/Debug/websocket-sharp.dll.mdb and b/websocket-sharp/bin/Debug/websocket-sharp.dll.mdb differ diff --git a/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll b/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll index a5583810..5d5fa248 100755 Binary files a/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll and b/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll differ diff --git a/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll.mdb b/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll.mdb index bf32ab3e..0803f12c 100644 Binary files a/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll.mdb and b/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll.mdb differ diff --git a/websocket-sharp/bin/Release/websocket-sharp.dll b/websocket-sharp/bin/Release/websocket-sharp.dll index 69e31c51..37bc66e7 100755 Binary files a/websocket-sharp/bin/Release/websocket-sharp.dll and b/websocket-sharp/bin/Release/websocket-sharp.dll differ diff --git a/websocket-sharp/bin/Release_Ubuntu/websocket-sharp.dll b/websocket-sharp/bin/Release_Ubuntu/websocket-sharp.dll index 6e9ae203..393f4f9a 100755 Binary files a/websocket-sharp/bin/Release_Ubuntu/websocket-sharp.dll and b/websocket-sharp/bin/Release_Ubuntu/websocket-sharp.dll differ diff --git a/websocket-sharp/websocket-sharp.pidb b/websocket-sharp/websocket-sharp.pidb index b9a618ae..03e85f14 100644 Binary files a/websocket-sharp/websocket-sharp.pidb and b/websocket-sharp/websocket-sharp.pidb differ