@@ -103,43 +103,13 @@ var HttpProxy = exports.HttpProxy = function (options) {
103
103
// Inherit from events.EventEmitter
104
104
util . inherits ( HttpProxy , events . EventEmitter ) ;
105
105
106
- function ReqCanary ( ) {
107
- this . ___events = [ ] ;
108
- }
109
- function ResCanary ( ) {
110
- this . ___events = [ ] ;
111
- }
112
-
113
- var cc = { } ;
114
-
115
- var logEvt = function ( canary , event ) {
116
- if ( canary && canary . ___events ) {
117
- canary . ___events . push ( event ) ;
118
- } else {
119
- console . log ( "No canary for event : " + event ) ;
120
- }
121
-
122
- if ( ! cc [ event ] ) {
123
- cc [ event ] = 1 ;
124
- } else {
125
- cc [ event ] += 1 ;
126
- }
127
-
128
- console . dir ( cc ) ;
129
- }
130
-
131
106
//
132
107
// ### function proxyRequest (req, res, buffer)
133
108
// #### @req {ServerRequest} Incoming HTTP Request to proxy.
134
109
// #### @res {ServerResponse} Outgoing HTTP Request to write proxied data to.
135
110
// #### @buffer {Object} Result from `httpProxy.buffer(req)`
136
111
//
137
112
HttpProxy . prototype . proxyRequest = function ( req , res , buffer ) {
138
- req . canary = new ReqCanary ( ) ;
139
- res . canary = new ResCanary ( ) ;
140
-
141
- logEvt ( res . canary , "Init" ) ;
142
-
143
113
var self = this ,
144
114
errState = false ,
145
115
outgoing = new ( this . target . base ) ,
@@ -196,8 +166,6 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
196
166
//
197
167
this . emit ( 'start' , req , res , this . target ) ;
198
168
199
- logEvt ( res . canary , "Start" ) ;
200
-
201
169
//
202
170
// #### function proxyError (err)
203
171
// #### @err {Error} Error contacting the proxy target
@@ -207,8 +175,6 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
207
175
function proxyError ( err ) {
208
176
errState = true ;
209
177
210
- logEvt ( res . canary , "ProxyError" ) ;
211
-
212
178
//
213
179
// Emit an `error` event, allowing the application to use custom
214
180
// error handling. The error handler should end the response.
@@ -217,8 +183,6 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
217
183
return ;
218
184
}
219
185
220
- logEvt ( res . canary , "Writing 500" ) ;
221
-
222
186
res . writeHead ( 500 , { 'Content-Type' : 'text/plain' } ) ;
223
187
224
188
if ( req . method !== 'HEAD' ) {
@@ -236,20 +200,15 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
236
200
237
201
try {
238
202
res . end ( ) ;
239
- logEvt ( res . canary , "Ended properly" ) ;
240
- //CLEANUP
203
+
241
204
if ( res ) {
242
205
res . removeAllListeners ( ) ;
243
206
res = null ;
244
207
}
245
208
if ( req ) {
246
209
req . removeAllListeners ( ) ;
247
210
req = null ;
248
- }
249
- if ( response ) {
250
- response . removeAllListeners ( ) ;
251
- response = null ;
252
- }
211
+ }
253
212
}
254
213
catch ( ex ) { console . error ( "res.end error: %s" , ex . message ) }
255
214
}
@@ -272,9 +231,6 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
272
231
// as a reverse proxy pass
273
232
//
274
233
reverseProxy = this . target . protocol . request ( outgoing , function ( response ) {
275
-
276
- logEvt ( res . canary , "Reverse proxying" ) ;
277
-
278
234
//
279
235
// Process the `reverseProxy` `response` when it's received.
280
236
//
@@ -300,7 +256,6 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
300
256
//
301
257
var ended = false ;
302
258
response . on ( 'close' , function ( ) {
303
- logEvt ( res . canary , "Close, emiting 'end'" ) ;
304
259
if ( ! ended ) { response . emit ( 'end' ) }
305
260
} ) ;
306
261
@@ -312,47 +267,25 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
312
267
// This code makes sure that we flush pending data.
313
268
//
314
269
response . connection . on ( 'end' , function ( ) {
315
- if ( res )
316
- logEvt ( res . canary , 'response connection ended' ) ;
317
270
318
271
if ( response && response . readable && response . resume ) {
319
- if ( res )
320
- logEvt ( res . canary , 'response resuming' ) ;
321
-
322
272
response . resume ( ) ;
323
273
}
324
- // CLEANUP?
325
- if ( res ) {
326
- res . removeAllListeners ( ) ;
327
- res = null ;
328
- }
329
- if ( req ) {
330
- req . removeAllListeners ( ) ;
331
- req = null ;
332
- }
333
274
if ( response ) {
334
275
response . removeAllListeners ( ) ;
335
276
response = null ;
336
277
}
337
278
} ) ;
338
279
339
280
response . on ( 'end' , function ( ) {
340
- logEvt ( res . canary , 'response ended' ) ;
341
281
ended = true ;
342
282
if ( ! errState ) {
343
283
try {
344
- res . end ( ) ;
345
- logEvt ( res . canary , 'end res' ) ;
346
- // CLEANUP?
284
+ res . end ( ) ;
347
285
res . removeAllListeners ( ) ;
348
- // Note that nulling this will prevent the response resuming' event from
349
- // coming in, since it won't fire on a null event.
350
286
res = null ;
351
287
req . removeAllListeners ( ) ;
352
288
req = null ;
353
- response . removeAllListeners ( ) ;
354
- response = null ;
355
-
356
289
}
357
290
catch ( ex ) { console . error ( "res.end error: %s" , ex . message ) }
358
291
@@ -375,15 +308,13 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
375
308
res . writeHead ( response . statusCode ) ;
376
309
377
310
function ondata ( chunk ) {
378
- logEvt ( res . canary , "Data" ) ;
379
311
if ( res . writable ) {
380
312
// Only pause if the underlying buffers are full,
381
313
// *and* the connection is not in 'closing' state.
382
314
// Otherwise, the pause will cause pending data to
383
315
// be discarded and silently lost.
384
316
if ( false === res . write ( chunk ) && response . pause
385
317
&& response . connection . readable ) {
386
- logEvt ( res . canary , "Pausing due to full buffer" ) ;
387
318
response . pause ( ) ;
388
319
}
389
320
}
@@ -392,9 +323,7 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
392
323
response . on ( 'data' , ondata ) ;
393
324
394
325
function ondrain ( ) {
395
- logEvt ( res . canary , "On drain" ) ;
396
326
if ( response . readable && response . resume ) {
397
- logEvt ( res . canary , "On drain resuming." ) ;
398
327
response . resume ( ) ;
399
328
}
400
329
}
@@ -412,7 +341,6 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
412
341
413
342
// Set a timeout on the socket if `this.timeout` is specified.
414
343
reverseProxy . once ( 'socket' , function ( socket ) {
415
- logEvt ( res . canary , "Socket" ) ;
416
344
if ( self . timeout ) {
417
345
socket . setTimeout ( self . timeout ) ;
418
346
}
@@ -427,7 +355,6 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
427
355
// If `req` is aborted, we abort our `reverseProxy` request as well.
428
356
//
429
357
req . on ( 'aborted' , function ( ) {
430
- logEvt ( res . canary , "Aborted" ) ;
431
358
reverseProxy . abort ( ) ;
432
359
} ) ;
433
360
@@ -436,13 +363,11 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
436
363
// `req` write it to the `reverseProxy` request.
437
364
//
438
365
req . on ( 'data' , function ( chunk ) {
439
- logEvt ( res . canary , 'req data' ) ;
440
366
if ( ! errState ) {
441
367
var flushed = reverseProxy . write ( chunk ) ;
442
368
if ( ! flushed ) {
443
369
req . pause ( ) ;
444
370
reverseProxy . once ( 'drain' , function ( ) {
445
- logEvt ( res . canary , "Drain" ) ;
446
371
try { req . resume ( ) }
447
372
catch ( er ) { console . error ( "req.resume error: %s" , er . message ) }
448
373
} ) ;
@@ -452,7 +377,6 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
452
377
// happened on its own.
453
378
//
454
379
setTimeout ( function ( ) {
455
- logEvt ( res . canary , "Force drain" ) ;
456
380
reverseProxy . emit ( 'drain' ) ;
457
381
} , 100 ) ;
458
382
}
@@ -464,29 +388,22 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
464
388
// request unless we have entered an error state.
465
389
//
466
390
req . on ( 'end' , function ( ) {
467
- logEvt ( res . canary , "Req ended" ) ;
468
391
if ( ! errState ) {
469
392
reverseProxy . end ( ) ;
470
- } else {
471
- logEvt ( res . canary , "Req errored end" ) ;
472
393
}
473
394
} ) ;
474
395
475
396
//Aborts reverseProxy if client aborts the connection.
476
397
req . on ( 'close' , function ( ) {
477
- logEvt ( res . canary , "Req closed" ) ;
478
398
if ( ! errState ) {
479
399
reverseProxy . abort ( ) ;
480
- } else {
481
- logEvt ( res . canary , "Req errored close" ) ;
482
400
}
483
401
} ) ;
484
402
485
403
//
486
404
// If we have been passed buffered data, resume it.
487
405
//
488
406
if ( buffer ) {
489
- logEvt ( res . canary , "Resuming buffer " + errState ? "1" : "0" ) ;
490
407
return ! errState
491
408
? buffer . resume ( )
492
409
: buffer . destroy ( ) ;
0 commit comments