@@ -77,6 +77,24 @@ module.exports = {
77
77
* @api private
78
78
*/
79
79
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
+
80
98
common . setupSocket ( socket ) ;
81
99
82
100
if ( head && head . length ) socket . unshift ( head ) ;
@@ -93,7 +111,10 @@ module.exports = {
93
111
proxyReq . on ( 'error' , onOutgoingError ) ;
94
112
proxyReq . on ( 'response' , function ( res ) {
95
113
// 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
+ }
97
118
} ) ;
98
119
99
120
proxyReq . on ( 'upgrade' , function ( proxyRes , proxySocket , proxyHead ) {
@@ -119,22 +140,7 @@ module.exports = {
119
140
// Remark: Handle writing the headers to the socket when switching protocols
120
141
// Also handles when a header is an array
121
142
//
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 ) ) ;
138
144
139
145
proxySocket . pipe ( socket ) . pipe ( proxySocket ) ;
140
146
0 commit comments