Skip to content
GitLab
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
  • Issues
  • #1597
Closed
Open
Issue created Jul 03, 2022 by Marc@cawoodm

Content-Length Header is Incorrect

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

Assignee
Assign to
Time tracking