@@ -163,6 +163,10 @@ RoutingProxy.prototype.close = function () {
163163 } ) ;
164164} ;
165165
166+ function getProto ( req ) {
167+ return req . isSpdy ? 'https' : ( req . connection . pair ? 'https' : 'http' ) ;
168+ }
169+
166170//
167171// ### function proxyRequest (req, res, [port, host, paused])
168172// #### @req {ServerRequest} Incoming HTTP Request to proxy.
@@ -176,7 +180,41 @@ RoutingProxy.prototype.close = function () {
176180//
177181RoutingProxy . prototype . proxyRequest = function ( req , res , options ) {
178182 options = options || { } ;
179-
183+
184+ //
185+ // Add common proxy headers to the request so that they can
186+ // be availible to the proxy target server. If the proxy is
187+ // part of proxy chain it will append the address:
188+ //
189+ // * `x-forwarded-for`: IP Address of the original request
190+ // * `x-forwarded-proto`: Protocol of the original request
191+ // * `x-forwarded-port`: Port of the original request.
192+ //
193+ if ( this . enable . xforward && req . connection && req . socket ) {
194+ if ( req . headers [ 'x-forwarded-for' ] ) {
195+ var addressToAppend = "," + req . connection . remoteAddress || req . socket . remoteAddress ;
196+ req . headers [ 'x-forwarded-for' ] += addressToAppend ;
197+ }
198+ else {
199+ req . headers [ 'x-forwarded-for' ] = req . connection . remoteAddress || req . socket . remoteAddress ;
200+ }
201+
202+ if ( req . headers [ 'x-forwarded-port' ] ) {
203+ var portToAppend = "," + req . connection . remotePort || req . socket . remotePort ;
204+ req . headers [ 'x-forwarded-port' ] += portToAppend ;
205+ }
206+ else {
207+ req . headers [ 'x-forwarded-port' ] = req . connection . remotePort || req . socket . remotePort ;
208+ }
209+
210+ if ( req . headers [ 'x-forwarded-proto' ] ) {
211+ var protoToAppend = "," + getProto ( req ) ;
212+ req . headers [ 'x-forwarded-proto' ] += protoToAppend ;
213+ }
214+ else {
215+ req . headers [ 'x-forwarded-proto' ] = getProto ( req ) ;
216+ }
217+ }
180218 var location ;
181219
182220 //
@@ -248,6 +286,40 @@ RoutingProxy.prototype.proxyRequest = function (req, res, options) {
248286RoutingProxy . prototype . proxyWebSocketRequest = function ( req , socket , head , options ) {
249287 options = options || { } ;
250288
289+ //
290+ // Add common proxy headers to the request so that they can
291+ // be availible to the proxy target server. If the proxy is
292+ // part of proxy chain it will append the address:
293+ //
294+ // * `x-forwarded-for`: IP Address of the original request
295+ // * `x-forwarded-proto`: Protocol of the original request
296+ // * `x-forwarded-port`: Port of the original request.
297+ //
298+ if ( this . enable . xforward && req . connection && req . socket ) {
299+ if ( req . headers [ 'x-forwarded-for' ] ) {
300+ var addressToAppend = "," + req . connection . remoteAddress || req . socket . remoteAddress ;
301+ req . headers [ 'x-forwarded-for' ] += addressToAppend ;
302+ }
303+ else {
304+ req . headers [ 'x-forwarded-for' ] = req . connection . remoteAddress || req . socket . remoteAddress ;
305+ }
306+
307+ if ( req . headers [ 'x-forwarded-port' ] ) {
308+ var portToAppend = "," + req . connection . remotePort || req . socket . remotePort ;
309+ req . headers [ 'x-forwarded-port' ] += portToAppend ;
310+ }
311+ else {
312+ req . headers [ 'x-forwarded-port' ] = req . connection . remotePort || req . socket . remotePort ;
313+ }
314+
315+ if ( req . headers [ 'x-forwarded-proto' ] ) {
316+ var protoToAppend = "," + getProto ( req ) ;
317+ req . headers [ 'x-forwarded-proto' ] += protoToAppend ;
318+ }
319+ else {
320+ req . headers [ 'x-forwarded-proto' ] = getProto ( req ) ;
321+ }
322+ }
251323 var location ,
252324 proxy ,
253325 key ;
0 commit comments