Nodejs:将字符串转换为缓冲区 [英] Nodejs: convert string to buffer

查看:133
本文介绍了Nodejs:将字符串转换为缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个字符串写入一个套接字(socket称为response)。这是我有sofar的代码(我试图实现一个字节缓存代理...):

I'm trying to write a string to a socket (socket is called "response"). Here is the code I have sofar (I'm trying to implement a byte caching proxy...):

var http = require('http');
var sys=require('sys');

var localHash={};

http.createServer(function(request, response) {
    var proxy = http.createClient(80, request.headers['host'])
    var proxy_request = proxy.request(request.method, request.url, request.headers);
    proxy_request.addListener('response', function (proxy_response) {
    proxy_response.addListener('data', function(x) {
        var responseData=x.toString();
        var f=50;
        var toTransmit="";
        var p=0;        

        var N=responseData.length;
        if(N>f){
            p=Math.floor(N/f);

            var hash="";
            var chunk="";
            for(var i=0;i<p;i++){
                chunk=responseData.substr(f*i,f);
                hash=DJBHash(chunk);
                if(localHash[hash]==undefined){
                    localHash[hash]=chunk;
                    toTransmit=toTransmit+chunk;
                }else{
                    sys.puts("***hit"+chunk);
                    toTransmit=toTransmit+chunk;//"***EOH"+hash;
                }
            }
            //remainder:
            chunk=responseData.substr(f*p);
            hash=DJBHash(chunk);
            if(localHash[hash]==undefined){
                localHash[hash]=chunk;
                toTransmit=toTransmit+chunk;
            }else{
                toTransmit=toTransmit+chunk;//"***EOH"+hash;
            }
        }else{
            toTransmit=responseData;
        }
        response.write(new Buffer(toTransmit));   /*error occurs here */
    });
    proxy_response.addListener('end', function() {
        response.end();
    });
    response.writeHead(proxy_response.statusCode, proxy_response.headers);
    });
    request.addListener('data', function(chunk) {
        sys.puts(chunk);
        proxy_request.write(chunk, 'binary');
    });
    request.addListener('end', function() {
        proxy_request.end();
    });
}).listen(8080);



function DJBHash(str) {
    var hash = 5381;
    for(var i = 0; i < str.length; i++) {
        hash = (((hash << 5) + hash) + str.charCodeAt(i)) & 0xffffffff;
    }
    if(hash<-1){
        hash=hash*-1;
    }
    return hash;
}

麻烦的是,我在Firefox中收到内容编码错误。就好像这个gizipped的内容没有正确传输。我确保通过console.log(x)和console.log(toTransmit)toTransmit与x相同。

The trouble is, I keep getting a "content encoding error" in Firefox. It's as if the gizipped content isn't being transmitted properly. I've ensured that "toTransmit" is the same as "x" via console.log(x) and console.log(toTransmit).

值得注意的是,如果代替 response.write(new Buffer(toTransmit))只需 response.write(x)如预期的那样,但是我需要做一些有效负载分析,然后通过toTransmit,而不是x。

It's worth noting that if I replace response.write(new Buffer(toTransmit)) with simply response.write(x), the proxy works as expected, but I need to do some payload analysis and then pass "toTransmit", not "x".

我还试图 response.write(toTransmit)(即没有转换到缓冲区),我不断得到相同的内容编码错误。

I've also tried to response.write(toTransmit) (i.e. without the conversion to buffer) and I keep getting the same content encoding error.

我真的卡住。我以为我通过将字符串转换为缓冲区按照另一个线程(http://stackoverflow.com/questions/7090510/nodejs-content-encoding-error)修复了这个问题,但是我已经重新打开一个新的线程讨论我遇到的这个新问题。

I'm really stuck. I thought I had this problem fixed by converting the string to a buffer as per another thread (http://stackoverflow.com/questions/7090510/nodejs-content-encoding-error), but I've re-opened a new thread to discuss this new problem I'm experiencing.

我应该补充说,如果我通过Opera中的代理打开一个页面,我得到gobblydeegook - 就好像gzipped的数据

I should add that if I open a page via the proxy in Opera, I get gobblydeegook - it's as if the gzipped data gets corrupted.

任何见解非常感激。

非常感谢提前,

推荐答案

没有深入挖掘你的代码,在我看来,你可能想改变

Without digging very deep into your code, it seems to me that you might want to change

var responseData=x.toString();

var responseData=x.toString("binary");

最后

response.write(new Buffer(toTransmit, "binary"));

这篇关于Nodejs:将字符串转换为缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆