diff --git a/websocket-sharp.userprefs b/websocket-sharp.userprefs index 4075dc26..a1f5581c 100644 --- a/websocket-sharp.userprefs +++ b/websocket-sharp.userprefs @@ -1,8 +1,8 @@  - + - + diff --git a/websocket-sharp/Ext.cs b/websocket-sharp/Ext.cs index f0c318a7..07128ecf 100644 --- a/websocket-sharp/Ext.cs +++ b/websocket-sharp/Ext.cs @@ -34,6 +34,22 @@ namespace WebSocketSharp { public static class Ext { + public static bool AreNotEqualDo( + this string expected, + string actual, + Func func, + out string ret) + { + if (expected != actual) + { + ret = func(expected, actual); + return true; + } + + ret = String.Empty; + return false; + } + public static bool EqualsWithSaveTo(this int asByte, char c, List dist) { byte b = (byte)asByte; @@ -83,7 +99,7 @@ namespace WebSocketSharp return new String(secKey.ToArray()); } - public static Byte[] InitializeWithPrintableASCII(this Byte[] bytes, Random rand) + public static byte[] InitializeWithPrintableASCII(this byte[] bytes, Random rand) { for (int i = 0; i < bytes.Length; i++) { @@ -93,12 +109,39 @@ namespace WebSocketSharp return bytes; } - public static void AreNotEqualDo(this string expected, string actual, Action act) + public static bool IsValid(this string[] response, byte[] expectedCR, byte[] actualCR, out string message) { - if (expected != actual) + string expectedCRtoHexStr = BitConverter.ToString(expectedCR); + string actualCRtoHexStr = BitConverter.ToString(actualCR); + + Func> func = s => { - act(expected, actual); + return (e, a) => + { +#if DEBUG + Console.WriteLine("WS: Error @IsValid: Invalid {0} response.", s); + Console.WriteLine(" expected: {0}", e); + Console.WriteLine(" actual : {0}", a); +#endif + return String.Format("Invalid {0} response: {1}", s, a); + }; + }; + + Func func1 = func("handshake"); + Func func2 = func("challenge"); + + string msg; + if ("HTTP/1.1 101 WebSocket Protocol Handshake".AreNotEqualDo(response[0], func1, out msg) || + "Upgrade: WebSocket".AreNotEqualDo(response[1], func1, out msg) || + "Connection: Upgrade".AreNotEqualDo(response[2], func1, out msg) || + expectedCRtoHexStr.AreNotEqualDo(actualCRtoHexStr, func2, out msg)) + { + message = msg; + return false; } + + message = String.Empty; + return true; } public static void Times(this int n, Action act) diff --git a/websocket-sharp/WebSocket.cs b/websocket-sharp/WebSocket.cs index 6b89aed0..779a4197 100644 --- a/websocket-sharp/WebSocket.cs +++ b/websocket-sharp/WebSocket.cs @@ -281,33 +281,12 @@ namespace WebSocketSharp private void doHandshake() { - byte[] expectedRes, actualRes = new byte[16]; - string request = createOpeningHandshake(out expectedRes); + byte[] expectedCR, actualCR; + string request = createOpeningHandshake(out expectedCR); #if DEBUG Console.WriteLine("WS: Info @doHandshake: Handshake from client: \n{0}", request); #endif - byte[] sendBuffer = Encoding.UTF8.GetBytes(request); - wsStream.Write(sendBuffer, 0, sendBuffer.Length); - - string[] response; - List rawdata = new List(); - - while (true) - { - if (wsStream.ReadByte().EqualsWithSaveTo('\r', rawdata) && - wsStream.ReadByte().EqualsWithSaveTo('\n', rawdata) && - wsStream.ReadByte().EqualsWithSaveTo('\r', rawdata) && - wsStream.ReadByte().EqualsWithSaveTo('\n', rawdata)) - { - wsStream.Read(actualRes, 0, actualRes.Length); - rawdata.AddRange(actualRes); - break; - } - } - - response = Encoding.UTF8.GetString(rawdata.ToArray()) - .Replace("\r\n", "\n").Replace("\n\n", "\n") - .Split('\n'); + string[] response = sendOpeningHandshake(request, out actualCR); #if DEBUG Console.WriteLine("WS: Info @doHandshake: Handshake from server:"); foreach (string s in response) @@ -315,14 +294,11 @@ namespace WebSocketSharp Console.WriteLine("{0}", s); } #endif - Action act = (e, a) => - { - throw new IOException("Invalid handshake response: " + a); - }; - - "HTTP/1.1 101 WebSocket Protocol Handshake".AreNotEqualDo(response[0], act); - "Upgrade: WebSocket".AreNotEqualDo(response[1], act); - "Connection: Upgrade".AreNotEqualDo(response[2], act); + string msg; + if (!(response.IsValid(expectedCR, actualCR, out msg))) + { + throw new IOException(msg); + } for (int i = 3; i < response.Length; i++) { @@ -336,23 +312,10 @@ namespace WebSocketSharp #if DEBUG Console.WriteLine("WS: Info @doHandshake: Sub protocol: {0}", protocol); #endif - string expectedResToHexStr = BitConverter.ToString(expectedRes); - string actualResToHexStr = BitConverter.ToString(actualRes); - - expectedResToHexStr.AreNotEqualDo(actualResToHexStr, (e, a) => - { -#if DEBUG - Console.WriteLine("WS: Error @doHandshake: Invalid challenge response."); - Console.WriteLine("\texpected: {0}", e); - Console.WriteLine("\tactual : {0}", a); -#endif - throw new IOException("Invalid challenge response: " + a); - }); - ReadyState = WsState.OPEN; } - private string createOpeningHandshake(out byte[] expectedRes) + private string createOpeningHandshake(out byte[] expectedCR) { string path = uri.PathAndQuery; string host = uri.DnsSafeHost; @@ -382,7 +345,7 @@ namespace WebSocketSharp string key3ToAscii = Encoding.ASCII.GetString(key3); - expectedRes = createExpectedRes(key1, key2, key3); + expectedCR = createExpectedCR(key1, key2, key3); return "GET " + path + " HTTP/1.1\r\n" + "Upgrade: WebSocket\r\n" + @@ -395,7 +358,7 @@ namespace WebSocketSharp key3ToAscii; } - private byte[] createExpectedRes(uint key1, uint key2, byte[] key3) + private byte[] createExpectedCR(uint key1, uint key2, byte[] key3) { byte[] key1Bytes = BitConverter.GetBytes(key1); byte[] key2Bytes = BitConverter.GetBytes(key2); @@ -409,6 +372,31 @@ namespace WebSocketSharp return md5.ComputeHash(concatKeys); } + private string[] sendOpeningHandshake(string openingHandshake, out byte[] challengeResponse) + { + challengeResponse = new byte[16]; + List rawdata = new List(); + + byte[] sendBuffer = Encoding.UTF8.GetBytes(openingHandshake); + wsStream.Write(sendBuffer, 0, sendBuffer.Length); + + while (true) + { + if (wsStream.ReadByte().EqualsWithSaveTo('\r', rawdata) && + wsStream.ReadByte().EqualsWithSaveTo('\n', rawdata) && + wsStream.ReadByte().EqualsWithSaveTo('\r', rawdata) && + wsStream.ReadByte().EqualsWithSaveTo('\n', rawdata)) + { + wsStream.Read(challengeResponse, 0, challengeResponse.Length); + break; + } + } + + return Encoding.UTF8.GetString(rawdata.ToArray()) + .Replace("\r\n", "\n").Replace("\n\n", "\n") + .Split('\n'); + } + private void message() { #if DEBUG diff --git a/websocket-sharp/bin/Debug/websocket-sharp.dll b/websocket-sharp/bin/Debug/websocket-sharp.dll index fc03a9e0..41001b3a 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 668c5ebe..ca151b40 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 bdb1f5a0..7136652b 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 abc32c45..6c2c977a 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 ff31194c..7efae5ab 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 5045e66a..7d0e2a0c 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 b5e98b78..43599e05 100644 Binary files a/websocket-sharp/websocket-sharp.pidb and b/websocket-sharp/websocket-sharp.pidb differ diff --git a/wsclient/bin/Debug/websocket-sharp.dll b/wsclient/bin/Debug/websocket-sharp.dll index fc03a9e0..41001b3a 100755 Binary files a/wsclient/bin/Debug/websocket-sharp.dll and b/wsclient/bin/Debug/websocket-sharp.dll differ diff --git a/wsclient/bin/Debug/websocket-sharp.dll.mdb b/wsclient/bin/Debug/websocket-sharp.dll.mdb index 668c5ebe..ca151b40 100644 Binary files a/wsclient/bin/Debug/websocket-sharp.dll.mdb and b/wsclient/bin/Debug/websocket-sharp.dll.mdb differ diff --git a/wsclient/bin/Debug/wsclient.exe b/wsclient/bin/Debug/wsclient.exe index 3d83163f..9d0c0fa3 100755 Binary files a/wsclient/bin/Debug/wsclient.exe and b/wsclient/bin/Debug/wsclient.exe differ diff --git a/wsclient/bin/Debug/wsclient.exe.mdb b/wsclient/bin/Debug/wsclient.exe.mdb index ece27c38..d58ff448 100644 Binary files a/wsclient/bin/Debug/wsclient.exe.mdb and b/wsclient/bin/Debug/wsclient.exe.mdb differ diff --git a/wsclient/bin/Debug_Ubuntu/websocket-sharp.dll b/wsclient/bin/Debug_Ubuntu/websocket-sharp.dll index bdb1f5a0..7136652b 100755 Binary files a/wsclient/bin/Debug_Ubuntu/websocket-sharp.dll and b/wsclient/bin/Debug_Ubuntu/websocket-sharp.dll differ diff --git a/wsclient/bin/Debug_Ubuntu/websocket-sharp.dll.mdb b/wsclient/bin/Debug_Ubuntu/websocket-sharp.dll.mdb index abc32c45..6c2c977a 100644 Binary files a/wsclient/bin/Debug_Ubuntu/websocket-sharp.dll.mdb and b/wsclient/bin/Debug_Ubuntu/websocket-sharp.dll.mdb differ diff --git a/wsclient/bin/Debug_Ubuntu/wsclient.exe b/wsclient/bin/Debug_Ubuntu/wsclient.exe index 7b6de2bd..304f5ee7 100755 Binary files a/wsclient/bin/Debug_Ubuntu/wsclient.exe and b/wsclient/bin/Debug_Ubuntu/wsclient.exe differ diff --git a/wsclient/bin/Debug_Ubuntu/wsclient.exe.mdb b/wsclient/bin/Debug_Ubuntu/wsclient.exe.mdb index 63ac6cc4..f6714016 100644 Binary files a/wsclient/bin/Debug_Ubuntu/wsclient.exe.mdb and b/wsclient/bin/Debug_Ubuntu/wsclient.exe.mdb differ diff --git a/wsclient/bin/Release/websocket-sharp.dll b/wsclient/bin/Release/websocket-sharp.dll index ff31194c..7efae5ab 100755 Binary files a/wsclient/bin/Release/websocket-sharp.dll and b/wsclient/bin/Release/websocket-sharp.dll differ diff --git a/wsclient/bin/Release/wsclient.exe b/wsclient/bin/Release/wsclient.exe index 203b1253..97e52216 100755 Binary files a/wsclient/bin/Release/wsclient.exe and b/wsclient/bin/Release/wsclient.exe differ diff --git a/wsclient/bin/Release_Ubuntu/websocket-sharp.dll b/wsclient/bin/Release_Ubuntu/websocket-sharp.dll index 5045e66a..7d0e2a0c 100755 Binary files a/wsclient/bin/Release_Ubuntu/websocket-sharp.dll and b/wsclient/bin/Release_Ubuntu/websocket-sharp.dll differ diff --git a/wsclient/bin/Release_Ubuntu/wsclient.exe b/wsclient/bin/Release_Ubuntu/wsclient.exe index 4f7bf6a9..06b8c077 100755 Binary files a/wsclient/bin/Release_Ubuntu/wsclient.exe and b/wsclient/bin/Release_Ubuntu/wsclient.exe differ diff --git a/wsclient1/bin/Debug/websocket-sharp.dll b/wsclient1/bin/Debug/websocket-sharp.dll index fc03a9e0..41001b3a 100755 Binary files a/wsclient1/bin/Debug/websocket-sharp.dll and b/wsclient1/bin/Debug/websocket-sharp.dll differ diff --git a/wsclient1/bin/Debug/websocket-sharp.dll.mdb b/wsclient1/bin/Debug/websocket-sharp.dll.mdb index 668c5ebe..ca151b40 100644 Binary files a/wsclient1/bin/Debug/websocket-sharp.dll.mdb and b/wsclient1/bin/Debug/websocket-sharp.dll.mdb differ diff --git a/wsclient1/bin/Debug/wsclient1.exe b/wsclient1/bin/Debug/wsclient1.exe index cf4e6f0d..cf137213 100755 Binary files a/wsclient1/bin/Debug/wsclient1.exe and b/wsclient1/bin/Debug/wsclient1.exe differ diff --git a/wsclient1/bin/Debug/wsclient1.exe.mdb b/wsclient1/bin/Debug/wsclient1.exe.mdb index 72b32853..6696dacf 100644 Binary files a/wsclient1/bin/Debug/wsclient1.exe.mdb and b/wsclient1/bin/Debug/wsclient1.exe.mdb differ diff --git a/wsclient1/bin/Debug_Ubuntu/websocket-sharp.dll b/wsclient1/bin/Debug_Ubuntu/websocket-sharp.dll index bdb1f5a0..7136652b 100755 Binary files a/wsclient1/bin/Debug_Ubuntu/websocket-sharp.dll and b/wsclient1/bin/Debug_Ubuntu/websocket-sharp.dll differ diff --git a/wsclient1/bin/Debug_Ubuntu/websocket-sharp.dll.mdb b/wsclient1/bin/Debug_Ubuntu/websocket-sharp.dll.mdb index abc32c45..6c2c977a 100644 Binary files a/wsclient1/bin/Debug_Ubuntu/websocket-sharp.dll.mdb and b/wsclient1/bin/Debug_Ubuntu/websocket-sharp.dll.mdb differ diff --git a/wsclient1/bin/Debug_Ubuntu/wsclient1.exe b/wsclient1/bin/Debug_Ubuntu/wsclient1.exe index e660a28e..20fc61d8 100755 Binary files a/wsclient1/bin/Debug_Ubuntu/wsclient1.exe and b/wsclient1/bin/Debug_Ubuntu/wsclient1.exe differ diff --git a/wsclient1/bin/Debug_Ubuntu/wsclient1.exe.mdb b/wsclient1/bin/Debug_Ubuntu/wsclient1.exe.mdb index 90b7ae43..ff7736f9 100644 Binary files a/wsclient1/bin/Debug_Ubuntu/wsclient1.exe.mdb and b/wsclient1/bin/Debug_Ubuntu/wsclient1.exe.mdb differ diff --git a/wsclient1/bin/Release/websocket-sharp.dll b/wsclient1/bin/Release/websocket-sharp.dll index ff31194c..7efae5ab 100755 Binary files a/wsclient1/bin/Release/websocket-sharp.dll and b/wsclient1/bin/Release/websocket-sharp.dll differ diff --git a/wsclient1/bin/Release/wsclient1.exe b/wsclient1/bin/Release/wsclient1.exe index 038b40d2..42562233 100755 Binary files a/wsclient1/bin/Release/wsclient1.exe and b/wsclient1/bin/Release/wsclient1.exe differ diff --git a/wsclient1/bin/Release_Ubuntu/websocket-sharp.dll b/wsclient1/bin/Release_Ubuntu/websocket-sharp.dll index 5045e66a..7d0e2a0c 100755 Binary files a/wsclient1/bin/Release_Ubuntu/websocket-sharp.dll and b/wsclient1/bin/Release_Ubuntu/websocket-sharp.dll differ diff --git a/wsclient1/bin/Release_Ubuntu/wsclient1.exe b/wsclient1/bin/Release_Ubuntu/wsclient1.exe index bb2e5fe2..98bfdc40 100755 Binary files a/wsclient1/bin/Release_Ubuntu/wsclient1.exe and b/wsclient1/bin/Release_Ubuntu/wsclient1.exe differ