r9702 - helma-ng/trunk/modules/helma

[email protected] Mon, 4 May 2009 13:52:23 +0200 (CEST)
Newsgroups gmane.comp.java.helma.cvs
Message-ID <20090504115223.A0FF53D0D6@mia>
Author: hannes
Date: 2009-05-04 13:52:23 +0200 (Mon, 04 May 2009)
New Revision: 9702

Modified:
   helma-ng/trunk/modules/helma/httpserver.js
Log:
Use toBinary() in commitResponse() to actually allow binary responses.

Details at http://dev.helma.org/trac/helma/changeset/9702

Modified: helma-ng/trunk/modules/helma/httpserver.js
===================================================================
--- helma-ng/trunk/modules/helma/httpserver.js	2009-05-04 11:52:22 UTC (rev 9701)
+++ helma-ng/trunk/modules/helma/httpserver.js	2009-05-04 11:52:23 UTC (rev 9702)
@@ -4,6 +4,7 @@
 
 include('helma/webapp/util');
 var IO = require('io').IO;
+var Binary = require("binary").Binary;
 var HashP = require('hashp').HashP;
 
 export('start', 'stop', 'initRequest', 'commitResponse');
@@ -148,24 +149,31 @@
             return; // TODO generate error page?
         }
     }
-	var [status, headers, body] = result;
-	response.status = status;
-	for (var name in headers) {
-		response.setHeader(name, headers[name]);
-	}
+    var [status, headers, body] = result;
+    response.status = status;
+    for (var name in headers) {
+        response.setHeader(name, headers[name]);
+    }
     var charset = getSubHeader(HashP.get(headers, "content-type"), "charset");
-    if (charset) {
-        response.setCharacterEncoding(charset);
+    var output = response.getOutputStream();
+    if (body && typeof body.forEach == "function") {
+        /* var contentLength = 0;
+        var bytes = [];
+        body.forEach(function(part) {
+            var bin = part.toBinary(charset);
+            contentLength += bin.getLength();
+            bytes.push(bin.bytes);
+        })
+        response.setContentLength(contentLength);
+        for (var i = 0; i < bytes.length; i++) {
+            output.write(bytes[i]);
+        } */
+        body.forEach(function(part) {
+            output.write(part.toBinary(charset).bytes);
+        });
+    } else {
+        throw new Error("Unsupported response body: " + body);
     }
-	var writer = response.getWriter();
-	if (body && typeof body.forEach == "function") {
-		body.forEach(function(chunk) {
-			writer.write(String(chunk));
-			writer.flush();
-		})
-	} else {
-		writer.write(String(body));
-	}
-    writer.close();
+    output.close();
 }