From 602f420f18c64081b89b81e15681add34e9f6705 Mon Sep 17 00:00:00 2001 From: hns Date: Tue, 21 Nov 2006 10:42:25 +0000 Subject: [PATCH] * Implement get/setBinaryMode(boolean) to allow delivery of the response body as byte array instead of as string. * Use OutputStreamWriter instead of DataOutputStream for writing the request body. --- helma/Http.js | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/helma/Http.js b/helma/Http.js index 604ad974..f9215c42 100644 --- a/helma/Http.js +++ b/helma/Http.js @@ -8,10 +8,10 @@ * * Copyright 1998-2006 Helma Software. All Rights Reserved. * - * $RCSfile: helma.Http.js,v $ + * $RCSfile: Http.js,v $ * $Author: czv $ - * $Revision: 1.11 $ - * $Date: 2006/04/18 13:06:58 $ + * $Revision: 1.2 $ + * $Date: 2006/04/24 07:02:17 $ */ @@ -26,6 +26,7 @@ helma.Http = function() { var method = "GET"; var credentials = null; var followRedirects = true; + var binaryMode = false; var headers = {}; var timeout = { "connect": 0, @@ -160,10 +161,20 @@ helma.Http = function() { this.setUserAgent = function(agent) { userAgent = agent; return true; - } + }; this.getUserAgent = function() { return userAgent; - } + }; + + /** + * switches content text encoding on/off + */ + this.setBinaryMode = function(mode) { + binaryMode = mode; + }; + this.getBinaryMode = function() { + return binaryMode; + }; /** * executes a http request @@ -204,8 +215,8 @@ helma.Http = function() { if (content) { conn.setRequestProperty("Content-Length", content.length); conn.setDoOutput(true); - var out = new java.io.DataOutputStream(conn.getOutputStream()); - out.writeBytes(new java.lang.String(content)); + var out = new java.io.OutputStreamWriter(conn.getOutputStream()); + out.write(content); out.flush(); out.close(); } @@ -236,7 +247,13 @@ helma.Http = function() { body.write(buf, 0, str); } input.close(); - result.content = result.encoding ? body.toString(result.encoding) : body.toString(); + if (binaryMode) { + result.content = body.toByteArray(); + } else { + result.content = result.encoding ? + body.toString(result.encoding) : + body.toString(); + } result.length = result.content.length; } conn.disconnect();