@@ -28,7 +28,6 @@ var sys = require('sys'),
28
28
http = require ( 'http' ) ,
29
29
events = require ( 'events' ) ,
30
30
pool = require ( './../vendor/pool/main' ) ,
31
- eyes = require ( 'eyes' ) ,
32
31
min = 0 ,
33
32
max = 100 ;
34
33
@@ -44,13 +43,9 @@ exports.createServer = function () {
44
43
if ( args [ 0 ] ) port = args [ 0 ] ;
45
44
if ( args [ 1 ] ) host = args [ 1 ] ;
46
45
47
- var server = http . createServer ( function ( req , res ) {
46
+ var server = http . createServer ( function ( req , res ) {
48
47
var proxy = new HttpProxy ( req , res ) ;
49
48
50
- proxy . emitter . on ( 'proxy' , function ( err , body ) {
51
- server . emit ( 'proxy' , err , body ) ;
52
- } ) ;
53
-
54
49
// If we were passed a callback to process the request
55
50
// or response in some way, then call it.
56
51
if ( callback ) {
@@ -61,15 +56,14 @@ exports.createServer = function () {
61
56
}
62
57
} ) ;
63
58
64
- // If callback is empty - tunnel websocket request automatically
65
59
if ( ! callback ) {
66
- // WebSocket support
60
+ // WebSocket support: if callback is empty tunnel
61
+ // websocket request automatically
67
62
server . on ( 'upgrade' , function ( req , socket , head ) {
68
63
var proxy = new HttpProxy ( req , socket , head ) ;
69
64
70
65
// Tunnel websocket requests too
71
66
proxy . proxyWebSocketRequest ( port , host ) ;
72
-
73
67
} ) ;
74
68
}
75
69
@@ -87,7 +81,6 @@ exports.setMax = function (value) {
87
81
} ;
88
82
89
83
var HttpProxy = function ( req , res , head ) {
90
- this . emitter = new ( events . EventEmitter ) ;
91
84
this . events = { } ;
92
85
this . req = req ;
93
86
@@ -145,96 +138,90 @@ HttpProxy.prototype = {
145
138
self . body = '' ;
146
139
147
140
// Open new HTTP request to internal resource with will act as a reverse proxy pass
148
- //var p = manager.getPool(port, server);
149
- //sys.puts('Current pool count for ' + req.headers.host + ":" + port + ' ' + p.clients.length + ', Busy: ' + p.getBusy() + ', Free: ' + p.getFree());
141
+ var p = manager . getPool ( port , server ) ;
150
142
151
- // p.on('error', function (err) {
143
+ p . on ( 'error' , function ( err ) {
152
144
// Remark: We should probably do something here
153
145
// but this is a hot-fix because I don't think 'pool'
154
146
// should be emitting this event.
155
- // });
147
+ } ) ;
156
148
157
149
var client = http . createClient ( port , server ) ;
158
- var reverse_proxy = client . request ( req . method , req . url , req . headers ) ;
159
-
160
- //p.request(req.method, req.url, req.headers, function (reverse_proxy) {
161
-
162
- // Create an error handler so we can use it temporarily
163
- function error ( obj ) {
164
- var fn = function ( err ) {
165
- res . writeHead ( 500 , { 'Content-Type' : 'text/plain' } ) ;
166
-
167
- if ( req . method !== 'HEAD' ) {
168
- res . write ( 'An error has occurred: ' + JSON . stringify ( err ) ) ;
169
- }
150
+ p . request ( req . method , req . url , req . headers , function ( reverse_proxy ) {
151
+ // Create an error handler so we can use it temporarily
152
+ function error ( obj ) {
153
+ var fn = function ( err ) {
154
+ res . writeHead ( 500 , { 'Content-Type' : 'text/plain' } ) ;
155
+
156
+ if ( req . method !== 'HEAD' ) {
157
+ res . write ( 'An error has occurred: ' + JSON . stringify ( err ) ) ;
158
+ }
170
159
171
- // Response end may never come so removeListener here
172
- obj . removeListener ( 'error' , fn ) ;
173
- res . end ( ) ;
174
- } ;
160
+ // Response end may never come so removeListener here
161
+ obj . removeListener ( 'error' , fn ) ;
162
+ res . end ( ) ;
163
+ } ;
175
164
176
- return fn ;
177
- } ;
165
+ return fn ;
166
+ } ;
178
167
179
- // Add a listener for the connection timeout event
180
- var reverseProxyError = error ( reverse_proxy ) ,
181
- clientError = error ( client ) ;
168
+ // Add a listener for the connection timeout event
169
+ var reverseProxyError = error ( reverse_proxy ) ,
170
+ clientError = error ( client ) ;
182
171
183
- reverse_proxy . addListener ( 'error' , reverseProxyError ) ;
184
- client . addListener ( 'error' , clientError ) ;
172
+ reverse_proxy . addListener ( 'error' , reverseProxyError ) ;
173
+ client . addListener ( 'error' , clientError ) ;
185
174
186
- // Add a listener for the reverse_proxy response event
187
- reverse_proxy . addListener ( 'response' , function ( response ) {
188
- if ( response . headers . connection ) {
189
- if ( req . headers . connection ) response . headers . connection = req . headers . connection ;
190
- else response . headers . connection = 'close' ;
191
- }
192
-
193
- // Set the response headers of the client response
194
- res . writeHead ( response . statusCode , response . headers ) ;
175
+ // Add a listener for the reverse_proxy response event
176
+ reverse_proxy . addListener ( 'response' , function ( response ) {
177
+ if ( response . headers . connection ) {
178
+ if ( req . headers . connection ) response . headers . connection = req . headers . connection ;
179
+ else response . headers . connection = 'close' ;
180
+ }
195
181
196
- // Status code = 304
197
- // No 'data' event and no 'end'
198
- if ( response . statusCode === 304 ) {
199
- res . end ( ) ;
200
- return ;
201
- }
182
+ // Set the response headers of the client response
183
+ res . writeHead ( response . statusCode , response . headers ) ;
202
184
203
- // Add event handler for the proxied response in chunks
204
- response . addListener ( 'data' , function ( chunk ) {
205
- if ( req . method !== 'HEAD' ) {
206
- res . write ( chunk , 'binary' ) ;
207
- self . body += chunk ;
185
+ // Status code = 304
186
+ // No 'data' event and no 'end'
187
+ if ( response . statusCode === 304 ) {
188
+ res . end ( ) ;
189
+ return ;
208
190
}
209
- } ) ;
210
191
211
- // Add event listener for end of proxied response
212
- response . addListener ( 'end' , function ( ) {
213
- // Remark: Emit the end event for testability
214
- self . emitter . emit ( 'proxy' , null , self . body ) ;
215
- reverse_proxy . removeListener ( 'error' , reverseProxyError ) ;
216
- res . end ( ) ;
192
+ // Add event handler for the proxied response in chunks
193
+ response . addListener ( 'data' , function ( chunk ) {
194
+ if ( req . method !== 'HEAD' ) {
195
+ res . write ( chunk , 'binary' ) ;
196
+ self . body += chunk ;
197
+ }
198
+ } ) ;
199
+
200
+ // Add event listener for end of proxied response
201
+ response . addListener ( 'end' , function ( ) {
202
+ reverse_proxy . removeListener ( 'error' , reverseProxyError ) ;
203
+ res . end ( ) ;
204
+ } ) ;
217
205
} ) ;
218
- } ) ;
219
206
220
- // Chunk the client request body as chunks from the proxied request come in
221
- req . addListener ( 'data' , function ( chunk ) {
222
- reverse_proxy . write ( chunk , 'binary' ) ;
223
- } )
207
+ // Chunk the client request body as chunks from the proxied request come in
208
+ req . addListener ( 'data' , function ( chunk ) {
209
+ reverse_proxy . write ( chunk , 'binary' ) ;
210
+ } )
224
211
225
- // At the end of the client request, we are going to stop the proxied request
226
- req . addListener ( 'end' , function ( ) {
227
- reverse_proxy . end ( ) ;
228
- } ) ;
212
+ // At the end of the client request, we are going to stop the proxied request
213
+ req . addListener ( 'end' , function ( ) {
214
+ reverse_proxy . end ( ) ;
215
+ } ) ;
229
216
230
- // On 'close' event remove 'error' listener
231
- client . addListener ( 'close' , function ( ) {
232
- client . removeListener ( 'error' , clientError ) ;
233
- } ) ;
217
+ // On 'close' event remove 'error' listener
218
+ client . addListener ( 'close' , function ( ) {
219
+ client . removeListener ( 'error' , clientError ) ;
220
+ } ) ;
234
221
235
- self . unwatch ( req ) ;
222
+ self . unwatch ( req ) ;
236
223
237
- // });
224
+ } ) ;
238
225
} ,
239
226
240
227
proxyWebSocketRequest : function ( port , server , host ) {
@@ -251,8 +238,7 @@ HttpProxy.prototype = {
251
238
return h ;
252
239
}
253
240
254
- // WebSocket requests has
255
- // method = GET
241
+ // WebSocket requests has method = GET
256
242
if ( req . method !== 'GET' || headers . upgrade . toLowerCase ( ) !== 'websocket' ) {
257
243
// This request is not WebSocket request
258
244
return ;
0 commit comments