diff --git a/helma/Http.js b/helma/Http.js index 7aaeebe6..a9c052f7 100644 --- a/helma/Http.js +++ b/helma/Http.js @@ -9,9 +9,9 @@ * Copyright 1998-2006 Helma Software. All Rights Reserved. * * $RCSfile: Http.js,v $ - * $Author: robert $ - * $Revision: 1.5 $ - * $Date: 2007/04/23 12:10:07 $ + * $Author: michi $ + * $Revision: 1.6 $ + * $Date: 2007/05/03 11:09:05 $ */ @@ -39,6 +39,7 @@ if (!global.helma) { * @constructor */ helma.Http = function() { + var self = this; var proxy = null; var content = ""; var userAgent = "Helma Http Client"; @@ -52,6 +53,31 @@ helma.Http = function() { "connect": 0, "socket": 0 }; + var maxResponseSize = null; + + var responseHandler = function(connection, result) { + var input = new java.io.BufferedInputStream(connection.getInputStream()); + var body = new java.io.ByteArrayOutputStream(); + var buf = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, 1024); + var len; + var currentSize = 0; + var maxResponseSize = self.getMaxResponseSize(); + while ((len = input.read(buf)) > -1) { + body.write(buf, 0, len); + currentSize += len; + if (maxResponseSize && currentSize > maxResponseSize) { + throw new Error("Maximum allowed response size is exceeded"); + } + } + input.close(); + if (binaryMode) { + result.content = body.toByteArray(); + } else { + result.content = result.charset ? + body.toString(result.charset) : + body.toString(); + } + }; /** @private */ var setTimeout = function(type, value) { @@ -351,6 +377,36 @@ helma.Http = function() { return binaryMode; }; + /** + * Sets the max allowed size for the response stream + * @param {Integer} Size in Byte + */ + this.setMaxResponseSize = function(size) { + maxResponseSize = size; + return; + }; + + /** + * Returns the currently set max response size + * @returns The max responsesize + * @type Integer + * @see #setMaxResponseSize + */ + this.getMaxResponseSize = function() { + return maxResponseSize; + }; + + /** + * Overloads the default response handler + * Use this do implement your own response handling, like storing the response directly to the harddisk + * The handler function gets two parameter, first is the Connection and second is the result object + * @param {function} Response handler function + */ + this.setResponseHandler = function(callback) { + responseHandler = callback; + return; + }; + /** * Executes a http request * @param {String} url The url to request @@ -365,11 +421,13 @@ helma.Http = function() { *
message
: (String) An optional HTTP response messagelength
: (Number) The content length of the responsetype
: (String) The mimetype of the responsecharset
: (String) The character set of the responseencoding
: (String) An optional encoding to use with the responselastModified
: (String) The value of the lastModified response header fieldeTag
: (String) The eTag as received from the remote servercookie
: (helma.Http.Cookie) An object containing the cookie parameters, if the remote
server has set the "Set-Cookie" header fieldheaders
: (java.util.Map) A map object containing the headers, access them using get("headername")
* content
: (String|ByteArray) The response received from the server. Can be either
a string or a byte array (see #setBinaryMode)