From 9e2a7e046a439980e73c0d05bdea0fc3b1df815d Mon Sep 17 00:00:00 2001 From: John Lancaster Date: Fri, 23 Oct 2015 22:24:59 -0400 Subject: [PATCH 1/2] reverted slashJoin function, the faulty logic was outside the function, I added a new case check for req.url URLs that contain '/?query=param' as well as '?query=param', tests pass --- index.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index b8c573e..7f26273 100644 --- a/index.js +++ b/index.js @@ -19,6 +19,7 @@ module.exports = function proxyMiddleware(options) { return function (req, resp, next) { var url = req.url; + // You can pass the route within the options, as well if (typeof options.route === 'string') { if (url === options.route) { @@ -32,13 +33,16 @@ module.exports = function proxyMiddleware(options) { //options for this request var opts = extend({}, options); - if (url && url.charAt(0) === '?') { // prevent /api/resource/?offset=0 + if (url && url.charAt(1) === '?') { // prevent /api/resource/?offset=0 when url === '/?query=param' if (options.pathname.length > 1 && options.pathname.charAt(options.pathname.length - 1) === '/') { opts.path = options.pathname.substring(0, options.pathname.length - 1) + url; - } else { - opts.path = options.pathname + url; + } else { // if pathname doesn't have a trailing slash + opts.path = options.pathname + url.substring(1); // discard leading slash in url } + } else if (url && url.charAt(0) === '?') { // prevent /api/resource/?offset=0 when url === '?query=param' + opts.path = options.pathname + url; } else if (url) { + if (url.charAt(0) === '/' && url.charAt(1) === '?') url = url.substring(0) opts.path = slashJoin(options.pathname, url); } else { opts.path = options.pathname; @@ -119,12 +123,9 @@ function rewriteCookieHosts(existingHeaders, opts, applyTo, req) { } function slashJoin(p1, p2) { - var trailing_slash = false; - - if (p1.length && p1[p1.length - 1] === '/') { trailing_slash = true; } - if (trailing_slash && p2.length && p2[0] === '/') {p2 = p2.substring(1); } - - return p1 + p2; + if (p1.length && p1[p1.length - 1] === '/') {p1 = p1.substring(0, p1.length - 1); } + if (p2.length && p2[0] === '/') {p2 = p2.substring(1); } + return p1 + '/' + p2; } function extend(obj, src) { From 3205362000c4d6e4fa82d60fd57e4ff502c91f4f Mon Sep 17 00:00:00 2001 From: John Lancaster Date: Mon, 26 Oct 2015 15:45:49 -0400 Subject: [PATCH 2/2] slashJoin() should only combine p1 & p2 and add a slash if a slash exists on either p1 or p2, or on both. --- index.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 7f26273..03e726e 100644 --- a/index.js +++ b/index.js @@ -123,9 +123,8 @@ function rewriteCookieHosts(existingHeaders, opts, applyTo, req) { } function slashJoin(p1, p2) { - if (p1.length && p1[p1.length - 1] === '/') {p1 = p1.substring(0, p1.length - 1); } - if (p2.length && p2[0] === '/') {p2 = p2.substring(1); } - return p1 + '/' + p2; + if (p1.length && p1[p1.length - 1] === '/' && p2.length && p2[0] === '/') {p2 = p2.substring(1); } + return p1 + p2; } function extend(obj, src) {