Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
tls http https: don't pollute user's options object
Browse files Browse the repository at this point in the history
  • Loading branch information
mmalecki authored and bnoordhuis committed Feb 20, 2012
1 parent c6c6f98 commit da90836
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 24 deletions.
31 changes: 14 additions & 17 deletions lib/http.js
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion lib/https.js
Expand Up @@ -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;
Expand Down
14 changes: 8 additions & 6 deletions lib/tls.js
Expand Up @@ -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];
}
Expand Down

0 comments on commit da90836

Please sign in to comment.