Coder Social home page Coder Social logo

Comments (8)

friskfly avatar friskfly commented on May 3, 2024

解决了,把用例里面的toString改成Buffer拼接就好了

from anyproxy.

ottomao avatar ottomao commented on May 3, 2024

感谢反馈!
能贴一下Buffer拼接的实践代码么?或许我们可以整理到sample去,给其他同学一些提示。

from anyproxy.

friskfly avatar friskfly commented on May 3, 2024
var appendHtml = "<script><script>"
var ret = Buffer.concat([serverResData, new Buffer(appendHtml)])

我的需求是直接在responseData后面加点东西,我就不用toString字符串拼接了,改成用Buffer拼接就好了。
如果是修改resonseData的某些内容的话,可能要用iconv来转码。

from anyproxy.

xunuo avatar xunuo commented on May 3, 2024

感谢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.

ottomao avatar ottomao commented on May 3, 2024

https://github.com/alibaba/anyproxy/blob/master/rule_sample/rule_replace_response_data.js

我在sample里加了一个链接,指向这个issue,希望能给后来的同学指条路

from anyproxy.

wssgcg1213 avatar wssgcg1213 commented on May 3, 2024

踩坑了, 感谢 @xunuo @ottomao

from anyproxy.

xunuo avatar xunuo commented on May 3, 2024

:)

from anyproxy.

wznpp1 avatar wznpp1 commented on May 3, 2024

感觉这个更简单一点

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.