Permalink
Browse files

fix Ext.cs, WebSocket.cs

  • Loading branch information...
1 parent 240a4e6 commit ab1f09283b77e782a18f1dd63a202c436f9a8cbe @sta committed Nov 9, 2010
Showing with 85 additions and 54 deletions.
  1. +2 −2 websocket-sharp.userprefs
  2. +47 −4 websocket-sharp/Ext.cs
  3. +36 −48 websocket-sharp/WebSocket.cs
  4. BIN websocket-sharp/bin/Debug/websocket-sharp.dll
  5. BIN websocket-sharp/bin/Debug/websocket-sharp.dll.mdb
  6. BIN websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll
  7. BIN websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll.mdb
  8. BIN websocket-sharp/bin/Release/websocket-sharp.dll
  9. BIN websocket-sharp/bin/Release_Ubuntu/websocket-sharp.dll
  10. BIN websocket-sharp/websocket-sharp.pidb
  11. BIN wsclient/bin/Debug/websocket-sharp.dll
  12. BIN wsclient/bin/Debug/websocket-sharp.dll.mdb
  13. BIN wsclient/bin/Debug/wsclient.exe
  14. BIN wsclient/bin/Debug/wsclient.exe.mdb
  15. BIN wsclient/bin/Debug_Ubuntu/websocket-sharp.dll
  16. BIN wsclient/bin/Debug_Ubuntu/websocket-sharp.dll.mdb
  17. BIN wsclient/bin/Debug_Ubuntu/wsclient.exe
  18. BIN wsclient/bin/Debug_Ubuntu/wsclient.exe.mdb
  19. BIN wsclient/bin/Release/websocket-sharp.dll
  20. BIN wsclient/bin/Release/wsclient.exe
  21. BIN wsclient/bin/Release_Ubuntu/websocket-sharp.dll
  22. BIN wsclient/bin/Release_Ubuntu/wsclient.exe
  23. BIN wsclient1/bin/Debug/websocket-sharp.dll
  24. BIN wsclient1/bin/Debug/websocket-sharp.dll.mdb
  25. BIN wsclient1/bin/Debug/wsclient1.exe
  26. BIN wsclient1/bin/Debug/wsclient1.exe.mdb
  27. BIN wsclient1/bin/Debug_Ubuntu/websocket-sharp.dll
  28. BIN wsclient1/bin/Debug_Ubuntu/websocket-sharp.dll.mdb
  29. BIN wsclient1/bin/Debug_Ubuntu/wsclient1.exe
  30. BIN wsclient1/bin/Debug_Ubuntu/wsclient1.exe.mdb
  31. BIN wsclient1/bin/Release/websocket-sharp.dll
  32. BIN wsclient1/bin/Release/wsclient1.exe
  33. BIN wsclient1/bin/Release_Ubuntu/websocket-sharp.dll
  34. BIN wsclient1/bin/Release_Ubuntu/wsclient1.exe
@@ -1,8 +1,8 @@
<Properties>
- <MonoDevelop.Ide.Workspace ActiveConfiguration="Debug_Ubuntu" ctype="Workspace" />
+ <MonoDevelop.Ide.Workspace ActiveConfiguration="Release_Ubuntu" ctype="Workspace" />
<MonoDevelop.Ide.Workbench ActiveDocument="websocket-sharp/WebSocket.cs" ctype="Workbench">
<Files>
- <File FileName="websocket-sharp/WebSocket.cs" Line="142" Column="22" />
+ <File FileName="websocket-sharp/WebSocket.cs" Line="282" Column="1" />
</Files>
</MonoDevelop.Ide.Workbench>
<MonoDevelop.Ide.DebuggingService.Breakpoints>
View
@@ -34,6 +34,22 @@ namespace WebSocketSharp
{
public static class Ext
{
+ public static bool AreNotEqualDo(
+ this string expected,
+ string actual,
+ Func<string, string, string> 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<byte> dist)
{
byte b = (byte)asByte;
@@ -83,7 +99,7 @@ public static string GenerateSecKey(this Random rand, out uint key)
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 @@ public static Byte[] InitializeWithPrintableASCII(this Byte[] bytes, Random rand
return bytes;
}
- public static void AreNotEqualDo(this string expected, string actual, Action<string, string> 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<string, Func<string, string, string>> func = s =>
+ {
+ 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<string, string, string> func1 = func("handshake");
+ Func<string, string, string> 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))
{
- act(expected, actual);
+ message = msg;
+ return false;
}
+
+ message = String.Empty;
+ return true;
}
public static void Times(this int n, Action act)
@@ -281,48 +281,24 @@ private void createConnection()
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<byte> rawdata = new List<byte>();
-
- 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)
{
Console.WriteLine("{0}", s);
}
#endif
- Action<string, string> 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 @@ private void doHandshake()
#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 @@ private string createOpeningHandshake(out byte[] expectedRes)
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 @@ private string createOpeningHandshake(out byte[] expectedRes)
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 @@ private byte[] createExpectedRes(uint key1, uint key2, byte[] key3)
return md5.ComputeHash(concatKeys);
}
+ private string[] sendOpeningHandshake(string openingHandshake, out byte[] challengeResponse)
+ {
+ challengeResponse = new byte[16];
+ List<byte> rawdata = new List<byte>();
+
+ 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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit ab1f092

Please sign in to comment.