changeset 3929:bd687d586a8a

mod_rest: Separate lists of mediatypes for input, output and errors
author Kim Alvefur <zash@zash.se>
date Sat, 07 Mar 2020 16:12:49 +0100
parents 7e7ac4af6e0c
children d5dafd617cd6
files mod_rest/mod_rest.lua
diffstat 1 files changed, 22 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/mod_rest/mod_rest.lua	Mon Mar 02 19:32:11 2020 +0100
+++ b/mod_rest/mod_rest.lua	Sat Mar 07 16:12:49 2020 +0100
@@ -83,14 +83,7 @@
 	return nil, "unknown-payload-type";
 end
 
-local supported_types = {
-	"application/xmpp+xml",
-	"application/json",
-	"application/x-www-form-urlencoded",
-	"text/plain",
-};
-
-local function decide_type(accept)
+local function decide_type(accept, supported_types)
 	-- assumes the accept header is sorted
 	local ret = supported_types[1];
 	for i = 2, #supported_types do
@@ -101,6 +94,18 @@
 	return ret;
 end
 
+local supported_inputs = {
+	"application/xmpp+xml",
+	"application/json",
+	"application/x-www-form-urlencoded",
+	"text/plain",
+};
+
+local supported_outputs = {
+	"application/xmpp+xml",
+	"application/json",
+};
+
 local function encode(type, s)
 	if type == "application/json" then
 		return json.encode(jsonmap.st2json(s));
@@ -158,7 +163,7 @@
 		["xml:lang"] = payload.attr["xml:lang"],
 	};
 	module:log("debug", "Received[rest]: %s", payload:top_tag());
-	local send_type = decide_type((request.headers.accept or "") ..",".. request.headers.content_type)
+	local send_type = decide_type((request.headers.accept or "") ..",".. request.headers.content_type, supported_outputs)
 	if payload.name == "iq" then
 		function origin.send(stanza)
 			module:send(stanza);
@@ -224,7 +229,7 @@
 				module:set_status("info", "Connected");
 			end
 			if code == 200 and response.headers.accept then
-				send_type = decide_type(response.headers.accept);
+				send_type = decide_type(response.headers.accept, supported_outputs);
 				module:log("debug", "Set 'rest_callback_content_type' = %q based on Accept header", send_type);
 			end
 		end);
@@ -280,7 +285,7 @@
 				headers = {
 					["Content-Type"] = send_type,
 					["Content-Language"] = stanza.attr["xml:lang"],
-					Accept = table.concat(supported_types, ", ");
+					Accept = table.concat(supported_inputs, ", ");
 				},
 			}, function (body, code, response)
 				if code == 0 then
@@ -376,10 +381,15 @@
 	end
 end
 
+local supported_errors = {
+	"text/html",
+	"application/json",
+};
+
 local http_server = require "net.http.server";
 module:hook_object_event(http_server, "http-error", function (event)
 	local request, response = event.request, event.response;
-	if decide_type(request and request.headers.accept or "") == "application/json" then
+	if decide_type(request and request.headers.accept or "", supported_errors) == "application/json" then
 		if response then
 			response.headers.content_type = "application/json";
 		end