luci-base: rpc.js: fix list requests, handle aborted http requests

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
This commit is contained in:
Jo-Philipp Wich 2019-09-12 14:02:06 +02:00
parent cc81d5a0d4
commit 473bd2741b

View file

@ -14,35 +14,29 @@ return L.Class.extend({
return Promise.resolve([]); return Promise.resolve([]);
for (var i = 0; i < req.length; i++) for (var i = 0; i < req.length; i++)
if (req[i].params)
q += '%s%s.%s'.format( q += '%s%s.%s'.format(
q ? ';' : '/', q ? ';' : '/',
req[i].params[1], req[i].params[1],
req[i].params[2] req[i].params[2]
); );
} }
else { else if (req.params) {
q += '/%s.%s'.format(req.params[1], req.params[2]); q += '/%s.%s'.format(req.params[1], req.params[2]);
} }
return L.Request.post(rpcBaseURL + q, req, { return L.Request.post(rpcBaseURL + q, req, {
timeout: (L.env.rpctimeout || 5) * 1000, timeout: (L.env.rpctimeout || 5) * 1000,
credentials: true credentials: true
}).then(cb); }).then(cb, cb);
},
handleListReply: function(req, msg) {
var list = msg.result;
/* verify message frame */
if (typeof(msg) != 'object' || msg.jsonrpc != '2.0' || !msg.id || !Array.isArray(list))
list = [ ];
req.resolve(list);
}, },
parseCallReply: function(req, res) { parseCallReply: function(req, res) {
var msg = null; var msg = null;
if (res instanceof Error)
return req.reject(res);
try { try {
if (!res.ok) if (!res.ok)
L.raise('RPCError', 'RPC call to %s/%s failed with HTTP error %d: %s', L.raise('RPCError', 'RPC call to %s/%s failed with HTTP error %d: %s',
@ -82,7 +76,10 @@ return L.Class.extend({
return req.reject(e); return req.reject(e);
} }
if (Array.isArray(msg.result)) { if (!req.object && !req.method) {
ret = msg.result;
}
else if (Array.isArray(msg.result)) {
ret = (msg.result.length > 1) ? msg.result[1] : msg.result[0]; ret = (msg.result.length > 1) ? msg.result[1] : msg.result[0];
} }
@ -116,7 +113,16 @@ return L.Class.extend({
params: arguments.length ? this.varargs(arguments) : undefined params: arguments.length ? this.varargs(arguments) : undefined
}; };
return this.call(msg, this.handleListReply); return new Promise(L.bind(function(resolveFn, rejectFn) {
/* store request info */
var req = {
resolve: resolveFn,
reject: rejectFn
};
/* call rpc */
this.call(msg, this.parseCallReply.bind(this, req));
}, this));
}, },
declare: function(options) { declare: function(options) {