Changeset

4503:80912726405d

mod_rest: Allow passing e.g. disco 'node' as a ?query variable This enables e.g. GET /disco/pubsub.example.org?node=princely_musings Note the hack to skip this for ping.
author Kim Alvefur <zash@zash.se>
date Sun, 07 Mar 2021 22:01:50 +0100
parents 4502:48afaec5d1de
children 4504:0136c98f574c
files mod_rest/mod_rest.lua
diffstat 1 files changed, 24 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/mod_rest/mod_rest.lua	Sun Mar 07 21:02:18 2021 +0100
+++ b/mod_rest/mod_rest.lua	Sun Mar 07 22:01:50 2021 +0100
@@ -65,9 +65,11 @@
 	if not st_kind then return; end
 	if st_kind == "iq" and st_type ~= "get" and st_type ~= "set" then
 		-- GET /iq/disco/jid
-		data.kind = "iq";
-		data.type = "get";
-		data[st_type] = true;
+		data = {
+			kind = "iq";
+			type = "get";
+			[st_type] = st_type == "ping" or data or {};
+		};
 	else
 		data.kind = st_kind;
 		data.type = st_type;
@@ -87,7 +89,10 @@
 		if not parsed then
 			return parsed, err;
 		end
-		if path and not amend_from_path(parsed, path) then return nil, "invalid-path"; end
+		if path then
+			parsed = amend_from_path(parsed, path);
+			if not parsed then return nil, "invalid-path"; end
+		end
 		return jsonmap.json2st(parsed);
 	elseif mimetype == "application/cbor" and have_cbor then
 		local parsed, err = cbor.decode(data);
@@ -98,19 +103,29 @@
 	elseif mimetype == "application/x-www-form-urlencoded"then
 		local parsed = http.formdecode(data);
 		if type(parsed) == "string" then
+			-- This should reject GET /iq/query/to?messagebody
+			if path then
+				return nil, "invalid-query";
+			end
 			return parse("text/plain", parsed);
 		end
 		for i = #parsed, 1, -1 do
 			parsed[i] = nil;
 		end
-		if path and not amend_from_path(parsed, path) then return nil, "invalid-path"; end
+		if path then
+			parsed = amend_from_path(parsed, path);
+			if not parsed then return nil, "invalid-path"; end
+		end
 		return jsonmap.json2st(parsed);
 	elseif mimetype == "text/plain" then
 		if not path then
 			return st.message({ type = "chat" }, data);
 		end
 		local parsed = {};
-		if not amend_from_path(parsed, path) then return nil, "invalid-path"; end
+		if path then
+			parsed = amend_from_path(parsed, path);
+			if not parsed then return nil, "invalid-path"; end
+		end
 		if parsed.kind == "message" then
 			parsed.body = data;
 		elseif parsed.kind == "presence" then
@@ -203,6 +218,9 @@
 local function parse_request(request, path)
 	if path and request.method == "GET" then
 		-- e.g. /verison/{to}
+		if request.url.query then
+			return parse("application/x-www-form-urlencoded", request.url.query, "iq/"..path);
+		end
 		return parse(nil, nil, "iq/"..path);
 	else
 		return parse(request.headers.content_type, request.body, path);