From b16b6ffa238c60fdc385dd7b78825144b193f69a Mon Sep 17 00:00:00 2001 From: arch Date: Thu, 10 Mar 2016 19:35:58 -0500 Subject: [PATCH 1/2] fix for 302 redirects where there is a uri tail after the host --- index.js | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index b8c573e..e5fa14f 100644 --- a/index.js +++ b/index.js @@ -58,10 +58,14 @@ module.exports = function proxyMiddleware(options) { , headers = myRes.headers , location = headers.location; // Fix the location - if (((statusCode > 300 && statusCode < 304) || statusCode === 201) && location && location.indexOf(options.href) > -1) { - // absoulte path - headers.location = location.replace(options.href, slashJoin('/', slashJoin((options.route || ''), ''))); - } + if ((statusCode > 300 && statusCode < 304) && location && location.indexOf(options.href) > -1) { + // don't use absoulte path as it often will be a problem + headers.location = replaceHostPreserveUri(location, req.headers.origin) + } else if ((statusCode === 201) && location && location.indexOf(options.href) > -1) { + //absoulte path + headers.location = location.replace(options.href, slashJoin('/', slashJoin((options.route || ''), ''))); + } + applyViaHeader(myRes.headers, opts, myRes.headers); rewriteCookieHosts(myRes.headers, opts, myRes.headers, req); resp.writeHead(myRes.statusCode, myRes.headers); @@ -139,3 +143,16 @@ function merge(src1, src2) { extend(merged, src2); return merged; } + +//keep any uri bits after the host +function replaceHostPreserveUri(uri, newHost) { + var uriStr = uri, + delimiter = '/', + start = 3, + tokens = uriStr.split(delimiter).slice(start), + result = tokens.join(delimiter); + + result = '/' + result + + return (newHost === undefined ? result : newHost + result) +} From 6c48261a084e97614735a397fabd447bed48678b Mon Sep 17 00:00:00 2001 From: arch Date: Wed, 16 Mar 2016 20:12:27 -0400 Subject: [PATCH 2/2] better solution --- index.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index e5fa14f..6c61fd3 100644 --- a/index.js +++ b/index.js @@ -60,7 +60,7 @@ module.exports = function proxyMiddleware(options) { // Fix the location if ((statusCode > 300 && statusCode < 304) && location && location.indexOf(options.href) > -1) { // don't use absoulte path as it often will be a problem - headers.location = replaceHostPreserveUri(location, req.headers.origin) + headers.location = preserveUri(location) } else if ((statusCode === 201) && location && location.indexOf(options.href) > -1) { //absoulte path headers.location = location.replace(options.href, slashJoin('/', slashJoin((options.route || ''), ''))); @@ -156,3 +156,16 @@ function replaceHostPreserveUri(uri, newHost) { return (newHost === undefined ? result : newHost + result) } + +//keep any uri bits after the host +function preserveUri(uri) { + var uriStr = uri, + delimiter = '/', + start = 3, + tokens = uriStr.split(delimiter).slice(start), + result = tokens.join(delimiter); + + result = '/' + result + + return result +}