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;
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);
}
// 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);
}
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;
//
//
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];
}