26
26
27
27
var util = require ( 'util' ) ,
28
28
http = require ( 'http' ) ,
29
- https = require ( 'https' ) ,
30
29
events = require ( 'events' ) ,
31
- maxSockets = 100 ;
30
+ maxSockets = 200 ;
32
31
33
32
//
34
33
// Expose version information through `pkginfo`.
@@ -38,10 +37,7 @@ require('pkginfo')(module, 'version');
38
37
//
39
38
// ### Export the relevant objects exposed by `node-http-proxy`
40
39
//
41
- var HttpProxy = exports . HttpProxy = require ( './node-http-proxy/http-proxy' ) . HttpProxy ,
42
- ProxyTable = exports . ProxyTable = require ( './node-http-proxy/proxy-table' ) . ProxyTable ,
43
- RoutingProxy = exports . RoutingProxy = require ( './node-http-proxy/routing-proxy' ) . RoutingProxy ;
44
-
40
+ var HttpProxy = exports . HttpProxy = require ( './node-http-proxy/http-proxy' ) . HttpProxy ;
45
41
//
46
42
// ### function createServer ([port, host, options, handler])
47
43
// #### @port {number} **Optional** Port to use on the proxy target host.
@@ -82,7 +78,7 @@ exports.createServer = function () {
82
78
case 'function' : callback = arg ; handlers . push ( callback ) ; break ;
83
79
} ;
84
80
} ) ;
85
-
81
+
86
82
//
87
83
// Helper function to create intelligent error message(s)
88
84
// for the very liberal arguments parsing performed by
@@ -91,32 +87,32 @@ exports.createServer = function () {
91
87
function validArguments ( ) {
92
88
var conditions = {
93
89
'port and host' : function ( ) {
94
- return port && host ;
90
+ return port && host ;
95
91
} ,
96
92
'options.target or options.router' : function ( ) {
97
- return options && ( options . router ||
93
+ return options && ( options . router ||
98
94
( options . target && options . target . host && options . target . port ) ) ;
99
95
} ,
100
96
'or proxy handlers' : function ( ) {
101
97
return handlers && handlers . length ;
102
98
}
103
99
}
104
-
100
+
105
101
var missing = Object . keys ( conditions ) . filter ( function ( name ) {
106
102
return ! conditions [ name ] ( ) ;
107
103
} ) ;
108
-
104
+
109
105
if ( missing . length === 3 ) {
110
106
message = 'Cannot proxy without ' + missing . join ( ', ' ) ;
111
107
return false ;
112
108
}
113
-
109
+
114
110
return true ;
115
- }
116
-
111
+ }
112
+
117
113
if ( ! validArguments ( ) ) {
118
114
//
119
- // If `host`, `port` and `options` are all not passed (with valid
115
+ // If `host`, `port` and `options` are all not passed (with valid
120
116
// options) then this server is improperly configured.
121
117
//
122
118
throw new Error ( message ) ;
@@ -131,7 +127,7 @@ exports.createServer = function () {
131
127
options . target = options . target || { } ;
132
128
options . target . port = options . target . port || port ;
133
129
options . target . host = options . target . host || host ;
134
-
130
+
135
131
if ( options . target && options . target . host && options . target . port ) {
136
132
//
137
133
// If an explicit `host` and `port` combination has been passed
@@ -144,39 +140,18 @@ exports.createServer = function () {
144
140
} ) ;
145
141
}
146
142
else {
147
- //
148
- // If no explicit `host` or `port` combination has been passed then
149
- // we have to assume that this is a "go-anywhere" Proxy (i.e. a `RoutingProxy`).
150
- //
151
- proxy = new RoutingProxy ( options ) ;
152
-
153
- if ( options . router ) {
154
- //
155
- // If a routing table has been supplied than we assume
156
- // the user intends us to add the "proxy" middleware layer
157
- // for them
158
- //
159
- handlers . push ( function ( req , res ) {
160
- proxy . proxyRequest ( req , res ) ;
161
- } ) ;
162
-
163
- proxy . on ( 'routes' , function ( routes ) {
164
- server . emit ( 'routes' , routes ) ;
165
- } ) ;
166
- }
143
+ console . error ( "The modified HTTP Proxy can't be used this way anymore." ) ;
167
144
}
168
-
145
+
169
146
//
170
147
// Create the `http[s].Server` instance which will use
171
148
// an instance of `httpProxy.HttpProxy`.
172
149
//
173
- handler = handlers . length > 1
150
+ handler = handlers . length > 1
174
151
? exports . stack ( handlers , proxy )
175
152
: function ( req , res ) { handlers [ 0 ] ( req , res , proxy ) } ;
176
-
177
- server = options . https
178
- ? https . createServer ( options . https , handler )
179
- : http . createServer ( handler ) ;
153
+
154
+ server = http . createServer ( handler ) ;
180
155
181
156
server . on ( 'close' , function ( ) {
182
157
proxy . close ( ) ;
@@ -185,8 +160,8 @@ exports.createServer = function () {
185
160
if ( ! callback ) {
186
161
//
187
162
// If an explicit callback has not been supplied then
188
- // automagically proxy the request using the `HttpProxy`
189
- // instance we have created.
163
+ // automatically proxy the request using the `HttpProxy`
164
+ // instance we have created.
190
165
//
191
166
server . on ( 'upgrade' , function ( req , socket , head ) {
192
167
proxy . proxyWebSocketRequest ( req , socket , head ) ;
@@ -223,7 +198,7 @@ exports.createServer = function () {
223
198
//
224
199
exports . buffer = function ( obj ) {
225
200
var events = [ ] ,
226
- onData ,
201
+ onData ,
227
202
onEnd ;
228
203
229
204
obj . on ( 'data' , onData = function ( data , encoding ) {
@@ -237,14 +212,14 @@ exports.buffer = function (obj) {
237
212
return {
238
213
end : function ( ) {
239
214
obj . removeListener ( 'data' , onData ) ;
240
- obj . removeListener ( 'end' , onEnd ) ;
215
+ obj . removeListener ( 'end' , onEnd ) ;
241
216
} ,
242
217
destroy : function ( ) {
243
218
this . end ( ) ;
244
219
this . resume = function ( ) {
245
220
console . error ( "Cannot resume buffer after destroying it." ) ;
246
221
} ;
247
-
222
+
248
223
onData = onEnd = events = obj = null ;
249
224
} ,
250
225
resume : function ( ) {
@@ -276,56 +251,6 @@ exports.setMaxSockets = function (value) {
276
251
maxSockets = value ;
277
252
} ;
278
253
279
- //
280
- // ### function stack (middlewares, proxy)
281
- // #### @middlewares {Array} Array of functions to stack.
282
- // #### @proxy {HttpProxy|RoutingProxy} Proxy instance to
283
- // Iteratively build up a single handler to the `http.Server`
284
- // `request` event (i.e. `function (req, res)`) by wrapping
285
- // each middleware `layer` into a `child` middleware which
286
- // is in invoked by the parent (i.e. predecessor in the Array).
287
- //
288
- // adapted from https://github.com/creationix/stack
289
- //
290
- exports . stack = function stack ( middlewares , proxy ) {
291
- var handle ;
292
- middlewares . reverse ( ) . forEach ( function ( layer ) {
293
- var child = handle ;
294
- handle = function ( req , res ) {
295
- var next = function ( err ) {
296
- if ( err ) {
297
- if ( res . _headerSent ) {
298
- res . destroy ( ) ;
299
- }
300
- else {
301
- res . statusCode = 500 ;
302
- res . setHeader ( 'Content-Type' , 'text/plain' ) ;
303
- res . end ( 'Internal Server Error' ) ;
304
- }
305
-
306
- console . error ( 'Error in middleware(s): %s' , err . stack ) ;
307
- return ;
308
- }
309
-
310
- if ( child ) {
311
- child ( req , res ) ;
312
- }
313
- } ;
314
-
315
- //
316
- // Set the prototype of the `next` function to the instance
317
- // of the `proxy` so that in can be used interchangably from
318
- // a `connect` style callback and a true `HttpProxy` object.
319
- //
320
- // e.g. `function (req, res, next)` vs. `function (req, res, proxy)`
321
- //
322
- next . __proto__ = proxy ;
323
- layer ( req , res , next ) ;
324
- } ;
325
- } ) ;
326
-
327
- return handle ;
328
- } ;
329
254
330
255
//
331
256
// ### function _getAgent (host, port, secure)
@@ -345,12 +270,12 @@ exports._getAgent = function _getAgent (options) {
345
270
if ( ! options || ! options . host ) {
346
271
throw new Error ( '`options.host` is required to create an Agent.' ) ;
347
272
}
348
-
273
+
349
274
if ( ! options . port ) {
350
- options . port = options . https ? 443 : 80 ;
275
+ options . port = 80 ;
351
276
}
352
277
353
- var Agent = options . https ? https . Agent : http . Agent ,
278
+ var Agent = http . Agent ,
354
279
agent ;
355
280
356
281
// require('http-proxy').setMaxSockets() should override http's default
@@ -364,11 +289,11 @@ exports._getAgent = function _getAgent (options) {
364
289
//
365
290
// ### function _getProtocol (options)
366
291
// #### @options {Object} Options for the proxy target.
367
- // Returns the appropriate node.js core protocol module (i.e. `http` or `https`)
368
- // based on the `options` supplied.
292
+ // Returns the appropriate node.js core protocol module (i.e. `http` or `https`)
293
+ // based on the `options` supplied.
369
294
//
370
295
exports . _getProtocol = function _getProtocol ( options ) {
371
- return options . https ? https : http ;
296
+ return http ;
372
297
} ;
373
298
374
299
@@ -381,7 +306,7 @@ exports._getProtocol = function _getProtocol (options) {
381
306
//
382
307
exports . _getBase = function _getBase ( options ) {
383
308
var result = function ( ) { } ;
384
-
309
+
385
310
if ( options . https && typeof options . https === 'object' ) {
386
311
[ 'ca' , 'cert' , 'key' ] . forEach ( function ( key ) {
387
312
if ( options . https [ key ] ) {
0 commit comments