Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support cookie hostname rewrite #1038

Open
JohnWu-Pro opened this issue Jul 19, 2016 · 0 comments
Open

Support cookie hostname rewrite #1038

JohnWu-Pro opened this issue Jul 19, 2016 · 0 comments

Comments

@JohnWu-Pro
Copy link

New option cookieRewrite, and sample implementation:

// Copied from https://github.com/andrewrk/node-proxy-middleware
function rewriteCookieHostname(req, res, proxyRes, options) {
  if (!(options.cookieRewrite && proxyRes.headers['set-cookie'])) {
    return;
  }

  var existingCookies = proxyRes.headers['set-cookie'],
      rewrittenCookies = [],
      rewriteHostname = (true === options.cookieRewrite) ? os.hostname() : options.cookieRewrite;

  if (!Array.isArray(existingCookies)) {
    existingCookies = [ existingCookies ];
  }

  for (var i = 0; i < existingCookies.length; i++) {
    var rewrittenCookie = existingCookies[i].replace(/(Domain)=[a-z\.-_]*?(;|$)/gi, '$1=' + rewriteHostname + '$2');

    if (!req.connection.encrypted) {
      rewrittenCookie = rewrittenCookie.replace(/;\s*?(Secure)/i, '');
    }
    rewrittenCookies.push(rewrittenCookie);
  }

  proxyRes.headers['set-cookie'] = rewrittenCookies;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant