@@ -77,6 +77,24 @@ module.exports = {
7777 * @api private
7878 */
7979 stream : function stream ( req , socket , options , head , server , clb ) {
80+
81+ var createHttpHeader = function ( line , headers ) {
82+ return Object . keys ( headers ) . reduce ( function ( head , key ) {
83+ var value = headers [ key ] ;
84+
85+ if ( ! Array . isArray ( value ) ) {
86+ head . push ( key + ': ' + value ) ;
87+ return head ;
88+ }
89+
90+ for ( var i = 0 ; i < value . length ; i ++ ) {
91+ head . push ( key + ': ' + value [ i ] ) ;
92+ }
93+ return head ;
94+ } , [ line ] )
95+ . join ( '\r\n' ) + '\r\n\r\n' ;
96+ }
97+
8098 common . setupSocket ( socket ) ;
8199
82100 if ( head && head . length ) socket . unshift ( head ) ;
@@ -93,7 +111,10 @@ module.exports = {
93111 proxyReq . on ( 'error' , onOutgoingError ) ;
94112 proxyReq . on ( 'response' , function ( res ) {
95113 // if upgrade event isn't going to happen, close the socket
96- if ( ! res . upgrade ) socket . end ( ) ;
114+ if ( ! res . upgrade ) {
115+ socket . write ( createHttpHeader ( 'HTTP/' + res . httpVersion + ' ' + res . statusCode + ' ' + res . statusMessage , res . headers ) ) ;
116+ res . pipe ( socket ) ;
117+ }
97118 } ) ;
98119
99120 proxyReq . on ( 'upgrade' , function ( proxyRes , proxySocket , proxyHead ) {
@@ -119,22 +140,7 @@ module.exports = {
119140 // Remark: Handle writing the headers to the socket when switching protocols
120141 // Also handles when a header is an array
121142 //
122- socket . write (
123- Object . keys ( proxyRes . headers ) . reduce ( function ( head , key ) {
124- var value = proxyRes . headers [ key ] ;
125-
126- if ( ! Array . isArray ( value ) ) {
127- head . push ( key + ': ' + value ) ;
128- return head ;
129- }
130-
131- for ( var i = 0 ; i < value . length ; i ++ ) {
132- head . push ( key + ': ' + value [ i ] ) ;
133- }
134- return head ;
135- } , [ 'HTTP/1.1 101 Switching Protocols' ] )
136- . join ( '\r\n' ) + '\r\n\r\n'
137- ) ;
143+ socket . write ( createHttpHeader ( 'HTTP/1.1 101 Switching Protocols' , proxyRes . headers ) ) ;
138144
139145 proxySocket . pipe ( socket ) . pipe ( proxySocket ) ;
140146
0 commit comments