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

Content-Length Header is Incorrect #1597

Open
cawoodm opened this issue Jul 3, 2022 · 0 comments
Open

Content-Length Header is Incorrect #1597

cawoodm opened this issue Jul 3, 2022 · 0 comments

Comments

@cawoodm
Copy link

cawoodm commented Jul 3, 2022

Not sure if this is an express issue or with http-proxy but I am modifying (extending) the response body and the resulting Content-Length header is not getting extended. When I try to extend it with proxyRes.headers this does not get passed on to the client and there is no res.headers method/hashmap.

const express = require('express');
var app = express();
var httpProxy = require('http-proxy');
var apiProxy = httpProxy.createProxyServer();

app.get('/', (req, res) => {
  apiProxy.web(req, res, {target: MYB});
});

apiProxy.on('proxyRes', (proxyRes, req, res) => {
  var _write = res.write;
  var body = '';
  proxyRes.on('data', function(data) {
    data = data.toString('utf-8');
    body += data;
  });
  res.write = function(data) {
    try {
      if (res.statusCode === 200 && proxyRes.headers['content-type'] === 'text/html') {
        // body.length is 411 bytes
        body = body.replace('</body>', '<hr>&copy; aaa 2022 Marc Cawood</body>');
        // body.length is now 425 bytes
        proxyRes.headers['content-length'] = Buffer.byteLength(body, 'utf-8');
        _write.call(res, body);
       // Browser still gets Content-Length: 411 :-(
      }
    } catch (e) {
      console.trace(e);
      console.error(e.message);
      throw e;
    }
  };
});

image

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