fix Ext.cs, WebSocket.cs

This commit is contained in:
sta
2010-10-26 21:30:58 +09:00
parent b683db43fc
commit 5a3545e911
22 changed files with 26 additions and 33 deletions
+2 -2
View File
@@ -1,8 +1,8 @@
<Properties> <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"> <MonoDevelop.Ide.Workbench ActiveDocument="websocket-sharp/WebSocket.cs" ctype="Workbench">
<Files> <Files>
<File FileName="websocket-sharp/WebSocket.cs" Line="264" Column="15" /> <File FileName="websocket-sharp/WebSocket.cs" Line="263" Column="2" />
</Files> </Files>
</MonoDevelop.Ide.Workbench> </MonoDevelop.Ide.Workbench>
<MonoDevelop.Ide.DebuggingService.Breakpoints> <MonoDevelop.Ide.DebuggingService.Breakpoints>
+16 -23
View File
@@ -51,6 +51,12 @@ namespace WebSocketSharp
return ((uint)rand.Next(upper16 + 1) << 16) + (uint)rand.Next(lower16 + 1); return ((uint)rand.Next(upper16 + 1) << 16) + (uint)rand.Next(lower16 + 1);
} }
public static char GeneratePrintableASCIIwithoutSPandNum(this Random rand)
{
int ascii = rand.Next(2) == 0 ? rand.Next(33, 48) : rand.Next(58, 127);
return Convert.ToChar(ascii);
}
public static string GenerateSecKey(this Random rand, out uint key) public static string GenerateSecKey(this Random rand, out uint key)
{ {
int space = rand.Next(1, 13); int space = rand.Next(1, 13);
@@ -59,35 +65,22 @@ namespace WebSocketSharp
key = rand.GenerateKey(space); key = rand.GenerateKey(space);
long mKey = key * space; long mKey = key * space;
char[] mKeyChars = mKey.ToString().ToCharArray(); List<char> secKey = new List<char>(mKey.ToString().ToCharArray());
int mKeyCharsLen = mKeyChars.Length;
int secKeyCharsLen = mKeyCharsLen + space + ascii; int i;
char[] secKeyChars = new char[secKeyCharsLen].InitializeWith(' '); for (int n = 0; n < ascii; n++)
secKeyChars[0] = mKeyChars[0];
secKeyChars[secKeyCharsLen - 1] = mKeyChars[mKeyCharsLen - 1];
int i = 0;
for (int j = 1; j < mKeyCharsLen - 1; j++)
{ {
i = rand.Next(i + 1, secKeyCharsLen - mKeyCharsLen + j + 1); i = rand.Next(secKey.Count + 1);
secKeyChars[i] = mKeyChars[j]; secKey.Insert(i, rand.GeneratePrintableASCIIwithoutSPandNum());
} }
var convToAscii = secKeyChars for (int n = 0; n < space; n++)
.IndexOf(' ')
.OrderBy( x => Guid.NewGuid() )
.Where( (x, idx) => idx < ascii );
int k;
foreach (int index in convToAscii)
{ {
k = rand.Next(2) == 0 ? rand.Next(33, 48) : rand.Next(58, 127); i = rand.Next(1, secKey.Count);
secKeyChars[index] = Convert.ToChar(k); secKey.Insert(i, ' ');
} }
return new String(secKeyChars); return new String(secKey.ToArray());
} }
public static IEnumerable<int> IndexOf<T>(this T[] array, T val) public static IEnumerable<int> IndexOf<T>(this T[] array, T val)
@@ -111,7 +104,7 @@ namespace WebSocketSharp
return array; return array;
} }
public static Byte[] InitializeWithASCII(this Byte[] bytes, Random rand) public static Byte[] InitializeWithPrintableASCII(this Byte[] bytes, Random rand)
{ {
for (int i = 0; i < bytes.Length; i++) for (int i = 0; i < bytes.Length; i++)
{ {
+8 -8
View File
@@ -263,7 +263,7 @@ namespace WebSocketSharp
: protocol; : protocol;
#if !CHALLENGE #if !CHALLENGE
string secKeys = String.Empty; string secKeys = String.Empty;
string key3AsASCII = String.Empty; string key3ToAscii = String.Empty;
#else #else
Random rand = new Random(); Random rand = new Random();
@@ -271,13 +271,13 @@ namespace WebSocketSharp
string secKey1 = rand.GenerateSecKey(out key1); string secKey1 = rand.GenerateSecKey(out key1);
string secKey2 = rand.GenerateSecKey(out key2); string secKey2 = rand.GenerateSecKey(out key2);
byte[] key3 = new byte[8].InitializeWithASCII(rand); byte[] key3 = new byte[8].InitializeWithPrintableASCII(rand);
string secKeys = "Sec-WebSocket-Key1: {0}\r\n" + string secKeys = "Sec-WebSocket-Key1: {0}\r\n" +
"Sec-WebSocket-Key2: {1}\r\n"; "Sec-WebSocket-Key2: {1}\r\n";
secKeys = String.Format(secKeys, secKey1, secKey2); secKeys = String.Format(secKeys, secKey1, secKey2);
string key3AsASCII = Encoding.ASCII.GetString(key3); string key3ToAscii = Encoding.ASCII.GetString(key3);
byte[] expectedRes = createExpectedRes(key1, key2, key3); byte[] expectedRes = createExpectedRes(key1, key2, key3);
byte[] actualRes = new byte[16]; byte[] actualRes = new byte[16];
@@ -290,7 +290,7 @@ namespace WebSocketSharp
"Origin: " + origin + "\r\n" + "Origin: " + origin + "\r\n" +
secKeys + secKeys +
"\r\n" + "\r\n" +
key3AsASCII; key3ToAscii;
#if DEBUG #if DEBUG
Console.WriteLine("WS: Info @doHandshake: Handshake from client: \n{0}", request); Console.WriteLine("WS: Info @doHandshake: Handshake from client: \n{0}", request);
#endif #endif
@@ -354,10 +354,10 @@ namespace WebSocketSharp
Console.WriteLine("WS: Info @doHandshake: Sub protocol: {0}", protocol); Console.WriteLine("WS: Info @doHandshake: Sub protocol: {0}", protocol);
#endif #endif
#if CHALLENGE #if CHALLENGE
string expectedResHexStr = BitConverter.ToString(expectedRes); string expectedResToHexStr = BitConverter.ToString(expectedRes);
string actualResHexStr = BitConverter.ToString(actualRes); string actualResToHexStr = BitConverter.ToString(actualRes);
actualResHexStr.NotEqualsDo(expectedResHexStr, (a, b) => actualResToHexStr.NotEqualsDo(expectedResToHexStr, (a, b) =>
{ {
Console.WriteLine("WS: Error @doHandshake: Invalid challenge response."); Console.WriteLine("WS: Error @doHandshake: Invalid challenge response.");
Console.WriteLine("\texpected: {0}", b); Console.WriteLine("\texpected: {0}", b);
@@ -366,7 +366,7 @@ namespace WebSocketSharp
} }
); );
#if DEBUG #if DEBUG
Console.WriteLine("WS: Info @doHandshake: challenge response: {0}", actualResHexStr); Console.WriteLine("WS: Info @doHandshake: challenge response: {0}", actualResToHexStr);
#endif #endif
#endif #endif
ReadyState = WsState.OPEN; ReadyState = WsState.OPEN;
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.