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

View File

@ -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="264" Column="15" />
<File FileName="websocket-sharp/WebSocket.cs" Line="263" Column="2" />
</Files>
</MonoDevelop.Ide.Workbench>
<MonoDevelop.Ide.DebuggingService.Breakpoints>

View File

@ -51,6 +51,12 @@ namespace WebSocketSharp
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)
{
int space = rand.Next(1, 13);
@ -59,35 +65,22 @@ namespace WebSocketSharp
key = rand.GenerateKey(space);
long mKey = key * space;
char[] mKeyChars = mKey.ToString().ToCharArray();
int mKeyCharsLen = mKeyChars.Length;
List<char> secKey = new List<char>(mKey.ToString().ToCharArray());
int secKeyCharsLen = mKeyCharsLen + space + ascii;
char[] secKeyChars = new char[secKeyCharsLen].InitializeWith(' ');
secKeyChars[0] = mKeyChars[0];
secKeyChars[secKeyCharsLen - 1] = mKeyChars[mKeyCharsLen - 1];
int i = 0;
for (int j = 1; j < mKeyCharsLen - 1; j++)
int i;
for (int n = 0; n < ascii; n++)
{
i = rand.Next(i + 1, secKeyCharsLen - mKeyCharsLen + j + 1);
secKeyChars[i] = mKeyChars[j];
i = rand.Next(secKey.Count + 1);
secKey.Insert(i, rand.GeneratePrintableASCIIwithoutSPandNum());
}
var convToAscii = secKeyChars
.IndexOf(' ')
.OrderBy( x => Guid.NewGuid() )
.Where( (x, idx) => idx < ascii );
int k;
foreach (int index in convToAscii)
for (int n = 0; n < space; n++)
{
k = rand.Next(2) == 0 ? rand.Next(33, 48) : rand.Next(58, 127);
secKeyChars[index] = Convert.ToChar(k);
i = rand.Next(1, secKey.Count);
secKey.Insert(i, ' ');
}
return new String(secKeyChars);
return new String(secKey.ToArray());
}
public static IEnumerable<int> IndexOf<T>(this T[] array, T val)
@ -111,7 +104,7 @@ namespace WebSocketSharp
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++)
{

View File

@ -263,7 +263,7 @@ namespace WebSocketSharp
: protocol;
#if !CHALLENGE
string secKeys = String.Empty;
string key3AsASCII = String.Empty;
string key3ToAscii = String.Empty;
#else
Random rand = new Random();
@ -271,13 +271,13 @@ namespace WebSocketSharp
string secKey1 = rand.GenerateSecKey(out key1);
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" +
"Sec-WebSocket-Key2: {1}\r\n";
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[] actualRes = new byte[16];
@ -290,7 +290,7 @@ namespace WebSocketSharp
"Origin: " + origin + "\r\n" +
secKeys +
"\r\n" +
key3AsASCII;
key3ToAscii;
#if DEBUG
Console.WriteLine("WS: Info @doHandshake: Handshake from client: \n{0}", request);
#endif
@ -354,10 +354,10 @@ namespace WebSocketSharp
Console.WriteLine("WS: Info @doHandshake: Sub protocol: {0}", protocol);
#endif
#if CHALLENGE
string expectedResHexStr = BitConverter.ToString(expectedRes);
string actualResHexStr = BitConverter.ToString(actualRes);
string expectedResToHexStr = BitConverter.ToString(expectedRes);
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("\texpected: {0}", b);
@ -366,7 +366,7 @@ namespace WebSocketSharp
}
);
#if DEBUG
Console.WriteLine("WS: Info @doHandshake: challenge response: {0}", actualResHexStr);
Console.WriteLine("WS: Info @doHandshake: challenge response: {0}", actualResToHexStr);
#endif
#endif
ReadyState = WsState.OPEN;

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.