Compare commits

...

2 Commits

Author SHA1 Message Date
sta
9029714f17 fix WebSocket.cs 2010-10-28 16:28:08 +09:00
sta
fb0485faec branched to draft75 2010-10-28 16:20:17 +09:00
25 changed files with 7 additions and 143 deletions

View File

@ -1,8 +1,8 @@
<Properties>
<MonoDevelop.Ide.Workspace ActiveConfiguration="Release" ctype="Workspace" />
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug_Ubuntu" ctype="Workspace" />
<MonoDevelop.Ide.Workbench ActiveDocument="websocket-sharp/WebSocket.cs" ctype="Workbench">
<Files>
<File FileName="websocket-sharp/WebSocket.cs" Line="261" Column="1" />
<File FileName="websocket-sharp/WebSocket.cs" Line="247" Column="31" />
</Files>
</MonoDevelop.Ide.Workbench>
<MonoDevelop.Ide.DebuggingService.Breakpoints>

View File

@ -17,7 +17,7 @@ using System.Runtime.CompilerServices;
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
// and "{Major}.{Minor}.{Build}.*" will update just the revision.
[assembly: AssemblyVersion("1.0.0.*")]
[assembly: AssemblyVersion("0.9.9.*")]
// The following attributes are used to specify the signing key for the assembly,
// if desired. See the Mono documentation for more information about signing.

View File

@ -28,7 +28,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace WebSocketSharp
{
@ -41,58 +40,6 @@ namespace WebSocketSharp
return b == Convert.ToByte(c);
}
public static uint GenerateKey(this Random rand, int space)
{
uint max = (uint)(0xffffffff / space);
int upper16 = (int)((max & 0xffff0000) >> 16);
int lower16 = (int)(max & 0x0000ffff);
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);
int ascii = rand.Next(1, 13);
key = rand.GenerateKey(space);
long mKey = key * space;
List<char> secKey = new List<char>(mKey.ToString().ToCharArray());
int i;
ascii.Times( () =>
{
i = rand.Next(secKey.Count + 1);
secKey.Insert(i, rand.GeneratePrintableASCIIwithoutSPandNum());
} );
space.Times( () =>
{
i = rand.Next(1, secKey.Count);
secKey.Insert(i, ' ');
} );
return new String(secKey.ToArray());
}
public static Byte[] InitializeWithPrintableASCII(this Byte[] bytes, Random rand)
{
for (int i = 0; i < bytes.Length; i++)
{
bytes[i] = (byte)rand.Next(32, 127);
}
return bytes;
}
public static void NotEqualsDo(this string expected, string actual, Action<string, string> act)
{
if (expected != actual)
@ -100,13 +47,5 @@ namespace WebSocketSharp
act(expected, actual);
}
}
public static void Times(this int n, Action act)
{
for (int i = 0; i < n; i++)
{
act();
}
}
}
}

View File

@ -33,13 +33,11 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Security.Cryptography;
namespace WebSocketSharp
{
@ -248,12 +246,7 @@ namespace WebSocketSharp
private void doHandshake()
{
#if !CHALLENGE
string request = createOpeningHandshake();
#else
byte[] expectedRes, actualRes = new byte[16];
string request = createOpeningHandshake(out expectedRes);
#endif
#if DEBUG
Console.WriteLine("WS: Info @doHandshake: Handshake from client: \n{0}", request);
#endif
@ -270,10 +263,6 @@ namespace WebSocketSharp
wsStream.ReadByte().EqualsWithSaveTo('\r', rawdata) &&
wsStream.ReadByte().EqualsWithSaveTo('\n', rawdata))
{
#if CHALLENGE
wsStream.Read(actualRes, 0, actualRes.Length);
rawdata.AddRange(actualRes);
#endif
break;
}
}
@ -292,21 +281,14 @@ namespace WebSocketSharp
{
throw new IOException("Invalid handshake response: " + a);
};
#if !CHALLENGE
"HTTP/1.1 101 Web Socket Protocol Handshake".NotEqualsDo(response[0], act);
#else
"HTTP/1.1 101 WebSocket Protocol Handshake".NotEqualsDo(response[0], act);
#endif
"Upgrade: WebSocket".NotEqualsDo(response[1], act);
"Connection: Upgrade".NotEqualsDo(response[2], act);
for (int i = 3; i < response.Length; i++)
{
#if !CHALLENGE
if (response[i].Contains("WebSocket-Protocol:"))
#else
if (response[i].Contains("Sec-WebSocket-Protocol:"))
#endif
{
int j = response[i].IndexOf(":");
protocol = response[i].Substring(j + 1).Trim();
@ -315,29 +297,11 @@ namespace WebSocketSharp
}
#if DEBUG
Console.WriteLine("WS: Info @doHandshake: Sub protocol: {0}", protocol);
#endif
#if CHALLENGE
string expectedResToHexStr = BitConverter.ToString(expectedRes);
string actualResToHexStr = BitConverter.ToString(actualRes);
expectedResToHexStr.NotEqualsDo(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);
});
#endif
ReadyState = WsState.OPEN;
}
#if !CHALLENGE
private string createOpeningHandshake()
#else
private string createOpeningHandshake(out byte[] expectedRes)
#endif
{
string path = uri.PathAndQuery;
string host = uri.DnsSafeHost;
@ -350,55 +314,16 @@ namespace WebSocketSharp
}
string subProtocol = protocol != String.Empty
#if !CHALLENGE
? String.Format("WebSocket-Protocol: {0}\r\n", protocol)
#else
? String.Format("Sec-WebSocket-Protocol: {0}\r\n", protocol)
#endif
: protocol;
#if !CHALLENGE
string secKeys = String.Empty;
string key3ToAscii = String.Empty;
#else
Random rand = new Random();
uint key1, key2;
string secKey1 = rand.GenerateSecKey(out key1);
string secKey2 = rand.GenerateSecKey(out key2);
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 key3ToAscii = Encoding.ASCII.GetString(key3);
expectedRes = createExpectedRes(key1, key2, key3);
#endif
return "GET " + path + " HTTP/1.1\r\n" +
"Upgrade: WebSocket\r\n" +
"Connection: Upgrade\r\n" +
subProtocol +
"Host: " + host + "\r\n" +
"Origin: " + origin + "\r\n" +
secKeys +
"\r\n" +
key3ToAscii;
}
private byte[] createExpectedRes(uint key1, uint key2, byte[] key3)
{
byte[] key1Bytes = BitConverter.GetBytes(key1);
byte[] key2Bytes = BitConverter.GetBytes(key2);
Array.Reverse(key1Bytes);
Array.Reverse(key2Bytes);
byte[] concatKeys = key1Bytes.Concat(key2Bytes).Concat(key3).ToArray();
MD5 md5 = MD5.Create();
return md5.ComputeHash(concatKeys);
"\r\n";
}
private void message()

View File

@ -18,7 +18,7 @@
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG,CHALLENGE</DefineConstants>
<DefineConstants>DEBUG</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
@ -36,7 +36,7 @@
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug_Ubuntu</OutputPath>
<DefineConstants>DEBUG,CHALLENGE</DefineConstants>
<DefineConstants>DEBUG</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.