* Make helma.Http always set result.content, regardless of response status code.
* Always call responseHandler, regardless of response status code. * Fix setFollowRedirects() bug. Fix for bug 583 http://helma.org/bugs/show_bug.cgi?id=583
This commit is contained in:
parent
f3bfee3a16
commit
6995b9b6a1
1 changed files with 55 additions and 40 deletions
|
@ -59,12 +59,17 @@ helma.Http = function() {
|
||||||
var maxResponseSize = null;
|
var maxResponseSize = null;
|
||||||
|
|
||||||
var responseHandler = function(connection, result) {
|
var responseHandler = function(connection, result) {
|
||||||
var input = new java.io.BufferedInputStream(connection.getInputStream());
|
var input;
|
||||||
|
try {
|
||||||
|
input = new java.io.BufferedInputStream(connection.getInputStream());
|
||||||
|
} catch (error) {
|
||||||
|
input = new java.io.BufferedInputStream(connection.getErrorStream());
|
||||||
|
}
|
||||||
|
if (input) {
|
||||||
var body = new java.io.ByteArrayOutputStream();
|
var body = new java.io.ByteArrayOutputStream();
|
||||||
var buf = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, 1024);
|
var buf = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, 1024);
|
||||||
var len;
|
var len;
|
||||||
var currentSize = 0;
|
var currentSize = 0;
|
||||||
var maxResponseSize = self.getMaxResponseSize();
|
|
||||||
while ((len = input.read(buf)) > -1) {
|
while ((len = input.read(buf)) > -1) {
|
||||||
body.write(buf, 0, len);
|
body.write(buf, 0, len);
|
||||||
currentSize += len;
|
currentSize += len;
|
||||||
|
@ -72,14 +77,24 @@ helma.Http = function() {
|
||||||
throw new Error("Maximum allowed response size is exceeded");
|
throw new Error("Maximum allowed response size is exceeded");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
input.close();
|
input.close();
|
||||||
if (binaryMode) {
|
} catch (error) {
|
||||||
|
// safe to ignore
|
||||||
|
}
|
||||||
|
if (binaryMode && (result.code >= 200 && result.code < 300)) {
|
||||||
|
// only honor binaryMode if the request succeeded
|
||||||
result.content = body.toByteArray();
|
result.content = body.toByteArray();
|
||||||
} else {
|
} else {
|
||||||
result.content = result.charset ?
|
result.content = result.charset ?
|
||||||
body.toString(result.charset) :
|
body.toString(result.charset) :
|
||||||
body.toString();
|
body.toString();
|
||||||
}
|
}
|
||||||
|
// adjust content length
|
||||||
|
if (result.content) {
|
||||||
|
result.length = result.content.length;
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/** @private */
|
/** @private */
|
||||||
|
@ -413,9 +428,13 @@ helma.Http = function() {
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Overloads the default response handler
|
* Overloads the default response handler.
|
||||||
* Use this do implement your own response handling, like storing the response directly to the harddisk
|
* 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
|
* The handler function gets two parameter, first is the java.net.URLConnection and second is the result object.
|
||||||
|
* Note that custom response handler functions should check the HTTP status code before reading
|
||||||
|
* the response. The status code for successful requests is 200. Response bodies for requests with
|
||||||
|
* status codes less than 400 can be read from the connection's input stream, while response bodies
|
||||||
|
* with 4xx or 5xx status codes must be read using the error stream.
|
||||||
* @param {function} Response handler function
|
* @param {function} Response handler function
|
||||||
*/
|
*/
|
||||||
this.setResponseHandler = function(callback) {
|
this.setResponseHandler = function(callback) {
|
||||||
|
@ -423,6 +442,14 @@ helma.Http = function() {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the response handler. This is the function used to read the HTTP response body.
|
||||||
|
* @returns The response handler function
|
||||||
|
*/
|
||||||
|
this.getResponseHandler = function() {
|
||||||
|
return responseHandler;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Executes a http request
|
* Executes a http request
|
||||||
* @param {String} url The url to request
|
* @param {String} url The url to request
|
||||||
|
@ -457,7 +484,11 @@ helma.Http = function() {
|
||||||
}
|
}
|
||||||
|
|
||||||
var conn = proxy ? url.openConnection(proxy) : url.openConnection();
|
var conn = proxy ? url.openConnection(proxy) : url.openConnection();
|
||||||
conn.setFollowRedirects(followRedirects);
|
// Note: we must call setInstanceFollowRedirects() instead of
|
||||||
|
// static method setFollowRedirects(), as the latter will
|
||||||
|
// set the default value for all url connections, and will not work for
|
||||||
|
// url connections that have already been created.
|
||||||
|
conn.setInstanceFollowRedirects(followRedirects);
|
||||||
conn.setAllowUserInteraction(false);
|
conn.setAllowUserInteraction(false);
|
||||||
conn.setRequestMethod(method);
|
conn.setRequestMethod(method);
|
||||||
conn.setRequestProperty("User-Agent", userAgent);
|
conn.setRequestProperty("User-Agent", userAgent);
|
||||||
|
@ -549,26 +580,10 @@ helma.Http = function() {
|
||||||
charset = charset.replace('"', ' ').trim();
|
charset = charset.replace('"', ' ').trim();
|
||||||
result.charset = charset;
|
result.charset = charset;
|
||||||
}
|
}
|
||||||
if (result.length != 0 && result.code == 200) {
|
|
||||||
|
// invoke response handler
|
||||||
responseHandler(conn, result);
|
responseHandler(conn, result);
|
||||||
if (result.content) {
|
|
||||||
result.length = result.content.length;
|
|
||||||
}
|
|
||||||
result.length = result.content.length;
|
|
||||||
} else {
|
|
||||||
var errorStream = conn.getErrorStream();
|
|
||||||
if (errorStream) {
|
|
||||||
var body = new java.io.ByteArrayOutputStream();
|
|
||||||
var input = new java.io.BufferedInputStream(errorStream);
|
|
||||||
var buf = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, 1024);
|
|
||||||
var str;
|
|
||||||
while ((str = input.read(buf)) > -1) {
|
|
||||||
body.write(buf, 0, str);
|
|
||||||
}
|
|
||||||
input.close();
|
|
||||||
result.error = body.toString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
conn.disconnect();
|
conn.disconnect();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue