comparison mod_rest/mod_rest.lua @ 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
comparison
equal deleted inserted replaced
3796:d1ad10b76b00 3797:ed5d7586a61e
88 }); 88 });
89 89
90 -- Forward stanzas from XMPP to HTTP and return any reply 90 -- Forward stanzas from XMPP to HTTP and return any reply
91 local rest_url = module:get_option_string("rest_callback_url", nil); 91 local rest_url = module:get_option_string("rest_callback_url", nil);
92 if rest_url then 92 if rest_url then
93
94 local code2err = {
95 [400] = { condition = "bad-request"; type = "modify" };
96 [401] = { condition = "not-authorized"; type = "auth" };
97 [402] = { condition = "not-authorized"; type = "auth" };
98 [403] = { condition = "forbidden"; type = "auth" };
99 [404] = { condition = "item-not-found"; type = "cancel" };
100 [406] = { condition = "not-acceptable"; type = "modify" };
101 [408] = { condition = "remote-server-timeout"; type = "wait" };
102 [409] = { condition = "conflict"; type = "cancel" };
103 [410] = { condition = "gone"; type = "cancel" };
104 [411] = { condition = "bad-request"; type = "modify" };
105 [412] = { condition = "bad-request"; type = "modify" };
106 [413] = { condition = "resource-constraint"; type = "modify" };
107 [414] = { condition = "resource-constraint"; type = "modify" };
108 [415] = { condition = "bad-request"; type = "modify" };
109 [429] = { condition = "resource-constraint"; type = "wait" };
110 [431] = { condition = "resource-constraint"; type = "wait" };
111
112 [500] = { condition = "internal-server-error"; type = "cancel" };
113 [501] = { condition = "feature-not-implemented"; type = "modify" };
114 [502] = { condition = "remote-server-timeout"; type = "wait" };
115 [503] = { condition = "service-unavailable"; type = "cancel" };
116 [504] = { condition = "remote-server-timeout"; type = "wait" };
117 [507] = { condition = "resource-constraint"; type = "wait" };
118 };
93 119
94 local function handle_stanza(event) 120 local function handle_stanza(event)
95 local stanza, origin = event.stanza, event.origin; 121 local stanza, origin = event.stanza, event.origin;
96 local reply_needed = stanza.name == "iq"; 122 local reply_needed = stanza.name == "iq";
97 123
140 end 166 end
141 if reply_text and reply.name == "message" then 167 if reply_text and reply.name == "message" then
142 reply:body(reply_text, { ["xml:lang"] = response.headers["content-language"] }); 168 reply:body(reply_text, { ["xml:lang"] = response.headers["content-language"] });
143 end 169 end
144 -- TODO presence/status=body ? 170 -- TODO presence/status=body ?
171 elseif code2err[code] then
172 reply = st.error_reply(stanza, errors.new(code, nil, code2err));
145 elseif code_hundreds == 400 then 173 elseif code_hundreds == 400 then
146 reply = st.error_reply(stanza, "modify", "bad-request", reply_text); 174 reply = st.error_reply(stanza, "modify", "bad-request", reply_text);
147 elseif code_hundreds == 500 then 175 elseif code_hundreds == 500 then
148 reply = st.error_reply(stanza, "cancel", "internal-server-error", reply_text); 176 reply = st.error_reply(stanza, "cancel", "internal-server-error", reply_text);
149 else 177 else