@@ -3,7 +3,11 @@ var Duplex = require('stream').Duplex,
3
3
http = require ( 'http' ) ,
4
4
https = require ( 'https' ) ;
5
5
6
- function ProxyStream ( ) {
6
+ function ProxyStream ( options , res , instance ) {
7
+ this . options = options ;
8
+ this . res = res ;
9
+ this . instance = instance ;
10
+
7
11
var self = this ;
8
12
9
13
Duplex . call ( this ) ;
@@ -14,26 +18,37 @@ function ProxyStream() {
14
18
15
19
require ( 'util' ) . inherits ( ProxyStream , Duplex ) ;
16
20
17
- ProxyStream . prototype . onPipe = function ( request ) {
21
+ ProxyStream . prototype . onPipe = function ( req ) {
22
+ this . req = req ;
23
+
18
24
var self = this ;
19
25
20
26
this . proxyReq = ( options . ssl ? https : http ) . request (
21
- common . setupOutgoing ( options . ssl || { } , options , request )
27
+ common . setupOutgoing ( options . ssl || { } , options , req )
22
28
) ;
23
29
24
- this . proxyReq . once ( 'response' , function ( response ) {
25
- self . onResponse ( response ) ;
30
+ this . proxyReq . once ( 'response' , function ( proxyRes ) {
31
+ self . onResponse ( proxyRes ) ;
26
32
} ) ;
27
- this . proxyReq . on ( 'error' , function ( ) { } ) ; // XXX TODO: add error handling
28
- }
33
+ this . proxyReq . on ( 'error' , function ( e ) {
34
+ self . onError ( e ) ;
35
+ } ) ;
36
+ } ;
29
37
30
38
ProxyStream . prototype . onFinish = function ( ) {
31
39
32
- }
40
+ } ;
33
41
34
42
ProxyStream . prototype . onResponse = function ( proxyRes ) {
35
43
this . proxyRes = proxyRes ;
36
- }
44
+ } ;
45
+
46
+ ProxyStream . prototype . onError = function ( e ) {
47
+ if ( this . instance . emit ( 'error' , this . req , this . res , e ) ) return ;
48
+
49
+ this . res . writeHead ( 500 , { 'Content-Type' : 'text/plain' } ) ;
50
+ this . res . end ( 'Internal Server Error' ) ;
51
+ } ;
37
52
38
53
ProxyStream . prototype . _write = function ( chunk , encoding , callback ) {
39
54
this . proxyReq . write ( chunk , encoding , callback ) ;
0 commit comments