27
27
var util = require ( 'util' ) ,
28
28
http = require ( 'http' ) ,
29
29
events = require ( 'events' ) ,
30
- pool = require ( 'pool' ) ,
31
30
ProxyTable = require ( './proxy-table' ) . ProxyTable ,
32
- min = 0 ,
33
- max = 100 ;
34
-
35
- // Setup the PoolManager
36
- var manager = pool . createPoolManager ( ) ;
37
- manager . setMinClients ( min ) ;
38
- manager . setMaxClients ( max ) ;
31
+ maxSockets = 100 ;
39
32
40
33
exports . createServer = function ( ) {
41
34
var args , callback , port , host , forward ,
@@ -114,17 +107,13 @@ exports.createServer = function () {
114
107
return server ;
115
108
} ;
116
109
117
- exports . setMin = function ( value ) {
118
- min = value ;
119
- manager . setMinClients ( min ) ;
110
+ exports . setMaxSockets = function ( value ) {
111
+ maxSockets = value ;
120
112
} ;
121
113
122
- exports . setMax = function ( value ) {
123
- max = value ;
124
- manager . setMaxClients ( max ) ;
125
- } ;
114
+ exports . ProxyTable = ProxyTable ;
126
115
127
- var HttpProxy = function ( req , res , head ) {
116
+ var HttpProxy = exports . HttpProxy = function ( req , res , head ) {
128
117
this . events = { } ;
129
118
this . req = req ;
130
119
@@ -177,116 +166,116 @@ HttpProxy.prototype = {
177
166
} ,
178
167
179
168
proxyRequest : function ( port , server ) {
180
- var self = this , req = this . req , res = this . res ;
169
+ var self = this , req = this . req , res = this . res , reverseProxy ;
181
170
182
- // Open new HTTP request to internal resource with will act as a reverse proxy pass
183
- var p = manager . getPool ( port , server ) ;
171
+ // Create an error handler so we can use it temporarily
172
+ function error ( obj ) {
173
+ var fn = function ( err ) {
174
+ res . writeHead ( 500 , { 'Content-Type' : 'text/plain' } ) ;
184
175
185
- p . on ( 'error' , function ( err ) {
186
- // Remark: We should probably do something here
187
- // but this is a hot-fix because I don't think 'pool'
188
- // should be emitting this event.
189
- } ) ;
190
-
191
- p . request ( req . method , req . url , req . headers , function ( reverse_proxy ) {
192
- // Create an error handler so we can use it temporarily
193
- function error ( obj ) {
194
- var fn = function ( err ) {
195
- res . writeHead ( 500 , { 'Content-Type' : 'text/plain' } ) ;
196
-
197
- if ( req . method !== 'HEAD' ) {
198
- res . write ( 'An error has occurred: ' + JSON . stringify ( err ) ) ;
199
- }
200
-
201
- // Response end may never come so removeListener here
202
- obj . removeListener ( 'error' , fn ) ;
203
- res . end ( ) ;
204
- } ;
176
+ if ( req . method !== 'HEAD' ) {
177
+ res . write ( 'An error has occurred: ' + JSON . stringify ( err ) ) ;
178
+ }
205
179
206
- return fn ;
180
+ // Response end may never come so removeListener here
181
+ obj . removeListener ( 'error' , fn ) ;
182
+ res . end ( ) ;
207
183
} ;
208
-
209
- // Add a listener for the connection timeout event
210
- var reverseProxyError = error ( reverse_proxy ) ;
211
- reverse_proxy . addListener ( 'error' , reverseProxyError ) ;
212
184
213
- // Add a listener for the reverse_proxy response event
214
- reverse_proxy . addListener ( 'response' , function ( response ) {
215
- if ( response . headers . connection ) {
216
- if ( req . headers . connection ) response . headers . connection = req . headers . connection ;
217
- else response . headers . connection = 'close' ;
218
- }
219
-
220
- // Set the response headers of the client response
221
- res . writeHead ( response . statusCode , response . headers ) ;
185
+ return fn ;
186
+ } ;
187
+
188
+ // Open new HTTP request to internal resource with will act as a reverse proxy pass
189
+ reverseProxy = http . request ( {
190
+ host : server ,
191
+ port : port ,
192
+ method : req . method ,
193
+ path : req . url ,
194
+ headers : req . headers
195
+ } , function ( response ) {
196
+
197
+ // Process the reverse_proxy response when it's received.
198
+ if ( response . headers . connection ) {
199
+ if ( req . headers . connection ) response . headers . connection = req . headers . connection ;
200
+ else response . headers . connection = 'close' ;
201
+ }
222
202
223
- // Status code = 304
224
- // No 'data' event and no 'end'
225
- if ( response . statusCode === 304 ) {
226
- res . end ( ) ;
227
- return ;
228
- }
203
+ // Set the response headers of the client response
204
+ res . writeHead ( response . statusCode , response . headers ) ;
229
205
230
- // Add event handler for the proxied response in chunks
231
- response . addListener ( 'data' , function ( chunk ) {
232
- if ( req . method !== 'HEAD' ) {
233
- res . write ( chunk , 'binary' ) ;
234
- self . body += chunk ;
235
- }
236
- } ) ;
206
+ // Status code = 304
207
+ // No 'data' event and no 'end'
208
+ if ( response . statusCode === 304 ) {
209
+ res . end ( ) ;
210
+ return ;
211
+ }
237
212
238
- // Add event listener for end of proxied response
239
- response . addListener ( 'end' , function ( ) {
240
- reverse_proxy . removeListener ( 'error' , reverseProxyError ) ;
241
- res . end ( ) ;
242
- } ) ;
213
+ // Add event handler for the proxied response in chunks
214
+ response . addListener ( 'data' , function ( chunk ) {
215
+ if ( req . method !== 'HEAD' ) {
216
+ res . write ( chunk , 'binary' ) ;
217
+ self . body += chunk ;
218
+ }
243
219
} ) ;
244
220
245
- // Chunk the client request body as chunks from the proxied request come in
246
- req . addListener ( 'data' , function ( chunk ) {
247
- reverse_proxy . write ( chunk , 'binary' ) ;
248
- } )
249
-
250
- // At the end of the client request, we are going to stop the proxied request
251
- req . addListener ( 'end' , function ( ) {
252
- reverse_proxy . end ( ) ;
221
+ // Add event listener for end of proxied response
222
+ response . addListener ( 'end' , function ( ) {
223
+ reverseProxy . removeListener ( 'error' , reverseProxyError ) ;
224
+ res . end ( ) ;
253
225
} ) ;
226
+ } ) ;
254
227
255
- self . unwatch ( req ) ;
228
+ // Add a listener for the connection timeout event
229
+ var reverseProxyError = error ( reverseProxy ) ;
230
+ reverseProxy . addListener ( 'error' , reverseProxyError ) ;
231
+
232
+ // Chunk the client request body as chunks from the proxied request come in
233
+ req . addListener ( 'data' , function ( chunk ) {
234
+ reverseProxy . write ( chunk , 'binary' ) ;
235
+ } )
236
+
237
+ // At the end of the client request, we are going to stop the proxied request
238
+ req . addListener ( 'end' , function ( ) {
239
+ reverseProxy . end ( ) ;
256
240
} ) ;
241
+
242
+ self . unwatch ( req ) ;
257
243
} ,
258
244
259
245
forwardRequest : function ( port , server ) {
260
- var self = this , req = this . req ;
246
+ var self = this , req = this . req , forwardProxy ;
261
247
262
248
// Open new HTTP request to internal resource with will act as a reverse proxy pass
263
- var p = manager . getPool ( port , server ) ;
264
-
265
- p . on ( 'error' , function ( err ) {
266
- // Remark: We should probably do something here
267
- // but this is a hot-fix because I don't think 'pool'
268
- // should be emitting this event.
249
+ forwardProxy = http . request ( {
250
+ host : server ,
251
+ port : port ,
252
+ method : req . method ,
253
+ path : req . url ,
254
+ headers : req . headers
255
+ } , function ( response ) {
256
+ //
257
+ // Ignore the response from the forward proxy since this is a 'fire-and-forget' proxy.
258
+ // Remark (indexzero): We will eventually emit a 'forward' event here for performance tuning.
259
+ //
269
260
} ) ;
270
261
271
- p . request ( req . method , req . url , req . headers , function ( forward_proxy ) {
272
- // Add a listener for the connection timeout event
273
- forward_proxy . addListener ( 'error' , function ( err ) {
274
- // Remark: Ignoring this error in the event
275
- // forward target doesn't exist.
276
- } ) ;
277
-
278
- // Chunk the client request body as chunks from the proxied request come in
279
- req . addListener ( 'data' , function ( chunk ) {
280
- forward_proxy . write ( chunk , 'binary' ) ;
281
- } )
282
-
283
- // At the end of the client request, we are going to stop the proxied request
284
- req . addListener ( 'end' , function ( ) {
285
- forward_proxy . end ( ) ;
286
- } ) ;
287
-
288
- self . unwatch ( req ) ;
262
+ // Add a listener for the connection timeout event
263
+ forwardProxy . addListener ( 'error' , function ( err ) {
264
+ // Remark: Ignoring this error in the event
265
+ // forward target doesn't exist.
289
266
} ) ;
267
+
268
+ // Chunk the client request body as chunks from the proxied request come in
269
+ req . addListener ( 'data' , function ( chunk ) {
270
+ forwardProxy . write ( chunk , 'binary' ) ;
271
+ } )
272
+
273
+ // At the end of the client request, we are going to stop the proxied request
274
+ req . addListener ( 'end' , function ( ) {
275
+ forwardProxy . end ( ) ;
276
+ } ) ;
277
+
278
+ self . unwatch ( req ) ;
290
279
} ,
291
280
292
281
proxyWebSocketRequest : function ( port , server , host ) {
@@ -477,7 +466,4 @@ HttpProxy.prototype = {
477
466
} ) ;
478
467
} ;
479
468
}
480
- } ;
481
-
482
- exports . HttpProxy = HttpProxy ;
483
- exports . ProxyTable = ProxyTable ;
469
+ } ;
0 commit comments