tls http https: don't pollute user's options object

This commit is contained in:
Maciej Małecki 2012-02-20 20:32:10 +01:00 committed by Ben Noordhuis
parent c6c6f98f1c
commit da908364a8
3 changed files with 22 additions and 24 deletions

View File

@ -1076,14 +1076,16 @@ exports.globalAgent = globalAgent;
function ClientRequest(options, cb) { function ClientRequest(options, cb) {
var self = this; var self = this;
OutgoingMessage.call(self); OutgoingMessage.call(self);
self.agent = options.agent;
options.defaultPort = options.defaultPort || 80;
options.port = options.port || options.defaultPort; self.agent = options.agent === undefined ? globalAgent : options.agent;
options.host = options.hostname || options.host || 'localhost';
var defaultPort = options.defaultPort || 80;
var port = options.port || defaultPort;
var host = options.hostname || options.host || 'localhost';
if (options.setHost === undefined) { if (options.setHost === undefined) {
options.setHost = true; var setHost = true;
} }
self.socketPath = options.socketPath; self.socketPath = options.socketPath;
@ -1102,10 +1104,10 @@ function ClientRequest(options, cb) {
self.setHeader(key, options.headers[key]); self.setHeader(key, options.headers[key]);
} }
} }
if (options.host && !this.getHeader('host') && options.setHost) { if (host && !this.getHeader('host') && setHost) {
var hostHeader = options.host; var hostHeader = host;
if (options.port && +options.port !== options.defaultPort) { if (port && +port !== defaultPort) {
hostHeader += ':' + options.port; hostHeader += ':' + port;
} }
this.setHeader('Host', hostHeader); this.setHeader('Host', hostHeader);
} }
@ -1142,15 +1144,15 @@ function ClientRequest(options, cb) {
// If there is an agent we should default to Connection:keep-alive. // If there is an agent we should default to Connection:keep-alive.
self._last = false; self._last = false;
self.shouldKeepAlive = true; self.shouldKeepAlive = true;
self.agent.addRequest(self, options.host, options.port); self.agent.addRequest(self, host, port);
} else { } else {
// No agent, default to Connection:close. // No agent, default to Connection:close.
self._last = true; self._last = true;
self.shouldKeepAlive = false; self.shouldKeepAlive = false;
if (options.createConnection) { if (options.createConnection) {
var conn = options.createConnection(options.port, options.host, options); var conn = options.createConnection(port, host, options);
} else { } else {
var conn = net.createConnection(options.port, options.host); var conn = net.createConnection(port, host);
} }
self.onSocket(conn); self.onSocket(conn);
} }
@ -1426,15 +1428,10 @@ exports.request = function(options, cb) {
throw new Error('Protocol:' + options.protocol + ' not supported.'); throw new Error('Protocol:' + options.protocol + ' not supported.');
} }
if (options.agent === undefined) {
options.agent = globalAgent;
}
return new ClientRequest(options, cb); return new ClientRequest(options, cb);
}; };
exports.get = function(options, cb) { exports.get = function(options, cb) {
options.method = 'GET';
var req = exports.request(options, cb); var req = exports.request(options, cb);
req.end(); req.end();
return req; return req;

View File

@ -83,7 +83,6 @@ exports.request = function(options, cb) {
}; };
exports.get = function(options, cb) { exports.get = function(options, cb) {
options.method = 'GET';
var req = exports.request(options, cb); var req = exports.request(options, cb);
req.end(); req.end();
return req; return req;

View File

@ -1061,28 +1061,30 @@ Server.prototype.SNICallback = function(servername) {
// //
// //
exports.connect = function(/* [port, host], options, cb */) { exports.connect = function(/* [port, host], options, cb */) {
var options = {}, cb; var options, port, host, cb;
if (typeof arguments[0] === 'object') { if (typeof arguments[0] === 'object') {
options = arguments[0]; options = arguments[0];
} else if (typeof arguments[1] === 'object') { } else if (typeof arguments[1] === 'object') {
options = arguments[1]; options = arguments[1];
options.port = arguments[0]; port = arguments[0];
} else if (typeof arguments[2] === 'object') { } else if (typeof arguments[2] === 'object') {
options = arguments[2]; options = arguments[2];
options.port = arguments[0]; port = arguments[0];
options.host = arguments[1]; host = arguments[1];
} else { } else {
// This is what happens when user passes no `options` argument, we can't // This is what happens when user passes no `options` argument, we can't
// throw `TypeError` here because it would be incompatible with old API // throw `TypeError` here because it would be incompatible with old API
if (typeof arguments[0] === 'number') { if (typeof arguments[0] === 'number') {
options.port = arguments[0]; port = arguments[0];
} }
if (typeof arguments[1] === 'string') { 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') { if (typeof arguments[arguments.length - 1] === 'function') {
cb = arguments[arguments.length - 1]; cb = arguments[arguments.length - 1];
} }