From 9f458e033dc067f59d3a27f34da0dfebf1fedaa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Gil?= Date: Thu, 24 Oct 2013 23:43:56 -0200 Subject: [PATCH 1/2] rewriteCookiePath option, to rewrite the "Path" from "Set-Cookie" response headers --- README.md | 5 +++++ lib/utils.js | 15 +++++++++++++++ tasks/connect_proxy.js | 3 ++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e5e9331..9dfc068 100644 --- a/README.md +++ b/README.md @@ -184,6 +184,11 @@ proxies: [ ] ``` +### options.rewriteCookiePath +Type: `String` + +Rewrite the "Path" from "Set-Cookie" response headers, useful when the host server has a different context path than your app. + #### options.timeout Type: `Number` diff --git a/lib/utils.js b/lib/utils.js index dc7c289..c9eafa5 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -77,6 +77,21 @@ utils.proxyRequest = function (req, res, next) { if (proxy.config.rules.length) { proxy.config.rules.forEach(rewrite(req)); } + + if(proxy.config.rewriteCookiePath) { + var _setHeader = res.setHeader.bind(res); + res.setHeader = function(name, value) { + if (name && value && name.toLowerCase() === 'set-cookie') { + var fixedCookie = value.toString().replace(/Path=([^;]*)(;|$)/gi, function(match, path, end){ + return 'Path=' + proxy.config.rewriteCookiePath + end; + }); + return _setHeader('Set-Cookie', fixedCookie); + } else { + return _setHeader(name, value); + } + }; + } + proxy.server.proxyRequest(req, res); // proxying twice would cause the writing to a response header that is already sent. Bad config! proxied = true; diff --git a/tasks/connect_proxy.js b/tasks/connect_proxy.js index 253db3b..612f8c7 100644 --- a/tasks/connect_proxy.js +++ b/tasks/connect_proxy.js @@ -45,7 +45,8 @@ module.exports = function(grunt) { changeOrigin: false, xforward: false, rejectUnauthorized: false, - rules: [] + rules: [], + rewriteCookiePath: null }); if (validateProxyConfig(proxyOption)) { proxyOption.rules = utils.processRewrites(proxyOption.rewrite); From 4230dd8f4bd9e8a888fa7f9d4e21c0e074eeadae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Gil?= Date: Fri, 25 Oct 2013 10:17:53 -0200 Subject: [PATCH 2/2] extracting the rewrite code to a separate function --- lib/utils.js | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index c9eafa5..4752953 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -69,6 +69,20 @@ utils.matchContext = function(context, url) { return matched; }; +utils.interceptCookiePaths = function(res, rewritePath) { + var _setHeader = res.setHeader.bind(res); + res.setHeader = function(name, value) { + if (name && value && name.toLowerCase() === 'set-cookie') { + var fixedCookie = value.toString().replace(/Path=([^;]*)(;|$)/gi, function(match, path, end){ + return 'Path=' + rewritePath + end; + }); + return _setHeader('Set-Cookie', fixedCookie); + } else { + return _setHeader(name, value); + } + }; +}; + utils.proxyRequest = function (req, res, next) { var proxied = false; @@ -77,21 +91,9 @@ utils.proxyRequest = function (req, res, next) { if (proxy.config.rules.length) { proxy.config.rules.forEach(rewrite(req)); } - if(proxy.config.rewriteCookiePath) { - var _setHeader = res.setHeader.bind(res); - res.setHeader = function(name, value) { - if (name && value && name.toLowerCase() === 'set-cookie') { - var fixedCookie = value.toString().replace(/Path=([^;]*)(;|$)/gi, function(match, path, end){ - return 'Path=' + proxy.config.rewriteCookiePath + end; - }); - return _setHeader('Set-Cookie', fixedCookie); - } else { - return _setHeader(name, value); - } - }; + utils.interceptCookiePaths(res, proxy.config.rewriteCookiePath); } - proxy.server.proxyRequest(req, res); // proxying twice would cause the writing to a response header that is already sent. Bad config! proxied = true;