From c04485671a147cd47d5ce1487bdc22187feccc7c Mon Sep 17 00:00:00 2001 From: Jarrett Cruger Date: Mon, 8 Dec 2014 23:04:23 -0500 Subject: [PATCH] [fix] use simple regex instead of indexOf to check the protocol to support without the colon fixes #711 --- lib/http-proxy/common.js | 12 +++++++++--- lib/http-proxy/passes/ws-incoming.js | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/http-proxy/common.js b/lib/http-proxy/common.js index 15e0392..679812a 100644 --- a/lib/http-proxy/common.js +++ b/lib/http-proxy/common.js @@ -3,7 +3,13 @@ var common = exports, extend = require('util')._extend, required = require('requires-port'); -var upgradeHeader = /(^|,)\s*upgrade\s*($|,)/i; +var upgradeHeader = /(^|,)\s*upgrade\s*($|,)/i, + isSSL = /^https|wss/; + +/** + * Simple Regex for testing if protocol is https + */ +common.isSSL = isSSL; /** * Copies the right headers from `options` and `req` to * `outgoing` which is then used to fire the proxied @@ -26,7 +32,7 @@ var upgradeHeader = /(^|,)\s*upgrade\s*($|,)/i; common.setupOutgoing = function(outgoing, options, req, forward) { outgoing.port = options[forward || 'target'].port || - (~['https:', 'wss:'].indexOf(options[forward || 'target'].protocol) ? 443 : 80); + (isSSL.test(options[forward || 'target'].protocol) ? 443 : 80); ['host', 'hostname', 'socketPath'].forEach( function(e) { outgoing[e] = options[forward || 'target'][e]; } @@ -39,7 +45,7 @@ common.setupOutgoing = function(outgoing, options, req, forward) { extend(outgoing.headers, options.headers); } - if (~['https:', 'wss:'].indexOf(options[forward || 'target'].protocol)) { + if (isSSL.test(options[forward || 'target'].protocol)) { outgoing.rejectUnauthorized = (typeof options.secure === "undefined") ? true : options.secure; } diff --git a/lib/http-proxy/passes/ws-incoming.js b/lib/http-proxy/passes/ws-incoming.js index c8a5f20..9ad8060 100644 --- a/lib/http-proxy/passes/ws-incoming.js +++ b/lib/http-proxy/passes/ws-incoming.js @@ -84,7 +84,7 @@ var passes = exports; if (head && head.length) socket.unshift(head); - var proxyReq = (~['https:', 'wss:'].indexOf(options.target.protocol) ? https : http).request( + var proxyReq = (common.isSSL.test(options.target.protocol) ? https : http).request( common.setupOutgoing(options.ssl || {}, options, req) ); // Error Handler