Skip to content

Commit

Permalink
added encode=UTF-8 in jsonp-polling.js and xhr-polling.js since UTF-8…
Browse files Browse the repository at this point in the history
… is the default encoding for http.ServerResponse.write

replaced string.length with Buffer.byteLength in jsonp-polling.js, listener.js and xhr-polling.js because content-length header requires number of bytes and not the number of symbols in string
  • Loading branch information
SlNPacifist committed Sep 28, 2010
1 parent c1e0831 commit d2ead47
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/socket.io/listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ Listener.prototype._serveClient = function(path, req, res){
}
self._clientFiles[path] = {
headers: {
'Content-Length': data.length,
'Content-Length': Buffer.byteLength(data),

This comment has been minimized.

Copy link
@jmoyers

jmoyers Sep 29, 2010

New to node/socket.io -- just cloned and installed both. Looks to me like fs.readFile returns a buffer unless you specify encoding? Buffer.byteLength expects a string, not a buffer. Similarly when you serve the file in write, thats expecting a string. I was getting "First argument expected to be string" on running the server and connecting a client until i changed this, then all was well. Feel free to delete this comment if I'm missing something.

This comment has been minimized.

Copy link
@SlNPacifist

SlNPacifist Sep 29, 2010

Author Contributor

This is write, data is of type Buffer here and we get an exception. But there's another problem because data.length is not actual number of bytes in data, this is amount of bytes allocated by buffer (see http://nodejs.org/api.html, buffer.length) and can vary due to the implementation. I am now looking for the way to get real amount of bytes in data because making data a string with some encoding and then counting it's byteLength seems to be rather unreliable.
returned to data.length in http://github.com/SlNPacifist/Socket.IO-node/commit/1907b41637ff232c78e7ce650f3b764fb631ffae

'Content-Type': types[ext],
'ETag': clientVersion
},
Expand Down
2 changes: 1 addition & 1 deletion lib/socket.io/transports/jsonp-polling.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ JSONPPolling.prototype._write = function(message){
} else {
message = "io.JSONP["+ this._index +"]._("+ JSON.stringify(message) +");";
}
this.response.writeHead(200, {'Content-Type': 'text/javascript', 'Content-Length': message.length});
this.response.writeHead(200, {'Content-Type': 'text/javascript; charset=UTF-8', 'Content-Length': Buffer.byteLength(message)});
this.response.write(message);
this.response.end();
this._onClose();
Expand Down
2 changes: 1 addition & 1 deletion lib/socket.io/transports/xhr-polling.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Polling.prototype._onConnect = function(req, res){

Polling.prototype._write = function(message){
if (this._closeTimeout) clearTimeout(this._closeTimeout);
var headers = {'Content-Type': 'text/plain', 'Content-Length': message.length};
var headers = {'Content-Type': 'text/plain; charset=UTF-8', 'Content-Length': Buffer.byteLength(message)};
// https://developer.mozilla.org/En/HTTP_Access_Control
if (this.request.headers.origin && this._verifyOrigin(this.request.headers.origin)){
headers['Access-Control-Allow-Origin'] = this.request.headers.origin;
Expand Down

0 comments on commit d2ead47

Please sign in to comment.