@@ -4,14 +4,14 @@ var Duplex = require('stream').Duplex,
4
4
https = require ( 'https' ) ;
5
5
6
6
function ProxyStream ( options , res , instance ) {
7
+ Duplex . call ( this ) ;
8
+
7
9
this . options = options ;
8
10
this . res = res ;
9
11
this . instance = instance ;
10
12
11
13
var self = this ;
12
14
13
- Duplex . call ( this ) ;
14
-
15
15
this . once ( 'pipe' , function ( pipe ) { self . onPipe ( pipe ) ; } ) ;
16
16
this . once ( 'finish' , function ( ) { self . onFinish ( ) ; } ) ;
17
17
}
@@ -23,11 +23,12 @@ ProxyStream.prototype.onPipe = function(req) {
23
23
24
24
var self = this ;
25
25
26
- this . proxyReq = ( options . ssl ? https : http ) . request (
27
- common . setupOutgoing ( options . ssl || { } , options , req )
26
+ this . proxyReq = ( self . options . ssl ? https : http ) . request (
27
+ common . setupOutgoing ( self . options . ssl || { } , self . options , req )
28
28
) ;
29
-
29
+ //console.log(common.setupOutgoing(self.options.ssl || {}, self.options, req));
30
30
this . proxyReq . once ( 'response' , function ( proxyRes ) {
31
+ console . log ( proxyRes ) ;
31
32
self . onResponse ( proxyRes ) ;
32
33
} ) ;
33
34
this . proxyReq . on ( 'error' , function ( e ) {
@@ -36,16 +37,57 @@ ProxyStream.prototype.onPipe = function(req) {
36
37
} ;
37
38
38
39
ProxyStream . prototype . onFinish = function ( ) {
39
-
40
+ this . proxyReq . end ( ) ;
40
41
} ;
41
42
42
43
ProxyStream . prototype . onResponse = function ( proxyRes ) {
43
44
this . proxyRes = proxyRes ;
45
+
46
+ // rewrite
47
+ if ( req . httpVersion === '1.0' ) {
48
+ res . headers . connection = req . headers . connection || 'close' ;
49
+ }
50
+ else if ( ! res . headers . connection ) {
51
+ res . headers . connection = req . headers . connection || 'keep-alive' ;
52
+ }
53
+
54
+ if ( req . httpVersion === '1.0' || ( req . method === 'DELETE' && ! req . headers [ 'content-length' ] ) ) {
55
+ delete res . headers [ 'transfer-encoding' ] ;
56
+ }
57
+
58
+ if ( ~ [ 301 , 302 ] . indexOf ( res . statusCode ) && typeof res . headers . location !== 'undefined' ) {
59
+ var location = url . parse ( res . headers . location ) ;
60
+ if (
61
+ location . host === req . headers . host &&
62
+ (
63
+ source . https && ! target . https ||
64
+ target . https && ! source . https
65
+ )
66
+ ) {
67
+ res . headers . location = res . headers . location . replace ( / ^ h t t p s \: / , 'http:' ) ;
68
+ }
69
+ }
70
+
71
+ self . emit ( 'proxyResponse' , req , response , res ) ;
72
+
73
+ Object . keys ( res . headers ) . forEach ( function ( key ) {
74
+ response . setHeader ( key , res . headers [ key ] ) ;
75
+ } ) ;
76
+ response . writeHead ( response . statusCode ) ;
77
+
78
+ res . on ( 'readable' , function ( ) {
79
+ self . read ( 0 ) ;
80
+ } ) ;
81
+
82
+ res . on ( 'end' , function ( ) {
83
+ self . push ( null ) ;
84
+ } ) ;
85
+ self . emit ( 'readable' ) ;
44
86
} ;
45
87
46
88
ProxyStream . prototype . onError = function ( e ) {
47
- if ( this . instance . emit ( 'error ' , this . req , this . res , e ) ) return ;
48
-
89
+ if ( this . instance . emit ( 'proxyError ' , this . req , this . res , e ) ) return ;
90
+
49
91
this . res . writeHead ( 500 , { 'Content-Type' : 'text/plain' } ) ;
50
92
this . res . end ( 'Internal Server Error' ) ;
51
93
} ;
@@ -60,3 +102,4 @@ ProxyStream.prototype._read = function(size) {
60
102
this . push ( chunk ) ;
61
103
} ;
62
104
105
+ module . exports = ProxyStream ;
0 commit comments