@@ -52,14 +52,14 @@ var HttpProxy = exports.HttpProxy = function (options) {
52
52
if ( ! options || ! options . target ) {
53
53
throw new Error ( 'Both `options` and `options.target` are required.' ) ;
54
54
}
55
-
55
+
56
56
events . EventEmitter . call ( this ) ;
57
-
57
+
58
58
var self = this ;
59
59
60
60
//
61
- // Setup basic proxying options:
62
- //
61
+ // Setup basic proxying options:
62
+ //
63
63
// * forward {Object} Options for a forward-proxy (if-any)
64
64
// * target {Object} Options for the **sole** proxy target of this instance
65
65
//
@@ -69,23 +69,23 @@ var HttpProxy = exports.HttpProxy = function (options) {
69
69
//
70
70
// Setup the necessary instances instance variables for
71
71
// the `target` and `forward` `host:port` combinations
72
- // used by this instance.
72
+ // used by this instance.
73
73
//
74
74
// * agent {http[s].Agent} Agent to be used by this instance.
75
75
// * protocol {http|https} Core node.js module to make requests with.
76
76
// * base {Object} Base object to create when proxying containing any https settings.
77
- //
77
+ //
78
78
function setupProxy ( key ) {
79
79
self [ key ] . agent = httpProxy . _getAgent ( self [ key ] ) ;
80
80
self [ key ] . protocol = httpProxy . _getProtocol ( self [ key ] ) ;
81
- self [ key ] . base = httpProxy . _getBase ( self [ key ] ) ;
81
+ self [ key ] . base = httpProxy . _getBase ( self [ key ] ) ;
82
82
}
83
-
83
+
84
84
setupProxy ( 'target' ) ;
85
- if ( this . forward ) {
86
- setupProxy ( 'forward' ) ;
85
+ if ( this . forward ) {
86
+ setupProxy ( 'forward' ) ;
87
87
}
88
-
88
+
89
89
//
90
90
// Setup opt-in features
91
91
//
@@ -103,7 +103,7 @@ var HttpProxy = exports.HttpProxy = function (options) {
103
103
//
104
104
this . source = options . source || { host : 'localhost' , port : 8000 } ;
105
105
this . source . https = this . source . https || options . https ;
106
- this . changeOrigin = options . changeOrigin || false ;
106
+ this . changeOrigin = options . changeOrigin || false ;
107
107
} ;
108
108
109
109
// Inherit from events.EventEmitter
@@ -116,14 +116,14 @@ util.inherits(HttpProxy, events.EventEmitter);
116
116
// #### @buffer {Object} Result from `httpProxy.buffer(req)`
117
117
//
118
118
HttpProxy . prototype . proxyRequest = function ( req , res , buffer ) {
119
- var self = this ,
119
+ var self = this ,
120
120
errState = false ,
121
121
outgoing = new ( this . target . base ) ,
122
122
reverseProxy ;
123
123
124
124
//
125
125
// Add common proxy headers to the request so that they can
126
- // be availible to the proxy target server. If the proxy is
126
+ // be availible to the proxy target server. If the proxy is
127
127
// part of proxy chain it will append the address:
128
128
//
129
129
// * `x-forwarded-for`: IP Address of the original request
@@ -134,23 +134,23 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
134
134
if ( req . headers [ 'x-forwarded-for' ] ) {
135
135
var addressToAppend = "," + req . connection . remoteAddress || req . socket . remoteAddress ;
136
136
req . headers [ 'x-forwarded-for' ] += addressToAppend ;
137
- }
137
+ }
138
138
else {
139
139
req . headers [ 'x-forwarded-for' ] = req . connection . remoteAddress || req . socket . remoteAddress ;
140
140
}
141
141
142
142
if ( req . headers [ 'x-forwarded-port' ] ) {
143
143
var portToAppend = "," + req . connection . remotePort || req . socket . remotePort ;
144
144
req . headers [ 'x-forwarded-port' ] += portToAppend ;
145
- }
145
+ }
146
146
else {
147
147
req . headers [ 'x-forwarded-port' ] = req . connection . remotePort || req . socket . remotePort ;
148
148
}
149
149
150
150
if ( req . headers [ 'x-forwarded-proto' ] ) {
151
151
var protoToAppend = "," + ( req . connection . pair ? 'https' : 'http' ) ;
152
152
req . headers [ 'x-forwarded-proto' ] += protoToAppend ;
153
- }
153
+ }
154
154
else {
155
155
req . headers [ 'x-forwarded-proto' ] = req . connection . pair ? 'https' : 'http' ;
156
156
}
@@ -217,7 +217,7 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
217
217
outgoing . headers = req . headers ;
218
218
219
219
//
220
- // Open new HTTP request to internal resource with will act
220
+ // Open new HTTP request to internal resource with will act
221
221
// as a reverse proxy pass
222
222
//
223
223
reverseProxy = this . target . protocol . request ( outgoing , function ( response ) {
@@ -286,10 +286,10 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
286
286
ended = true
287
287
if ( ! errState ) {
288
288
reverseProxy . removeListener ( 'error' , proxyError ) ;
289
-
289
+
290
290
try { res . end ( ) }
291
291
catch ( ex ) { console . error ( "res.end error: %s" , ex . message ) }
292
-
292
+
293
293
// Emit the `end` event now that we have completed proxying
294
294
self . emit ( 'end' , req , res ) ;
295
295
}
@@ -313,19 +313,19 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
313
313
// `req` write it to the `reverseProxy` request.
314
314
//
315
315
req . on ( 'data' , function ( chunk ) {
316
-
316
+
317
317
if ( ! errState ) {
318
318
var flushed = reverseProxy . write ( chunk ) ;
319
319
if ( ! flushed ) {
320
320
req . pause ( ) ;
321
321
reverseProxy . once ( 'drain' , function ( ) {
322
- try { req . resume ( ) }
322
+ try { req . resume ( ) }
323
323
catch ( er ) { console . error ( "req.resume error: %s" , er . message ) }
324
324
} ) ;
325
-
325
+
326
326
//
327
327
// Force the `drain` event in 100ms if it hasn't
328
- // happened on its own.
328
+ // happened on its own.
329
329
//
330
330
setTimeout ( function ( ) {
331
331
reverseProxy . emit ( 'drain' ) ;
@@ -367,7 +367,7 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
367
367
// #### @socket {net.Socket} Socket for the underlying HTTP request
368
368
// #### @head {string} Headers for the Websocket request.
369
369
// #### @buffer {Object} Result from `httpProxy.buffer(req)`
370
- // Performs a WebSocket proxy operation to the location specified by
370
+ // Performs a WebSocket proxy operation to the location specified by
371
371
// `this.target`.
372
372
//
373
373
HttpProxy . prototype . proxyWebSocketRequest = function ( req , socket , head , buffer ) {
@@ -387,10 +387,10 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
387
387
//
388
388
return socket . destroy ( ) ;
389
389
}
390
-
390
+
391
391
//
392
392
// Add common proxy headers to the request so that they can
393
- // be availible to the proxy target server. If the proxy is
393
+ // be availible to the proxy target server. If the proxy is
394
394
// part of proxy chain it will append the address:
395
395
//
396
396
// * `x-forwarded-for`: IP Address of the original request
@@ -401,23 +401,23 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
401
401
if ( req . headers [ 'x-forwarded-for' ] ) {
402
402
var addressToAppend = "," + req . connection . remoteAddress || req . connection . socket . remoteAddress ;
403
403
req . headers [ 'x-forwarded-for' ] += addressToAppend ;
404
- }
404
+ }
405
405
else {
406
406
req . headers [ 'x-forwarded-for' ] = req . connection . remoteAddress || req . connection . socket . remoteAddress ;
407
407
}
408
408
409
409
if ( req . headers [ 'x-forwarded-port' ] ) {
410
410
var portToAppend = "," + req . connection . remotePort || req . connection . socket . remotePort ;
411
411
req . headers [ 'x-forwarded-port' ] += portToAppend ;
412
- }
412
+ }
413
413
else {
414
414
req . headers [ 'x-forwarded-port' ] = req . connection . remotePort || req . connection . socket . remotePort ;
415
415
}
416
416
417
417
if ( req . headers [ 'x-forwarded-proto' ] ) {
418
418
var protoToAppend = "," + ( req . connection . pair ? 'wss' : 'ws' ) ;
419
419
req . headers [ 'x-forwarded-proto' ] += protoToAppend ;
420
- }
420
+ }
421
421
else {
422
422
req . headers [ 'x-forwarded-proto' ] = req . connection . pair ? 'wss' : 'ws' ;
423
423
}
@@ -432,7 +432,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
432
432
function _socket ( socket , keepAlive ) {
433
433
socket . setTimeout ( 0 ) ;
434
434
socket . setNoDelay ( true ) ;
435
-
435
+
436
436
if ( keepAlive ) {
437
437
if ( socket . setKeepAlive ) {
438
438
socket . setKeepAlive ( true , 0 ) ;
@@ -445,7 +445,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
445
445
socket . setEncoding ( 'utf8' ) ;
446
446
}
447
447
}
448
-
448
+
449
449
//
450
450
// Setup the incoming client socket.
451
451
//
@@ -474,13 +474,13 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
474
474
if ( ! flushed ) {
475
475
proxySocket . pause ( ) ;
476
476
reverseProxy . incoming . socket . once ( 'drain' , function ( ) {
477
- try { proxySocket . resume ( ) }
477
+ try { proxySocket . resume ( ) }
478
478
catch ( er ) { console . error ( "proxySocket.resume error: %s" , er . message ) }
479
479
} ) ;
480
-
480
+
481
481
//
482
482
// Force the `drain` event in 100ms if it hasn't
483
- // happened on its own.
483
+ // happened on its own.
484
484
//
485
485
setTimeout ( function ( ) {
486
486
reverseProxy . incoming . socket . emit ( 'drain' ) ;
@@ -506,13 +506,13 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
506
506
if ( ! flushed ) {
507
507
reverseProxy . incoming . socket . pause ( ) ;
508
508
proxySocket . once ( 'drain' , function ( ) {
509
- try { reverseProxy . incoming . socket . resume ( ) }
509
+ try { reverseProxy . incoming . socket . resume ( ) }
510
510
catch ( er ) { console . error ( "reverseProxy.incoming.socket.resume error: %s" , er . message ) }
511
511
} ) ;
512
-
512
+
513
513
//
514
514
// Force the `drain` event in 100ms if it hasn't
515
- // happened on its own.
515
+ // happened on its own.
516
516
//
517
517
setTimeout ( function ( ) {
518
518
proxySocket . emit ( 'drain' ) ;
@@ -572,7 +572,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
572
572
protocolName = this . target . https ? 'https' : 'http' ,
573
573
portUri = getPort ( this . source . port ) ,
574
574
remoteHost = this . target . host + portUri ;
575
-
575
+
576
576
//
577
577
// Change headers (if requested).
578
578
//
@@ -591,7 +591,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
591
591
outgoing . path = req . url ;
592
592
outgoing . headers = req . headers ;
593
593
outgoing . agent = agent ;
594
-
594
+
595
595
var reverseProxy = this . target . protocol . request ( outgoing ) ;
596
596
597
597
//
@@ -678,13 +678,13 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
678
678
if ( ! flushed ) {
679
679
revSocket . pause ( ) ;
680
680
socket . once ( 'drain' , function ( ) {
681
- try { revSocket . resume ( ) }
681
+ try { revSocket . resume ( ) }
682
682
catch ( er ) { console . error ( "reverseProxy.socket.resume error: %s" , er . message ) }
683
683
} ) ;
684
684
685
685
//
686
686
// Force the `drain` event in 100ms if it hasn't
687
- // happened on its own.
687
+ // happened on its own.
688
688
//
689
689
setTimeout ( function ( ) {
690
690
socket . emit ( 'drain' ) ;
@@ -693,7 +693,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
693
693
}
694
694
catch ( ex ) {
695
695
//
696
- // Remove data listener on socket error because the
696
+ // Remove data listener on socket error because the
697
697
// 'handshake' has failed.
698
698
//
699
699
revSocket . removeListener ( 'data' , handshake ) ;
@@ -714,8 +714,8 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
714
714
715
715
try {
716
716
//
717
- // Attempt to write the upgrade-head to the reverseProxy
718
- // request. This is small, and there's only ever one of
717
+ // Attempt to write the upgrade-head to the reverseProxy
718
+ // request. This is small, and there's only ever one of
719
719
// it; no need for pause/resume.
720
720
//
721
721
// XXX This is very wrong and should be fixed in node's core
@@ -763,7 +763,7 @@ HttpProxy.prototype.close = function () {
763
763
// by `this.forward` ignoring errors and the subsequent response.
764
764
//
765
765
HttpProxy . prototype . _forwardRequest = function ( req ) {
766
- var self = this ,
766
+ var self = this ,
767
767
outgoing = new ( this . forward . base ) ,
768
768
forwardProxy ;
769
769
@@ -778,7 +778,7 @@ HttpProxy.prototype._forwardRequest = function (req) {
778
778
outgoing . headers = req . headers ;
779
779
780
780
//
781
- // Open new HTTP request to internal resource with will
781
+ // Open new HTTP request to internal resource with will
782
782
// act as a reverse proxy pass.
783
783
//
784
784
forwardProxy = this . forward . protocol . request ( outgoing , function ( response ) {
@@ -805,13 +805,13 @@ HttpProxy.prototype._forwardRequest = function (req) {
805
805
if ( ! flushed ) {
806
806
req . pause ( ) ;
807
807
forwardProxy . once ( 'drain' , function ( ) {
808
- try { req . resume ( ) }
808
+ try { req . resume ( ) }
809
809
catch ( er ) { console . error ( "req.resume error: %s" , er . message ) }
810
810
} ) ;
811
811
812
812
//
813
813
// Force the `drain` event in 100ms if it hasn't
814
- // happened on its own.
814
+ // happened on its own.
815
815
//
816
816
setTimeout ( function ( ) {
817
817
forwardProxy . emit ( 'drain' ) ;
@@ -820,7 +820,7 @@ HttpProxy.prototype._forwardRequest = function (req) {
820
820
} ) ;
821
821
822
822
//
823
- // At the end of the client request, we are going to
823
+ // At the end of the client request, we are going to
824
824
// stop the proxied request
825
825
//
826
826
req . on ( 'end' , function ( ) {
0 commit comments