Rewrite response headers with node-http-proxy
Asked Answered
I

3

18

I'm using node-http-proxy and want to watch for a particular response header and rewrite it if necessary. Anyone here have suggestions on to do this?

My proxy server sits in front of a couple different node servers as well as a java webapp. The java app is setting a cookie, but the cookie has a path that is relative the the webapp's context. I need the cookie to be secure and have a path to root without modifying the Java application.

In other words, the following header is returned:

set-cookie: MYSPECIALCOOKIE=679b6291-d1cc-47be; Path=/app; HttpOnly

And I'd like to rewrite the Path value to:

set-cookie: MYSPECIALCOOKIE=679b6291-d1cc-47be; Path=/; HttpOnly; Secure

I'm not clear how I would do this using node-http-proxy. Suggestions? Is there middleware to help with this?

Insouciance answered 6/10, 2012 at 1:15 Comment(1)
In case someone else comes across this question, the gzip connect middleware uses a pattern that may be able so solve my problem. I've hacked my own middleware baased on the gzip implementation, but haven't taken the time to getting it fully working. However, it looks like it will work with a bit more tweaking. Check out github.com/nodejitsu/node-http-proxy/blob/master/examples/…Insouciance
A
27

You can achieve this by overloading the writeHead function of the response object. For example, this code will set the 'foo' response header to the value 'bar'. I've indicated where you can add your own logic to change the header values.

JavaScript is not my primary language, so there may be a more idiomatic way to overload the writeHead method.

httpProxy = require('http-proxy');

httpProxy.createServer(function (req, res, proxy) {

  res.oldWriteHead = res.writeHead;
  res.writeHead = function(statusCode, headers) {
    /* add logic to change headers here */
    var contentType = res.getHeader('content-type');
    res.setHeader('content-type', 'text/plain');

    // old way: might not work now
    // as headers param is not always provided
    // https://github.com/nodejitsu/node-http-proxy/pull/260/files
    // headers['foo'] = 'bar';       

    res.oldWriteHead(statusCode, headers);
  }

  proxy.proxyRequest(req, res, {
    host: 'localhost',
    port: 3000
  });
}).listen(8000);
Authorized answered 6/10, 2012 at 1:15 Comment(2)
Thanks! This answer is essentially the same as what I ended up doing. Just forgot to come back here and add the answer. I found an example of how to do it by examining this code: github.com/nateps/connect-gzip/blob/master/lib/gzip.jsInsouciance
Great answer! Bear in mind that for some headers, getHeader will return an array if more than one instance of the header was present. setHeader also accepts an array. Easy to get tripped up by. This shouldn't come up for content-type though.Epicycloid
T
0

Just listen to proxyRes event and put your logic.

proxy.on('proxyRes', (proxyRes, req, res) => {
    // modifying headers goes here
});

See https://www.npmjs.com/package/http-proxy#listening-for-proxy-events

Tenantry answered 12/3, 2021 at 5:39 Comment(0)
N
-1

I didn't test this code, but it should allow you to edit your header before sending the request. Let me know if it works.

var httpProxy = require('http-proxy');

var server = httpProxy.createServer(function (req, res, proxy) {
  var buffer = httpProxy.buffer(req);
  req.headers['x-host'] = process.env.PROXY_URI;
  proxy.proxyRequest(req, res, {
    host: '127.0.0.1',
    port: 9000,
  });
});
Nickell answered 7/10, 2012 at 11:23 Comment(1)
Your code is setting a header on the request. I need to alter a header on the response.Insouciance

© 2022 - 2024 — McMap. All rights reserved.