@@ -32,7 +32,7 @@ module.exports = function proxy(host, options) {
3232 * intercept(data, res, req, function(err, json));
3333 */
3434 var intercept = options . intercept ;
35- var decorateProxyRequest = options . decorateProxyRequest || function ( ) { return arguments ; } ;
35+ var decorateRequest = options . decorateRequest ;
3636 var forwardPath = options . forwardPath ;
3737 var filter = options . filter ;
3838
@@ -50,28 +50,33 @@ module.exports = function proxy(host, options) {
5050 // var hasRequestBody = 'content-type' in req.headers || 'transfer-encoding' in req.headers;
5151 getRawBody ( req , {
5252 length : req . headers [ 'content-length' ] ,
53- limit : '1mb' , // let options do here?
53+ limit : '1mb' // TODO let options do here?
5454 } , function ( err , bodyContent ) {
55- var decoratedRequest ;
55+ if ( err ) return next ( err ) ;
5656
57- if ( err ) return next ( err )
58-
59- decoratedRequest = decorateProxyRequest ( hds , bodyContent ) ;
60- hds = decoratedRequest . headers ;
61- bodyContent = decoratedRequest . body ;
62-
63- if ( bodyContent . length )
64- hds [ 'content-length' ] = bodyContent . length
65-
66- var chunks = [ ] ;
67- var realRequest = http . request ( {
57+ var reqOpt = {
6858 hostname : ( typeof host == 'function' ) ? host ( req ) : host . toString ( ) ,
6959 port : port ,
7060 headers : hds ,
7161 method : req . method ,
72- path : path
73- } , function ( rsp ) {
62+ path : path ,
63+ bodyContent : bodyContent
64+ } ;
65+
66+
67+ if ( decorateRequest )
68+ reqOpt = decorateRequest ( reqOpt ) || reqOpt ;
69+
70+ bodyContent = reqOpt . bodyContent ;
71+ delete reqOpt . bodyContent ;
7472
73+ if ( typeof bodyContent == 'string' )
74+ reqOpt . headers [ 'content-length' ] = Buffer . byteLength ( bodyContent ) ;
75+ else if ( Buffer . isBuffer ( bodyContent ) ) // Buffer
76+ reqOpt . headers [ 'content-length' ] = bodyContent . length ;
77+
78+ var chunks = [ ] ;
79+ var realRequest = http . request ( reqOpt , function ( rsp ) {
7580 var rspData = null ;
7681 rsp . on ( 'data' , function ( chunk ) {
7782 chunks . push ( chunk ) ;
@@ -89,6 +94,20 @@ module.exports = function proxy(host, options) {
8994 if ( err ) {
9095 return next ( err ) ;
9196 }
97+
98+ if ( typeof rsp == 'string' )
99+ rsp = new Buffer ( rsp , 'utf8' ) ;
100+
101+ if ( ! Buffer . isBuffer ( rsp ) ) {
102+ next ( new Error ( "intercept should return string or buffer as data" ) ) ;
103+ }
104+
105+ if ( ! res . headersSent )
106+ res . set ( 'content-length' , rsp . length ) ;
107+ else if ( rsp . length != rspData . length ) {
108+ next ( new Error ( "'Content-Length' is already sent, the length of response data can not be changed" ) ) ;
109+ }
110+
92111 if ( ! sent )
93112 res . send ( rsp ) ;
94113 } ) ;
@@ -107,6 +126,7 @@ module.exports = function proxy(host, options) {
107126 res . set ( p , rsp . headers [ p ] ) ;
108127 }
109128 }
129+
110130 } ) ;
111131
112132 realRequest . on ( 'error' , function ( e ) {
@@ -132,4 +152,4 @@ function extend(obj, source, skips) {
132152 }
133153
134154 return obj ;
135- } ;
155+ }
0 commit comments