diff mbox series

[luci] luci-base: use actual JSON-RPC for verifying ubus RPC URL

Message ID 20200914163107.27739-1-zajec5@gmail.com
State Accepted
Headers show
Series [luci] luci-base: use actual JSON-RPC for verifying ubus RPC URL | expand

Commit Message

Rafał Miłecki Sept. 14, 2020, 4:31 p.m. UTC
From: Rafał Miłecki <rafal@milecki.pl>

Sending GET request to the main RPC base URL and expecting HTTP response
code 400 had two flaws:
1. It was not verifying actual JSON-RPC interface availability
2. It did not allow implementing support for new requests

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
---
 .../luci-base/htdocs/luci-static/resources/luci.js  | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

Comments

Rafał Miłecki Sept. 15, 2020, 8:37 a.m. UTC | #1
On 14.09.2020 18:31, Rafał Miłecki wrote:
> @@ -2553,10 +2553,19 @@
>   				rpcBaseURL = Session.getLocalData('rpcBaseURL');
>   
>   			if (rpcBaseURL == null) {
> +				var msg = {
> +					jsonrpc: '2.0',
> +					id:      'init',
> +					method:  'list',
> +					params:  undefined
> +				};
> +				var options = {
> +					nobatch: true
> +				};
>   				var rpcFallbackURL = this.url('admin/ubus');
>   
> -				rpcBaseURL = Request.get(env.ubuspath).then(function(res) {
> -					return (rpcBaseURL = (res.status == 400) ? env.ubuspath : rpcFallbackURL);
> +				rpcBaseURL = Request.post(env.ubuspath, msg, options).then(function(res) {
> +					return (rpcBaseURL = res.status == 200 ? env.ubuspath : rpcFallbackURL);

Pushing with "options" variable object inlined.
diff mbox series

Patch

diff --git a/modules/luci-base/htdocs/luci-static/resources/luci.js b/modules/luci-base/htdocs/luci-static/resources/luci.js
index 83c2807d7..e285f999e 100644
--- a/modules/luci-base/htdocs/luci-static/resources/luci.js
+++ b/modules/luci-base/htdocs/luci-static/resources/luci.js
@@ -2553,10 +2553,19 @@ 
 				rpcBaseURL = Session.getLocalData('rpcBaseURL');
 
 			if (rpcBaseURL == null) {
+				var msg = {
+					jsonrpc: '2.0',
+					id:      'init',
+					method:  'list',
+					params:  undefined
+				};
+				var options = {
+					nobatch: true
+				};
 				var rpcFallbackURL = this.url('admin/ubus');
 
-				rpcBaseURL = Request.get(env.ubuspath).then(function(res) {
-					return (rpcBaseURL = (res.status == 400) ? env.ubuspath : rpcFallbackURL);
+				rpcBaseURL = Request.post(env.ubuspath, msg, options).then(function(res) {
+					return (rpcBaseURL = res.status == 200 ? env.ubuspath : rpcFallbackURL);
 				}, function() {
 					return (rpcBaseURL = rpcFallbackURL);
 				}).then(function(url) {