Skip to content

Commit

Permalink
Merge pull request ringo#233 from pykl/httptimeout
Browse files Browse the repository at this point in the history
Connection timeout for HttpClient
  • Loading branch information
oberhamsi committed Jun 29, 2013
2 parents 6235144 + 629a926 commit ac16d15
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
26 changes: 24 additions & 2 deletions modules/ringo/httpclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ var prepareOptions = function(options) {
"username": undefined,
"password": undefined,
"followRedirects": true,
"readTimeout": 0,
"connectTimeout": 0,
"binary": false
};
var opts = options ? objects.merge(options, defaultValues) : defaultValues;
Expand Down Expand Up @@ -283,6 +285,7 @@ var Exchange = function(url, options, callbacks) {
var responseContent;
var responseContentBytes;
var isDone = false;
var isException;

Object.defineProperties(this, {
/**
Expand Down Expand Up @@ -335,6 +338,8 @@ var Exchange = function(url, options, callbacks) {
}
connection = (new URL(url)).openConnection();
connection.setAllowUserInteraction(false);
connection.setConnectTimeout(options.connectTimeout);
connection.setReadTimeout(options.readTimeout);
connection.setFollowRedirects(options.followRedirects);
connection.setRequestMethod(options.method);
connection.setRequestProperty("User-Agent", "RingoJS HttpClient " + VERSION);
Expand Down Expand Up @@ -377,6 +382,11 @@ var Exchange = function(url, options, callbacks) {
var content = (options.binary === true) ? this.contentBytes : this.content;
callbacks.success(content, this.status, this.contentType, this);
}
} catch (e if e.javaException instanceof java.net.SocketTimeoutException) {
isException = 'timeout';
if (typeof(callbacks.error) === "function") {
callbacks.error("timeout", 500, this);
}
} catch (e) {
if (typeof(callbacks.error) === "function") {
callbacks.error(this.message, this.status, this);
Expand All @@ -385,8 +395,12 @@ var Exchange = function(url, options, callbacks) {
isDone = true;
try {
if (typeof(callbacks.complete) === "function") {
var content = (options.binary === true) ? this.contentBytes : this.content;
callbacks.complete(content, this.status, this.contentType, this);
if (isException) {
callbacks.complete(isException, 500, undefined, this);
} else {
var content = (options.binary === true) ? this.contentBytes : this.content;
callbacks.complete(content, this.status, this.contentType, this);
}
}
} finally {
connection && connection.disconnect();
Expand Down Expand Up @@ -505,6 +519,12 @@ Object.defineProperties(Exchange.prototype, {
* else it will be decoded to string
* - `followRedirects`: whether HTTP redirects (response code 3xx) should be
* automatically followed; default: true
* - `readTimeout`: setting for read timeout in millis. 0 return implies that the option
* is disabled (i.e., timeout of infinity); default: 0 (or until impl decides its time)
* - `connectTimeout`: Sets a specified timeout value, in milliseconds, to be used
* when opening a communications link to the resource referenced by this
* URLConnection. A timeout of zero is interpreted as an infinite timeout.;
* default: 0 (or until impl decides its time)
*
* #### Callbacks
*
Expand Down Expand Up @@ -544,6 +564,8 @@ var request = function(options) {
"password": opts.password,
"contentType": opts.contentType,
"followRedirects": opts.followRedirects,
"connectTimeout": opts.connectTimeout,
"readTimeout": opts.readTimeout,
"binary": opts.binary
}, {
"beforeSend": opts.beforeSend,
Expand Down
26 changes: 26 additions & 0 deletions test/ringo/httpclient_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,32 @@ exports.testBasic = function() {
assert.strictEqual(myData, text);
};

exports.testConnectTimeout = function() {
var text = "<h1>This is the Response Text</h1>";

getResponse = function(req) {
return response.html(text);
};

var errorCalled, myData;
var exchange = request({
url: 'http://nosuchurl.visualxs.com',
connectTimeout: 5000,
success: function(data, status, contentType, exchange) {
assert.fail( 'Should of timed out before connection' );
},
error: function(message, status, exchange) {
assert.equal( message, 'timeout' );
assert.equal( status, 500 );
},
complete: function(data, status, contentType, exchange) {
assert.equal( data, 'timeout' );
assert.equal( status, 500 );
assert.isUndefined( contentType );
}
});
};

/**
* test user info in url
*/
Expand Down

0 comments on commit ac16d15

Please sign in to comment.