# HG changeset patch # User Kim Alvefur # Date 1615150910 -3600 # Node ID 80912726405d2d99e04444d99e86d31c6a68b76a # Parent 48afaec5d1de0b54e06b8e8c38841a1f77f10f4c 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. diff -r 48afaec5d1de -r 80912726405d mod_rest/mod_rest.lua --- 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);