changeset 3797:ed5d7586a61e

mod_rest: Map various HTTP status codes to XMPP stanza errors
author Kim Alvefur <zash@zash.se>
date Mon, 30 Dec 2019 05:17:18 +0100
parents d1ad10b76b00
children 9b4fd2553365
files mod_rest/README.markdown mod_rest/mod_rest.lua
diffstat 2 files changed, 31 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mod_rest/README.markdown	Mon Dec 30 05:14:49 2019 +0100
+++ b/mod_rest/README.markdown	Mon Dec 30 05:17:18 2019 +0100
@@ -82,6 +82,9 @@
 To accept the stanza without returning a reply, respond with HTTP status
 code `202` or `204`.
 
+HTTP status codes in the `4xx` and `5xx` range are mapped to an
+appropriate stanza error.
+
 For full control over the response, set the `Content-Type` header to
 `application/xmpp+xml` and return an XMPP stanza as an XML snippet.
 
--- a/mod_rest/mod_rest.lua	Mon Dec 30 05:14:49 2019 +0100
+++ b/mod_rest/mod_rest.lua	Mon Dec 30 05:17:18 2019 +0100
@@ -91,6 +91,32 @@
 local rest_url = module:get_option_string("rest_callback_url", nil);
 if rest_url then
 
+	local code2err = {
+		[400] = { condition = "bad-request"; type = "modify" };
+		[401] = { condition = "not-authorized"; type = "auth" };
+		[402] = { condition = "not-authorized"; type = "auth" };
+		[403] = { condition = "forbidden"; type = "auth" };
+		[404] = { condition = "item-not-found"; type = "cancel" };
+		[406] = { condition = "not-acceptable"; type = "modify" };
+		[408] = { condition = "remote-server-timeout"; type = "wait" };
+		[409] = { condition = "conflict"; type = "cancel" };
+		[410] = { condition = "gone"; type = "cancel" };
+		[411] = { condition = "bad-request"; type = "modify" };
+		[412] = { condition = "bad-request"; type = "modify" };
+		[413] = { condition = "resource-constraint"; type = "modify" };
+		[414] = { condition = "resource-constraint"; type = "modify" };
+		[415] = { condition = "bad-request"; type = "modify" };
+		[429] = { condition = "resource-constraint"; type = "wait" };
+		[431] = { condition = "resource-constraint"; type = "wait" };
+
+		[500] = { condition = "internal-server-error"; type = "cancel" };
+		[501] = { condition = "feature-not-implemented"; type = "modify" };
+		[502] = { condition = "remote-server-timeout"; type = "wait" };
+		[503] = { condition = "service-unavailable"; type = "cancel" };
+		[504] = { condition = "remote-server-timeout"; type = "wait" };
+		[507] = { condition = "resource-constraint"; type = "wait" };
+	};
+
 	local function handle_stanza(event)
 		local stanza, origin = event.stanza, event.origin;
 		local reply_needed = stanza.name == "iq";
@@ -142,6 +168,8 @@
 							reply:body(reply_text, { ["xml:lang"] = response.headers["content-language"] });
 						end
 						-- TODO presence/status=body ?
+					elseif code2err[code] then
+						reply = st.error_reply(stanza, errors.new(code, nil, code2err));
 					elseif code_hundreds == 400 then
 						reply = st.error_reply(stanza, "modify", "bad-request", reply_text);
 					elseif code_hundreds == 500 then