@@ -115,7 +115,21 @@ function Manager (server, options) {
115115 server . removeAllListeners ( 'request' ) ;
116116
117117 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 ) ;
119133 } ) ;
120134
121135 server . on ( 'upgrade' , function ( req , socket , head ) {
@@ -579,7 +593,7 @@ Manager.prototype.onDisconnect = function (id) {
579593 * @api private
580594 */
581595
582- Manager . prototype . handleRequest = function ( req , res ) {
596+ Manager . prototype . handleRequest = function ( req , res , socket ) {
583597 var data = this . checkRequest ( req ) ;
584598
585599 if ( ! data ) {
@@ -610,9 +624,9 @@ Manager.prototype.handleRequest = function (req, res) {
610624 this . log . info ( 'client protocol version unsupported' ) ;
611625 } else {
612626 if ( data . id ) {
613- this . handleHTTPRequest ( data , req , res ) ;
627+ this . handleHTTPRequest ( data , req , res , socket ) ;
614628 } else {
615- this . handleHandshake ( data , req , res ) ;
629+ this . handleHandshake ( data , req , res , socket ) ;
616630 }
617631 }
618632} ;
@@ -637,7 +651,7 @@ Manager.prototype.handleUpgrade = function (req, socket, head) {
637651 }
638652
639653 req . head = head ;
640- this . handleClient ( data , req ) ;
654+ this . handleClient ( data , req , socket ) ;
641655 req . head = null ;
642656} ;
643657
@@ -647,9 +661,9 @@ Manager.prototype.handleUpgrade = function (req, socket, head) {
647661 * @api private
648662 */
649663
650- Manager . prototype . handleHTTPRequest = function ( data , req , res ) {
664+ Manager . prototype . handleHTTPRequest = function ( data , req , res , socket ) {
651665 req . res = res ;
652- this . handleClient ( data , req ) ;
666+ this . handleClient ( data , req , socket ) ;
653667} ;
654668
655669/**
@@ -658,11 +672,7 @@ Manager.prototype.handleHTTPRequest = function (data, req, res) {
658672 * @api private
659673 */
660674
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 ) {
666676 // handle sync disconnect xhrs
667677 if ( undefined != data . query . disconnect ) {
668678 if ( this . transports [ data . id ] && this . transports [ data . id ] . open ) {
@@ -677,16 +687,16 @@ Manager.prototype.handleClient = function (data, req) {
677687
678688 if ( ! ~ this . get ( 'transports' ) . indexOf ( data . transport ) ) {
679689 this . log . warn ( 'unknown transport: "' + data . transport + '"' ) ;
680- req . connection . end ( ) ;
690+ socket . end ( ) ;
681691 return ;
682692 }
683693
684- var transport = new transports [ data . transport ] ( this , data , req )
694+ var transport = new transports [ data . transport ] ( this , data , req , socket )
685695 , handshaken = this . handshaken [ data . id ] ;
686696
687697 if ( transport . disconnected ) {
688698 // failed during transport setup
689- req . connection . end ( ) ;
699+ socket . end ( ) ;
690700 return ;
691701 }
692702 if ( handshaken ) {
@@ -713,7 +723,7 @@ Manager.prototype.handleClient = function (data, req) {
713723 // initialize the socket for all namespaces
714724 for ( var i in this . namespaces ) {
715725 if ( this . namespaces . hasOwnProperty ( i ) ) {
716- var socket = this . namespaces [ i ] . socket ( data . id , true ) ;
726+ this . namespaces [ i ] . socket ( data . id , true ) ;
717727
718728 // echo back connect packet and fire connection event
719729 if ( i === '' ) {
@@ -762,7 +772,7 @@ Manager.prototype.generateId = function () {
762772 * @api private
763773 */
764774
765- Manager . prototype . handleHandshake = function ( data , req , res ) {
775+ Manager . prototype . handleHandshake = function ( data , req , res , socket ) {
766776 var self = this
767777 , origin = req . headers . origin
768778 , headers = {
@@ -789,7 +799,7 @@ Manager.prototype.handleHandshake = function (data, req, res) {
789799 return ;
790800 }
791801
792- var handshakeData = this . handshakeData ( data ) ;
802+ var handshakeData = this . handshakeData ( data , socket ) ;
793803
794804 if ( origin ) {
795805 // https://developer.mozilla.org/En/HTTP_Access_Control
@@ -835,9 +845,8 @@ Manager.prototype.handleHandshake = function (data, req, res) {
835845 * @api private
836846 */
837847
838- Manager . prototype . handshakeData = function ( data ) {
839- var connection = data . request . connection
840- , connectionAddress
848+ Manager . prototype . handshakeData = function ( data , connection ) {
849+ var connectionAddress
841850 , date = new Date ;
842851
843852 if ( connection . remoteAddress ) {
@@ -859,7 +868,7 @@ Manager.prototype.handshakeData = function (data) {
859868 , query : data . query
860869 , url : data . request . url
861870 , xdomain : ! ! data . request . headers . origin
862- , secure : data . request . connection . secure
871+ , secure : connection . secure
863872 , issued : + date
864873 } ;
865874} ;
0 commit comments