tls http https: don't pollute user's `options` object
authorMaciej Małecki <maciej.malecki@notimplemented.org>
Mon, 20 Feb 2012 19:32:10 +0000 (20:32 +0100)
committerBen Noordhuis <info@bnoordhuis.nl>
Mon, 20 Feb 2012 20:58:00 +0000 (21:58 +0100)
lib/http.js
lib/https.js
lib/tls.js

index 91ba17e..f1e9941 100644 (file)
@@ -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;
index bda0373..640de1d 100644 (file)
@@ -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;
index 4d7da1d..a49e36e 100644 (file)
@@ -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];
   }