@@ -302,7 +302,12 @@ Connection.prototype.readHandshake = function () {
302
302
// Do the handshake and try to connect
303
303
if ( this . buffer . length > Connection . maxBufferLength ) {
304
304
// Too big for a handshake
305
- this . socket . end ( this . server ? 'HTTP/1.1 400 Bad Request\r\n\r\n' : undefined )
305
+ if ( this . server ) {
306
+ this . socket . end ( 'HTTP/1.1 400 Bad Request\r\n\r\n' )
307
+ } else {
308
+ this . socket . end ( )
309
+ this . emit ( 'error' , new Error ( 'Handshake is too big' ) )
310
+ }
306
311
return false
307
312
}
308
313
@@ -359,9 +364,11 @@ Connection.prototype.checkHandshake = function (lines) {
359
364
360
365
// First line
361
366
if ( lines . length < 4 ) {
367
+ this . emit ( 'error' , new Error ( 'Invalid handshake: too short' ) )
362
368
return false
363
369
}
364
370
if ( ! lines [ 0 ] . match ( / ^ H T T P \/ \d \. \d 1 0 1 ( .* ) ? $ / i) ) {
371
+ this . emit ( 'error' , new Error ( 'Invalid handshake: invalid first line format' ) )
365
372
return false
366
373
}
367
374
@@ -372,10 +379,12 @@ Connection.prototype.checkHandshake = function (lines) {
372
379
if ( ! ( 'upgrade' in this . headers ) ||
373
380
! ( 'sec-websocket-accept' in this . headers ) ||
374
381
! ( 'connection' in this . headers ) ) {
382
+ this . emit ( 'error' , new Error ( 'Invalid handshake: missing required headers' ) )
375
383
return false
376
384
}
377
385
if ( this . headers . upgrade . toLowerCase ( ) !== 'websocket' ||
378
386
this . headers . connection . toLowerCase ( ) . split ( ', ' ) . indexOf ( 'upgrade' ) === - 1 ) {
387
+ this . emit ( 'error' , new Error ( 'Invalid handshake: invalid Upgrade or Connection header' ) )
379
388
return false
380
389
}
381
390
key = this . headers [ 'sec-websocket-accept' ]
@@ -385,11 +394,13 @@ Connection.prototype.checkHandshake = function (lines) {
385
394
if ( this . protocols && this . protocols . length ) {
386
395
// The server must choose one from our list
387
396
if ( ! protocol || this . protocols . indexOf ( protocol ) === - 1 ) {
397
+ this . emit ( 'error' , new Error ( 'Invalid handshake: no protocol was negotiated' ) )
388
398
return false
389
399
}
390
400
} else {
391
401
// The server must not choose a protocol
392
402
if ( protocol ) {
403
+ this . emit ( 'error' , new Error ( 'Invalid handshake: no protocol negotiation was expected' ) )
393
404
return false
394
405
}
395
406
}
@@ -399,6 +410,7 @@ Connection.prototype.checkHandshake = function (lines) {
399
410
sha1 = crypto . createHash ( 'sha1' )
400
411
sha1 . end ( this . key + '258EAFA5-E914-47DA-95CA-C5AB0DC85B11' )
401
412
if ( key !== sha1 . read ( ) . toString ( 'base64' ) ) {
413
+ this . emit ( 'error' , new Error ( 'Invalid handshake: hash mismatch' ) )
402
414
return false
403
415
}
404
416
return true
@@ -457,7 +469,6 @@ Connection.prototype.answerHandshake = function (lines) {
457
469
}
458
470
459
471
// Build and send the response
460
-
461
472
sha1 = crypto . createHash ( 'sha1' )
462
473
sha1 . end ( this . key + '258EAFA5-E914-47DA-95CA-C5AB0DC85B11' )
463
474
key = sha1 . read ( ) . toString ( 'base64' )
0 commit comments