@@ -41,7 +41,7 @@ exports.version = [0, 4, 2];
41
41
// #### @host {string} Host of the agent to get
42
42
// #### @port {number} Port of the agent to get
43
43
// #### @secure {boolean} Value indicating whether or not to use HTTPS
44
- // Retreives an agent from the `http` module
44
+ // Retreives an agent from the `http` or `https` module
45
45
// and sets the `maxSockets` property appropriately.
46
46
//
47
47
function _getAgent ( host , port , secure ) {
@@ -55,18 +55,19 @@ function _getAgent (host, port, secure) {
55
55
}
56
56
57
57
//
58
- // ### function _getProtocol (outgoing, https)
59
- // #### @outgoing {Object} Outgoing request options
58
+ // ### function _getProtocol (secure, outgoing)
60
59
// #### @secure {Object|boolean} Settings for `https`
60
+ // #### @outgoing {Object} Outgoing request options
61
61
// Returns the appropriate protocol based on the settings in
62
62
// `secure`. If the protocol is `https` this function will update
63
63
// the options in `outgoing` as appropriate by adding `ca`, `key`,
64
64
// and `cert` if they exist in `secure`.
65
65
//
66
- function _getProtocol ( outgoing , secure ) {
66
+ function _getProtocol ( secure , outgoing ) {
67
67
var protocol = secure ? https : http ;
68
68
69
69
if ( typeof secure === 'object' ) {
70
+ outgoing = outgoing || { } ;
70
71
[ 'ca' , 'cert' , 'key' ] . forEach ( function ( prop ) {
71
72
if ( secure [ prop ] ) {
72
73
outgoing [ prop ] = secure [ prop ] ;
@@ -110,11 +111,10 @@ exports.setMaxSockets = function (value) {
110
111
// * `httpPRoxy.createServer(function (req, res, proxy) { ... })`
111
112
//
112
113
exports . createServer = function ( ) {
113
- var args , callback , port , host , forward ,
114
- silent , options , proxy , server ;
115
-
116
- args = Array . prototype . slice . call ( arguments ) ;
117
- callback = typeof args [ args . length - 1 ] === 'function' && args . pop ( ) ;
114
+ var args = Array . prototype . slice . call ( arguments ) ,
115
+ callback = typeof args [ 0 ] === 'function' && args . shift ( ) ,
116
+ options = { } ,
117
+ port , host , forward , silent , proxy , server ;
118
118
119
119
if ( args . length >= 2 ) {
120
120
port = args [ 0 ] ;
@@ -129,7 +129,8 @@ exports.createServer = function () {
129
129
}
130
130
131
131
proxy = new HttpProxy ( options ) ;
132
- server = http . createServer ( function ( req , res ) {
132
+
133
+ handler = function ( req , res ) {
133
134
if ( callback ) {
134
135
//
135
136
// If we were passed a callback to process the request
@@ -160,7 +161,11 @@ exports.createServer = function () {
160
161
//
161
162
throw new Error ( 'Cannot proxy without port, host, or router.' )
162
163
}
163
- } ) ;
164
+ } ;
165
+
166
+ server = options . https
167
+ ? https . createServer ( options . https , handler )
168
+ : http . createServer ( handler ) ;
164
169
165
170
server . on ( 'close' , function ( ) {
166
171
proxy . close ( ) ;
@@ -187,7 +192,7 @@ exports.createServer = function () {
187
192
// Set the proxy on the server so it is available
188
193
// to the consumer of the server
189
194
//
190
- server . proxy = proxy ;
195
+ // server.proxy = proxy;
191
196
192
197
return server ;
193
198
} ;
@@ -374,12 +379,12 @@ HttpProxy.prototype.proxyRequest = function (req, res, options) {
374
379
path : req . url ,
375
380
headers : req . headers
376
381
} ;
377
-
382
+
378
383
// Force the `connection` header to be 'close' until
379
384
// node.js core re-implements 'keep-alive'.
380
385
outgoing . headers [ 'connection' ] = 'close' ;
381
386
382
- protocol = _getProtocol ( outgoing , options . https || this . https ) ;
387
+ protocol = _getProtocol ( options . https || this . https , outgoing ) ;
383
388
384
389
// Open new HTTP request to internal resource with will act as a reverse proxy pass
385
390
reverseProxy = protocol . request ( outgoing , function ( response ) {
@@ -474,7 +479,7 @@ HttpProxy.prototype._forwardRequest = function (req) {
474
479
// node.js core re-implements 'keep-alive'.
475
480
outgoing . headers [ 'connection' ] = 'close' ;
476
481
477
- protocol = _getProtocol ( outgoing , this . forward . https ) ;
482
+ protocol = _getProtocol ( this . forward . https , outgoing ) ;
478
483
479
484
// Open new HTTP request to internal resource with will act as a reverse proxy pass
480
485
forwardProxy = protocol . request ( outgoing , function ( response ) {
0 commit comments