Repository: websockets/ws
Pull request #1347 (2018-03-31 20:20:04)

Add default value for `servername` option
This fixes a regression introduced in 9e152f920a1818d8a23e0a113b4625c83f90d30d.


Patch:
diff --git a/lib/buffer-util.js b/lib/buffer-util.js
index 38e16df3..6974dd6a 100644
--- a/lib/buffer-util.js
+++ b/lib/buffer-util.js
@@ -8,7 +8,7 @@
  * @return {Buffer} The resulting buffer
  * @public
  */
-const concat = (list, totalLength) => {
+function concat (list, totalLength) {
   const target = Buffer.allocUnsafe(totalLength);
   var offset = 0;
 
@@ -19,43 +19,54 @@ const concat = (list, totalLength) => {
   }
 
   return target;
-};
+}
+
+/**
+ * Masks a buffer using the given mask.
+ *
+ * @param {Buffer} source The buffer to mask
+ * @param {Buffer} mask The mask to use
+ * @param {Buffer} output The buffer where to store the result
+ * @param {Number} offset The offset at which to start writing
+ * @param {Number} length The number of bytes to mask.
+ * @public
+ */
+function _mask (source, mask, output, offset, length) {
+  for (var i = 0; i < length; i++) {
+    output[offset + i] = source[i] ^ mask[i & 3];
+  }
+}
+
+/**
+ * Unmasks a buffer using the given mask.
+ *
+ * @param {Buffer} buffer The buffer to unmask
+ * @param {Buffer} mask The mask to use
+ * @public
+ */
+function _unmask (buffer, mask) {
+  // Required until https://github.com/nodejs/node/issues/9006 is resolved.
+  const length = buffer.length;
+  for (var i = 0; i < length; i++) {
+    buffer[i] ^= mask[i & 3];
+  }
+}
 
 try {
   const bufferUtil = require('bufferutil');
+  const bu = bufferUtil.BufferUtil || bufferUtil;
 
-  module.exports = Object.assign({ concat }, bufferUtil.BufferUtil || bufferUtil);
-} catch (e) /* istanbul ignore next */ {
-  /**
-   * Masks a buffer using the given mask.
-   *
-   * @param {Buffer} source The buffer to mask
-   * @param {Buffer} mask The mask to use
-   * @param {Buffer} output The buffer where to store the result
-   * @param {Number} offset The offset at which to start writing
-   * @param {Number} length The number of bytes to mask.
-   * @public
-   */
-  const mask = (source, mask, output, offset, length) => {
-    for (var i = 0; i < length; i++) {
-      output[offset + i] = source[i] ^ mask[i & 3];
-    }
-  };
-
-  /**
-   * Unmasks a buffer using the given mask.
-   *
-   * @param {Buffer} buffer The buffer to unmask
-   * @param {Buffer} mask The mask to use
-   * @public
-   */
-  const unmask = (buffer, mask) => {
-    // Required until https://github.com/nodejs/node/issues/9006 is resolved.
-    const length = buffer.length;
-    for (var i = 0; i < length; i++) {
-      buffer[i] ^= mask[i & 3];
-    }
+  module.exports = {
+    mask (source, mask, output, offset, length) {
+      if (length < 48) _mask(source, mask, output, offset, length);
+      else bu.mask(source, mask, output, offset, length);
+    },
+    unmask (buffer, mask) {
+      if (buffer.length < 32) _unmask(buffer, mask);
+      else bu.unmask(buffer, mask);
+    },
+    concat
   };
-
-  module.exports = { concat, mask, unmask };
+} catch (e) /* istanbul ignore next */ {
+  module.exports = { concat, mask: _mask, unmask: _unmask };
 }
diff --git a/lib/websocket.js b/lib/websocket.js
index c603675a..67cd8a49 100644
--- a/lib/websocket.js
+++ b/lib/websocket.js
@@ -465,7 +465,9 @@ function initAsClient (address, protocols, options) {
 
   options.createConnection = isSecure ? tlsConnect : netConnect;
   options.port = parsedUrl.port || (isSecure ? 443 : 80);
-  options.host = parsedUrl.hostname;
+  options.host = parsedUrl.hostname.startsWith('[')
+    ? parsedUrl.hostname.slice(1, -1)
+    : parsedUrl.hostname;
   options.headers = Object.assign({
     'Sec-WebSocket-Version': options.protocolVersion,
     'Sec-WebSocket-Key': key,
@@ -622,6 +624,7 @@ function netConnect (options) {
  */
 function tlsConnect (options) {
   options.path = options.socketPath || options._socketPath || undefined;
+  options.servername = options.servername || options.host;
   return tls.connect(options);
 }
 


Test patch:
diff --git a/test/websocket.test.js b/test/websocket.test.js
index a3318748..2807e59f 100644
--- a/test/websocket.test.js
+++ b/test/websocket.test.js
@@ -41,12 +41,13 @@ describe('WebSocket', function () {
 
       const agent = new CustomAgent();
 
-      agent.addRequest = (req) => {
+      agent.addRequest = (req, opts) => {
+        assert.strictEqual(opts.host, '::1');
         assert.strictEqual(req.path, '/');
         done();
       };
 
-      const ws = new WebSocket(new url.URL('ws://localhost'), { agent });
+      const ws = new WebSocket(new url.URL('ws://[::1]'), { agent });
     });
 
     describe('options', function () {