From 62989a205e6e16adf1cca5b1ab0329e3bc8c91ba Mon Sep 17 00:00:00 2001 From: Kris Zyp Date: Mon, 11 Jan 2010 21:47:40 -0700 Subject: [PATCH] Fixes to reading input --- lib/jsgi-node.js | 45 +++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/lib/jsgi-node.js b/lib/jsgi-node.js index df29f63..1175489 100644 --- a/lib/jsgi-node.js +++ b/lib/jsgi-node.js @@ -91,27 +91,28 @@ function Input( request ) { }); this.read = function( length ) { - if ( !length ) { - if ( !requestCompletePromise._hasFired ) { - requestCompletePromise.wait(); - } - } - - else if ( length > inputLength ) { - waitingForLength = length; - inputPromise = new process.Promise(); - inputPromise.wait(); - waitingForLength = Infinity; - } - - var chunk = inputBuffer.join(""); - var keepInBuffer = length ? chunk.substring( length ) : ""; - - inputBuffer = [ keepInBuffer ]; - inputLength = keepInBuffer.length; - return { - decodeToString: function() { + decodeToString: function( encoding ) { + request.setBodyEncoding( encoding ); + if ( !length ) { + if ( !requestCompletePromise.hasFired ) { + requestCompletePromise.wait(); + } + } + + else if ( length > inputLength ) { + waitingForLength = length; + inputPromise = new process.Promise(); + inputPromise.wait(); + waitingForLength = Infinity; + } + + var chunk = inputBuffer.join(""); + var keepInBuffer = length ? chunk.substring( length ) : ""; + + inputBuffer = [ keepInBuffer ]; + inputLength = keepInBuffer.length; + return length ? chunk.substring( 0, length ) : chunk; } }; @@ -149,8 +150,8 @@ function Response( response ) { throw new Error("The body does not have a forEach function"); } - forEachResult = data.body.forEach( function( chunk ) { - response.sendBody( chunk ); + forEachResult = data.body.forEach( function( chunk, encoding ) { + response.sendBody( chunk, encoding ); }); if ( !notDone && forEachResult && ( typeof forEachResult.then === "function" ) ) {