[英]Modify Gzipped HTML response from node-http-proxy
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响应会显示白屏。
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服务器进行了测试)。
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已被修改-只是根本不会在浏览器中显示...
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();
});
});
});
Any ideas? 有任何想法吗?
I have solved this problem. 我已经解决了这个问题。
Can't upload the source, but I can give you some hints on this. 无法上传源,但是我可以给您一些提示。
Hope this help you out. 希望这对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.