Skip to content

Commit abab24d

Browse files
committed
fix WebSocket.cs
1 parent 0b9e37b commit abab24d

18 files changed

+59
-46
lines changed

websocket-sharp.userprefs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<Properties>
2-
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug_Ubuntu" ctype="Workspace" />
2+
<MonoDevelop.Ide.Workspace ActiveConfiguration="Release" ctype="Workspace" />
33
<MonoDevelop.Ide.Workbench ActiveDocument="websocket-sharp/WebSocket.cs" ctype="Workbench">
44
<Files>
5-
<File FileName="websocket-sharp/WebSocket.cs" Line="249" Column="1" />
5+
<File FileName="websocket-sharp/WebSocket.cs" Line="261" Column="1" />
66
</Files>
77
</MonoDevelop.Ide.Workbench>
88
<MonoDevelop.Ide.DebuggingService.Breakpoints>

websocket-sharp/WebSocket.cs

Lines changed: 57 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -248,53 +248,12 @@ private void createConnection()
248248

249249
private void doHandshake()
250250
{
251-
string path = uri.PathAndQuery;
252-
string host = uri.DnsSafeHost;
253-
string origin = "http://" + host;
254-
255-
int port = ((IPEndPoint)tcpClient.Client.RemoteEndPoint).Port;
256-
if (port != 80)
257-
{
258-
host += ":" + port;
259-
}
260-
261-
string subProtocol = protocol != String.Empty
262251
#if !CHALLENGE
263-
? String.Format("WebSocket-Protocol: {0}\r\n", protocol)
264-
#else
265-
? String.Format("Sec-WebSocket-Protocol: {0}\r\n", protocol)
266-
#endif
267-
: protocol;
268-
#if !CHALLENGE
269-
string secKeys = String.Empty;
270-
string key3ToAscii = String.Empty;
252+
string request = createOpeningHandshake();
271253
#else
272-
Random rand = new Random();
273-
274-
uint key1, key2;
275-
string secKey1 = rand.GenerateSecKey(out key1);
276-
string secKey2 = rand.GenerateSecKey(out key2);
277-
278-
byte[] key3 = new byte[8].InitializeWithPrintableASCII(rand);
279-
280-
string secKeys = "Sec-WebSocket-Key1: {0}\r\n" +
281-
"Sec-WebSocket-Key2: {1}\r\n";
282-
secKeys = String.Format(secKeys, secKey1, secKey2);
283-
284-
string key3ToAscii = Encoding.ASCII.GetString(key3);
285-
286-
byte[] expectedRes = createExpectedRes(key1, key2, key3);
287-
byte[] actualRes = new byte[16];
254+
byte[] expectedRes, actualRes = new byte[16];
255+
string request = createOpeningHandshake(out expectedRes);
288256
#endif
289-
string request = "GET " + path + " HTTP/1.1\r\n" +
290-
"Upgrade: WebSocket\r\n" +
291-
"Connection: Upgrade\r\n" +
292-
subProtocol +
293-
"Host: " + host + "\r\n" +
294-
"Origin: " + origin + "\r\n" +
295-
secKeys +
296-
"\r\n" +
297-
key3ToAscii;
298257
#if DEBUG
299258
Console.WriteLine("WS: Info @doHandshake: Handshake from client: \n{0}", request);
300259
#endif
@@ -374,6 +333,60 @@ private void doHandshake()
374333
ReadyState = WsState.OPEN;
375334
}
376335

336+
#if !CHALLENGE
337+
private string createOpeningHandshake()
338+
#else
339+
private string createOpeningHandshake(out byte[] expectedRes)
340+
#endif
341+
{
342+
string path = uri.PathAndQuery;
343+
string host = uri.DnsSafeHost;
344+
string origin = "http://" + host;
345+
346+
int port = ((IPEndPoint)tcpClient.Client.RemoteEndPoint).Port;
347+
if (port != 80)
348+
{
349+
host += ":" + port;
350+
}
351+
352+
string subProtocol = protocol != String.Empty
353+
#if !CHALLENGE
354+
? String.Format("WebSocket-Protocol: {0}\r\n", protocol)
355+
#else
356+
? String.Format("Sec-WebSocket-Protocol: {0}\r\n", protocol)
357+
#endif
358+
: protocol;
359+
#if !CHALLENGE
360+
string secKeys = String.Empty;
361+
string key3ToAscii = String.Empty;
362+
#else
363+
Random rand = new Random();
364+
365+
uint key1, key2;
366+
string secKey1 = rand.GenerateSecKey(out key1);
367+
string secKey2 = rand.GenerateSecKey(out key2);
368+
369+
byte[] key3 = new byte[8].InitializeWithPrintableASCII(rand);
370+
371+
string secKeys = "Sec-WebSocket-Key1: {0}\r\n" +
372+
"Sec-WebSocket-Key2: {1}\r\n";
373+
secKeys = String.Format(secKeys, secKey1, secKey2);
374+
375+
string key3ToAscii = Encoding.ASCII.GetString(key3);
376+
377+
expectedRes = createExpectedRes(key1, key2, key3);
378+
#endif
379+
return "GET " + path + " HTTP/1.1\r\n" +
380+
"Upgrade: WebSocket\r\n" +
381+
"Connection: Upgrade\r\n" +
382+
subProtocol +
383+
"Host: " + host + "\r\n" +
384+
"Origin: " + origin + "\r\n" +
385+
secKeys +
386+
"\r\n" +
387+
key3ToAscii;
388+
}
389+
377390
private byte[] createExpectedRes(uint key1, uint key2, byte[] key3)
378391
{
379392
byte[] key1Bytes = BitConverter.GetBytes(key1);
0 Bytes
Binary file not shown.
33 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
33 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
83 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
33 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)