0

I have the following Node.js code:

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

var reqHash={};
var resHash={};

var server = net.createServer( function(soc){

  soc.on('data', function(data){
    //sys.puts(data);
    var requestData=data;

    var conn = net.Socket();
    conn.connect(80,"www.xlhi.com",function(){
       conn.write(data);
    });
    conn.on("connect",function(){
       //sys.puts("connected");
    });
    conn.on("data",function(x){
      var responseData=x.toString();
      //sys.puts(responseData);
      var f=50;
      var toTransmit="";

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

          var hash="";
          var chunk="";
          for(var i=0;i<p;i++){
            chunk=responseData.substr(f*i,f);
            hash=DJBHash(chunk);
            toTransmit+=chunk;
            if(hash<0){
              hash=hash*-1;
            }
            if(resHash[hash]!=undefined){
              //sys.puts("***Hit"+resHash[hash]);
              //toTransmit=toTransmit+"***EOH"+hash+"EOH***";
            }else{
              resHash[hash]=chunk;
              //toTransmit+=chunk;
            }
          }
          //remainder:
          //toTransmit+="***";
          //sys.puts(f*p+" "+p_rem);
          chunk=responseData.substr(f*p);
          //sys.puts("EOH"+chunk+"\n");
          hash=DJBHash(chunk);
          toTransmit+=chunk;
        }else{
           toTransmit=responseData;
        }
        //sys.puts(x.toString().substr(0,50));
        //sys.puts(toTransmit.substr(0,50));
        sys.puts(toTransmit);
        sys.puts(x);
        soc.write(toTransmit);   /*This line causes content encoding error!*/
      }
    });
  });
});

server.on('error', function (err){
  // Error processing i just pass whole object
  console.log(err);
});

server.listen(8080,"172.16.1.218");
console.log('Server is listening %d\n', 8080);


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

Specifically, I'm having trouble with the line marked above. When I change soc.write(toTransmit); to soc.write(x), everything works fine and I don't get any content encoding errors when I hit refresh in my browser.

As you can see, toTransmit is a chunked version of responseData=x.toString();. I'm guessing I need some way of converting toTransmit into a format suitable for writing to the socket. I don't know how to do this.

Any help greatly appreciated.

Many thanks in advance,

3
  • What version of node are you using? Commented Aug 17, 2011 at 9:26
  • Many thanks for the reply. I'm using v0.4.10 Commented Aug 17, 2011 at 9:29
  • I've figured it out now... soc.write(new Buffer(toTransmit)) Commented Aug 17, 2011 at 9:34

1 Answer 1

1

According to the documentation : http://nodejs.org/docs/v0.4.10/api/http.html#response.write

response.write takes string or buffer. If it is a string, it also takes an encoding, the default encoding is utf-8.

So the encoding problems comes from the fact that you give him a string in UTF-8 and your browser is set in to another encoding.

In this code you also don't write any header, the browser must guess if it is text/plain or text/html you are sending him.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.