Comments (8)
解决了,把用例里面的toString改成Buffer拼接就好了
from anyproxy.
感谢反馈!
能贴一下Buffer拼接的实践代码么?或许我们可以整理到sample去,给其他同学一些提示。
from anyproxy.
var appendHtml = "<script><script>"
var ret = Buffer.concat([serverResData, new Buffer(appendHtml)])
我的需求是直接在responseData后面加点东西,我就不用toString字符串拼接了,改成用Buffer拼接就好了。
如果是修改resonseData的某些内容的话,可能要用iconv来转码。
from anyproxy.
感谢friskfly,我这里附上修改responseData的某些内容方式。
npm install iconv-lite
var iconv = require('iconv-lite');
module.exports = {
replaceServerResDataAsync: function(req,res,serverResData,callback){
if(/html/i.test(res.headers['content-type'])){
// 根据 content-type header 信息判断当前的文件编码,默认utf-8。
var charset = 'utf-8',
charsetMatches = res.headers['content-type'].match(/;\s*charset=(.+)/i);
if(charsetMatches){
charset = charsetMatches[1];
}
// 根据文件编码解码,方便修改
var newDataStr = iconv.decode(serverResData,charset);
// 开始修改内容
newDataStr = newDataStr.replace('</body>','<!-- FROM ANYPROXY --></body>');
// 重新编码
newDataStr = iconv.encode(newDataStr, charset);
// 返回内容
callback(newDataStr);
}else{
// 对于其它类型不予处理
callback(serverResData);
}
}
};
from anyproxy.
https://github.com/alibaba/anyproxy/blob/master/rule_sample/rule_replace_response_data.js
我在sample里加了一个链接,指向这个issue,希望能给后来的同学指条路
from anyproxy.
from anyproxy.
:)
from anyproxy.
感觉这个更简单一点
module.exports = {
replaceServerResDataAsync: function(req,res,serverResData,callback){
if(/html/i.test(res.headers['content-type'])){
var newDataStr = serverResData.toString('hex');//将Buffer转换成hex
var substr = new Buffer("</body>").toString('hex');
var hexstr = new Buffer("<!-- FROM ANYPROXY --></body>").toString('hex');
hexstr = newDataStr.replace(substr, hexstr);// 开始修改内容
newDataStr = new Buffer(hexstr, 'hex');// 重新编码
callback(newDataStr);// 返回内容
}else{
callback(serverResData);
}
}
};
from anyproxy.
Related Issues (20)
- 在代理一段时间后,代理失效
- 页面打开报错:Failed to load latest log
- can look respose body HOT 1
- Post request http 2 response not supported
- 开发团队你们好,想请教一下响应数据转换乱码问题
- How do I get the IP address of the request side
- 抓取https请求时 无法获取客户端真实ip地址
- http2 support for anyproxy
- YN0002: │ anyproxy@npm:4.1.3 doesn't provide react (p233ec), requested by svg-inline-react
- 运行 anyproxy --intercept 时报错 throw er; // Unhandled 'error' even
- 未抓取到埋点请求,一个断裂的gif文件 HOT 1
- anyproxy与SwitchyOmega配合怎么获取翻墙的数据包 HOT 1
- iOS 手机连接代理 没有网络! HOT 2
- 怎么开启到一个指定IP
- 请教一下,怎么从源代码启动anyproxy呢
- 请更换安全的证书
- Check the proxy page
- 文档域名过期
- 抓包信息无法查看响应BODY
- Anyproxy 的 requestBody 和 downloadBody 提供了一个错误的数据 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from anyproxy.