changeset 3812:f027b8b1e794

mod_rest: Factor out serialization of outgoing stanzas More preparation for content negotiation
author Kim Alvefur <zash@zash.se>
date Wed, 01 Jan 2020 16:19:10 +0100
parents eb25110696cd
children aa1ad69c7c10
files mod_rest/mod_rest.lua
diffstat 1 files changed, 8 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/mod_rest/mod_rest.lua	Wed Jan 01 16:01:25 2020 +0100
+++ b/mod_rest/mod_rest.lua	Wed Jan 01 16:19:10 2020 +0100
@@ -36,6 +36,10 @@
 	return "application/xmpp+xml";
 end
 
+local function encode(type, s)
+	return tostring(s);
+end
+
 local function handle_post(event)
 	local request, response = event.request, event.response;
 	if not request.headers.authorization then
@@ -85,13 +89,13 @@
 			function (result)
 				module:log("debug", "Sending[rest]: %s", result.stanza:top_tag());
 				response.headers.content_type = send_type;
-				return tostring(result.stanza);
+				return encode(send_type, result.stanza);
 			end,
 			function (error)
 				if error.context.stanza then
 					response.headers.content_type = send_type;
 					module:log("debug", "Sending[rest]: %s", error.context.stanza:top_tag());
-					return tostring(error.context.stanza);
+					return encode(send_type, error.context.stanza);
 				else
 					return error;
 				end
@@ -101,7 +105,7 @@
 		function origin.send(stanza)
 			module:log("debug", "Sending[rest]: %s", stanza:top_tag());
 			response.headers.content_type = send_type;
-			response:send(tostring(stanza));
+			response:send(encode(send_type, stanza));
 			return true;
 		end
 		if module:send(payload, origin) then
@@ -161,7 +165,7 @@
 			receipt = st.stanza("received", { xmlns = "urn:xmpp:receipts", id = stanza.id });
 		end
 
-		local request_body = tostring(stanza);
+		local request_body = encode(send_type, stanza);
 
 		-- Keep only the top level element and let the rest be GC'd
 		stanza = st.clone(stanza, true);