summaryrefslogtreecommitdiffhomepage
path: root/modules/luci-base/htdocs/luci-static/resources
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2019-09-12 14:02:06 +0200
committerJo-Philipp Wich <jo@mein.io>2019-09-12 14:02:06 +0200
commit473bd2741bd9c57ab5e6c73671138df8f99792fe (patch)
treee89335a879a70922eb0513de5df07154607d14c9 /modules/luci-base/htdocs/luci-static/resources
parentcc81d5a0d4b73fc62e67c4659e08f653cbcdfaef (diff)
luci-base: rpc.js: fix list requests, handle aborted http requests
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'modules/luci-base/htdocs/luci-static/resources')
-rw-r--r--modules/luci-base/htdocs/luci-static/resources/rpc.js44
1 files changed, 25 insertions, 19 deletions
diff --git a/modules/luci-base/htdocs/luci-static/resources/rpc.js b/modules/luci-base/htdocs/luci-static/resources/rpc.js
index 9a0f0164ae..87850b8564 100644
--- a/modules/luci-base/htdocs/luci-static/resources/rpc.js
+++ b/modules/luci-base/htdocs/luci-static/resources/rpc.js
@@ -14,35 +14,29 @@ return L.Class.extend({
return Promise.resolve([]);
for (var i = 0; i < req.length; i++)
- q += '%s%s.%s'.format(
- q ? ';' : '/',
- req[i].params[1],
- req[i].params[2]
- );
+ if (req[i].params)
+ q += '%s%s.%s'.format(
+ q ? ';' : '/',
+ req[i].params[1],
+ req[i].params[2]
+ );
}
- else {
+ else if (req.params) {
q += '/%s.%s'.format(req.params[1], req.params[2]);
}
return L.Request.post(rpcBaseURL + q, req, {
timeout: (L.env.rpctimeout || 5) * 1000,
credentials: true
- }).then(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);
+ }).then(cb, cb);
},
parseCallReply: function(req, res) {
var msg = null;
+ if (res instanceof Error)
+ return req.reject(res);
+
try {
if (!res.ok)
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);
}
- 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];
}
@@ -116,7 +113,16 @@ return L.Class.extend({
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) {