简体   繁体   English

修改来自node-http-proxy的压缩后的HTML响应

[英]Modify Gzipped HTML response from node-http-proxy

Problem: 问题:

Modifying a Gzipped HTML response gives a white-screen when using node-http-proxy to proxy requests to a vagrant box. 使用node-http-proxy将请求代理到无用信息框时,修改Gzip的HTML响应会显示白屏。

What I have so far: 到目前为止,我有:

My method of intercepting the requests & modifying the html (seen below) is working well for all requests that are NOT Gzipped. 我的拦截请求和修改html的方法(如下所示)对于所有未压缩的请求均适用。 (I've tested using node, ruby, PHP & apache servers). (我已经使用节点,红宝石,PHP和Apache服务器进行了测试)。

The confusing part: 令人困惑的部分:

I have a test suite that starts the proxy & makes a request to it. 我有一个测试套件,可以启动代理并向其发出请求。 If I console.log the response I can clearly see that the HTML has been modified - it's just that it won't display in the browser at all... 如果我console.log响应,我可以清楚地看到HTML已被修改-只是根本不会在浏览器中显示...

The proxy setup 代理设置

var server = httpProxy.createServer(function (req, res, proxy) {

    var next = function () {
        proxy.proxyRequest(req, res, {
            host: "172.22.22.22",
            port: 80,
            changeOrigin: true
        });
    };

    var write = res.write;

    res.write = function (string, encoding) {

        var body = string instanceof Buffer ? string.toString() : string;

        body = body.replace(/<\/body>/, function (w) {
            return "<script>console.log('injected')</script>\n" + w;
        });

        if (string instanceof Buffer) {
            string = new Buffer(body);
        } else {
            string = body;
        }

        write.call(res, string, encoding);
    };

    next();

}).listen(3002);

// Remove content-length, defaults to 'chunked'
server.proxy.on("proxyResponse", function (req, res, response) {
    if (response.headers.hasOwnProperty("content-length")) {
        delete response.headers["content-length"];
    }
});

My test case showing correctly modified HTML 我的测试用例显示正确修改的HTML

it("can modify the html response", function (done) {
    var data;
    http.get(proxyHost, function (res) {
        var chunks = [];
        res.on("data", function (chunk) {
            chunks.push(chunk.toString());
        });
        res.on("end", function () {
            data = chunks.join("");
            console.log(data); // snippet can be seen in this response
            done();
        });
    });
});

Any ideas? 有任何想法吗?

I have solved this problem. 我已经解决了这个问题。

Can't upload the source, but I can give you some hints on this. 无法上传源,但是我可以给您一些提示。

  1. use server.proxy.on("proxyResponse" to check content-encoding 使用server.proxy.on(“ proxyResponse”检查内容编码
  2. do nothing but gather chunk on res.write 除了在res.write上收集块外什么都不做
  3. on res.end, zlib.unzip gathered chunks if content-encoding was "gzip" then oldWrite.call(res,decoded_string), oldEnd.apply(res,arguments) 在res.end上,如果内容编码为“ gzip”,则zlib.unzip会收集块,然后oldWrite.call(res,decoded_string),oldEnd.apply(res,arguments)
  4. if you want to change something do it while #3 如果您想更改某些内容,请在#3时执行

Hope this help you out. 希望这对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM