Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 修改来自节点http代理的gzip HTML响应 问题:_Php_Html_Node.js_Proxy - Fatal编程技术网

Php 修改来自节点http代理的gzip HTML响应 问题:

Php 修改来自节点http代理的gzip HTML响应 问题:,php,html,node.js,proxy,Php,Html,Node.js,Proxy,当使用节点http代理将请求代理到一个漫游框时,修改Gzip HTML响应会出现一个白色屏幕 到目前为止,我所拥有的: 我拦截请求的方法&修改html(如下所示)对于所有没有gzip的请求都很有效。(我已经使用node、ruby、PHP和apache服务器进行了测试) 令人困惑的部分是: 我有一个启动代理并向其发出请求的测试套件。如果我将响应记录到console.log中,我可以清楚地看到HTML已被修改-只是它根本不会显示在浏览器中 代理设置 有什么想法吗?我已经解决了这个问题 无法上传源代码

当使用节点http代理将请求代理到一个漫游框时,修改Gzip HTML响应会出现一个白色屏幕

到目前为止,我所拥有的: 我拦截请求的方法&修改html(如下所示)对于所有没有gzip的请求都很有效。(我已经使用node、ruby、PHP和apache服务器进行了测试)

令人困惑的部分是: 我有一个启动代理并向其发出请求的测试套件。如果我将响应记录到console.log中,我可以清楚地看到HTML已被修改-只是它根本不会显示在浏览器中

代理设置
有什么想法吗?

我已经解决了这个问题

无法上传源代码,但我可以给你一些提示

  • 使用server.proxy.on(“proxyResponse”)检查内容编码
  • 只需在res.write上收集数据块即可
  • 在res.end上,如果内容编码为“gzip”,则zlib.unzip会收集块,然后是oldswrite.call(res,解码的字符串),oldEnd.apply(res,参数)
  • 如果你想改变什么,趁着
  • 希望这对你有所帮助

    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"];
        }
    });
    
    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();
            });
        });
    });