2013-01-29 13:47:40 +01:00
|
|
|
angular
|
|
|
|
.module('webui.services.rpc.jsoncall', [
|
|
|
|
'webui.services.deps', 'webui.services.base64'])
|
|
|
|
.factory('$jsoncall', ['$', '$json', '$base64', function($, JSON, base64) {
|
2013-01-15 09:24:09 +01:00
|
|
|
return {
|
|
|
|
init: function(conf) {
|
|
|
|
this.avgTimeout = 2000;
|
|
|
|
this.serverConf = conf;
|
|
|
|
},
|
|
|
|
ariaRequest: function(url, funcName, params, success, error) {
|
|
|
|
var startTime = new Date();
|
|
|
|
var conn = this;
|
2017-08-04 14:50:34 +02:00
|
|
|
$.post({
|
2013-01-15 09:24:09 +01:00
|
|
|
url: url,
|
|
|
|
timeout: this.avgTimeout,
|
2017-08-04 14:50:34 +02:00
|
|
|
contentType: 'application/json',
|
|
|
|
data: JSON.stringify({
|
2013-01-15 09:24:09 +01:00
|
|
|
jsonrpc: 2.0,
|
|
|
|
id: 'webui',
|
|
|
|
method: funcName,
|
2017-08-04 14:50:34 +02:00
|
|
|
params: params
|
|
|
|
}),
|
2013-01-15 09:24:09 +01:00
|
|
|
success: function(data) {
|
2013-01-26 17:54:17 +01:00
|
|
|
conn.avgTimeout = 2000 + 3 * (new Date() - startTime);
|
2014-05-17 17:41:00 +02:00
|
|
|
return success(data);
|
2013-01-15 09:24:09 +01:00
|
|
|
},
|
2017-08-04 14:50:34 +02:00
|
|
|
error: error
|
2013-01-15 09:24:09 +01:00
|
|
|
});
|
|
|
|
},
|
|
|
|
invoke: function(opts) {
|
|
|
|
var rpc = this;
|
2013-03-09 06:35:53 +01:00
|
|
|
var scheme = rpc.serverConf.encrypt ? 'https' : 'http';
|
2013-01-15 09:24:09 +01:00
|
|
|
rpc.ariaRequest(
|
2015-09-12 10:55:02 +02:00
|
|
|
scheme + '://' + rpc.serverConf.host + ':' + rpc.serverConf.port + (rpc.serverConf.path || '/jsonrpc'),
|
2013-01-15 09:24:09 +01:00
|
|
|
opts.name,
|
|
|
|
opts.params,
|
|
|
|
opts.success,
|
|
|
|
function() {
|
|
|
|
// check if authentication details are given, if yes then use a hack to support
|
|
|
|
// http authentication otherwise emit error
|
2014-05-17 17:41:00 +02:00
|
|
|
if (!rpc.serverConf.auth || !rpc.serverConf.auth.user) {
|
2013-01-17 07:51:03 +01:00
|
|
|
console.log("jsonrpc disconnect!!!");
|
2013-01-15 09:24:09 +01:00
|
|
|
return opts.error();
|
|
|
|
}
|
|
|
|
|
|
|
|
var authUrl = scheme + '://' +
|
|
|
|
rpc.serverConf.auth.user + ":" +
|
|
|
|
rpc.serverConf.auth.pass + "@" +
|
|
|
|
rpc.serverConf.host + ':' +
|
2015-09-12 10:55:02 +02:00
|
|
|
rpc.serverConf.port + (rpc.serverConf.path || '/jsonrpc');
|
2013-01-15 09:24:09 +01:00
|
|
|
|
|
|
|
// hack is to basically inject an image with same uri as the aria2 rpc url,
|
|
|
|
// most browsers will then cache the authentication details and we dont have
|
|
|
|
// to give them next time we make a request
|
|
|
|
var img = $('<img/>').attr("src", authUrl);
|
|
|
|
$('body').append(img);
|
|
|
|
img.remove();
|
|
|
|
|
|
|
|
// timeout to let the image load and then make a request,
|
|
|
|
setTimeout(function() {
|
|
|
|
rpc.ariaRequest(
|
|
|
|
authUrl,
|
2013-03-11 17:01:58 +01:00
|
|
|
opts.name,
|
2013-01-15 09:24:09 +01:00
|
|
|
opts.params,
|
|
|
|
opts.success,
|
2013-03-09 06:35:53 +01:00
|
|
|
function() {
|
|
|
|
console.log("jsonrpc disconnect!!!");
|
|
|
|
return opts.error();
|
|
|
|
}
|
2013-01-15 09:24:09 +01:00
|
|
|
);
|
|
|
|
}, rpc.avgTimeout);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}]);
|