33
33
using System ;
34
34
using System . Collections . Generic ;
35
35
using System . IO ;
36
- using System . Linq ;
37
36
using System . Net ;
38
37
using System . Net . Security ;
39
38
using System . Net . Sockets ;
40
39
using System . Text ;
41
40
using System . Threading ;
42
- using System . Security . Cryptography ;
43
41
44
42
namespace WebSocketSharp
45
43
{
@@ -248,12 +246,7 @@ private void createConnection()
248
246
249
247
private void doHandshake ( )
250
248
{
251
- #if ! CHALLENGE
252
249
string request = createOpeningHandshake ( ) ;
253
- #else
254
- byte [ ] expectedRes , actualRes = new byte [ 16 ] ;
255
- string request = createOpeningHandshake ( out expectedRes ) ;
256
- #endif
257
250
#if DEBUG
258
251
Console . WriteLine ( "WS: Info @doHandshake: Handshake from client: \n {0}" , request ) ;
259
252
#endif
@@ -270,10 +263,6 @@ private void doHandshake()
270
263
wsStream . ReadByte ( ) . EqualsWithSaveTo ( '\r ' , rawdata ) &&
271
264
wsStream . ReadByte ( ) . EqualsWithSaveTo ( '\n ' , rawdata ) )
272
265
{
273
- #if CHALLENGE
274
- wsStream . Read ( actualRes , 0 , actualRes . Length ) ;
275
- rawdata . AddRange ( actualRes ) ;
276
- #endif
277
266
break ;
278
267
}
279
268
}
@@ -292,21 +281,14 @@ private void doHandshake()
292
281
{
293
282
throw new IOException ( "Invalid handshake response: " + a ) ;
294
283
} ;
295
- #if ! CHALLENGE
284
+
296
285
"HTTP/1.1 101 Web Socket Protocol Handshake" . NotEqualsDo ( response [ 0 ] , act ) ;
297
- #else
298
- "HTTP/1.1 101 WebSocket Protocol Handshake" . NotEqualsDo ( response [ 0 ] , act ) ;
299
- #endif
300
286
"Upgrade: WebSocket" . NotEqualsDo ( response [ 1 ] , act ) ;
301
287
"Connection: Upgrade" . NotEqualsDo ( response [ 2 ] , act ) ;
302
288
303
289
for ( int i = 3 ; i < response . Length ; i ++ )
304
290
{
305
- #if ! CHALLENGE
306
291
if ( response [ i ] . Contains ( "WebSocket-Protocol:" ) )
307
- #else
308
- if ( response [ i ] . Contains ( "Sec-WebSocket-Protocol:" ) )
309
- #endif
310
292
{
311
293
int j = response [ i ] . IndexOf ( ":" ) ;
312
294
protocol = response [ i ] . Substring ( j + 1 ) . Trim ( ) ;
@@ -315,29 +297,11 @@ private void doHandshake()
315
297
}
316
298
#if DEBUG
317
299
Console . WriteLine ( "WS: Info @doHandshake: Sub protocol: {0}" , protocol ) ;
318
- #endif
319
- #if CHALLENGE
320
- string expectedResToHexStr = BitConverter . ToString ( expectedRes ) ;
321
- string actualResToHexStr = BitConverter . ToString ( actualRes ) ;
322
-
323
- expectedResToHexStr . NotEqualsDo ( actualResToHexStr , ( e , a ) =>
324
- {
325
- #if DEBUG
326
- Console . WriteLine ( "WS: Error @doHandshake: Invalid challenge response." ) ;
327
- Console . WriteLine ( "\t expected: {0}" , e ) ;
328
- Console . WriteLine ( "\t actual : {0}" , a ) ;
329
- #endif
330
- throw new IOException ( "Invalid challenge response: " + a ) ;
331
- } ) ;
332
300
#endif
333
301
ReadyState = WsState . OPEN ;
334
302
}
335
303
336
- #if ! CHALLENGE
337
304
private string createOpeningHandshake ( )
338
- #else
339
- private string createOpeningHandshake ( out byte [ ] expectedRes )
340
- #endif
341
305
{
342
306
string path = uri . PathAndQuery ;
343
307
string host = uri . DnsSafeHost ;
@@ -350,43 +314,18 @@ private string createOpeningHandshake(out byte[] expectedRes)
350
314
}
351
315
352
316
string subProtocol = protocol != String . Empty
353
- #if ! CHALLENGE
354
317
? String . Format ( "WebSocket-Protocol: {0}\r \n " , protocol )
355
- #else
356
- ? String . Format ( "Sec-WebSocket-Protocol: {0}\r \n " , protocol )
357
- #endif
358
318
: 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
319
377
- expectedRes = createExpectedRes ( key1 , key2 , key3 ) ;
378
- #endif
379
320
return "GET " + path + " HTTP/1.1\r \n " +
380
321
"Upgrade: WebSocket\r \n " +
381
322
"Connection: Upgrade\r \n " +
382
323
subProtocol +
383
324
"Host: " + host + "\r \n " +
384
325
"Origin: " + origin + "\r \n " +
385
- secKeys +
386
- "\r \n " +
387
- key3ToAscii ;
326
+ "\r \n " ;
388
327
}
389
-
328
+ /*
390
329
private byte[] createExpectedRes(uint key1, uint key2, byte[] key3)
391
330
{
392
331
byte[] key1Bytes = BitConverter.GetBytes(key1);
@@ -400,7 +339,7 @@ private byte[] createExpectedRes(uint key1, uint key2, byte[] key3)
400
339
MD5 md5 = MD5.Create();
401
340
return md5.ComputeHash(concatKeys);
402
341
}
403
-
342
+ */
404
343
private void message ( )
405
344
{
406
345
#if DEBUG
0 commit comments