Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
17 changes: 17 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -77,6 +91,9 @@ utils.proxyRequest = function (req, res, next) {
if (proxy.config.rules.length) {
proxy.config.rules.forEach(rewrite(req));
}
if(proxy.config.rewriteCookiePath) {
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;
Expand Down
3 changes: 2 additions & 1 deletion tasks/connect_proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down