Skip to content
GitLab
    • Explore Projects Groups Snippets
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • N node-http-proxy
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 482
    • Issues 482
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 102
    • Merge requests 102
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • http ... PARTY!
  • node-http-proxy
  • Merge requests
  • !1235

Add option to rewrite path in set-cookie headers

  • Review changes

  • Download
  • Email patches
  • Plain diff
Closed Administrator requested to merge github/fork/swillis12/master into master 7 years ago
  • Overview 4
  • Commits 4
  • Pipelines 0
  • Changes 4

Created by: swillis12

Adding ability to rewrite the path of a cookie in the set-cookie header, building on top of what was there for https://github.com/nodejitsu/node-http-proxy/pull/1009. I added some documentation and test cases and am open to feedback. I look forward to having this merged in.

Example usage: "cookiePathRewrite": "/"

This will rewrite cookie paths from /dummyPath to /. This is useful when using Tomcat and each application is hosted in it's own "context" (which is the war file name), so the path for each cookie is set to the war file name.

Compare
  • master (base)

and
  • latest version
    9ddb8b88
    4 commits, 2 years ago

4 files
+ 65
- 15

    Preferences

    File browser
    Compare changes
lib/htt‎p-proxy‎
pas‎ses‎
web-out‎going.js‎ +9 -1
comm‎on.js‎ +13 -14
te‎st‎
lib-http-proxy-passes‎-web-outgoing-test.js‎ +31 -0
READ‎ME.md‎ +12 -0
lib/http-proxy/passes/web-outgoing.js
+ 9
- 1
  • View file @ 9ddb8b88


@@ -84,12 +84,16 @@ module.exports = { // <--
*/
writeHeaders: function writeHeaders(req, res, proxyRes, options) {
var rewriteCookieDomainConfig = options.cookieDomainRewrite,
rewriteCookiePathConfig = options.cookiePathRewrite,
preserveHeaderKeyCase = options.preserveHeaderKeyCase,
rawHeaderKeyMap,
setHeader = function(key, header) {
if (header == undefined) return;
if (rewriteCookieDomainConfig && key.toLowerCase() === 'set-cookie') {
header = common.rewriteCookieDomain(header, rewriteCookieDomainConfig);
header = common.rewriteCookieProperty(header, rewriteCookieDomainConfig, 'domain');
}
if (rewriteCookiePathConfig && key.toLowerCase() === 'set-cookie') {
header = common.rewriteCookieProperty(header, rewriteCookiePathConfig, 'path');
}
res.setHeader(String(key).trim(), header);
};
@@ -98,6 +102,10 @@ module.exports = { // <--
rewriteCookieDomainConfig = { '*': rewriteCookieDomainConfig };
}
if (typeof rewriteCookiePathConfig === 'string') { //also test for ''
rewriteCookiePathConfig = { '*': rewriteCookiePathConfig };
}
// message.rawHeaders is added in: v0.11.6
// https://nodejs.org/api/http.html#http_message_rawheaders
if (preserveHeaderKeyCase && proxyRes.rawHeaders != undefined) {
lib/http-proxy/common.js
+ 13
- 14
  • View file @ 9ddb8b88


@@ -4,8 +4,7 @@ var common = exports,
required = require('requires-port');
var upgradeHeader = /(^|,)\s*upgrade\s*($|,)/i,
isSSL = /^https|wss/,
cookieDomainRegex = /(;\s*domain=)([^;]+)/i;
isSSL = /^https|wss/;
/**
* Simple Regex for testing if protocol is https
@@ -211,27 +210,27 @@ common.urlJoin = function() {
*
* @api private
*/
common.rewriteCookieDomain = function rewriteCookieDomain(header, config) {
common.rewriteCookieProperty = function rewriteCookieProperty(header, config, property) {
if (Array.isArray(header)) {
return header.map(function (headerElement) {
return rewriteCookieDomain(headerElement, config);
return rewriteCookieProperty(headerElement, config, property);
});
}
return header.replace(cookieDomainRegex, function(match, prefix, previousDomain) {
var newDomain;
if (previousDomain in config) {
newDomain = config[previousDomain];
return header.replace(new RegExp("(;\\s*" + property + "=)([^;]+)", 'i'), function(match, prefix, previousValue) {
var newValue;
if (previousValue in config) {
newValue = config[previousValue];
} else if ('*' in config) {
newDomain = config['*'];
newValue = config['*'];
} else {
//no match, return previous domain
//no match, return previous value
return match;
}
if (newDomain) {
//replace domain
return prefix + newDomain;
if (newValue) {
//replace value
return prefix + newValue;
} else {
//remove domain
//remove value
return '';
}
});
test/lib-http-proxy-passes-web-outgoing-test.js
+ 31
- 0
  • View file @ 9ddb8b88


@@ -289,6 +289,37 @@ describe('lib/http-proxy/passes/web-outgoing.js', function () {
expect(this.res.headers['set-cookie']).to.have.length(2);
});
it('rewrites path', function() {
var options = {
cookiePathRewrite: '/dummyPath'
};
httpProxy.writeHeaders({}, this.res, this.proxyRes, options);
expect(this.res.headers['set-cookie'])
.to.contain('hello; domain=my.domain; path=/dummyPath');
});
it('does not rewrite path', function() {
var options = {};
httpProxy.writeHeaders({}, this.res, this.proxyRes, options);
expect(this.res.headers['set-cookie'])
.to.contain('hello; domain=my.domain; path=/');
});
it('removes path', function() {
var options = {
cookiePathRewrite: ''
};
httpProxy.writeHeaders({}, this.res, this.proxyRes, options);
expect(this.res.headers['set-cookie'])
.to.contain('hello; domain=my.domain');
});
it('does not rewrite domain', function() {
var options = {};
README.md
+ 12
- 0
  • View file @ 9ddb8b88


@@ -350,6 +350,18 @@ proxyServer.listen(8015);
"*": ""
}
```
* **cookiePathRewrite**: rewrites path of `set-cookie` headers. Possible values:
* `false` (default): disable cookie rewriting
* String: new path, for example `cookiePathRewrite: "/newPath/"`. To remove the path, use `cookiePathRewrite: ""`. To set path to root use `cookiePathRewrite: "/"`.
* Object: mapping of paths to new paths, use `"*"` to match all paths.
For example keep one path unchanged, rewrite one path and remove other paths:
```
cookiePathRewrite: {
"/unchanged.path/": "/unchanged.path/",
"/old.path/": "/new.path/",
"*": ""
}
```
* **headers**: object with extra headers to be added to target requests.
* **proxyTimeout**: timeout (in millis) when proxy receives no response from target
0 Assignees
None
Assign to
0 Reviewers
None
Request review from
Labels
0
None
0
None
    Assign labels
  • Manage project labels

Milestone
No milestone
None
None
Time tracking
No estimate or time spent
Lock merge request
Unlocked
1
1 participant
Administrator
Reference: http-party/node-http-proxy!1235
Source branch: github/fork/swillis12/master

Menu

Explore Projects Groups Snippets