From f987ecf45be6f5c7c18d5cebd38be0b411648140 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Geisend=C3=B6rfer?= Date: Mon, 17 May 2010 22:42:24 -0400 Subject: [PATCH] Use Utf8Stream for http Streams with utf8 encoding --- lib/http.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/http.js b/lib/http.js index 352a05ed3e..0030ddcc0b 100644 --- a/lib/http.js +++ b/lib/http.js @@ -9,6 +9,7 @@ function debug (x) { var sys = require('sys'); var net = require('net'); +var Utf8Stream = require('utf8_stream').Utf8Stream; var events = require('events'); var Buffer = require('buffer').Buffer; @@ -91,6 +92,8 @@ var parsers = new FreeList('parsers', 1000, function () { var enc = parser.incoming._encoding; if (!enc) { parser.incoming.emit('data', b.slice(start, start+len)); + } else if (this._decoder) { + this._decoder.write(pool.slice(start, end)); } else { var string = b.toString(enc, start, start+len); parser.incoming.emit('data', string); @@ -198,6 +201,15 @@ IncomingMessage.prototype.setBodyEncoding = function (enc) { IncomingMessage.prototype.setEncoding = function (enc) { // TODO check values, error out on bad, and deprecation message? this._encoding = enc.toLowerCase(); + if (this._encoding == 'utf-8' || this._encoding == 'utf8') { + this._decoder = new Utf8Stream(); + this._decoder.onString = function(str) { + this.emit('data', str); + }; + } else if (this._decoder) { + delete this._decoder; + } + }; IncomingMessage.prototype.pause = function () {