diff --git a/lib/http.js b/lib/http.js index 91ba17ee564..f1e9941a51a 100644 --- a/lib/http.js +++ b/lib/http.js @@ -1076,14 +1076,16 @@ exports.globalAgent = globalAgent; function ClientRequest(options, cb) { var self = this; OutgoingMessage.call(self); - self.agent = options.agent; - options.defaultPort = options.defaultPort || 80; - options.port = options.port || options.defaultPort; - options.host = options.hostname || options.host || 'localhost'; + self.agent = options.agent === undefined ? globalAgent : options.agent; + + var defaultPort = options.defaultPort || 80; + + var port = options.port || defaultPort; + var host = options.hostname || options.host || 'localhost'; if (options.setHost === undefined) { - options.setHost = true; + var setHost = true; } self.socketPath = options.socketPath; @@ -1102,10 +1104,10 @@ function ClientRequest(options, cb) { self.setHeader(key, options.headers[key]); } } - if (options.host && !this.getHeader('host') && options.setHost) { - var hostHeader = options.host; - if (options.port && +options.port !== options.defaultPort) { - hostHeader += ':' + options.port; + if (host && !this.getHeader('host') && setHost) { + var hostHeader = host; + if (port && +port !== defaultPort) { + hostHeader += ':' + port; } this.setHeader('Host', hostHeader); } @@ -1142,15 +1144,15 @@ function ClientRequest(options, cb) { // If there is an agent we should default to Connection:keep-alive. self._last = false; self.shouldKeepAlive = true; - self.agent.addRequest(self, options.host, options.port); + self.agent.addRequest(self, host, port); } else { // No agent, default to Connection:close. self._last = true; self.shouldKeepAlive = false; if (options.createConnection) { - var conn = options.createConnection(options.port, options.host, options); + var conn = options.createConnection(port, host, options); } else { - var conn = net.createConnection(options.port, options.host); + var conn = net.createConnection(port, host); } self.onSocket(conn); } @@ -1426,15 +1428,10 @@ exports.request = function(options, cb) { throw new Error('Protocol:' + options.protocol + ' not supported.'); } - if (options.agent === undefined) { - options.agent = globalAgent; - } - return new ClientRequest(options, cb); }; exports.get = function(options, cb) { - options.method = 'GET'; var req = exports.request(options, cb); req.end(); return req; diff --git a/lib/https.js b/lib/https.js index bda03733a34..640de1dd005 100644 --- a/lib/https.js +++ b/lib/https.js @@ -83,7 +83,6 @@ exports.request = function(options, cb) { }; exports.get = function(options, cb) { - options.method = 'GET'; var req = exports.request(options, cb); req.end(); return req; diff --git a/lib/tls.js b/lib/tls.js index 4d7da1dde45..a49e36e7384 100644 --- a/lib/tls.js +++ b/lib/tls.js @@ -1061,28 +1061,30 @@ Server.prototype.SNICallback = function(servername) { // // exports.connect = function(/* [port, host], options, cb */) { - var options = {}, cb; + var options, port, host, cb; if (typeof arguments[0] === 'object') { options = arguments[0]; } else if (typeof arguments[1] === 'object') { options = arguments[1]; - options.port = arguments[0]; + port = arguments[0]; } else if (typeof arguments[2] === 'object') { options = arguments[2]; - options.port = arguments[0]; - options.host = arguments[1]; + port = arguments[0]; + host = arguments[1]; } else { // This is what happens when user passes no `options` argument, we can't // throw `TypeError` here because it would be incompatible with old API if (typeof arguments[0] === 'number') { - options.port = arguments[0]; + port = arguments[0]; } if (typeof arguments[1] === 'string') { - options.host = arguments[1]; + host = arguments[1]; } } + options = util._extend({ port: port, host: host }, options || {}); + if (typeof arguments[arguments.length - 1] === 'function') { cb = arguments[arguments.length - 1]; }