@@ -115,7 +115,21 @@ function Manager (server, options) {
115
115
server . removeAllListeners ( 'request' ) ;
116
116
117
117
server . on ( 'request' , function ( req , res ) {
118
- self . handleRequest ( req , res ) ;
118
+ var socket = req . socket ;
119
+ if ( ! socket ) {
120
+ // aborted request
121
+ self . log . warn ( 'bail out from aborted request (req.socket missing)' ) ;
122
+ // nginx uses status code 499 when clients close the connection before a response could be sent.
123
+ // https://github.com/nginx/nginx/blob/bfc5b35827903a3c543b58e4562db8b62021c164/src/http/ngx_http_request.h#L128-L134
124
+ try {
125
+ res . writeHead ( 499 ) ;
126
+ res . end ( ) ;
127
+ } catch ( e ) {
128
+ self . log . warn ( 'cannot reply to aborted request: ' + e . message ) ;
129
+ }
130
+ return ;
131
+ }
132
+ self . handleRequest ( req , res , socket ) ;
119
133
} ) ;
120
134
121
135
server . on ( 'upgrade' , function ( req , socket , head ) {
@@ -579,7 +593,7 @@ Manager.prototype.onDisconnect = function (id) {
579
593
* @api private
580
594
*/
581
595
582
- Manager . prototype . handleRequest = function ( req , res ) {
596
+ Manager . prototype . handleRequest = function ( req , res , socket ) {
583
597
var data = this . checkRequest ( req ) ;
584
598
585
599
if ( ! data ) {
@@ -610,9 +624,9 @@ Manager.prototype.handleRequest = function (req, res) {
610
624
this . log . info ( 'client protocol version unsupported' ) ;
611
625
} else {
612
626
if ( data . id ) {
613
- this . handleHTTPRequest ( data , req , res ) ;
627
+ this . handleHTTPRequest ( data , req , res , socket ) ;
614
628
} else {
615
- this . handleHandshake ( data , req , res ) ;
629
+ this . handleHandshake ( data , req , res , socket ) ;
616
630
}
617
631
}
618
632
} ;
@@ -637,7 +651,7 @@ Manager.prototype.handleUpgrade = function (req, socket, head) {
637
651
}
638
652
639
653
req . head = head ;
640
- this . handleClient ( data , req ) ;
654
+ this . handleClient ( data , req , socket ) ;
641
655
req . head = null ;
642
656
} ;
643
657
@@ -647,9 +661,9 @@ Manager.prototype.handleUpgrade = function (req, socket, head) {
647
661
* @api private
648
662
*/
649
663
650
- Manager . prototype . handleHTTPRequest = function ( data , req , res ) {
664
+ Manager . prototype . handleHTTPRequest = function ( data , req , res , socket ) {
651
665
req . res = res ;
652
- this . handleClient ( data , req ) ;
666
+ this . handleClient ( data , req , socket ) ;
653
667
} ;
654
668
655
669
/**
@@ -658,11 +672,7 @@ Manager.prototype.handleHTTPRequest = function (data, req, res) {
658
672
* @api private
659
673
*/
660
674
661
- Manager . prototype . handleClient = function ( data , req ) {
662
- var socket = req . socket
663
- , store = this . store
664
- , self = this ;
665
-
675
+ Manager . prototype . handleClient = function ( data , req , socket ) {
666
676
// handle sync disconnect xhrs
667
677
if ( undefined != data . query . disconnect ) {
668
678
if ( this . transports [ data . id ] && this . transports [ data . id ] . open ) {
@@ -677,16 +687,16 @@ Manager.prototype.handleClient = function (data, req) {
677
687
678
688
if ( ! ~ this . get ( 'transports' ) . indexOf ( data . transport ) ) {
679
689
this . log . warn ( 'unknown transport: "' + data . transport + '"' ) ;
680
- req . connection . end ( ) ;
690
+ socket . end ( ) ;
681
691
return ;
682
692
}
683
693
684
- var transport = new transports [ data . transport ] ( this , data , req )
694
+ var transport = new transports [ data . transport ] ( this , data , req , socket )
685
695
, handshaken = this . handshaken [ data . id ] ;
686
696
687
697
if ( transport . disconnected ) {
688
698
// failed during transport setup
689
- req . connection . end ( ) ;
699
+ socket . end ( ) ;
690
700
return ;
691
701
}
692
702
if ( handshaken ) {
@@ -713,7 +723,7 @@ Manager.prototype.handleClient = function (data, req) {
713
723
// initialize the socket for all namespaces
714
724
for ( var i in this . namespaces ) {
715
725
if ( this . namespaces . hasOwnProperty ( i ) ) {
716
- var socket = this . namespaces [ i ] . socket ( data . id , true ) ;
726
+ this . namespaces [ i ] . socket ( data . id , true ) ;
717
727
718
728
// echo back connect packet and fire connection event
719
729
if ( i === '' ) {
@@ -762,7 +772,7 @@ Manager.prototype.generateId = function () {
762
772
* @api private
763
773
*/
764
774
765
- Manager . prototype . handleHandshake = function ( data , req , res ) {
775
+ Manager . prototype . handleHandshake = function ( data , req , res , socket ) {
766
776
var self = this
767
777
, origin = req . headers . origin
768
778
, headers = {
@@ -789,7 +799,7 @@ Manager.prototype.handleHandshake = function (data, req, res) {
789
799
return ;
790
800
}
791
801
792
- var handshakeData = this . handshakeData ( data ) ;
802
+ var handshakeData = this . handshakeData ( data , socket ) ;
793
803
794
804
if ( origin ) {
795
805
// https://developer.mozilla.org/En/HTTP_Access_Control
@@ -835,9 +845,8 @@ Manager.prototype.handleHandshake = function (data, req, res) {
835
845
* @api private
836
846
*/
837
847
838
- Manager . prototype . handshakeData = function ( data ) {
839
- var connection = data . request . connection
840
- , connectionAddress
848
+ Manager . prototype . handshakeData = function ( data , connection ) {
849
+ var connectionAddress
841
850
, date = new Date ;
842
851
843
852
if ( connection . remoteAddress ) {
@@ -859,7 +868,7 @@ Manager.prototype.handshakeData = function (data) {
859
868
, query : data . query
860
869
, url : data . request . url
861
870
, xdomain : ! ! data . request . headers . origin
862
- , secure : data . request . connection . secure
871
+ , secure : connection . secure
863
872
, issued : + date
864
873
} ;
865
874
} ;
0 commit comments